diff --git a/src/main/java/io/github/valfadeev/rundeck/plugin/vault/VaultStoragePlugin.java b/src/main/java/io/github/valfadeev/rundeck/plugin/vault/VaultStoragePlugin.java index 79d7a23..2d1ffe7 100644 --- a/src/main/java/io/github/valfadeev/rundeck/plugin/vault/VaultStoragePlugin.java +++ b/src/main/java/io/github/valfadeev/rundeck/plugin/vault/VaultStoragePlugin.java @@ -5,6 +5,7 @@ import java.util.*; import java.util.stream.Collectors; +import com.bettercloud.vault.Vault; import com.bettercloud.vault.VaultException; import com.bettercloud.vault.api.Logical; import com.bettercloud.vault.response.VaultResponse; @@ -52,6 +53,8 @@ public VaultStoragePlugin() {} private Logical vault; //if is true, objects will be saved with rundeck default headers behaivour private boolean rundeckObject=true; + private VaultClientProvider clientProvider; + private Vault vaultClient; @Override @@ -63,10 +66,8 @@ public Description getDescription() { public void configure(Properties configuration) throws ConfigurationException { vaultPrefix = configuration.getProperty(VAULT_PREFIX); vaultSecretBackend = configuration.getProperty(VAULT_SECRET_BACKEND); - - vault = new VaultClientProvider(configuration) - .getVaultClient() - .logical(); + clientProvider = new VaultClientProvider(configuration); + loginVault(clientProvider); //check storage behaivour String storageBehaviour=configuration.getProperty(VAULT_STORAGE_BEHAVIOUR); @@ -84,9 +85,31 @@ private boolean isDir(String key) { return key.endsWith("/"); } + private void lookup(){ + try { + vaultClient.auth().lookupSelf(); + } catch (VaultException e) { + if(e.getHttpStatusCode() == 403){//try login again + loginVault(clientProvider); + } else { + e.printStackTrace(); + } + } + } + + private void loginVault(VaultClientProvider provider){ + try { + vaultClient = provider.getVaultClient(); + vault = vaultClient.logical(); + } catch (ConfigurationException e) { + e.printStackTrace(); + } + } + private boolean isVaultDir(String key) { try{ + lookup(); if(vault.list(getVaultPath(key,vaultSecretBackend,vaultPrefix)).size() > 0){ return true; }else{ @@ -141,6 +164,7 @@ private VaultResponse saveResource(Path path, ResourceMeta content, String event Map payload=object.saveResource(content,event,baoStream); try { + lookup(); return vault.write(getVaultPath(object.getPath().getPath(),vaultSecretBackend,vaultPrefix), payload); } catch (VaultException e) { throw new StorageException( @@ -181,6 +205,7 @@ private Set> listResources(Path path, KeyType type) { List response; try { + lookup(); response = vault.list(getVaultPath(path.getPath(),vaultSecretBackend,vaultPrefix)); } catch (VaultException e) { @@ -257,6 +282,7 @@ private Set> listResources(Path path, KeyType type) { @Override public boolean hasPath(Path path) { try { + lookup(); if(vault.list(getVaultPath(path.getPath(),vaultSecretBackend,vaultPrefix)).size() > 0){ return true; } @@ -297,6 +323,7 @@ public boolean hasResource(String path) { @Override public boolean hasDirectory(Path path) { try { + lookup(); List list=vault.list(getVaultPath(path.getPath(),vaultSecretBackend,vaultPrefix)); if(list.size() > 0){ @@ -408,7 +435,7 @@ public Resource updateResource(String path, ResourceMeta content) } public KeyObject getVaultObject(Path path){ - + lookup(); KeyObject value= KeyObjectBuilder.builder() .path(path) .vault(vault)