Skip to content

Commit 08e23ff

Browse files
committed
Rename SigningCredentials to ServiceAccountSigner
1 parent de3c47f commit 08e23ff

7 files changed

Lines changed: 76 additions & 55 deletions

File tree

gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public abstract class AuthCredentials implements Restorable<AuthCredentials> {
4444
* Represents built-in credentials when running in Google App Engine.
4545
*/
4646
public static class AppEngineAuthCredentials extends AuthCredentials
47-
implements SigningAuthCredentials {
47+
implements ServiceAccountSigner {
4848

4949
private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials();
5050
private static final AppEngineAuthCredentialsState STATE = new AppEngineAuthCredentialsState();
5151

5252
private AppEngineCredentials credentials;
5353

5454
private static class AppEngineCredentials extends GoogleCredentials
55-
implements SigningAuthCredentials {
55+
implements ServiceAccountSigner {
5656

5757
private final Object appIdentityService;
5858
private final String account;
@@ -191,7 +191,7 @@ public byte[] sign(byte[] toSign) {
191191
* User accounts and service accounts</a>
192192
*/
193193
public static class ServiceAccountAuthCredentials extends AuthCredentials
194-
implements SigningAuthCredentials {
194+
implements ServiceAccountSigner {
195195

196196
private final ServiceAccountCredentials credentials;
197197
private final String account;
@@ -232,15 +232,13 @@ public boolean equals(Object obj) {
232232
}
233233

234234
ServiceAccountAuthCredentials(String account, PrivateKey privateKey) {
235-
this.account = checkNotNull(account);
236-
this.privateKey = checkNotNull(privateKey);
237-
this.credentials = new ServiceAccountCredentials(null, account, privateKey, null, null);
235+
this(new ServiceAccountCredentials(null, account, privateKey, null, null));
238236
}
239237

240238
ServiceAccountAuthCredentials(ServiceAccountCredentials credentials) {
241239
this.credentials = checkNotNull(credentials);
242-
this.account = credentials.getClientEmail();
243-
this.privateKey = credentials.getPrivateKey();
240+
this.account = checkNotNull(credentials.getClientEmail());
241+
this.privateKey = checkNotNull(credentials.getPrivateKey());
244242
}
245243

246244
@Override

gcloud-java-core/src/main/java/com/google/gcloud/SigningAuthCredentials.java renamed to gcloud-java-core/src/main/java/com/google/gcloud/ServiceAccountSigner.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,48 @@
1616

1717
package com.google.gcloud;
1818

19+
import java.util.Objects;
20+
1921
/**
20-
* Interface for authentication credentials that support signing.
22+
* Interface for a service account signer. A signer for a service account is capable of signing
23+
* bytes using the private key associated with its service account.
2124
*/
22-
public interface SigningAuthCredentials {
25+
public interface ServiceAccountSigner {
2326

2427
class SigningException extends RuntimeException {
2528

29+
private static final long serialVersionUID = 8962780757822799255L;
30+
2631
SigningException(String message, Exception cause) {
2732
super(message, cause);
2833
}
34+
35+
@Override
36+
public boolean equals(Object obj) {
37+
if (obj == this) {
38+
return true;
39+
}
40+
if (!(obj instanceof SigningException)) {
41+
return false;
42+
}
43+
SigningException other = (SigningException) obj;
44+
return Objects.equals(getCause(), other.getCause())
45+
&& Objects.equals(getMessage(), other.getMessage());
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return Objects.hash(getMessage(), getCause());
51+
}
2952
}
3053

3154
/**
32-
* Returns the service account associated with the auth credentials.
55+
* Returns the service account associated with the signer.
3356
*/
3457
String account();
3558

3659
/**
37-
* Signs the provided bytes using the private key associated with the credentials.
60+
* Signs the provided bytes using the private key associated with the service account.
3861
*
3962
* @param toSign bytes to sign
4063
* @return signed bytes

gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.gcloud;
1818

1919
import com.google.common.collect.ImmutableList;
20+
import com.google.gcloud.ServiceAccountSigner.SigningException;
2021

2122
import java.io.ByteArrayInputStream;
2223
import java.io.IOException;
@@ -52,6 +53,8 @@ public Builder toBuilder() {
5253
private static final Identity IDENTITY = Identity.allAuthenticatedUsers();
5354
private static final PageImpl<String> PAGE =
5455
new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2"));
56+
private static final SigningException SIGNING_EXCEPTION =
57+
new SigningException("message", BASE_SERVICE_EXCEPTION);
5558
private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance();
5659
private static final SomeIamPolicy SOME_IAM_POLICY = new SomeIamPolicy.Builder().build();
5760
private static final String JSON_KEY = "{\n"
@@ -84,7 +87,7 @@ public Builder toBuilder() {
8487
@Override
8588
protected Serializable[] serializableObjects() {
8689
return new Serializable[]{BASE_SERVICE_EXCEPTION, EXCEPTION_HANDLER, IDENTITY, PAGE,
87-
RETRY_PARAMS, SOME_IAM_POLICY};
90+
RETRY_PARAMS, SOME_IAM_POLICY, SIGNING_EXCEPTION};
8891
}
8992

9093
@Override

gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, BlobInfo>
489489
private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo) {
490490
Blob blob = storage.get(blobInfo.blobId());
491491
System.out.println("Signed URL: "
492-
+ blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.serviceAccount(cred)));
492+
+ blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.signer(cred)));
493493
}
494494

495495
@Override

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import com.google.gcloud.AuthCredentials.AppEngineAuthCredentials;
2727
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
2828
import com.google.gcloud.ReadChannel;
29-
import com.google.gcloud.SigningAuthCredentials;
30-
import com.google.gcloud.SigningAuthCredentials.SigningException;
29+
import com.google.gcloud.ServiceAccountSigner;
30+
import com.google.gcloud.ServiceAccountSigner.SigningException;
3131
import com.google.gcloud.WriteChannel;
3232
import com.google.gcloud.storage.Storage.BlobTargetOption;
3333
import com.google.gcloud.storage.Storage.BlobWriteOption;
@@ -462,17 +462,16 @@ public WriteChannel writer(BlobWriteOption... options) {
462462
* this blob, you can use this method to generate a URL that is only valid within a certain time
463463
* period. This is particularly useful if you don't want publicly accessible blobs, but also don't
464464
* want to require users to explicitly log in. Signing a URL requires
465-
* authentication credentials capable of signing. If a {@link ServiceAccountAuthCredentials} or an
465+
* a service account signer. If a {@link ServiceAccountAuthCredentials} or an
466466
* {@link AppEngineAuthCredentials} was passed to
467-
* {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials
468-
* are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then
467+
* {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials are
468+
* being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then
469469
* {@code signUrl} will use that credentials to sign the URL. If the credentials passed to
470-
* {@link StorageOptions} do not implement {@link SigningAuthCredentials}
471-
* (this is the case for Compute Engine credentials and Google Cloud SDK credentials) then
472-
* {@code signUrl} will throw an {@link IllegalArgumentException} unless an implementation of
473-
* {@link SigningAuthCredentials} is passed using the {@code SignUrlOption.serviceAccount()}
474-
* option. The service account and private key passed with {@code SignUrlOption.serviceAccount()}
475-
* have priority over any credentials set with
470+
* {@link StorageOptions} do not implement {@link ServiceAccountSigner} (this is the case for
471+
* Compute Engine credentials and Google Cloud SDK credentials) then {@code signUrl} will throw an
472+
* {@link IllegalArgumentException} unless an implementation of {@link ServiceAccountSigner} is
473+
* passed using the {@code SignUrlOption.signer()} option. The signer passed with
474+
* {@code SignUrlOption.signer()} has priority over any credentials set with
476475
* {@link StorageOptions.Builder#authCredentials(AuthCredentials)}.
477476
*
478477
* <p>Example usage of creating a signed URL that is valid for 2 weeks, using the default
@@ -481,10 +480,10 @@ public WriteChannel writer(BlobWriteOption... options) {
481480
* blob.signUrl(14, TimeUnit.DAYS);
482481
* }</pre>
483482
*
484-
* <p>Example usage of creating a signed URL passing the {@code SignUrlOption.serviceAccount()}
485-
* option, that will be used for signing the URL:
483+
* <p>Example usage of creating a signed URL passing the {@code SignUrlOption.signer()} option,
484+
* that will be used for signing the URL:
486485
* <pre> {@code
487-
* blob.signUrl(14, TimeUnit.DAYS, SignUrlOption.serviceAccount(
486+
* blob.signUrl(14, TimeUnit.DAYS, SignUrlOption.signer(
488487
* AuthCredentials.createForJson(new FileInputStream("/path/to/key.json"))));
489488
* }</pre>
490489
*
@@ -493,8 +492,8 @@ public WriteChannel writer(BlobWriteOption... options) {
493492
* @param unit time unit of the {@code duration} parameter
494493
* @param options optional URL signing options
495494
* @return a signed URL for this blob and the specified options
496-
* @throws IllegalArgumentException if {@code SignUrlOption.serviceAccount()} was not used and no
497-
* implementation of {@link SigningAuthCredentials} was provided to {@link StorageOptions}
495+
* @throws IllegalArgumentException if {@code SignUrlOption.signer()} was not used and no
496+
* implementation of {@link ServiceAccountSigner} was provided to {@link StorageOptions}
498497
* @throws IllegalArgumentException if {@code SignUrlOption.withMd5()} option is used and
499498
* {@code blobInfo.md5()} is {@code null}
500499
* @throws IllegalArgumentException if {@code SignUrlOption.withContentType()} option is used and

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import com.google.gcloud.Page;
3131
import com.google.gcloud.ReadChannel;
3232
import com.google.gcloud.Service;
33-
import com.google.gcloud.SigningAuthCredentials;
34-
import com.google.gcloud.SigningAuthCredentials.SigningException;
33+
import com.google.gcloud.ServiceAccountSigner;
34+
import com.google.gcloud.ServiceAccountSigner.SigningException;
3535
import com.google.gcloud.WriteChannel;
3636
import com.google.gcloud.storage.spi.StorageRpc;
3737
import com.google.gcloud.storage.spi.StorageRpc.Tuple;
@@ -768,13 +768,13 @@ public static SignUrlOption withMd5() {
768768
}
769769

770770
/**
771-
* Signing credentials which should be used to sign the URL. If not provided an attempt will be
772-
* made to get it from the environment.
771+
* Provides a service account signer to sign the URL. If not provided an attempt will be made to
772+
* get it from the environment.
773773
*
774774
* @see <a href="https://cloud.google.com/storage/docs/authentication#service_accounts">Service
775775
* account</a>
776776
*/
777-
public static SignUrlOption serviceAccount(SigningAuthCredentials credentials) {
777+
public static SignUrlOption signer(ServiceAccountSigner credentials) {
778778
return new SignUrlOption(Option.SERVICE_ACCOUNT_CRED, credentials);
779779
}
780780
}
@@ -1474,17 +1474,16 @@ public static Builder builder() {
14741474
* fixed amount of time, you can use this method to generate a URL that is only valid within a
14751475
* certain time period. This is particularly useful if you don't want publicly accessible blobs,
14761476
* but also don't want to require users to explicitly log in. Signing a URL requires
1477-
* authentication credentials capable of signing. If a {@link ServiceAccountAuthCredentials} or an
1477+
* a service account signer. If a {@link ServiceAccountAuthCredentials} or an
14781478
* {@link AppEngineAuthCredentials} was passed to
1479-
* {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials
1480-
* are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then
1479+
* {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials are
1480+
* being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then
14811481
* {@code signUrl} will use that credentials to sign the URL. If the credentials passed to
1482-
* {@link StorageOptions} do not implement {@link SigningAuthCredentials}
1483-
* (this is the case for Compute Engine credentials and Google Cloud SDK credentials) then
1484-
* {@code signUrl} will throw an {@link IllegalArgumentException} unless an implementation of
1485-
* {@link SigningAuthCredentials} is passed using the {@code SignUrlOption.serviceAccount()}
1486-
* option. The service account and private key passed with {@code SignUrlOption.serviceAccount()}
1487-
* have priority over any credentials set with
1482+
* {@link StorageOptions} do not implement {@link ServiceAccountSigner} (this is the case for
1483+
* Compute Engine credentials and Google Cloud SDK credentials) then {@code signUrl} will throw an
1484+
* {@link IllegalArgumentException} unless an implementation of {@link ServiceAccountSigner} is
1485+
* passed using the {@code SignUrlOption.signer()} option. The signer passed with
1486+
* {@code SignUrlOption.signer()} has priority over any credentials set with
14881487
* {@link StorageOptions.Builder#authCredentials(AuthCredentials)}.
14891488
*
14901489
* <p>Example usage of creating a signed URL that is valid for 2 weeks, using the default
@@ -1493,11 +1492,11 @@ public static Builder builder() {
14931492
* service.signUrl(BlobInfo.builder("bucket", "name").build(), 14, TimeUnit.DAYS);
14941493
* }</pre>
14951494
*
1496-
* <p>Example usage of creating a signed URL passing the {@code SignUrlOption.serviceAccount()}
1497-
* option, that will be used for signing the URL:
1495+
* <p>Example usage of creating a signed URL passing the {@code SignUrlOption.signer()} option,
1496+
* that will be used for signing the URL:
14981497
* <pre> {@code
14991498
* service.signUrl(BlobInfo.builder("bucket", "name").build(), 14, TimeUnit.DAYS,
1500-
* SignUrlOption.serviceAccount(
1499+
* SignUrlOption.signer(
15011500
* AuthCredentials.createForJson(new FileInputStream("/path/to/key.json"))));
15021501
* }</pre>
15031502
*
@@ -1506,8 +1505,8 @@ public static Builder builder() {
15061505
* granularity supported is 1 second, finer granularities will be truncated
15071506
* @param unit time unit of the {@code duration} parameter
15081507
* @param options optional URL signing options
1509-
* @throws IllegalArgumentException if {@code SignUrlOption.serviceAccount()} was not used and no
1510-
* implementation of {@link SigningAuthCredentials} was provided to {@link StorageOptions}
1508+
* @throws IllegalArgumentException if {@code SignUrlOption.signer()} was not used and no
1509+
* implementation of {@link ServiceAccountSigner} was provided to {@link StorageOptions}
15111510
* @throws IllegalArgumentException if {@code SignUrlOption.withMd5()} option is used and
15121511
* {@code blobInfo.md5()} is {@code null}
15131512
* @throws IllegalArgumentException if {@code SignUrlOption.withContentType()} option is used and

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import com.google.gcloud.PageImpl.NextPageFetcher;
4747
import com.google.gcloud.ReadChannel;
4848
import com.google.gcloud.RetryHelper.RetryHelperException;
49-
import com.google.gcloud.SigningAuthCredentials;
49+
import com.google.gcloud.ServiceAccountSigner;
5050
import com.google.gcloud.storage.spi.StorageRpc;
5151
import com.google.gcloud.storage.spi.StorageRpc.RewriteResponse;
5252
import com.google.gcloud.storage.spi.StorageRpc.Tuple;
@@ -533,13 +533,12 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
533533
for (SignUrlOption option : options) {
534534
optionMap.put(option.option(), option.value());
535535
}
536-
SigningAuthCredentials authCredentials =
537-
(SigningAuthCredentials) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
536+
ServiceAccountSigner authCredentials =
537+
(ServiceAccountSigner) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
538538
if (authCredentials == null) {
539-
checkArgument(this.options().authCredentials() != null
540-
&& this.options().authCredentials() instanceof SigningAuthCredentials,
539+
checkArgument(this.options().authCredentials() instanceof ServiceAccountSigner,
541540
"Signing key was not provided and could not be derived");
542-
authCredentials = (SigningAuthCredentials) this.options().authCredentials();
541+
authCredentials = (ServiceAccountSigner) this.options().authCredentials();
543542
}
544543
// construct signature - see https://cloud.google.com/storage/docs/access-control#Signed-URLs
545544
StringBuilder stBuilder = new StringBuilder();

0 commit comments

Comments
 (0)