19
19
import java .util .List ;
20
20
import java .util .Objects ;
21
21
import java .util .StringJoiner ;
22
+ import java .util .concurrent .CompletableFuture ;
23
+ import java .util .concurrent .TimeUnit ;
22
24
import java .util .function .Function ;
23
25
import java .util .function .Supplier ;
24
26
import org .slf4j .Logger ;
@@ -37,14 +39,16 @@ public class VaultServiceRolesInstaller {
37
39
new ObjectMapper (new YAMLFactory ()).setVisibility (PropertyAccessor .FIELD , Visibility .ANY );
38
40
39
41
private final String vaultAddress ;
40
- private final Supplier <String > vaultTokenSupplier ;
42
+ private final CompletableFuture <String > vaultTokenSupplier ;
41
43
private final Supplier <String > keyNameSupplier ;
42
44
private final Function <String , String > roleNameBuilder ;
43
45
private final List <Supplier <ServiceRoles >> serviceRolesSources ;
44
46
private final String keyAlgorithm ;
45
47
private final String keyRotationPeriod ;
46
48
private final String keyVerificationTtl ;
47
49
private final String roleTtl ;
50
+ private final long timeout ;
51
+ private final TimeUnit timeUnit ;
48
52
49
53
private VaultServiceRolesInstaller (Builder builder ) {
50
54
this .vaultAddress = builder .vaultAddress ;
@@ -56,6 +60,8 @@ private VaultServiceRolesInstaller(Builder builder) {
56
60
this .keyRotationPeriod = builder .keyRotationPeriod ;
57
61
this .keyVerificationTtl = builder .keyVerificationTtl ;
58
62
this .roleTtl = builder .roleTtl ;
63
+ this .timeout = builder .timeout ;
64
+ this .timeUnit = builder .timeUnit ;
59
65
}
60
66
61
67
/**
@@ -74,19 +80,30 @@ public void install() {
74
80
return ;
75
81
}
76
82
77
- final String token = vaultTokenSupplier .get ();
78
- final Rest rest = new Rest ().header (VAULT_TOKEN_HEADER , token );
79
-
80
- final String keyName = keyNameSupplier .get ();
81
- createVaultIdentityKey (rest .url (buildVaultIdentityKeyUri (keyName )), keyName );
82
-
83
- for (Role role : serviceRoles .roles ) {
84
- String roleName = roleNameBuilder .apply (role .role );
85
- createVaultIdentityRole (
86
- rest .url (buildVaultIdentityRoleUri (roleName )), keyName , roleName , role .permissions );
83
+ try {
84
+ vaultTokenSupplier
85
+ .thenAcceptAsync (
86
+ token -> {
87
+ final var rest = new Rest ().header (VAULT_TOKEN_HEADER , token );
88
+ final var keyName = keyNameSupplier .get ();
89
+
90
+ createVaultIdentityKey (rest .url (buildVaultIdentityKeyUri (keyName )), keyName );
91
+
92
+ for (var role : serviceRoles .roles ) {
93
+ String roleName = roleNameBuilder .apply (role .role );
94
+ createVaultIdentityRole (
95
+ rest .url (buildVaultIdentityRoleUri (roleName )),
96
+ keyName ,
97
+ roleName ,
98
+ role .permissions );
99
+ }
100
+
101
+ LOGGER .debug ("Installed serviceRoles ({})" , serviceRoles );
102
+ })
103
+ .get (timeout , timeUnit );
104
+ } catch (Exception e ) {
105
+ throw new RuntimeException (e );
87
106
}
88
-
89
- LOGGER .debug ("Installed serviceRoles ({})" , serviceRoles );
90
107
}
91
108
92
109
private ServiceRoles loadServiceRoles () {
@@ -338,14 +355,16 @@ public String toString() {
338
355
public static class Builder {
339
356
340
357
private String vaultAddress ;
341
- private Supplier <String > vaultTokenSupplier ;
358
+ private CompletableFuture <String > vaultTokenSupplier ;
342
359
private Supplier <String > keyNameSupplier ;
343
360
private Function <String , String > roleNameBuilder ;
344
361
private List <Supplier <ServiceRoles >> serviceRolesSources = DEFAULT_SERVICE_ROLES_SOURCES ;
345
362
private String keyAlgorithm = "RS256" ;
346
363
private String keyRotationPeriod = "1h" ;
347
364
private String keyVerificationTtl = "1h" ;
348
365
private String roleTtl = "1m" ;
366
+ private long timeout = 10 ;
367
+ private TimeUnit timeUnit = TimeUnit .SECONDS ;
349
368
350
369
public Builder () {}
351
370
@@ -354,7 +373,7 @@ public Builder vaultAddress(String vaultAddress) {
354
373
return this ;
355
374
}
356
375
357
- public Builder vaultTokenSupplier (Supplier <String > vaultTokenSupplier ) {
376
+ public Builder vaultTokenSupplier (CompletableFuture <String > vaultTokenSupplier ) {
358
377
this .vaultTokenSupplier = vaultTokenSupplier ;
359
378
return this ;
360
379
}
@@ -399,6 +418,12 @@ public Builder roleTtl(String roleTtl) {
399
418
return this ;
400
419
}
401
420
421
+ public Builder timeout (long timeout , TimeUnit timeUnit ) {
422
+ this .timeout = timeout ;
423
+ this .timeUnit = timeUnit ;
424
+ return this ;
425
+ }
426
+
402
427
public VaultServiceRolesInstaller build () {
403
428
return new VaultServiceRolesInstaller (this );
404
429
}
0 commit comments