use of com.tremolosecurity.provisioning.tasks.dataobj.GitFile in project OpenUnison by TremoloSecurity.
the class PushToGit method doTask.
@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
String localSecretName = task.renderTemplate(secretName, request);
String localNameSpace = task.renderTemplate(nameSpace, request);
String localTarget = task.renderTemplate(this.target, request);
String localKeyName = task.renderTemplate(this.keyName, request);
String localGitRepo = task.renderTemplate(gitRepo, request);
String localCommitMsg = task.renderTemplate(commitMsg, request);
OpenShiftTarget target = (OpenShiftTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(localTarget).getProvider();
HttpCon con = null;
GitUtils gitUtil = null;
try {
con = target.createClient();
StringBuilder sb = new StringBuilder();
sb.append("/api/v1/namespaces/").append(localNameSpace).append("/secrets/").append(localSecretName);
String json = target.callWS(target.getAuthToken(), con, sb.toString());
JSONObject secret = (JSONObject) new JSONParser().parse(json);
JSONObject data = (JSONObject) secret.get("data");
if (data == null) {
throw new Exception("No data found for " + sb.toString());
}
String b64KeyData = (String) data.get(localKeyName);
if (b64KeyData == null) {
throw new ProvisioningException("Could not find key '" + localKeyName + "' in '" + sb.toString() + "'");
}
String privateKey = new String(java.util.Base64.getDecoder().decode(b64KeyData));
gitUtil = new GitUtils(localGitRepo, privateKey);
try {
gitUtil.checkOut();
} catch (Exception e) {
throw new Exception("Could not checkout repo");
}
List<GitFile> files = (List<GitFile>) request.get(requestObject);
if (files == null) {
throw new Exception("No gitfiles stored in '" + requestObject + "'");
}
gitUtil.applyFiles(files);
gitUtil.commitAndPush(localCommitMsg);
} catch (Exception e) {
throw new ProvisioningException("Could not push to git", e);
} finally {
if (con != null) {
try {
con.getHttp().close();
} catch (IOException e) {
}
con.getBcm().close();
}
if (gitUtil != null) {
gitUtil.cleanup();
}
}
return true;
}
Aggregations