Search in sources :

Example 6 with GitFile

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;
}
Also used : GitFile(com.tremolosecurity.provisioning.tasks.dataobj.GitFile) GitUtils(com.tremolosecurity.git.GitUtils) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) IOException(java.io.IOException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) JSONParser(org.json.simple.parser.JSONParser) List(java.util.List)

Aggregations

GitFile (com.tremolosecurity.provisioning.tasks.dataobj.GitFile)6 JSONObject (org.json.simple.JSONObject)6 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)5 OpenShiftTarget (com.tremolosecurity.unison.openshiftv3.OpenShiftTarget)5 IOException (java.io.IOException)5 List (java.util.List)5 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)4 JSONParser (org.json.simple.parser.JSONParser)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Yaml (org.yaml.snakeyaml.Yaml)3 Workflow (com.tremolosecurity.provisioning.core.Workflow)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 ParseException (org.json.simple.parser.ParseException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonPatch (com.github.fge.jsonpatch.JsonPatch)1 Patch (com.github.fge.jsonpatch.Patch)1 JsonMergePatch (com.github.fge.jsonpatch.mergepatch.JsonMergePatch)1