Skip to content

Commit

Permalink
fix encryptExtraVarsKey
Browse files Browse the repository at this point in the history
move file copier clean to a finally block
  • Loading branch information
ltamaster committed Mar 19, 2024
1 parent 1e2ab9e commit 50e0ccd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public int run() throws Exception {
String addeExtraVars = extraVars;

if (encryptExtraVars && useAnsibleVault) {
addeExtraVars = encryptExtraVarsKey(extraVars, tempInternalVaultFile);
addeExtraVars = encryptExtraVarsKey(extraVars);
}

tempVarsFile = AnsibleUtil.createTemporaryFile("extra-vars", addeExtraVars);
Expand Down Expand Up @@ -462,7 +462,7 @@ public int run() throws Exception {
String finalextraVarsPassword = extraVarsPassword;

if(useAnsibleVault){
finalextraVarsPassword = encryptExtraVarsKey(extraVarsPassword, tempInternalVaultFile);
finalextraVarsPassword = encryptExtraVarsKey(extraVarsPassword);
}

tempSshVarsFile = AnsibleUtil.createTemporaryFile("ssh-extra-vars", finalextraVarsPassword);
Expand All @@ -481,7 +481,7 @@ public int run() throws Exception {
String finalextraVarsPassword = extraVarsPassword;

if (useAnsibleVault) {
finalextraVarsPassword = encryptExtraVarsKey(extraVarsPassword, tempInternalVaultFile);
finalextraVarsPassword = encryptExtraVarsKey(extraVarsPassword);
}

tempBecameVarsFile = AnsibleUtil.createTemporaryFile("become-extra-vars", finalextraVarsPassword);
Expand Down Expand Up @@ -696,22 +696,13 @@ public boolean registerKeySshAgent(String keyPath) throws Exception {
}


public String encryptExtraVarsKey(String extraVars, File vaultPasswordFile) throws Exception {
Map<String, String> extraVarsMap = null;
public String encryptExtraVarsKey(String extraVars) throws Exception {
Map<String, String> extraVarsMap = new HashMap<>();
Map<String, String> encryptedExtraVarsMap = new HashMap<>();
try {
extraVarsMap = mapperYaml.readValue(extraVars, new TypeReference<Map<String, String>>() {
});

for (Map.Entry<String, String> entry : extraVarsMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
String encryptedKey = ansibleVault.encryptVariable(key, value);
if (encryptedKey != null) {
encryptedExtraVarsMap.put(key, encryptedKey);
}
}

} catch (Exception e) {
try {
extraVarsMap = mapperJson.readValue(extraVars, new TypeReference<Map<String, String>>() {
Expand All @@ -722,6 +713,15 @@ public String encryptExtraVarsKey(String extraVars, File vaultPasswordFile) thro
}
}

for (Map.Entry<String, String> entry : extraVarsMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
String encryptedKey = ansibleVault.encryptVariable(key, value);
if (encryptedKey != null) {
encryptedExtraVarsMap.put(key, encryptedKey);
}
}

StringBuilder stringBuilder = new StringBuilder();
encryptedExtraVarsMap.forEach((key, value) -> {
stringBuilder.append(key).append(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ private String doFileCopy(
runner.run();
} catch (Exception e) {
throw new FileCopierException("Error running Ansible.", AnsibleFailureReason.AnsibleError, e);
}finally {
contextBuilder.cleanupTempFiles();
}

contextBuilder.cleanupTempFiles();

return destinationPath;
}

Expand Down

0 comments on commit 50e0ccd

Please sign in to comment.