From af94b5312daa978b693ce38b5f2314a335f3d531 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 1 Dec 2015 15:33:03 -0800 Subject: [PATCH] cleanup + cast fix --- .../com/google/gcloud/AuthCredentials.java | 24 ++++++++----------- .../google/gcloud/storage/StorageImpl.java | 16 +++++++------ 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java index 7f5d7a8ab89b..f3a0024129d7 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java @@ -46,7 +46,6 @@ private static class AppEngineCredentials extends GoogleCredentials { private final Method getAccessToken; private final Method getAccessTokenResult; private final Collection scopes; - private final boolean scopesRequired; AppEngineCredentials() { try { @@ -61,19 +60,16 @@ private static class AppEngineCredentials extends GoogleCredentials { this.getAccessTokenResult = serviceClass.getMethod("getAccessToken", Iterable.class); this.getAccessToken = tokenResultClass.getMethod("getAccessToken"); this.scopes = null; - this.scopesRequired = true; } catch (Exception e) { - throw new RuntimeException("Could not create AppEngineCredentials using reflection."); + throw new RuntimeException("Could not create AppEngineCredentials.", e); } } - AppEngineCredentials(Collection scopes, Object appIdentityService, - Method getAccessToken, Method getAccessTokenResult) { - this.appIdentityService = appIdentityService; - this.getAccessToken = getAccessToken; - this.getAccessTokenResult = getAccessTokenResult; + AppEngineCredentials(Collection scopes, AppEngineCredentials unscoped) { + this.appIdentityService = unscoped.appIdentityService; + this.getAccessToken = unscoped.getAccessToken; + this.getAccessTokenResult = unscoped.getAccessTokenResult; this.scopes = scopes; - this.scopesRequired = (scopes == null || scopes.isEmpty()); } /** @@ -89,19 +85,18 @@ public AccessToken refreshAccessToken() throws IOException { String accessToken = (String) getAccessToken.invoke(accessTokenResult); return new AccessToken(accessToken, null); } catch (Exception e) { - throw new RuntimeException("Could not get the access token using reflection."); + throw new IOException("Could not get the access token.", e); } } @Override public boolean createScopedRequired() { - return scopesRequired; + return scopes == null || scopes.isEmpty(); } @Override public GoogleCredentials createScoped(Collection scopes) { - return new AppEngineCredentials( - scopes, appIdentityService, getAccessToken, getAccessTokenResult); + return new AppEngineCredentials(scopes, this); } } @@ -308,6 +303,7 @@ public static ServiceAccountAuthCredentials createForJson(InputStream jsonCreden tempServiceAccountCredentials.getClientEmail(), tempServiceAccountCredentials.getPrivateKey()); } - throw new IOException("The given JSON Credentials Stream is not a service account credential."); + throw new IOException( + "The given JSON Credentials Stream is not for a service account credential."); } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java index fdb643c725ac..9207e4905c54 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java @@ -562,16 +562,18 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio for (SignUrlOption option : options) { optionMap.put(option.option(), option.value()); } - ServiceAccountAuthCredentials cred = + ServiceAccountAuthCredentials serviceAccountAuthCred = (ServiceAccountAuthCredentials) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED); - if (cred == null) { - AuthCredentials authCredentials = this.options().authCredentials(); + ServiceAccountCredentials cred = (ServiceAccountCredentials) (serviceAccountAuthCred != null + ? serviceAccountAuthCred.credentials() : null); + if (serviceAccountAuthCred == null) { + AuthCredentials authCred = this.options().authCredentials(); GoogleCredentials serviceCred = - authCredentials != null ? authCredentials.credentials() : null; + authCred != null ? authCred.credentials() : null; checkArgument( serviceCred instanceof ServiceAccountCredentials, "Signing key was not provided and could not be derived"); - cred = (ServiceAccountAuthCredentials) authCredentials; + cred = (ServiceAccountCredentials) serviceCred; } // construct signature - see https://cloud.google.com/storage/docs/access-control#Signed-URLs StringBuilder stBuilder = new StringBuilder(); @@ -607,12 +609,12 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio stBuilder.append(path); try { Signature signer = Signature.getInstance("SHA256withRSA"); - signer.initSign(cred.privateKey()); + signer.initSign(cred.getPrivateKey()); signer.update(stBuilder.toString().getBytes(UTF_8)); String signature = URLEncoder.encode(BaseEncoding.base64().encode(signer.sign()), UTF_8.name()); stBuilder = new StringBuilder("https://storage.googleapis.com").append(path); - stBuilder.append("?GoogleAccessId=").append(cred.account()); + stBuilder.append("?GoogleAccessId=").append(cred.getClientEmail()); stBuilder.append("&Expires=").append(expiration); stBuilder.append("&Signature=").append(signature); return new URL(stBuilder.toString());