• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.services.s3.endpoints.internal;
17 
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.function.Function;
21 import software.amazon.awssdk.annotations.SdkInternalApi;
22 import software.amazon.awssdk.awscore.endpoints.authscheme.EndpointAuthScheme;
23 import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4AuthScheme;
24 import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4aAuthScheme;
25 import software.amazon.awssdk.services.s3.endpoints.authscheme.S3ExpressEndpointAuthScheme;
26 
27 @SdkInternalApi
28 public final class S3EndpointAuthSchemeStrategyFactory implements EndpointAuthSchemeStrategyFactory {
29 
30     public static final String SIGNING_NAME_ID = "signingName";
31     public static final String SIGNING_REGION_SET_ID = "signingRegionSet";
32     public static final String DISABLE_DOUBLE_ENCODING_ID = "disableDoubleEncoding";
33     public static final String SIGNING_REGION_ID = "signingRegion";
34 
35     private static final String SIGV4_NAME = "sigv4";
36     private static final String SIGV4A_NAME = "sigv4a";
37     private static final String S3EXPRESS_NAME = "sigv4-s3express";
38 
39     @Override
endpointAuthSchemeStrategy()40     public EndpointAuthSchemeStrategy endpointAuthSchemeStrategy() {
41         Map<String, Function<Value.Record, EndpointAuthScheme>> knownAuthSchemesMapping = new HashMap<>();
42         knownAuthSchemesMapping.put(SIGV4A_NAME, this::sigV4A);
43         knownAuthSchemesMapping.put(SIGV4_NAME, this::sigV4);
44         knownAuthSchemesMapping.put(S3EXPRESS_NAME, this::s3Express);
45         return new DefaultEndpointAuthSchemeStrategy(knownAuthSchemesMapping);
46     }
47 
sigV4A(Value.Record scheme)48     private EndpointAuthScheme sigV4A(Value.Record scheme) {
49         SigV4aAuthScheme.Builder schemeBuilder = SigV4aAuthScheme.builder();
50 
51         Value signingName = scheme.get(Identifier.of(SIGNING_NAME_ID));
52         if (signingName != null) {
53             schemeBuilder.signingName(signingName.expectString());
54         }
55 
56         Value signingRegionSet = scheme.get(Identifier.of(SIGNING_REGION_SET_ID));
57         if (signingRegionSet != null) {
58             Value.Array signingRegionSetArray = signingRegionSet.expectArray();
59             for (int j = 0; j < signingRegionSetArray.size(); ++j) {
60                 schemeBuilder.addSigningRegion(signingRegionSetArray.get(j).expectString());
61             }
62         }
63 
64         Value disableDoubleEncoding = scheme.get(Identifier.of(DISABLE_DOUBLE_ENCODING_ID));
65         if (disableDoubleEncoding != null) {
66             schemeBuilder.disableDoubleEncoding(disableDoubleEncoding.expectBool());
67         }
68 
69         return schemeBuilder.build();
70     }
71 
sigV4(Value.Record scheme)72     private EndpointAuthScheme sigV4(Value.Record scheme) {
73         SigV4AuthScheme.Builder schemeBuilder = SigV4AuthScheme.builder();
74 
75         Value signingName = scheme.get(Identifier.of(SIGNING_NAME_ID));
76         if (signingName != null) {
77             schemeBuilder.signingName(signingName.expectString());
78         }
79 
80         Value signingRegion = scheme.get(Identifier.of(SIGNING_REGION_ID));
81         if (signingRegion != null) {
82             schemeBuilder.signingRegion(signingRegion.expectString());
83         }
84 
85         Value disableDoubleEncoding = scheme.get(Identifier.of(DISABLE_DOUBLE_ENCODING_ID));
86         if (disableDoubleEncoding != null) {
87             schemeBuilder.disableDoubleEncoding(disableDoubleEncoding.expectBool());
88         }
89 
90         return schemeBuilder.build();
91     }
92 
s3Express(Value.Record scheme)93     private EndpointAuthScheme s3Express(Value.Record scheme) {
94         S3ExpressEndpointAuthScheme.Builder schemeBuilder = S3ExpressEndpointAuthScheme.builder();
95 
96         Value signingName = scheme.get(Identifier.of(SIGNING_NAME_ID));
97         if (signingName != null) {
98             schemeBuilder.signingName(signingName.expectString());
99         }
100 
101         Value signingRegion = scheme.get(Identifier.of(SIGNING_REGION_ID));
102         if (signingRegion != null) {
103             schemeBuilder.signingRegion(signingRegion.expectString());
104         }
105 
106         Value disableDoubleEncoding = scheme.get(Identifier.of(DISABLE_DOUBLE_ENCODING_ID));
107         if (disableDoubleEncoding != null) {
108             schemeBuilder.disableDoubleEncoding(disableDoubleEncoding.expectBool());
109         }
110 
111         return schemeBuilder.build();
112     }
113 }
114