11package io .scalecube .config .vault ;
22
3+ import static io .scalecube .config .vault .VaultInvoker .STATUS_CODE_NOT_FOUND ;
4+
35import com .bettercloud .vault .EnvironmentLoader ;
46import com .bettercloud .vault .VaultConfig ;
57import com .bettercloud .vault .VaultException ;
1113import java .util .ArrayList ;
1214import java .util .Arrays ;
1315import java .util .Collection ;
14- import java .util .Collections ;
1516import java .util .HashMap ;
1617import java .util .HashSet ;
1718import java .util .Map ;
@@ -33,7 +34,6 @@ public class VaultConfigSource implements ConfigSource {
3334 private static final Logger LOGGER = LoggerFactory .getLogger (VaultConfigSource .class );
3435
3536 private static final EnvironmentLoader ENVIRONMENT_LOADER = new EnvironmentLoader ();
36-
3737 private static final String PATHS_SEPARATOR = ":" ;
3838
3939 private final VaultInvoker vault ;
@@ -46,7 +46,7 @@ private VaultConfigSource(VaultInvoker vault, Collection<String> secretsPaths) {
4646
4747 @ Override
4848 public Map <String , ConfigProperty > loadConfig () {
49- Map <String , ConfigProperty > result = new HashMap <>();
49+ Map <String , ConfigProperty > propertyMap = new HashMap <>();
5050 for (String path : secretsPaths ) {
5151 try {
5252 LogicalResponse response = vault .invoke (vault -> vault .logical ().read (path ));
@@ -55,9 +55,9 @@ public Map<String, ConfigProperty> loadConfig() {
5555 .map (LoadedConfigProperty ::withNameAndValue )
5656 .map (LoadedConfigProperty .Builder ::build )
5757 .collect (Collectors .toMap (LoadedConfigProperty ::name , Function .identity ()));
58- result .putAll (pathProps );
58+ propertyMap .putAll (pathProps );
5959 } catch (VaultException ex ) {
60- if (ex .getHttpStatusCode () == 404 ) {
60+ if (ex .getHttpStatusCode () == STATUS_CODE_NOT_FOUND ) {
6161 LOGGER .error ("Unable to load config properties from: {}" , path );
6262 } else {
6363 throw new ConfigSourceNotAvailableException (ex );
@@ -67,13 +67,12 @@ public Map<String, ConfigProperty> loadConfig() {
6767 throw new ConfigSourceNotAvailableException (ex );
6868 }
6969 }
70- return result ;
70+ return propertyMap ;
7171 }
7272
7373 public static final class Builder {
7474
75- private Function <VaultInvoker .Builder , VaultInvoker .Builder > builderFunction =
76- Function .identity ();
75+ private Function <VaultInvoker .Builder , VaultInvoker .Builder > builderFunction = b -> b ;
7776
7877 private VaultInvoker invoker ;
7978
@@ -89,37 +88,21 @@ public static final class Builder {
8988 public Builder () {}
9089
9190 /**
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).
10792 *
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
11195 */
11296 public Builder addSecretsPath (String ... secretsPath ) {
113- this . secretsPaths .addAll (toSecretsPaths (Arrays .asList (secretsPath )));
97+ secretsPaths .addAll (toSecretsPaths (Arrays .asList (secretsPath )));
11498 return this ;
11599 }
116100
117101 /**
118- * Setter for {@code secretsPaths} .
102+ * Setter for secrets paths (each path value may contain values separated by colons) .
119103 *
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
123106 */
124107 public Builder secretsPaths (Collection <String > secretsPaths ) {
125108 this .secretsPaths = toSecretsPaths (secretsPaths );
@@ -132,31 +115,50 @@ private static Set<String> toSecretsPaths(Collection<String> secretsPaths) {
132115 .collect (Collectors .toSet ());
133116 }
134117
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 ;
137126 return this ;
138127 }
139128
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 );
142137 return this ;
143138 }
144139
140+ /**
141+ * Setter for {@link VaultConfig}.
142+ *
143+ * @param vaultConfig vaultConfig
144+ * @return this
145+ */
145146 public Builder config (UnaryOperator <VaultConfig > vaultConfig ) {
146147 this .builderFunction = this .builderFunction .andThen (b -> b .options (vaultConfig ));
147148 return this ;
148149 }
149150
150- public Builder tokenSupplier (VaultTokenSupplier supplier ) {
151- this .builderFunction = this .builderFunction .andThen (b -> b .tokenSupplier (supplier ));
152- return this ;
153- }
154-
155151 /**
156- * Builds vault config source .
152+ * Setter for {@link VaultTokenSupplier} .
157153 *
158- * @return instance of {@link VaultConfigSource}
154+ * @param tokenSupplier tokenSupplier
155+ * @return this
159156 */
157+ public Builder tokenSupplier (VaultTokenSupplier tokenSupplier ) {
158+ this .builderFunction = this .builderFunction .andThen (b -> b .tokenSupplier (tokenSupplier ));
159+ return this ;
160+ }
161+
160162 public VaultConfigSource build () {
161163 return new VaultConfigSource (
162164 invoker != null ? invoker : builderFunction .apply (new VaultInvoker .Builder ()).build (),
0 commit comments