Skip to content

Commit f922de7

Browse files
authored
Enhance VaultServiceRolesInstaller (#92)
* Enhance VaultServiceRolesInstaller (Add serviceRolesSources)
1 parent 1394e14 commit f922de7

File tree

1 file changed

+138
-11
lines changed

1 file changed

+138
-11
lines changed

vault/src/main/java/io/scalecube/security/vault/VaultServiceRolesInstaller.java

Lines changed: 138 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import com.bettercloud.vault.rest.Rest;
55
import com.bettercloud.vault.rest.RestException;
66
import io.scalecube.security.vault.VaultServiceRolesInstaller.ServiceRoles.Role;
7+
import java.io.File;
8+
import java.io.FileInputStream;
79
import java.io.InputStream;
10+
import java.util.Arrays;
811
import java.util.Base64;
12+
import java.util.Collections;
913
import java.util.List;
1014
import java.util.Objects;
1115
import java.util.StringJoiner;
@@ -25,11 +29,14 @@ public final class VaultServiceRolesInstaller {
2529

2630
private static final String VAULT_TOKEN_HEADER = "X-Vault-Token";
2731

32+
private static final List<Supplier<ServiceRoles>> DEFAULT_SERVICE_ROLES_SOURCES =
33+
Collections.singletonList(new ResourcesServiceRolesSupplier());
34+
2835
private String vaultAddress;
2936
private Mono<String> vaultTokenSupplier;
3037
private Supplier<String> keyNameSupplier;
3138
private Function<String, String> roleNameBuilder;
32-
private String inputFileName = "service-roles.yaml";
39+
private List<Supplier<ServiceRoles>> serviceRolesSources = DEFAULT_SERVICE_ROLES_SOURCES;
3340
private String keyAlgorithm = "RS256";
3441
private String keyRotationPeriod = "1h";
3542
private String keyVerificationTtl = "1h";
@@ -42,7 +49,7 @@ private VaultServiceRolesInstaller(VaultServiceRolesInstaller other) {
4249
this.vaultTokenSupplier = other.vaultTokenSupplier;
4350
this.keyNameSupplier = other.keyNameSupplier;
4451
this.roleNameBuilder = other.roleNameBuilder;
45-
this.inputFileName = other.inputFileName;
52+
this.serviceRolesSources = other.serviceRolesSources;
4653
this.keyAlgorithm = other.keyAlgorithm;
4754
this.keyRotationPeriod = other.keyRotationPeriod;
4855
this.keyVerificationTtl = other.keyVerificationTtl;
@@ -102,14 +109,28 @@ public VaultServiceRolesInstaller roleNameBuilder(Function<String, String> roleN
102109
}
103110

104111
/**
105-
* Setter for inputFileName.
112+
* Setter for serviceRolesSources.
106113
*
107-
* @param inputFileName inputFileName
114+
* @param serviceRolesSources serviceRolesSources
108115
* @return new instance with applied setting
109116
*/
110-
public VaultServiceRolesInstaller inputFileName(String inputFileName) {
117+
public VaultServiceRolesInstaller serviceRolesSources(
118+
List<Supplier<ServiceRoles>> serviceRolesSources) {
111119
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);
113134
return c;
114135
}
115136

@@ -209,11 +230,23 @@ private Mono<Void> install0() {
209230
}
210231

211232
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;
217250
}
218251

219252
private static void verifyOk(int status, String operation) {
@@ -322,4 +355,98 @@ public void setPermissions(List<String> permissions) {
322355
}
323356
}
324357
}
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+
}
325452
}

0 commit comments

Comments
 (0)