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 .axdbfrontend ;
17
+
18
+ import java .time .Clock ;
19
+ import java .time .Instant ;
20
+ import software .amazon .awssdk .annotations .Immutable ;
21
+ import software .amazon .awssdk .annotations .SdkInternalApi ;
22
+ import software .amazon .awssdk .auth .credentials .AwsCredentials ;
23
+ import software .amazon .awssdk .auth .credentials .CredentialUtils ;
24
+ import software .amazon .awssdk .auth .signer .Aws4Signer ;
25
+ import software .amazon .awssdk .auth .signer .params .Aws4PresignerParams ;
26
+ import software .amazon .awssdk .awscore .client .config .AwsClientOption ;
27
+ import software .amazon .awssdk .core .client .config .SdkClientConfiguration ;
28
+ import software .amazon .awssdk .http .SdkHttpFullRequest ;
29
+ import software .amazon .awssdk .http .SdkHttpMethod ;
30
+ import software .amazon .awssdk .identity .spi .AwsCredentialsIdentity ;
31
+ import software .amazon .awssdk .identity .spi .IdentityProvider ;
32
+ import software .amazon .awssdk .regions .Region ;
33
+ import software .amazon .awssdk .services .axdbfrontend .model .GenerateAuthenticationTokenRequest ;
34
+ import software .amazon .awssdk .utils .CompletableFutureUtils ;
35
+ import software .amazon .awssdk .utils .Logger ;
36
+ import software .amazon .awssdk .utils .StringUtils ;
37
+
38
+ @ Immutable
39
+ @ SdkInternalApi
40
+ final class DefaultAxdbFrontendUtilities implements AxdbFrontendUtilities {
41
+ private static final Logger log = Logger .loggerFor (AxdbFrontendUtilities .class );
42
+ private final Aws4Signer signer = Aws4Signer .create ();
43
+ private final Region region ;
44
+ private final IdentityProvider <? extends AwsCredentialsIdentity > credentialsProvider ;
45
+ private final Clock clock ;
46
+
47
+ DefaultAxdbFrontendUtilities (DefaultBuilder builder ) {
48
+ this (builder , Clock .systemUTC ());
49
+ }
50
+
51
+ /**
52
+ * For testing purposes only
53
+ */
54
+ DefaultAxdbFrontendUtilities (DefaultBuilder builder , Clock clock ) {
55
+ this .credentialsProvider = builder .credentialsProvider ;
56
+ this .region = builder .region ;
57
+ this .clock = clock ;
58
+ }
59
+
60
+ /**
61
+ * Used by AxdbFrontend low-level client's utilities() method
62
+ */
63
+ @ SdkInternalApi
64
+ static AxdbFrontendUtilities create (SdkClientConfiguration clientConfiguration ) {
65
+ return new DefaultBuilder ().clientConfiguration (clientConfiguration ).build ();
66
+ }
67
+
68
+ @ Override
69
+ public String generateAuthenticationToken (GenerateAuthenticationTokenRequest request ) {
70
+ SdkHttpFullRequest httpRequest = SdkHttpFullRequest .builder ()
71
+ .method (SdkHttpMethod .GET )
72
+ .protocol ("https" )
73
+ .host (request .hostname ())
74
+ .encodedPath ("/" )
75
+ .putRawQueryParameter ("Action" , request .action ().name ())
76
+ .build ();
77
+
78
+ Instant expirationTime = Instant .now (clock ).plus (request .expiresIn ());
79
+
80
+ Aws4PresignerParams presignRequest = Aws4PresignerParams .builder ()
81
+ .signingClockOverride (clock )
82
+ .expirationTime (expirationTime )
83
+ .awsCredentials (resolveCredentials (request ))
84
+ .signingName ("xanadu" )
85
+ .signingRegion (resolveRegion (request ))
86
+ .build ();
87
+
88
+ SdkHttpFullRequest fullRequest = signer .presign (httpRequest , presignRequest );
89
+ String signedUrl = fullRequest .getUri ().toString ();
90
+
91
+ String result = StringUtils .replacePrefixIgnoreCase (signedUrl , "https://" , "" );
92
+ return result ;
93
+ }
94
+
95
+ private Region resolveRegion (GenerateAuthenticationTokenRequest request ) {
96
+ if (request .region () != null ) {
97
+ return request .region ();
98
+ }
99
+
100
+ if (this .region != null ) {
101
+ return this .region ;
102
+ }
103
+
104
+ throw new IllegalArgumentException ("Region should be provided either in GenerateAuthenticationTokenRequest object " +
105
+ "or AxdbFrontendUtilities object" );
106
+ }
107
+
108
+ private AwsCredentials resolveCredentials (GenerateAuthenticationTokenRequest request ) {
109
+ if (request .credentialsIdentityProvider () != null ) {
110
+ return CredentialUtils .toCredentials (
111
+ CompletableFutureUtils .joinLikeSync (request .credentialsIdentityProvider ().resolveIdentity ()));
112
+ }
113
+
114
+ if (this .credentialsProvider != null ) {
115
+ return CredentialUtils .toCredentials (CompletableFutureUtils .joinLikeSync (this .credentialsProvider .resolveIdentity ()));
116
+ }
117
+
118
+ throw new IllegalArgumentException ("CredentialProvider should be provided either in GenerateAuthenticationTokenRequest " +
119
+ "object or AxdbFrontendUtilities object" );
120
+ }
121
+
122
+ @ SdkInternalApi
123
+ static final class DefaultBuilder implements Builder {
124
+ private Region region ;
125
+ private IdentityProvider <? extends AwsCredentialsIdentity > credentialsProvider ;
126
+
127
+ DefaultBuilder () {
128
+ }
129
+
130
+ Builder clientConfiguration (SdkClientConfiguration clientConfiguration ) {
131
+ this .credentialsProvider = clientConfiguration .option (AwsClientOption .CREDENTIALS_IDENTITY_PROVIDER );
132
+ this .region = clientConfiguration .option (AwsClientOption .AWS_REGION );
133
+
134
+ return this ;
135
+ }
136
+
137
+ @ Override
138
+ public Builder region (Region region ) {
139
+ this .region = region ;
140
+ return this ;
141
+ }
142
+
143
+ @ Override
144
+ public Builder credentialsProvider (IdentityProvider <? extends AwsCredentialsIdentity > credentialsProvider ) {
145
+ this .credentialsProvider = credentialsProvider ;
146
+ return this ;
147
+ }
148
+
149
+ /**
150
+ * Construct a {@link AxdbFrontendUtilities} object.
151
+ */
152
+ @ Override
153
+ public AxdbFrontendUtilities build () {
154
+ return new DefaultAxdbFrontendUtilities (this );
155
+ }
156
+ }
157
+ }
0 commit comments