-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated dependencies, updated KubernetesVaultTokenSupplier, added Vau…
…ltClientTokenSupplier
- Loading branch information
Showing
3 changed files
with
154 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
config-vault/src/main/java/io/scalecube/config/vault/VaultClientTokenSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package io.scalecube.config.vault; | ||
|
||
import com.bettercloud.vault.VaultConfig; | ||
import com.bettercloud.vault.VaultException; | ||
import java.util.Objects; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class VaultClientTokenSupplier { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(VaultClientTokenSupplier.class); | ||
|
||
private final String vaultAddress; | ||
private final String vaultToken; | ||
private final String vaultRole; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param vaultAddress vaultAddress | ||
* @param vaultToken vaultToken (must not set be together with vaultRole) | ||
* @param vaultRole vaultRole (must not set be together with vaultToken) | ||
*/ | ||
public VaultClientTokenSupplier(String vaultAddress, String vaultToken, String vaultRole) { | ||
this.vaultAddress = vaultAddress; | ||
this.vaultToken = vaultToken; | ||
this.vaultRole = vaultRole; | ||
if (isNullOrNoneOrEmpty(vaultAddress)) { | ||
throw new IllegalArgumentException("Vault address is required"); | ||
} | ||
if (isNullOrNoneOrEmpty(vaultToken) && isNullOrNoneOrEmpty(vaultRole)) { | ||
throw new IllegalArgumentException( | ||
"Vault auth scheme is required (specify either vaultToken or vaultRole)"); | ||
} | ||
} | ||
|
||
/** | ||
* Returns new instance of {@link VaultClientTokenSupplier}. | ||
* | ||
* @param vaultAddress vaultAddress | ||
* @param vaultToken vaultToken | ||
* @return new instance of {@link VaultClientTokenSupplier} | ||
*/ | ||
public static VaultClientTokenSupplier supplierByToken(String vaultAddress, String vaultToken) { | ||
return new VaultClientTokenSupplier(vaultAddress, vaultToken, null); | ||
} | ||
|
||
/** | ||
* Returns new instance of {@link VaultClientTokenSupplier}. | ||
* | ||
* @param vaultAddress vaultAddress | ||
* @param vaultRole vaultRole | ||
* @return new instance of {@link VaultClientTokenSupplier} | ||
*/ | ||
public static VaultClientTokenSupplier supplierByRole(String vaultAddress, String vaultRole) { | ||
return new VaultClientTokenSupplier(vaultAddress, null, vaultRole); | ||
} | ||
|
||
/** | ||
* Obtains vault client token. | ||
* | ||
* @return future result | ||
*/ | ||
public Future<String> getToken() { | ||
return CompletableFuture.supplyAsync(this::getToken0); | ||
} | ||
|
||
private String getToken0() { | ||
try { | ||
VaultTokenSupplier vaultTokenSupplier; | ||
VaultConfig vaultConfig; | ||
|
||
if (!isNullOrNoneOrEmpty(vaultRole)) { | ||
if (!isNullOrNoneOrEmpty(vaultToken)) { | ||
LOGGER.warn( | ||
"Taking KubernetesVaultTokenSupplier by precedence rule, " | ||
+ "ignoring EnvironmentVaultTokenSupplier " | ||
+ "(specify either vaultToken or vaultRole, not both)"); | ||
} | ||
vaultTokenSupplier = KubernetesVaultTokenSupplier.builder().vaultRole(vaultRole).build(); | ||
vaultConfig = new VaultConfig().address(vaultAddress).build(); | ||
} else { | ||
vaultTokenSupplier = new EnvironmentVaultTokenSupplier(); | ||
vaultConfig = new VaultConfig().address(vaultAddress).token(vaultToken).build(); | ||
} | ||
|
||
return vaultTokenSupplier.getToken(vaultConfig); | ||
} catch (VaultException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static boolean isNullOrNoneOrEmpty(String value) { | ||
return Objects.isNull(value) | ||
|| "none".equalsIgnoreCase(value) | ||
|| "null".equals(value) | ||
|| value.isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters