Search in sources :

Example 1 with GitUtils

use of com.tremolosecurity.git.GitUtils in project OpenUnison by TremoloSecurity.

the class K8sProjectCheck method createTremoloUser.

@Override
public String createTremoloUser(NewUserRequest newUser, List<String> errors, AuthInfo userData) throws ProvisioningException {
    if (errors.size() == 0) {
        String targetName = newUser.getAttributes().get("cluster");
        if (targetName == null) {
            targetName = this.targetName;
        }
        OpenShiftTarget target = (OpenShiftTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(targetName).getProvider();
        HttpCon con = null;
        try {
            String token = target.getAuthToken();
            con = target.createClient();
            if (target.isObjectExistsByName(token, con, "/api/v1/namespaces", newUser.getAttributes().get(this.projectAttributeName))) {
                errors.add("Namespace name already exists");
                return "";
            }
        } catch (Exception e) {
            throw new ProvisioningException("Could not check if namespace exists", e);
        } finally {
            if (con != null) {
                try {
                    con.getHttp().close();
                } catch (IOException e) {
                // doesn't matter
                }
                con.getBcm().close();
            }
        }
        if (target.getGitUrl() != null && !target.getGitUrl().isEmpty()) {
            String gitUrlForNs = newUser.getAttributes().get("gitUrl");
            String sshPrivKey = newUser.getAttributes().get("gitSshKey");
            if (gitUrlForNs == null || gitUrlForNs.isEmpty()) {
                errors.add("Git URL is required for clusters configured to use git");
            }
            if (sshPrivKey == null || sshPrivKey.isEmpty()) {
                errors.add("Git SSH Private Key is required for clusters configured to use git");
            }
            if (errors.size() > 0) {
                return "";
            }
            GitUtils gitUtil = new GitUtils(gitUrlForNs, sshPrivKey);
            try {
                gitUtil.checkOut();
            } catch (Throwable t) {
                logger.warn("Could not checkout '" + gitUrlForNs + "'", t);
                errors.add(t.getMessage());
            } finally {
                gitUtil.cleanup();
            }
        }
        return this.workflowName;
    } else {
        return "";
    }
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) 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)

Example 2 with GitUtils

use of com.tremolosecurity.git.GitUtils 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

GitUtils (com.tremolosecurity.git.GitUtils)2 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)2 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)2 OpenShiftTarget (com.tremolosecurity.unison.openshiftv3.OpenShiftTarget)2 IOException (java.io.IOException)2 GitFile (com.tremolosecurity.provisioning.tasks.dataobj.GitFile)1 List (java.util.List)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1