-
Notifications
You must be signed in to change notification settings - Fork 174
/
S3MockStarter.java
301 lines (260 loc) · 9.82 KB
/
S3MockStarter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright 2017-2023 Adobe.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adobe.testing.s3mock.testsupport.common;
import static java.lang.String.join;
import static software.amazon.awssdk.http.SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES;
import com.adobe.testing.s3mock.S3MockApplication;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import java.net.Socket;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.utils.AttributeMap;
/**
* Helps configuring and starting the S3Mock app and provides a configured client for it.
*/
public abstract class S3MockStarter {
protected S3MockApplication s3MockFileStore;
protected final Map<String, Object> properties;
protected S3MockStarter(final Map<String, Object> properties) {
this.properties = defaultProps();
if (properties != null) {
this.properties.putAll(properties);
}
}
protected Map<String, Object> defaultProps() {
var args = new HashMap<String, Object>();
args.put(S3MockApplication.PROP_HTTPS_PORT, "0");
args.put(S3MockApplication.PROP_HTTP_PORT, "0");
return args;
}
/**
* Creates an {@link S3Client} client instance that is configured to call the started S3Mock
* server using HTTPS.
*
* @return The {@link S3Client} instance.
*/
public S3Client createS3ClientV2() {
return S3Client.builder()
.region(Region.of("us-east-1"))
.credentialsProvider(
StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar")))
.serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).build())
.endpointOverride(URI.create(getServiceEndpoint()))
.httpClient(UrlConnectionHttpClient.builder().buildWithDefaults(AttributeMap.builder()
.put(TRUST_ALL_CERTIFICATES, Boolean.TRUE)
.build()))
.build();
}
/**
* Creates an {@link AmazonS3} client instance that is configured to call the started S3Mock
* server using HTTPS.
*
* @return The {@link AmazonS3} instance.
*/
public AmazonS3 createS3Client() {
return createS3Client("us-east-1");
}
/**
* Creates an {@link AmazonS3} client instance that is configured to call the started S3Mock
* server using HTTPS for a given region.
*
* @param region Region to define regional endpoint.
*
* @return The {@link AmazonS3} instance.
*/
public AmazonS3 createS3Client(final String region) {
var credentials = new BasicAWSCredentials("foo", "bar");
return AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withClientConfiguration(
configureClientToIgnoreInvalidSslCertificates(new ClientConfiguration()))
.withEndpointConfiguration(getEndpointConfiguration(region))
.enablePathStyleAccess()
.build();
}
public int getPort() {
return s3MockFileStore.getPort();
}
public int getHttpPort() {
return s3MockFileStore.getHttpPort();
}
/**
* Registers a valid KMS key reference in the mock server.
*
* @param keyRef A KMS Key Reference
*/
public void registerKMSKeyRef(final String keyRef) {
s3MockFileStore.registerKMSKeyRef(keyRef);
}
/**
* Adjusts the given client configuration to allow the communication with the mock server using
* HTTPS, although that one uses a self-signed SSL certificate.
*
* @param clientConfiguration The {@link ClientConfiguration} to adjust.
*
* @return The adjusted instance.
*/
public ClientConfiguration configureClientToIgnoreInvalidSslCertificates(
final ClientConfiguration clientConfiguration) {
clientConfiguration.getApacheHttpClientConfig()
.withSslSocketFactory(new SSLConnectionSocketFactory(
createBlindlyTrustingSslContext(),
NoopHostnameVerifier.INSTANCE));
return clientConfiguration;
}
protected EndpointConfiguration getEndpointConfiguration(final String region) {
return new EndpointConfiguration(getServiceEndpoint(), region);
}
/**
* Returns endpoint URL for connecting to the mock server.
* @return endpoint URL for connecting to the mock server.
*/
public String getServiceEndpoint() {
var isSecureConnection = (boolean) properties.getOrDefault(
S3MockApplication.PROP_SECURE_CONNECTION, true);
return isSecureConnection ? "https://localhost:" + getPort()
: "http://localhost:" + getHttpPort();
}
protected void start() {
s3MockFileStore = S3MockApplication.start(properties);
}
protected void stop() {
s3MockFileStore.stop();
}
private SSLContext createBlindlyTrustingSslContext() {
try {
var sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[]{new X509ExtendedTrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(final X509Certificate[] arg0, final String arg1,
final Socket arg2) {
// no-op
}
@Override
public void checkClientTrusted(final X509Certificate[] arg0, final String arg1,
final SSLEngine arg2) {
// no-op
}
@Override
public void checkClientTrusted(final X509Certificate[] certs, final String authType) {
// no-op
}
@Override
public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
// no-op
}
@Override
public void checkServerTrusted(final X509Certificate[] arg0, final String arg1,
final Socket arg2) {
// no-op
}
@Override
public void checkServerTrusted(final X509Certificate[] arg0, final String arg1,
final SSLEngine arg2) {
// no-op
}
}
}, new java.security.SecureRandom());
return sc;
} catch (final NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException("Unexpected exception", e);
}
}
public abstract static class BaseBuilder<T extends S3MockStarter> {
protected final Map<String, Object> arguments = new HashMap<>();
public BaseBuilder<T> withProperty(String name, String value) {
arguments.put(name, value);
return this;
}
public BaseBuilder<T> withInitialBuckets(final String... initialBuckets) {
arguments.put(S3MockApplication.PROP_INITIAL_BUCKETS, join(",", initialBuckets));
return this;
}
public BaseBuilder<T> withHttpsPort(final int httpsPort) {
arguments.put(S3MockApplication.PROP_HTTPS_PORT, String.valueOf(httpsPort));
return this;
}
public BaseBuilder<T> withHttpPort(final int httpPort) {
arguments.put(S3MockApplication.PROP_HTTP_PORT, String.valueOf(httpPort));
return this;
}
public BaseBuilder<T> withRootFolder(final String rootFolder) {
arguments.put(S3MockApplication.PROP_ROOT_DIRECTORY, rootFolder);
return this;
}
public BaseBuilder<T> withSecureConnection(final boolean secureConnection) {
arguments.put(S3MockApplication.PROP_SECURE_CONNECTION, secureConnection);
return this;
}
/**
* Configures SSL parameters for the mock server.
* @param keyStore value for server.ssl.key-store
* @param keyStorePassword value for server.ssl.key-store-password
* @param keyAlias value for server.ssl.key-alias
* @param keyPassword value for server.ssl.key-password
* @return this builder
*/
public BaseBuilder<T> withSslParameters(
String keyStore, String keyStorePassword, String keyAlias, String keyPassword
) {
arguments.put(S3MockApplication.SERVER_SSL_KEY_STORE, keyStore);
arguments.put(S3MockApplication.SERVER_SSL_KEY_STORE_PASSWORD, keyStorePassword);
arguments.put(S3MockApplication.SERVER_SSL_KEY_ALIAS, keyAlias);
arguments.put(S3MockApplication.SERVER_SSL_KEY_PASSWORD, keyPassword);
return this;
}
/**
* Reduces logging level WARN and suppresses the startup banner.
*
* @return the builder
*/
public BaseBuilder<T> silent() {
arguments.put(S3MockApplication.PROP_SILENT, true);
return this;
}
/**
* Creates the instance.
*
* @return The configured instance.
*/
public abstract T build();
}
}