Skip to content

Commit 9e273b4

Browse files
committed
Add support for signUrl when default credentials are used
- Add a method to convert ApplicationDefaultCredentials to ServiceAccountAuthCredentials - Add type check and conversion to Storage.signUrl
1 parent 4106fc6 commit 9e273b4

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.api.client.json.jackson.JacksonFactory;
2929
import com.google.auth.http.HttpCredentialsAdapter;
3030
import com.google.auth.oauth2.GoogleCredentials;
31+
import com.google.auth.oauth2.ServiceAccountCredentials;
3132

3233
import java.io.IOException;
3334
import java.io.InputStream;
@@ -212,7 +213,7 @@ public RestorableState<AuthCredentials> capture() {
212213
}
213214
}
214215

215-
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
216+
public static class ApplicationDefaultAuthCredentials extends AuthCredentials {
216217

217218
private GoogleCredentials googleCredentials;
218219

@@ -255,6 +256,15 @@ protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
255256
return new HttpCredentialsAdapter(googleCredentials.createScoped(scopes));
256257
}
257258

259+
public ServiceAccountAuthCredentials toServiceAccountCredentials() {
260+
if (googleCredentials instanceof ServiceAccountCredentials) {
261+
ServiceAccountCredentials credentials = (ServiceAccountCredentials) googleCredentials;
262+
return new ServiceAccountAuthCredentials(credentials.getClientEmail(),
263+
credentials.getPrivateKey());
264+
}
265+
return null;
266+
}
267+
258268
@Override
259269
public RestorableState<AuthCredentials> capture() {
260270
return STATE;

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import com.google.common.hash.Hashing;
4444
import com.google.common.io.BaseEncoding;
4545
import com.google.common.primitives.Ints;
46+
import com.google.gcloud.AuthCredentials;
47+
import com.google.gcloud.AuthCredentials.ApplicationDefaultAuthCredentials;
4648
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
4749
import com.google.gcloud.PageImpl;
4850
import com.google.gcloud.BaseService;
@@ -584,9 +586,15 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
584586
ServiceAccountAuthCredentials cred =
585587
(ServiceAccountAuthCredentials) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
586588
if (cred == null) {
587-
checkArgument(options().authCredentials() instanceof ServiceAccountAuthCredentials,
588-
"Signing key was not provided and could not be derived");
589-
cred = (ServiceAccountAuthCredentials) this.options().authCredentials();
589+
AuthCredentials serviceCred = this.options().authCredentials();
590+
if (serviceCred instanceof ServiceAccountAuthCredentials) {
591+
cred = (ServiceAccountAuthCredentials) serviceCred;
592+
} else {
593+
if (serviceCred instanceof ApplicationDefaultAuthCredentials) {
594+
cred = ((ApplicationDefaultAuthCredentials) serviceCred).toServiceAccountCredentials();
595+
}
596+
}
597+
checkArgument(cred != null, "Signing key was not provided and could not be derived");
590598
}
591599
// construct signature - see https://cloud.google.com/storage/docs/access-control#Signed-URLs
592600
StringBuilder stBuilder = new StringBuilder();

0 commit comments

Comments
 (0)