Skip to content

Commit

Permalink
Merge pull request #29 from rundeck-plugins/issue/refresh-token-if-ex…
Browse files Browse the repository at this point in the history
…pired

Changing so that Rundeck can make a new login if the token has expired.
  • Loading branch information
ltamaster authored Jan 7, 2021
2 parents 6f3054b + 95a37ed commit c84bdb4
Showing 1 changed file with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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{
Expand Down Expand Up @@ -141,6 +164,7 @@ private VaultResponse saveResource(Path path, ResourceMeta content, String event
Map<String, Object> payload=object.saveResource(content,event,baoStream);

try {
lookup();
return vault.write(getVaultPath(object.getPath().getPath(),vaultSecretBackend,vaultPrefix), payload);
} catch (VaultException e) {
throw new StorageException(
Expand Down Expand Up @@ -181,6 +205,7 @@ private Set<Resource<ResourceMeta>> listResources(Path path, KeyType type) {
List<String> response;

try {
lookup();
response = vault.list(getVaultPath(path.getPath(),vaultSecretBackend,vaultPrefix));

} catch (VaultException e) {
Expand Down Expand Up @@ -257,6 +282,7 @@ private Set<Resource<ResourceMeta>> 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;
}
Expand Down Expand Up @@ -297,6 +323,7 @@ public boolean hasResource(String path) {
@Override
public boolean hasDirectory(Path path) {
try {
lookup();
List<String> list=vault.list(getVaultPath(path.getPath(),vaultSecretBackend,vaultPrefix));

if(list.size() > 0){
Expand Down Expand Up @@ -408,7 +435,7 @@ public Resource<ResourceMeta> updateResource(String path, ResourceMeta content)
}

public KeyObject getVaultObject(Path path){

lookup();
KeyObject value= KeyObjectBuilder.builder()
.path(path)
.vault(vault)
Expand Down

0 comments on commit c84bdb4

Please sign in to comment.