2525
2626import com .google .api .client .util .Data ;
2727import com .google .api .core .ApiFuture ;
28+ import com .google .api .gax .grpc .GrpcStatusCode ;
2829import com .google .api .gax .paging .Page ;
2930import com .google .api .gax .retrying .BasicResultRetryAlgorithm ;
3031import com .google .api .gax .retrying .ResultRetryAlgorithm ;
32+ import com .google .api .gax .rpc .EndpointContext ;
33+ import com .google .api .gax .rpc .UnauthenticatedException ;
3134import com .google .api .services .storage .model .BucketAccessControl ;
3235import com .google .api .services .storage .model .ObjectAccessControl ;
3336import com .google .api .services .storage .model .StorageObject ;
37+ import com .google .auth .Credentials ;
3438import com .google .auth .ServiceAccountSigner ;
39+ import com .google .auth .oauth2 .ComputeEngineCredentials ;
40+ import com .google .auth .oauth2 .GdchCredentials ;
3541import com .google .cloud .BaseService ;
3642import com .google .cloud .BatchResult ;
43+ import com .google .cloud .NoCredentials ;
3744import com .google .cloud .PageImpl ;
3845import com .google .cloud .PageImpl .NextPageFetcher ;
3946import com .google .cloud .Policy ;
6673import com .google .common .collect .Maps ;
6774import com .google .common .io .BaseEncoding ;
6875import com .google .common .io .CountingOutputStream ;
76+ import io .grpc .Status ;
6977import java .io .ByteArrayInputStream ;
7078import java .io .IOException ;
7179import java .io .InputStream ;
8795import java .util .EnumMap ;
8896import java .util .HashMap ;
8997import java .util .HashSet ;
98+ import java .util .LinkedHashMap ;
9099import java .util .List ;
91100import java .util .Map ;
92101import java .util .Set ;
@@ -126,8 +135,7 @@ final class StorageImpl extends BaseService<StorageOptions> implements Storage,
126135 this .storageRpc = options .getStorageRpcV1 ();
127136 this .writerFactory = writerFactory ;
128137 try {
129- String resolvedApiaryHost = options .getResolvedApiaryHost ("storage" );
130- URI uri = new URI (resolvedApiaryHost );
138+ URI uri = new URI (options .getHost ());
131139 STORAGE_XML_URI_HOST_NAME = uri .getHost ();
132140 STORAGE_XML_URI_SCHEME = firstNonNull (uri .getScheme (), "https" );
133141 } catch (URISyntaxException e ) {
@@ -890,6 +898,7 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
890898 "Signing key was not provided and could not be derived" );
891899 credentials = (ServiceAccountSigner ) this .getOptions ().getCredentials ();
892900 }
901+ validateSigningCredentialsUniverseDomain (credentials );
893902
894903 long expiration =
895904 isV4
@@ -997,6 +1006,7 @@ public PostPolicyV4 generateSignedPostPolicyV4(
9971006 "Signing key was not provided and could not be derived" );
9981007 credentials = (ServiceAccountSigner ) this .getOptions ().getCredentials ();
9991008 }
1009+ validateSigningCredentialsUniverseDomain (credentials );
10001010
10011011 checkArgument (
10021012 !(optionMap .containsKey (SignUrlOption .Option .VIRTUAL_HOSTED_STYLE )
@@ -1106,6 +1116,46 @@ public PostPolicyV4 generateSignedPostPolicyV4(
11061116 blobInfo , duration , unit , PostFieldsV4 .newBuilder ().build (), options );
11071117 }
11081118
1119+ private void validateSigningCredentialsUniverseDomain (ServiceAccountSigner signer ) {
1120+ Credentials credentialsToValidate = null ;
1121+ if (signer instanceof Credentials ) {
1122+ credentialsToValidate = (Credentials ) signer ;
1123+ } else {
1124+ Credentials clientCredentials = getOptions ().getCredentials ();
1125+ if (clientCredentials != null && !(clientCredentials instanceof NoCredentials )) {
1126+ credentialsToValidate = clientCredentials ;
1127+ }
1128+ }
1129+ if (credentialsToValidate == null
1130+ || credentialsToValidate instanceof GdchCredentials
1131+ || credentialsToValidate instanceof ComputeEngineCredentials ) {
1132+ return ;
1133+ }
1134+ String credentialsUniverseDomain ;
1135+ try {
1136+ credentialsUniverseDomain =
1137+ firstNonNull (
1138+ credentialsToValidate .getUniverseDomain (), Credentials .GOOGLE_DEFAULT_UNIVERSE );
1139+ } catch (IOException e ) {
1140+ throw StorageException .translate (e );
1141+ }
1142+ String configuredUniverseDomain = getOptions ().getResolvedUniverseDomain ();
1143+ if (!configuredUniverseDomain .equals (credentialsUniverseDomain )) {
1144+ String errorMessage =
1145+ String .format (
1146+ EndpointContext .INVALID_UNIVERSE_DOMAIN_ERROR_TEMPLATE ,
1147+ configuredUniverseDomain ,
1148+ credentialsUniverseDomain );
1149+ UnauthenticatedException unauthenticatedException =
1150+ new UnauthenticatedException (
1151+ errorMessage ,
1152+ new Throwable (errorMessage ),
1153+ GrpcStatusCode .of (Status .Code .UNAUTHENTICATED ),
1154+ false );
1155+ throw StorageException .asStorageException (unauthenticatedException );
1156+ }
1157+ }
1158+
11091159 private String constructResourceUriPath (
11101160 String slashlessBucketName ,
11111161 String escapedBlobName ,
@@ -1202,27 +1252,31 @@ private SignatureInfo buildSignatureInfo(
12021252
12031253 signatureInfoBuilder .setTimestamp (getOptions ().getClock ().millisTime ());
12041254
1205- ImmutableMap . Builder <String , String > extHeadersBuilder = new ImmutableMap . Builder <>();
1255+ Map <String , String > extHeaders = new LinkedHashMap <>();
12061256
12071257 boolean isV4 =
12081258 SignUrlOption .SignatureVersion .V4 .equals (
12091259 optionMap .get (SignUrlOption .Option .SIGNATURE_VERSION ));
12101260 if (isV4 ) { // We don't sign the host header for V2 signed URLs; only do this for V4.
12111261 // Add the host here first, allowing it to be overridden in the EXT_HEADERS option below.
12121262 if (optionMap .containsKey (SignUrlOption .Option .VIRTUAL_HOSTED_STYLE )) {
1213- extHeadersBuilder .put (
1263+ extHeaders .put (
12141264 "host" ,
12151265 slashlessBucketNameFromBlobInfo (blobInfo ) + "." + getBaseStorageHostName (optionMap ));
12161266 } else if (optionMap .containsKey (SignUrlOption .Option .HOST_NAME )
12171267 || optionMap .containsKey (SignUrlOption .Option .BUCKET_BOUND_HOST_NAME )
1218- || getOptions ().getUniverseDomain () != null ) {
1219- extHeadersBuilder .put ("host" , getBaseStorageHostName (optionMap ));
1268+ || getOptions ().getConfiguredUniverseDomain () != null ) {
1269+ extHeaders .put ("host" , getBaseStorageHostName (optionMap ));
12201270 }
12211271 }
12221272
12231273 if (optionMap .containsKey (SignUrlOption .Option .EXT_HEADERS )) {
1224- extHeadersBuilder .putAll (
1225- (Map <String , String >) optionMap .get (SignUrlOption .Option .EXT_HEADERS ));
1274+ Map <String , String > userExtHeaders =
1275+ (Map <String , String >) optionMap .get (SignUrlOption .Option .EXT_HEADERS );
1276+ for (Map .Entry <String , String > entry : userExtHeaders .entrySet ()) {
1277+ String key = "host" .equalsIgnoreCase (entry .getKey ()) ? "host" : entry .getKey ();
1278+ extHeaders .put (key , entry .getValue ());
1279+ }
12261280 }
12271281
12281282 ImmutableMap .Builder <String , String > queryParamsBuilder = new ImmutableMap .Builder <>();
@@ -1232,7 +1286,7 @@ private SignatureInfo buildSignatureInfo(
12321286 }
12331287
12341288 return signatureInfoBuilder
1235- .setCanonicalizedExtensionHeaders (extHeadersBuilder . build ( ))
1289+ .setCanonicalizedExtensionHeaders (ImmutableMap . copyOf ( extHeaders ))
12361290 .setCanonicalizedQueryParams (queryParamsBuilder .build ())
12371291 .build ();
12381292 }
0 commit comments