4
4
import com .bettercloud .vault .rest .Rest ;
5
5
import com .bettercloud .vault .rest .RestException ;
6
6
import io .scalecube .security .vault .VaultServiceRolesInstaller .ServiceRoles .Role ;
7
+ import java .io .File ;
8
+ import java .io .FileInputStream ;
7
9
import java .io .InputStream ;
10
+ import java .util .Arrays ;
8
11
import java .util .Base64 ;
12
+ import java .util .Collections ;
9
13
import java .util .List ;
10
14
import java .util .Objects ;
11
15
import java .util .StringJoiner ;
@@ -25,11 +29,14 @@ public final class VaultServiceRolesInstaller {
25
29
26
30
private static final String VAULT_TOKEN_HEADER = "X-Vault-Token" ;
27
31
32
+ private static final List <Supplier <ServiceRoles >> DEFAULT_SERVICE_ROLES_SOURCES =
33
+ Collections .singletonList (new ResourcesServiceRolesSupplier ());
34
+
28
35
private String vaultAddress ;
29
36
private Mono <String > vaultTokenSupplier ;
30
37
private Supplier <String > keyNameSupplier ;
31
38
private Function <String , String > roleNameBuilder ;
32
- private String inputFileName = "service-roles.yaml" ;
39
+ private List < Supplier < ServiceRoles >> serviceRolesSources = DEFAULT_SERVICE_ROLES_SOURCES ;
33
40
private String keyAlgorithm = "RS256" ;
34
41
private String keyRotationPeriod = "1h" ;
35
42
private String keyVerificationTtl = "1h" ;
@@ -42,7 +49,7 @@ private VaultServiceRolesInstaller(VaultServiceRolesInstaller other) {
42
49
this .vaultTokenSupplier = other .vaultTokenSupplier ;
43
50
this .keyNameSupplier = other .keyNameSupplier ;
44
51
this .roleNameBuilder = other .roleNameBuilder ;
45
- this .inputFileName = other .inputFileName ;
52
+ this .serviceRolesSources = other .serviceRolesSources ;
46
53
this .keyAlgorithm = other .keyAlgorithm ;
47
54
this .keyRotationPeriod = other .keyRotationPeriod ;
48
55
this .keyVerificationTtl = other .keyVerificationTtl ;
@@ -102,14 +109,28 @@ public VaultServiceRolesInstaller roleNameBuilder(Function<String, String> roleN
102
109
}
103
110
104
111
/**
105
- * Setter for inputFileName .
112
+ * Setter for serviceRolesSources .
106
113
*
107
- * @param inputFileName inputFileName
114
+ * @param serviceRolesSources serviceRolesSources
108
115
* @return new instance with applied setting
109
116
*/
110
- public VaultServiceRolesInstaller inputFileName (String inputFileName ) {
117
+ public VaultServiceRolesInstaller serviceRolesSources (
118
+ List <Supplier <ServiceRoles >> serviceRolesSources ) {
111
119
final VaultServiceRolesInstaller c = copy ();
112
- c .inputFileName = inputFileName ;
120
+ c .serviceRolesSources = serviceRolesSources ;
121
+ return c ;
122
+ }
123
+
124
+ /**
125
+ * Setter for serviceRolesSources.
126
+ *
127
+ * @param serviceRolesSources serviceRolesSources
128
+ * @return new instance with applied setting
129
+ */
130
+ public VaultServiceRolesInstaller serviceRolesSources (
131
+ Supplier <ServiceRoles >... serviceRolesSources ) {
132
+ final VaultServiceRolesInstaller c = copy ();
133
+ c .serviceRolesSources = Arrays .asList (serviceRolesSources );
113
134
return c ;
114
135
}
115
136
@@ -209,11 +230,23 @@ private Mono<Void> install0() {
209
230
}
210
231
211
232
private ServiceRoles loadServiceRoles () {
212
- ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
213
- InputStream inputStream = classLoader .getResourceAsStream (inputFileName );
214
- return inputStream != null
215
- ? new Yaml (new Constructor (ServiceRoles .class )).load (inputStream )
216
- : null ;
233
+ if (serviceRolesSources == null ) {
234
+ return null ;
235
+ }
236
+
237
+ for (Supplier <ServiceRoles > serviceRolesSource : serviceRolesSources ) {
238
+ try {
239
+ final ServiceRoles serviceRoles = serviceRolesSource .get ();
240
+ if (serviceRoles != null ) {
241
+ return serviceRoles ;
242
+ }
243
+ } catch (Throwable th ) {
244
+ LOGGER .warn (
245
+ "Fail to load ServiceRoles from {}, cause {}" , serviceRolesSource , th .getMessage ());
246
+ }
247
+ }
248
+
249
+ return null ;
217
250
}
218
251
219
252
private static void verifyOk (int status , String operation ) {
@@ -322,4 +355,98 @@ public void setPermissions(List<String> permissions) {
322
355
}
323
356
}
324
357
}
358
+
359
+ public static class ResourcesServiceRolesSupplier implements Supplier <ServiceRoles > {
360
+
361
+ public static final String DEFAULT_FILE_NAME = "service-roles.yaml" ;
362
+
363
+ private final String fileName ;
364
+
365
+ public ResourcesServiceRolesSupplier () {
366
+ this (DEFAULT_FILE_NAME );
367
+ }
368
+
369
+ public ResourcesServiceRolesSupplier (String fileName ) {
370
+ this .fileName = Objects .requireNonNull (fileName , "fileName" );
371
+ }
372
+
373
+ @ Override
374
+ public ServiceRoles get () {
375
+ ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
376
+ InputStream inputStream = classLoader .getResourceAsStream (fileName );
377
+ return inputStream != null
378
+ ? new Yaml (new Constructor (ServiceRoles .class )).load (inputStream )
379
+ : null ;
380
+ }
381
+
382
+ @ Override
383
+ public String toString () {
384
+ return new StringJoiner (", " , ResourcesServiceRolesSupplier .class .getSimpleName () + "[" , "]" )
385
+ .add ("fileName='" + fileName + "'" )
386
+ .toString ();
387
+ }
388
+ }
389
+
390
+ public static class EnvironmentServiceRolesSupplier implements Supplier <ServiceRoles > {
391
+
392
+ public static final String DEFAULT_ENV_KEY = "SERVICE_ROLES" ;
393
+
394
+ private final String envKey ;
395
+
396
+ public EnvironmentServiceRolesSupplier () {
397
+ this (DEFAULT_ENV_KEY );
398
+ }
399
+
400
+ public EnvironmentServiceRolesSupplier (String envKey ) {
401
+ this .envKey = Objects .requireNonNull (envKey , "envKey" );
402
+ }
403
+
404
+ @ Override
405
+ public ServiceRoles get () {
406
+ final String value = System .getenv (envKey );
407
+ return value != null ? new Yaml (new Constructor (ServiceRoles .class )).load (value ) : null ;
408
+ }
409
+
410
+ @ Override
411
+ public String toString () {
412
+ return new StringJoiner (
413
+ ", " , EnvironmentServiceRolesSupplier .class .getSimpleName () + "[" , "]" )
414
+ .add ("envKey='" + envKey + "'" )
415
+ .toString ();
416
+ }
417
+ }
418
+
419
+ public static class FileServiceRolesSupplier implements Supplier <ServiceRoles > {
420
+
421
+ public static final String DEFAULT_FILE = "service_roles.yaml" ;
422
+
423
+ private final String file ;
424
+
425
+ public FileServiceRolesSupplier () {
426
+ this (DEFAULT_FILE );
427
+ }
428
+
429
+ public FileServiceRolesSupplier (String file ) {
430
+ this .file = Objects .requireNonNull (file , "file" );
431
+ }
432
+
433
+ @ Override
434
+ public ServiceRoles get () {
435
+ try {
436
+ final File file = new File (this .file );
437
+ return file .exists ()
438
+ ? new Yaml (new Constructor (ServiceRoles .class )).load (new FileInputStream (file ))
439
+ : null ;
440
+ } catch (Exception e ) {
441
+ throw Exceptions .propagate (e );
442
+ }
443
+ }
444
+
445
+ @ Override
446
+ public String toString () {
447
+ return new StringJoiner (", " , FileServiceRolesSupplier .class .getSimpleName () + "[" , "]" )
448
+ .add ("file='" + file + "'" )
449
+ .toString ();
450
+ }
451
+ }
325
452
}
0 commit comments