1
1
package io .scalecube .config .vault ;
2
2
3
+ import static io .scalecube .config .vault .VaultInvoker .STATUS_CODE_NOT_FOUND ;
4
+
3
5
import com .bettercloud .vault .EnvironmentLoader ;
4
6
import com .bettercloud .vault .VaultConfig ;
5
7
import com .bettercloud .vault .VaultException ;
11
13
import java .util .ArrayList ;
12
14
import java .util .Arrays ;
13
15
import java .util .Collection ;
14
- import java .util .Collections ;
15
16
import java .util .HashMap ;
16
17
import java .util .HashSet ;
17
18
import java .util .Map ;
@@ -33,7 +34,6 @@ public class VaultConfigSource implements ConfigSource {
33
34
private static final Logger LOGGER = LoggerFactory .getLogger (VaultConfigSource .class );
34
35
35
36
private static final EnvironmentLoader ENVIRONMENT_LOADER = new EnvironmentLoader ();
36
-
37
37
private static final String PATHS_SEPARATOR = ":" ;
38
38
39
39
private final VaultInvoker vault ;
@@ -46,7 +46,7 @@ private VaultConfigSource(VaultInvoker vault, Collection<String> secretsPaths) {
46
46
47
47
@ Override
48
48
public Map <String , ConfigProperty > loadConfig () {
49
- Map <String , ConfigProperty > result = new HashMap <>();
49
+ Map <String , ConfigProperty > propertyMap = new HashMap <>();
50
50
for (String path : secretsPaths ) {
51
51
try {
52
52
LogicalResponse response = vault .invoke (vault -> vault .logical ().read (path ));
@@ -55,9 +55,9 @@ public Map<String, ConfigProperty> loadConfig() {
55
55
.map (LoadedConfigProperty ::withNameAndValue )
56
56
.map (LoadedConfigProperty .Builder ::build )
57
57
.collect (Collectors .toMap (LoadedConfigProperty ::name , Function .identity ()));
58
- result .putAll (pathProps );
58
+ propertyMap .putAll (pathProps );
59
59
} catch (VaultException ex ) {
60
- if (ex .getHttpStatusCode () == 404 ) {
60
+ if (ex .getHttpStatusCode () == STATUS_CODE_NOT_FOUND ) {
61
61
LOGGER .error ("Unable to load config properties from: {}" , path );
62
62
} else {
63
63
throw new ConfigSourceNotAvailableException (ex );
@@ -67,13 +67,12 @@ public Map<String, ConfigProperty> loadConfig() {
67
67
throw new ConfigSourceNotAvailableException (ex );
68
68
}
69
69
}
70
- return result ;
70
+ return propertyMap ;
71
71
}
72
72
73
73
public static final class Builder {
74
74
75
- private Function <VaultInvoker .Builder , VaultInvoker .Builder > builderFunction =
76
- Function .identity ();
75
+ private Function <VaultInvoker .Builder , VaultInvoker .Builder > builderFunction = b -> b ;
77
76
78
77
private VaultInvoker invoker ;
79
78
@@ -89,37 +88,21 @@ public static final class Builder {
89
88
public Builder () {}
90
89
91
90
/**
92
- * Appends {@code secretsPath} to {@code secretsPaths}.
93
- *
94
- * @param secretsPath secretsPath (may contain value with paths separated by {@code :})
95
- * @return this builder
96
- * @deprecated will be removed in future releases without notice, use {@link
97
- * #addSecretsPath(String...)} or {@link #secretsPaths(Collection)}.
98
- */
99
- @ Deprecated
100
- public Builder secretsPath (String secretsPath ) {
101
- this .secretsPaths .addAll (toSecretsPaths (Collections .singletonList (secretsPath )));
102
- return this ;
103
- }
104
-
105
- /**
106
- * Appends one or several secretsPath\es to {@code secretsPaths}.
91
+ * Appends secrets paths (each path value may contain values separated by colons).
107
92
*
108
- * @param secretsPath one or several secretsPath\es (each value may contain paths separated by
109
- * {@code :})
110
- * @return this builder
93
+ * @param secretsPath secretsPath
94
+ * @return this
111
95
*/
112
96
public Builder addSecretsPath (String ... secretsPath ) {
113
- this . secretsPaths .addAll (toSecretsPaths (Arrays .asList (secretsPath )));
97
+ secretsPaths .addAll (toSecretsPaths (Arrays .asList (secretsPath )));
114
98
return this ;
115
99
}
116
100
117
101
/**
118
- * Setter for {@code secretsPaths} .
102
+ * Setter for secrets paths (each path value may contain values separated by colons) .
119
103
*
120
- * @param secretsPaths collection of secretsPath\es (each value may contain paths separated by
121
- * colon)
122
- * @return this builder
104
+ * @param secretsPaths secretsPaths
105
+ * @return this
123
106
*/
124
107
public Builder secretsPaths (Collection <String > secretsPaths ) {
125
108
this .secretsPaths = toSecretsPaths (secretsPaths );
@@ -132,31 +115,50 @@ private static Set<String> toSecretsPaths(Collection<String> secretsPaths) {
132
115
.collect (Collectors .toSet ());
133
116
}
134
117
135
- public Builder invoker (VaultInvoker invoker ) {
136
- this .invoker = invoker ;
118
+ /**
119
+ * Setter for {@link VaultInvoker}.
120
+ *
121
+ * @param vaultInvoker vaultInvoker
122
+ * @return this
123
+ */
124
+ public Builder invoker (VaultInvoker vaultInvoker ) {
125
+ this .invoker = vaultInvoker ;
137
126
return this ;
138
127
}
139
128
140
- public Builder vault (UnaryOperator <VaultInvoker .Builder > opts ) {
141
- this .builderFunction = this .builderFunction .andThen (opts );
129
+ /**
130
+ * Setter for {@link VaultInvoker.Builder} operator.
131
+ *
132
+ * @param operator operator for {@link VaultInvoker.Builder}
133
+ * @return this
134
+ */
135
+ public Builder vault (UnaryOperator <VaultInvoker .Builder > operator ) {
136
+ this .builderFunction = this .builderFunction .andThen (operator );
142
137
return this ;
143
138
}
144
139
140
+ /**
141
+ * Setter for {@link VaultConfig}.
142
+ *
143
+ * @param vaultConfig vaultConfig
144
+ * @return this
145
+ */
145
146
public Builder config (UnaryOperator <VaultConfig > vaultConfig ) {
146
147
this .builderFunction = this .builderFunction .andThen (b -> b .options (vaultConfig ));
147
148
return this ;
148
149
}
149
150
150
- public Builder tokenSupplier (VaultTokenSupplier supplier ) {
151
- this .builderFunction = this .builderFunction .andThen (b -> b .tokenSupplier (supplier ));
152
- return this ;
153
- }
154
-
155
151
/**
156
- * Builds vault config source .
152
+ * Setter for {@link VaultTokenSupplier} .
157
153
*
158
- * @return instance of {@link VaultConfigSource}
154
+ * @param tokenSupplier tokenSupplier
155
+ * @return this
159
156
*/
157
+ public Builder tokenSupplier (VaultTokenSupplier tokenSupplier ) {
158
+ this .builderFunction = this .builderFunction .andThen (b -> b .tokenSupplier (tokenSupplier ));
159
+ return this ;
160
+ }
161
+
160
162
public VaultConfigSource build () {
161
163
return new VaultConfigSource (
162
164
invoker != null ? invoker : builderFunction .apply (new VaultInvoker .Builder ()).build (),
0 commit comments