forked from TritonDataCenter/java-manta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthAwareConfigContext.java
491 lines (402 loc) · 13.7 KB
/
AuthAwareConfigContext.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* Copyright (c) 2017, Joyent, Inc. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.joyent.manta.config;
import com.joyent.http.signature.Signer;
import com.joyent.http.signature.ThreadLocalSigner;
import com.joyent.http.signature.apache.httpclient.HttpSignatureAuthScheme;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import java.security.KeyPair;
import java.util.Objects;
/**
* Object for tracking configuration changes related to authentication and recreating dependent objects as needed.
* It combines the users' configuration with the derived runtime objects needed to authenticate requests.
* objects like the {@link ThreadLocalSigner} which needs careful lifecycle management.
*
* As far as users are concerned, this class is just as thread-safe as every other {@link ConfigContext} (i.e. generally
* not) but we're using a private object as a lock in order to at least synchronize reloads and field updates.
*
* @author <a href="https://github.com/tjcelaya">Tomas Celaya</a>
* @since 3.1.7
*/
public class AuthAwareConfigContext
extends StandardConfigContext
implements AutoCloseable {
/**
* Private lock object for synchronizing updates to fields and building a derived {@link AuthContext}.
*/
private final Object lock = new Object();
/**
* Atomic reference to objects we provide.
*/
private volatile AuthContext authContext;
/**
* Build an empty AuthAwareConfigContext.
*/
public AuthAwareConfigContext() {
this(null);
}
/**
* Build an AuthAwareConfigContext from an existing {@link ConfigContext}.
*
* @param config configuration context from which to extract values
*/
public AuthAwareConfigContext(final ConfigContext config) {
if (config != null) {
overwriteWithContext(config);
}
reload();
}
/**
* Check the configuration for authentication-related changes. Clean up old authentication objects which might
* still exist and load new instances.
*
* @return this after reload
*/
@SuppressWarnings("unchecked")
public AuthAwareConfigContext reload() {
synchronized (lock) {
final int newParamsFingerprint = calculateAuthParamsFingerprint(this);
if (authContext != null) {
if (authContext.paramsFingerprint == newParamsFingerprint) {
return this;
} else {
authContext.signer.clearAll();
authContext = null;
}
}
if (BooleanUtils.isNotTrue(noAuth())) {
authContext = doLoad(newParamsFingerprint);
}
}
return this;
}
/**
* Internal method for updating authentication parameters and derived objects.
*
* @param paramsFingerprint identifier for the new AuthContext
* @return the new {@link AuthContext}
*/
private AuthContext doLoad(final int paramsFingerprint) {
if (BooleanUtils.isNotFalse(noAuth())) {
return null;
}
final KeyPair keyPair = new KeyPairFactory(this).createKeyPair();
final Signer.Builder builder = new Signer.Builder(keyPair);
if (BooleanUtils.isTrue(disableNativeSignatures())) {
// DefaultConfigContext#DEFAULT_DISABLE_NATIVE_SIGNATURES is false
builder.providerCode("stdlib");
}
final ThreadLocalSigner signer = new ThreadLocalSigner(builder);
return new AuthContext(
paramsFingerprint,
keyPair,
signer,
new UsernamePasswordCredentials(getMantaUser(), null),
new HttpSignatureAuthScheme(keyPair, signer));
}
/**
* This getter is public as a result of package organization.
* Users are strongly discouraged from directly interacting with this object.
*
* @return the credentials used to sign requests
*/
public Credentials getCredentials() {
if (authContext == null) {
return null;
}
return authContext.credentials;
}
/**
* This getter is public as a result of package organization.
* Users are strongly discouraged from directly interacting with this object.
*
* @return the auth scheme which does request signing
*/
public HttpSignatureAuthScheme getAuthScheme() {
if (authContext == null) {
return null;
}
return authContext.authScheme;
}
/**
* This getter is public as a result of package organization.
* Users are strongly discouraged from directly interacting with this object.
*
* @return the keypair used to sign requests
*/
public KeyPair getKeyPair() {
if (authContext == null) {
return null;
}
return authContext.keyPair;
}
/**
* This getter is public as a result of package organization.
* Users are strongly discouraged from directly interacting with this object.
*
* @return the object used to sign requests
*/
public ThreadLocalSigner getSigner() {
if (authContext == null) {
return null;
}
return authContext.signer;
}
/**
* Calculate a hashcode of the currently-used configuration parameters.
*
* @param config ConfigContext from which to read authentication parameters
* @return the computed hashcode
*/
private static int calculateAuthParamsFingerprint(final ConfigContext config) {
return Objects.hash(
config.noAuth(),
config.disableNativeSignatures(),
config.getMantaUser(),
config.getPassword(),
config.getMantaKeyId(),
config.getMantaKeyPath(),
config.getPrivateKeyContent());
}
@Override
public void close() throws Exception {
if (authContext != null) {
authContext.signer.clearAll();
}
authContext = null;
}
/**
* Class for holding references to bundled authentication objects so they can be swapped out atomically.
*/
private static final class AuthContext {
/**
* (Mostly) unique identifier for the config parameters which produced this AuthContext.
*/
private final int paramsFingerprint;
/**
* Reference to loaded KeyPair.
*/
private final KeyPair keyPair;
/**
* Reference to signing object built from {@link #keyPair}.
*/
private final ThreadLocalSigner signer;
/**
* Credentials object used for authenticating requests.
*/
private final Credentials credentials;
/**
* Strategy object for generating headers when generating authenticated requests.
*/
private final HttpSignatureAuthScheme authScheme;
@SuppressWarnings("JavadocMethod")
private AuthContext(final int paramsFingerprint,
final KeyPair keyPair,
final ThreadLocalSigner signer,
final Credentials credentials,
final HttpSignatureAuthScheme authScheme) {
this.paramsFingerprint = paramsFingerprint;
this.keyPair = keyPair;
this.signer = signer;
this.credentials = credentials;
this.authScheme = authScheme;
}
}
@Override
public AuthAwareConfigContext setMantaURL(final String mantaURL) {
synchronized (lock) {
super.setMantaURL(mantaURL);
}
return this;
}
@Override
public AuthAwareConfigContext setMantaUser(final String mantaUser) {
synchronized (lock) {
super.setMantaUser(mantaUser);
}
return this;
}
@Override
public AuthAwareConfigContext setMantaKeyId(final String mantaKeyId) {
synchronized (lock) {
super.setMantaKeyId(mantaKeyId);
}
return this;
}
@Override
public AuthAwareConfigContext setMantaKeyPath(final String mantaKeyPath) {
synchronized (lock) {
super.setMantaKeyPath(mantaKeyPath);
}
return this;
}
@Override
public AuthAwareConfigContext setTimeout(final Integer timeout) {
synchronized (lock) {
super.setTimeout(timeout);
}
return this;
}
@Override
public AuthAwareConfigContext setRetries(final Integer retries) {
synchronized (lock) {
super.setRetries(retries);
}
return this;
}
@Override
public AuthAwareConfigContext setMaximumConnections(final Integer maxConns) {
synchronized (lock) {
super.setMaximumConnections(maxConns);
}
return this;
}
@Override
public AuthAwareConfigContext setPrivateKeyContent(final String privateKeyContent) {
synchronized (lock) {
super.setPrivateKeyContent(privateKeyContent);
}
return this;
}
@Override
public AuthAwareConfigContext setPassword(final String password) {
synchronized (lock) {
super.setPassword(password);
}
return this;
}
@Override
public AuthAwareConfigContext setHttpBufferSize(final Integer httpBufferSize) {
synchronized (lock) {
super.setHttpBufferSize(httpBufferSize);
}
return this;
}
@Override
public AuthAwareConfigContext setHttpsProtocols(final String httpsProtocols) {
synchronized (lock) {
super.setHttpsProtocols(httpsProtocols);
}
return this;
}
@Override
public AuthAwareConfigContext setHttpsCipherSuites(final String httpsCipherSuites) {
synchronized (lock) {
super.setHttpsCipherSuites(httpsCipherSuites);
}
return this;
}
@Override
public AuthAwareConfigContext setNoAuth(final Boolean noAuth) {
synchronized (lock) {
super.setNoAuth(noAuth);
}
return this;
}
@Override
public AuthAwareConfigContext setDisableNativeSignatures(final Boolean disableNativeSignatures) {
synchronized (lock) {
super.setDisableNativeSignatures(disableNativeSignatures);
}
return this;
}
@Override
public AuthAwareConfigContext setTcpSocketTimeout(final Integer tcpSocketTimeout) {
synchronized (lock) {
super.setTcpSocketTimeout(tcpSocketTimeout);
}
return this;
}
@Override
public AuthAwareConfigContext setConnectionRequestTimeout(final Integer connectionRequestTimeout) {
synchronized (lock) {
super.setConnectionRequestTimeout(connectionRequestTimeout);
}
return this;
}
@Override
public AuthAwareConfigContext setVerifyUploads(final Boolean verify) {
synchronized (lock) {
super.setVerifyUploads(verify);
}
return this;
}
@Override
public AuthAwareConfigContext setUploadBufferSize(final Integer size) {
synchronized (lock) {
super.setUploadBufferSize(size);
}
return this;
}
@Override
public AuthAwareConfigContext setMetricReporterMode(final MetricReporterMode metricReporterMode) {
synchronized (lock) {
super.setMetricReporterMode(metricReporterMode);
}
return this;
}
@Override
public AuthAwareConfigContext setMetricReporterOutputInterval(final Integer metricReporterOutputInterval) {
synchronized (lock) {
super.setMetricReporterOutputInterval(metricReporterOutputInterval);
}
return this;
}
@Override
public AuthAwareConfigContext setClientEncryptionEnabled(final Boolean clientEncryptionEnabled) {
synchronized (lock) {
super.setClientEncryptionEnabled(clientEncryptionEnabled);
}
return this;
}
@Override
public AuthAwareConfigContext setEncryptionKeyId(final String keyId) {
synchronized (lock) {
super.setEncryptionKeyId(keyId);
}
return this;
}
@Override
public AuthAwareConfigContext setEncryptionAlgorithm(final String algorithm) {
synchronized (lock) {
super.setEncryptionAlgorithm(algorithm);
}
return this;
}
@Override
public AuthAwareConfigContext setPermitUnencryptedDownloads(final Boolean permitUnencryptedDownloads) {
synchronized (lock) {
super.setPermitUnencryptedDownloads(permitUnencryptedDownloads);
}
return this;
}
@Override
public AuthAwareConfigContext setEncryptionAuthenticationMode(
final EncryptionAuthenticationMode encryptionAuthenticationMode) {
synchronized (lock) {
super.setEncryptionAuthenticationMode(encryptionAuthenticationMode);
}
return this;
}
@Override
public AuthAwareConfigContext setEncryptionPrivateKeyPath(final String encryptionPrivateKeyPath) {
synchronized (lock) {
super.setEncryptionPrivateKeyPath(encryptionPrivateKeyPath);
}
return this;
}
@Override
public AuthAwareConfigContext setEncryptionPrivateKeyBytes(final byte[] encryptionPrivateKeyBytes) {
synchronized (lock) {
super.setEncryptionPrivateKeyBytes(encryptionPrivateKeyBytes);
}
return this;
}
}