From 935765f0ad3909b1e5c2cb82cca17346aed9ac3d Mon Sep 17 00:00:00 2001 From: Dan Sanche Date: Thu, 8 Feb 2018 10:39:06 -0800 Subject: [PATCH 01/13] added cdn url signing samples --- cdn/pom.xml | 25 +++++ .../main/java/com/google/cdn/SignedUrls.java | 97 +++++++++++++++++++ cdn/src/main/resources/my-key | 1 + .../java/com/google/cdn/SignedUrlsTest.java | 61 ++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 cdn/pom.xml create mode 100644 cdn/src/main/java/com/google/cdn/SignedUrls.java create mode 100644 cdn/src/main/resources/my-key create mode 100644 cdn/src/test/java/com/google/cdn/SignedUrlsTest.java diff --git a/cdn/pom.xml b/cdn/pom.xml new file mode 100644 index 00000000000..bb3dacf68eb --- /dev/null +++ b/cdn/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + com.google.cdn + signedurls + 1.0 + jar + + signedurls + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.12 + test + + + diff --git a/cdn/src/main/java/com/google/cdn/SignedUrls.java b/cdn/src/main/java/com/google/cdn/SignedUrls.java new file mode 100644 index 00000000000..7ee281c932d --- /dev/null +++ b/cdn/src/main/java/com/google/cdn/SignedUrls.java @@ -0,0 +1,97 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cdn; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.Key; +import java.util.Calendar; +import java.util.Date; + +public class SignedUrls { + + // [START signUrl] + /** + * Creates a signed URL for a Cloud CDN endpoint with the given key + * should pass in a properly formatted URL + * good: + * https://www.google.com/ + * http://www.google.com/ + * https://www.google.com/?foo=test&bar=test + * https://www.google.com/foo + * + * bad: + * https://www.google.com + * https://www.google.com?test + * www.google.com + * + * @param url the Cloud CDN endpoint to sign + * @param key encoded as a 16-byte array + * @param keyName the name of the signing key added to the back end bucket or service + * @param expirationTime the date that the signed URL expires + * @return a properly formatted signed URL + * @throws InvalidKeyException when there is an error generating the signature for the input key + * @throws NoSuchAlgorithmException when the HmacSHA1 algorithm is not available in the environment + */ + public static String signUrl(String url, + byte[] key, + String keyName, + Date expirationTime) + throws InvalidKeyException, NoSuchAlgorithmException { + + final long unixTime = expirationTime.getTime() / 1000; + + String urlToSign = url + + (url.contains("?") ? "&":"?") + + "Expires="+ unixTime + + "&KeyName="+ keyName; + + String encoded = SignedUrls.getSignature(key, urlToSign); + return urlToSign+"&Signature="+encoded; + } + + public static String getSignature(byte[] privateKey, String input) throws InvalidKeyException, NoSuchAlgorithmException { + final String algorithm = "HmacSHA1"; + Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm); + Mac mac = Mac.getInstance(algorithm); + mac.init(key); + return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes())); + } + // [END signUrl] + + public static void main(String[] args) throws Exception { + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.DATE, 1); + Date tomorrow = cal.getTime(); + + //read the key as a base 64 url-safe encoded string + String base64String = new String(Files.readAllBytes(Paths.get("./src/main/resources/my-key"))); + //turn the key string into a byte array + byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); + + String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow); + System.out.println(result); + } +} diff --git a/cdn/src/main/resources/my-key b/cdn/src/main/resources/my-key new file mode 100644 index 00000000000..3c5492c5e5d --- /dev/null +++ b/cdn/src/main/resources/my-key @@ -0,0 +1 @@ +nZtRohdNF9m3cKM24IcK4w== \ No newline at end of file diff --git a/cdn/src/test/java/com/google/cdn/SignedUrlsTest.java b/cdn/src/test/java/com/google/cdn/SignedUrlsTest.java new file mode 100644 index 00000000000..27073cd3e64 --- /dev/null +++ b/cdn/src/test/java/com/google/cdn/SignedUrlsTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package com.google.cdn; + +import java.util.Base64; +import java.util.Date; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static com.google.cdn.SignedUrls.signUrl; +import static junit.framework.TestCase.assertEquals; + + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class SignedUrlsTest { + private static Date EXPIRATION = new Date((long)1518135754*1000); + private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa=="); + private static String KEY_NAME = "my-key"; + private static String BASE_URL = "https://www.google.com/"; + + @Test + public void testUrlPath() throws Exception { + String result = signUrl(BASE_URL+"foo", KEY_BYTES, KEY_NAME, EXPIRATION); + final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s="; + assertEquals(result, expected); + } + + @Test + public void testUrlParams() throws Exception { + String result = signUrl(BASE_URL+"?param=true", KEY_BYTES, KEY_NAME, EXPIRATION); + final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o="; + assertEquals(result, expected); + } + + + @Test + public void testStandard() throws Exception { + String result = signUrl(BASE_URL, KEY_BYTES, KEY_NAME, EXPIRATION); + final String expected = "https://www.google.com/?Expires=1518135754&KeyName=my-key&Signature=yYnIFLMqsuGfpSuo7nf7wk21boM="; + assertEquals(result, expected); + } + + +} From e95fd5b750e082e5dd5f308462b233709f9a3806 Mon Sep 17 00:00:00 2001 From: Dan Sanche Date: Thu, 8 Feb 2018 10:48:24 -0800 Subject: [PATCH 02/13] added to root pom --- cdn/{ => signed-urls}/pom.xml | 0 .../src/main/java/com/google/cdn/SignedUrls.java | 0 cdn/{ => signed-urls}/src/main/resources/my-key | 0 .../src/test/java/com/google/cdn/SignedUrlsTest.java | 0 pom.xml | 1 + 5 files changed, 1 insertion(+) rename cdn/{ => signed-urls}/pom.xml (100%) rename cdn/{ => signed-urls}/src/main/java/com/google/cdn/SignedUrls.java (100%) rename cdn/{ => signed-urls}/src/main/resources/my-key (100%) rename cdn/{ => signed-urls}/src/test/java/com/google/cdn/SignedUrlsTest.java (100%) diff --git a/cdn/pom.xml b/cdn/signed-urls/pom.xml similarity index 100% rename from cdn/pom.xml rename to cdn/signed-urls/pom.xml diff --git a/cdn/src/main/java/com/google/cdn/SignedUrls.java b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java similarity index 100% rename from cdn/src/main/java/com/google/cdn/SignedUrls.java rename to cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java diff --git a/cdn/src/main/resources/my-key b/cdn/signed-urls/src/main/resources/my-key similarity index 100% rename from cdn/src/main/resources/my-key rename to cdn/signed-urls/src/main/resources/my-key diff --git a/cdn/src/test/java/com/google/cdn/SignedUrlsTest.java b/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java similarity index 100% rename from cdn/src/test/java/com/google/cdn/SignedUrlsTest.java rename to cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java diff --git a/pom.xml b/pom.xml index eb3b98e070b..39ff6e30fc8 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,7 @@ cloud-tasks compute + cdn/signed-urls dataflow/spanner-io From e5e6b0cdfd954c2a746f374de8a8d9572c32b48f Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Thu, 8 Feb 2018 11:11:51 -0800 Subject: [PATCH 03/13] fixed lint issues --- .../main/java/com/google/cdn/SignedUrls.java | 34 +++++++++++-------- .../java/com/google/cdn/SignedUrlsTest.java | 17 ++++++---- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java index 7ee281c932d..6a1685659fb 100644 --- a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java +++ b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java @@ -15,20 +15,21 @@ */ package com.google.cdn; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.util.Base64; -import javax.crypto.Mac; -import javax.crypto.spec.SecretKeySpec; import java.nio.file.Files; import java.nio.file.Paths; +import java.security.InvalidKeyException; import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; import java.util.Calendar; import java.util.Date; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +/** + * Samples to create a signed URL for a Cloud CDN endpoint + */ public class SignedUrls { // [START signUrl] @@ -52,7 +53,7 @@ public class SignedUrls { * @param expirationTime the date that the signed URL expires * @return a properly formatted signed URL * @throws InvalidKeyException when there is an error generating the signature for the input key - * @throws NoSuchAlgorithmException when the HmacSHA1 algorithm is not available in the environment + * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment */ public static String signUrl(String url, byte[] key, @@ -62,16 +63,18 @@ public static String signUrl(String url, final long unixTime = expirationTime.getTime() / 1000; - String urlToSign = url + - (url.contains("?") ? "&":"?") + - "Expires="+ unixTime + - "&KeyName="+ keyName; + String urlToSign = url + + (url.contains("?") ? "&" : "?") + + "Expires=" + unixTime + + "&KeyName=" + keyName; String encoded = SignedUrls.getSignature(key, urlToSign); - return urlToSign+"&Signature="+encoded; + return urlToSign + "&Signature=" + encoded; } - public static String getSignature(byte[] privateKey, String input) throws InvalidKeyException, NoSuchAlgorithmException { + public static String getSignature(byte[] privateKey, String input) + throws InvalidKeyException, NoSuchAlgorithmException { + final String algorithm = "HmacSHA1"; Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm); Mac mac = Mac.getInstance(algorithm); @@ -87,7 +90,8 @@ public static void main(String[] args) throws Exception { Date tomorrow = cal.getTime(); //read the key as a base 64 url-safe encoded string - String base64String = new String(Files.readAllBytes(Paths.get("./src/main/resources/my-key"))); + final String keyPath = "./src/main/resources/my-key"; + String base64String = new String(Files.readAllBytes(Paths.get(keyPath))); //turn the key string into a byte array byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); diff --git a/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java b/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java index 27073cd3e64..9553d67d6d1 100644 --- a/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java +++ b/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java @@ -17,34 +17,37 @@ package com.google.cdn; +import static com.google.cdn.SignedUrls.signUrl; +import static junit.framework.TestCase.assertEquals; + import java.util.Base64; import java.util.Date; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import static com.google.cdn.SignedUrls.signUrl; -import static junit.framework.TestCase.assertEquals; - - +/** + * Test SignedUrls samples + */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class SignedUrlsTest { - private static Date EXPIRATION = new Date((long)1518135754*1000); + private static long TIMESTAMP = 1518135754; + private static Date EXPIRATION = new Date(TIMESTAMP * 1000); private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa=="); private static String KEY_NAME = "my-key"; private static String BASE_URL = "https://www.google.com/"; @Test public void testUrlPath() throws Exception { - String result = signUrl(BASE_URL+"foo", KEY_BYTES, KEY_NAME, EXPIRATION); + String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION); final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s="; assertEquals(result, expected); } @Test public void testUrlParams() throws Exception { - String result = signUrl(BASE_URL+"?param=true", KEY_BYTES, KEY_NAME, EXPIRATION); + String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION); final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o="; assertEquals(result, expected); } From 182445d1dfc7d60825d4bf2facac4451e0d931df Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Thu, 8 Feb 2018 11:15:27 -0800 Subject: [PATCH 04/13] changed comment wording --- cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java index 6a1685659fb..807c3ea8eee 100644 --- a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java +++ b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java @@ -36,13 +36,13 @@ public class SignedUrls { /** * Creates a signed URL for a Cloud CDN endpoint with the given key * should pass in a properly formatted URL - * good: + * will work: * https://www.google.com/ * http://www.google.com/ * https://www.google.com/?foo=test&bar=test * https://www.google.com/foo * - * bad: + * won't work: * https://www.google.com * https://www.google.com?test * www.google.com From 6dbbb2ad5fb973ef38945c23c3602565a47f141f Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Thu, 8 Feb 2018 11:24:03 -0800 Subject: [PATCH 05/13] added parent to pom file --- cdn/signed-urls/pom.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cdn/signed-urls/pom.xml b/cdn/signed-urls/pom.xml index bb3dacf68eb..b8882fc1152 100644 --- a/cdn/signed-urls/pom.xml +++ b/cdn/signed-urls/pom.xml @@ -7,6 +7,16 @@ 1.0 jar + + + com.google.cloud.samples + shared-configuration + 1.0.8 + + signedurls http://maven.apache.org From d4ce25903a6c6565bce5f1e59d21535cb2a50e4d Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Thu, 8 Feb 2018 11:30:36 -0800 Subject: [PATCH 06/13] added compiler verson to pom --- cdn/signed-urls/pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cdn/signed-urls/pom.xml b/cdn/signed-urls/pom.xml index b8882fc1152..821e914cd60 100644 --- a/cdn/signed-urls/pom.xml +++ b/cdn/signed-urls/pom.xml @@ -21,6 +21,8 @@ http://maven.apache.org + 1.7 + 1.7 UTF-8 From 96054595e115ff84a820b7ffb666653592770088 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Thu, 8 Feb 2018 12:12:51 -0800 Subject: [PATCH 07/13] fixed indents --- .../main/java/com/google/cdn/SignedUrls.java | 116 +++++++++--------- .../java/com/google/cdn/SignedUrlsTest.java | 56 ++++----- 2 files changed, 85 insertions(+), 87 deletions(-) diff --git a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java index 807c3ea8eee..4bcc1462931 100644 --- a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java +++ b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java @@ -32,70 +32,70 @@ */ public class SignedUrls { - // [START signUrl] - /** - * Creates a signed URL for a Cloud CDN endpoint with the given key - * should pass in a properly formatted URL - * will work: - * https://www.google.com/ - * http://www.google.com/ - * https://www.google.com/?foo=test&bar=test - * https://www.google.com/foo - * - * won't work: - * https://www.google.com - * https://www.google.com?test - * www.google.com - * - * @param url the Cloud CDN endpoint to sign - * @param key encoded as a 16-byte array - * @param keyName the name of the signing key added to the back end bucket or service - * @param expirationTime the date that the signed URL expires - * @return a properly formatted signed URL - * @throws InvalidKeyException when there is an error generating the signature for the input key - * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment - */ - public static String signUrl(String url, - byte[] key, - String keyName, - Date expirationTime) - throws InvalidKeyException, NoSuchAlgorithmException { + // [START signUrl] + /** + * Creates a signed URL for a Cloud CDN endpoint with the given key + * should pass in a properly formatted URL + * will work: + * https://www.google.com/ + * http://www.google.com/ + * https://www.google.com/?foo=test&bar=test + * https://www.google.com/foo + * + * won't work: + * https://www.google.com + * https://www.google.com?test + * www.google.com + * + * @param url the Cloud CDN endpoint to sign + * @param key encoded as a 16-byte array + * @param keyName the name of the signing key added to the back end bucket or service + * @param expirationTime the date that the signed URL expires + * @return a properly formatted signed URL + * @throws InvalidKeyException when there is an error generating the signature for the input key + * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment + */ + public static String signUrl(String url, + byte[] key, + String keyName, + Date expirationTime) + throws InvalidKeyException, NoSuchAlgorithmException { - final long unixTime = expirationTime.getTime() / 1000; + final long unixTime = expirationTime.getTime() / 1000; - String urlToSign = url - + (url.contains("?") ? "&" : "?") - + "Expires=" + unixTime - + "&KeyName=" + keyName; + String urlToSign = url + + (url.contains("?") ? "&" : "?") + + "Expires=" + unixTime + + "&KeyName=" + keyName; - String encoded = SignedUrls.getSignature(key, urlToSign); - return urlToSign + "&Signature=" + encoded; - } + String encoded = SignedUrls.getSignature(key, urlToSign); + return urlToSign + "&Signature=" + encoded; + } - public static String getSignature(byte[] privateKey, String input) - throws InvalidKeyException, NoSuchAlgorithmException { + public static String getSignature(byte[] privateKey, String input) + throws InvalidKeyException, NoSuchAlgorithmException { - final String algorithm = "HmacSHA1"; - Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm); - Mac mac = Mac.getInstance(algorithm); - mac.init(key); - return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes())); - } - // [END signUrl] + final String algorithm = "HmacSHA1"; + Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm); + Mac mac = Mac.getInstance(algorithm); + mac.init(key); + return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes())); + } + // [END signUrl] - public static void main(String[] args) throws Exception { - Calendar cal = Calendar.getInstance(); - cal.setTime(new Date()); - cal.add(Calendar.DATE, 1); - Date tomorrow = cal.getTime(); + public static void main(String[] args) throws Exception { + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.DATE, 1); + Date tomorrow = cal.getTime(); - //read the key as a base 64 url-safe encoded string - final String keyPath = "./src/main/resources/my-key"; - String base64String = new String(Files.readAllBytes(Paths.get(keyPath))); - //turn the key string into a byte array - byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); + //read the key as a base 64 url-safe encoded string + final String keyPath = "./src/main/resources/my-key"; + String base64String = new String(Files.readAllBytes(Paths.get(keyPath))); + //turn the key string into a byte array + byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); - String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow); - System.out.println(result); - } + String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow); + System.out.println(result); + } } diff --git a/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java b/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java index 9553d67d6d1..a09008f1c10 100644 --- a/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java +++ b/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java @@ -32,33 +32,31 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class SignedUrlsTest { - private static long TIMESTAMP = 1518135754; - private static Date EXPIRATION = new Date(TIMESTAMP * 1000); - private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa=="); - private static String KEY_NAME = "my-key"; - private static String BASE_URL = "https://www.google.com/"; - - @Test - public void testUrlPath() throws Exception { - String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION); - final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s="; - assertEquals(result, expected); - } - - @Test - public void testUrlParams() throws Exception { - String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION); - final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o="; - assertEquals(result, expected); - } - - - @Test - public void testStandard() throws Exception { - String result = signUrl(BASE_URL, KEY_BYTES, KEY_NAME, EXPIRATION); - final String expected = "https://www.google.com/?Expires=1518135754&KeyName=my-key&Signature=yYnIFLMqsuGfpSuo7nf7wk21boM="; - assertEquals(result, expected); - } - - + private static long TIMESTAMP = 1518135754; + private static Date EXPIRATION = new Date(TIMESTAMP * 1000); + private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa=="); + private static String KEY_NAME = "my-key"; + private static String BASE_URL = "https://www.google.com/"; + + @Test + public void testUrlPath() throws Exception { + String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION); + final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s="; + assertEquals(result, expected); + } + + @Test + public void testUrlParams() throws Exception { + String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION); + final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o="; + assertEquals(result, expected); + } + + + @Test + public void testStandard() throws Exception { + String result = signUrl(BASE_URL, KEY_BYTES, KEY_NAME, EXPIRATION); + final String expected = "https://www.google.com/?Expires=1518135754&KeyName=my-key&Signature=yYnIFLMqsuGfpSuo7nf7wk21boM="; + assertEquals(result, expected); + } } From a242795daf6a7c2198ca0948c8e79aeac774646d Mon Sep 17 00:00:00 2001 From: Dan Sanche Date: Thu, 8 Feb 2018 11:11:51 -0800 Subject: [PATCH 08/13] fixed lint issues --- .../main/java/com/google/cdn/SignedUrls.java | 34 +++++++++++-------- .../java/com/google/cdn/SignedUrlsTest.java | 17 ++++++---- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java index 7ee281c932d..6a1685659fb 100644 --- a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java +++ b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java @@ -15,20 +15,21 @@ */ package com.google.cdn; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.util.Base64; -import javax.crypto.Mac; -import javax.crypto.spec.SecretKeySpec; import java.nio.file.Files; import java.nio.file.Paths; +import java.security.InvalidKeyException; import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; import java.util.Calendar; import java.util.Date; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +/** + * Samples to create a signed URL for a Cloud CDN endpoint + */ public class SignedUrls { // [START signUrl] @@ -52,7 +53,7 @@ public class SignedUrls { * @param expirationTime the date that the signed URL expires * @return a properly formatted signed URL * @throws InvalidKeyException when there is an error generating the signature for the input key - * @throws NoSuchAlgorithmException when the HmacSHA1 algorithm is not available in the environment + * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment */ public static String signUrl(String url, byte[] key, @@ -62,16 +63,18 @@ public static String signUrl(String url, final long unixTime = expirationTime.getTime() / 1000; - String urlToSign = url + - (url.contains("?") ? "&":"?") + - "Expires="+ unixTime + - "&KeyName="+ keyName; + String urlToSign = url + + (url.contains("?") ? "&" : "?") + + "Expires=" + unixTime + + "&KeyName=" + keyName; String encoded = SignedUrls.getSignature(key, urlToSign); - return urlToSign+"&Signature="+encoded; + return urlToSign + "&Signature=" + encoded; } - public static String getSignature(byte[] privateKey, String input) throws InvalidKeyException, NoSuchAlgorithmException { + public static String getSignature(byte[] privateKey, String input) + throws InvalidKeyException, NoSuchAlgorithmException { + final String algorithm = "HmacSHA1"; Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm); Mac mac = Mac.getInstance(algorithm); @@ -87,7 +90,8 @@ public static void main(String[] args) throws Exception { Date tomorrow = cal.getTime(); //read the key as a base 64 url-safe encoded string - String base64String = new String(Files.readAllBytes(Paths.get("./src/main/resources/my-key"))); + final String keyPath = "./src/main/resources/my-key"; + String base64String = new String(Files.readAllBytes(Paths.get(keyPath))); //turn the key string into a byte array byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); diff --git a/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java b/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java index 27073cd3e64..9553d67d6d1 100644 --- a/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java +++ b/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java @@ -17,34 +17,37 @@ package com.google.cdn; +import static com.google.cdn.SignedUrls.signUrl; +import static junit.framework.TestCase.assertEquals; + import java.util.Base64; import java.util.Date; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import static com.google.cdn.SignedUrls.signUrl; -import static junit.framework.TestCase.assertEquals; - - +/** + * Test SignedUrls samples + */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class SignedUrlsTest { - private static Date EXPIRATION = new Date((long)1518135754*1000); + private static long TIMESTAMP = 1518135754; + private static Date EXPIRATION = new Date(TIMESTAMP * 1000); private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa=="); private static String KEY_NAME = "my-key"; private static String BASE_URL = "https://www.google.com/"; @Test public void testUrlPath() throws Exception { - String result = signUrl(BASE_URL+"foo", KEY_BYTES, KEY_NAME, EXPIRATION); + String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION); final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s="; assertEquals(result, expected); } @Test public void testUrlParams() throws Exception { - String result = signUrl(BASE_URL+"?param=true", KEY_BYTES, KEY_NAME, EXPIRATION); + String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION); final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o="; assertEquals(result, expected); } From 3216f2ad60050f2daab28f109e1fe91584156463 Mon Sep 17 00:00:00 2001 From: Dan Sanche Date: Thu, 8 Feb 2018 11:15:27 -0800 Subject: [PATCH 09/13] changed comment wording --- cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java index 6a1685659fb..807c3ea8eee 100644 --- a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java +++ b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java @@ -36,13 +36,13 @@ public class SignedUrls { /** * Creates a signed URL for a Cloud CDN endpoint with the given key * should pass in a properly formatted URL - * good: + * will work: * https://www.google.com/ * http://www.google.com/ * https://www.google.com/?foo=test&bar=test * https://www.google.com/foo * - * bad: + * won't work: * https://www.google.com * https://www.google.com?test * www.google.com From 2316d35c33cace8eb598600a57b8870afad525dd Mon Sep 17 00:00:00 2001 From: Dan Sanche Date: Thu, 8 Feb 2018 11:24:03 -0800 Subject: [PATCH 10/13] added parent to pom file --- cdn/signed-urls/pom.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cdn/signed-urls/pom.xml b/cdn/signed-urls/pom.xml index bb3dacf68eb..b8882fc1152 100644 --- a/cdn/signed-urls/pom.xml +++ b/cdn/signed-urls/pom.xml @@ -7,6 +7,16 @@ 1.0 jar + + + com.google.cloud.samples + shared-configuration + 1.0.8 + + signedurls http://maven.apache.org From 481a26efa0bc5dd20de31a32ed6861bbc2b19d9e Mon Sep 17 00:00:00 2001 From: Dan Sanche Date: Thu, 8 Feb 2018 11:30:36 -0800 Subject: [PATCH 11/13] added compiler verson to pom --- cdn/signed-urls/pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cdn/signed-urls/pom.xml b/cdn/signed-urls/pom.xml index b8882fc1152..821e914cd60 100644 --- a/cdn/signed-urls/pom.xml +++ b/cdn/signed-urls/pom.xml @@ -21,6 +21,8 @@ http://maven.apache.org + 1.7 + 1.7 UTF-8 From de2a3f0abc3ed3c69d8dda8b950dac4729286074 Mon Sep 17 00:00:00 2001 From: Dan Sanche Date: Thu, 8 Feb 2018 12:12:51 -0800 Subject: [PATCH 12/13] fixed indents --- .../main/java/com/google/cdn/SignedUrls.java | 116 +++++++++--------- .../java/com/google/cdn/SignedUrlsTest.java | 56 ++++----- 2 files changed, 85 insertions(+), 87 deletions(-) diff --git a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java index 807c3ea8eee..4bcc1462931 100644 --- a/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java +++ b/cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java @@ -32,70 +32,70 @@ */ public class SignedUrls { - // [START signUrl] - /** - * Creates a signed URL for a Cloud CDN endpoint with the given key - * should pass in a properly formatted URL - * will work: - * https://www.google.com/ - * http://www.google.com/ - * https://www.google.com/?foo=test&bar=test - * https://www.google.com/foo - * - * won't work: - * https://www.google.com - * https://www.google.com?test - * www.google.com - * - * @param url the Cloud CDN endpoint to sign - * @param key encoded as a 16-byte array - * @param keyName the name of the signing key added to the back end bucket or service - * @param expirationTime the date that the signed URL expires - * @return a properly formatted signed URL - * @throws InvalidKeyException when there is an error generating the signature for the input key - * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment - */ - public static String signUrl(String url, - byte[] key, - String keyName, - Date expirationTime) - throws InvalidKeyException, NoSuchAlgorithmException { + // [START signUrl] + /** + * Creates a signed URL for a Cloud CDN endpoint with the given key + * should pass in a properly formatted URL + * will work: + * https://www.google.com/ + * http://www.google.com/ + * https://www.google.com/?foo=test&bar=test + * https://www.google.com/foo + * + * won't work: + * https://www.google.com + * https://www.google.com?test + * www.google.com + * + * @param url the Cloud CDN endpoint to sign + * @param key encoded as a 16-byte array + * @param keyName the name of the signing key added to the back end bucket or service + * @param expirationTime the date that the signed URL expires + * @return a properly formatted signed URL + * @throws InvalidKeyException when there is an error generating the signature for the input key + * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment + */ + public static String signUrl(String url, + byte[] key, + String keyName, + Date expirationTime) + throws InvalidKeyException, NoSuchAlgorithmException { - final long unixTime = expirationTime.getTime() / 1000; + final long unixTime = expirationTime.getTime() / 1000; - String urlToSign = url - + (url.contains("?") ? "&" : "?") - + "Expires=" + unixTime - + "&KeyName=" + keyName; + String urlToSign = url + + (url.contains("?") ? "&" : "?") + + "Expires=" + unixTime + + "&KeyName=" + keyName; - String encoded = SignedUrls.getSignature(key, urlToSign); - return urlToSign + "&Signature=" + encoded; - } + String encoded = SignedUrls.getSignature(key, urlToSign); + return urlToSign + "&Signature=" + encoded; + } - public static String getSignature(byte[] privateKey, String input) - throws InvalidKeyException, NoSuchAlgorithmException { + public static String getSignature(byte[] privateKey, String input) + throws InvalidKeyException, NoSuchAlgorithmException { - final String algorithm = "HmacSHA1"; - Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm); - Mac mac = Mac.getInstance(algorithm); - mac.init(key); - return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes())); - } - // [END signUrl] + final String algorithm = "HmacSHA1"; + Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm); + Mac mac = Mac.getInstance(algorithm); + mac.init(key); + return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes())); + } + // [END signUrl] - public static void main(String[] args) throws Exception { - Calendar cal = Calendar.getInstance(); - cal.setTime(new Date()); - cal.add(Calendar.DATE, 1); - Date tomorrow = cal.getTime(); + public static void main(String[] args) throws Exception { + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.DATE, 1); + Date tomorrow = cal.getTime(); - //read the key as a base 64 url-safe encoded string - final String keyPath = "./src/main/resources/my-key"; - String base64String = new String(Files.readAllBytes(Paths.get(keyPath))); - //turn the key string into a byte array - byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); + //read the key as a base 64 url-safe encoded string + final String keyPath = "./src/main/resources/my-key"; + String base64String = new String(Files.readAllBytes(Paths.get(keyPath))); + //turn the key string into a byte array + byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); - String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow); - System.out.println(result); - } + String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow); + System.out.println(result); + } } diff --git a/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java b/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java index 9553d67d6d1..a09008f1c10 100644 --- a/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java +++ b/cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java @@ -32,33 +32,31 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class SignedUrlsTest { - private static long TIMESTAMP = 1518135754; - private static Date EXPIRATION = new Date(TIMESTAMP * 1000); - private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa=="); - private static String KEY_NAME = "my-key"; - private static String BASE_URL = "https://www.google.com/"; - - @Test - public void testUrlPath() throws Exception { - String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION); - final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s="; - assertEquals(result, expected); - } - - @Test - public void testUrlParams() throws Exception { - String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION); - final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o="; - assertEquals(result, expected); - } - - - @Test - public void testStandard() throws Exception { - String result = signUrl(BASE_URL, KEY_BYTES, KEY_NAME, EXPIRATION); - final String expected = "https://www.google.com/?Expires=1518135754&KeyName=my-key&Signature=yYnIFLMqsuGfpSuo7nf7wk21boM="; - assertEquals(result, expected); - } - - + private static long TIMESTAMP = 1518135754; + private static Date EXPIRATION = new Date(TIMESTAMP * 1000); + private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa=="); + private static String KEY_NAME = "my-key"; + private static String BASE_URL = "https://www.google.com/"; + + @Test + public void testUrlPath() throws Exception { + String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION); + final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s="; + assertEquals(result, expected); + } + + @Test + public void testUrlParams() throws Exception { + String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION); + final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o="; + assertEquals(result, expected); + } + + + @Test + public void testStandard() throws Exception { + String result = signUrl(BASE_URL, KEY_BYTES, KEY_NAME, EXPIRATION); + final String expected = "https://www.google.com/?Expires=1518135754&KeyName=my-key&Signature=yYnIFLMqsuGfpSuo7nf7wk21boM="; + assertEquals(result, expected); + } } From c80d441da5264ccb1b036c7a6f8c56f53cf76482 Mon Sep 17 00:00:00 2001 From: Dan Sanche Date: Wed, 7 Feb 2018 17:19:00 -0800 Subject: [PATCH 13/13] Auto-update dependencies. (#1017) --- vision/face-detection/pom.xml | 2 +- vision/label/pom.xml | 2 +- vision/landmark-detection/pom.xml | 2 +- vision/text/pom.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vision/face-detection/pom.xml b/vision/face-detection/pom.xml index 5a8d122727d..41f6d7f3379 100644 --- a/vision/face-detection/pom.xml +++ b/vision/face-detection/pom.xml @@ -41,7 +41,7 @@ com.google.apis google-api-services-vision - v1-rev369-1.23.0 + v1-rev370-1.23.0 com.google.api-client diff --git a/vision/label/pom.xml b/vision/label/pom.xml index faf37e7438a..e8447d1283f 100644 --- a/vision/label/pom.xml +++ b/vision/label/pom.xml @@ -40,7 +40,7 @@ com.google.apis google-api-services-vision - v1-rev369-1.23.0 + v1-rev370-1.23.0 com.google.api-client diff --git a/vision/landmark-detection/pom.xml b/vision/landmark-detection/pom.xml index 0d05491b1c3..b74ff4b904a 100644 --- a/vision/landmark-detection/pom.xml +++ b/vision/landmark-detection/pom.xml @@ -40,7 +40,7 @@ com.google.apis google-api-services-vision - v1-rev369-1.23.0 + v1-rev370-1.23.0 com.google.api-client diff --git a/vision/text/pom.xml b/vision/text/pom.xml index 2e3e15fbfb9..3f0768bb7f2 100644 --- a/vision/text/pom.xml +++ b/vision/text/pom.xml @@ -40,7 +40,7 @@ com.google.apis google-api-services-vision - v1-rev369-1.23.0 + v1-rev370-1.23.0 com.google.api-client