Search in sources :

Example 1 with GitFile

use of com.tremolosecurity.provisioning.tasks.dataobj.GitFile in project OpenUnison by TremoloSecurity.

the class CreateK8sObject method doTask.

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
    String localTemplate = task.renderTemplate(template, request);
    if (logger.isDebugEnabled()) {
        logger.debug("localTemplate : '" + localTemplate + "'");
    }
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    String localURL = task.renderTemplate(this.url, request);
    String localTemplateJSON = "";
    HttpCon con = null;
    String localTarget = task.renderTemplate(this.targetName, request);
    OpenShiftTarget os = (OpenShiftTarget) task.getConfigManager().getProvisioningEngine().getTarget(localTarget).getProvider();
    try {
        String token = os.getAuthToken();
        con = os.createClient();
        if (this.yaml) {
            Yaml yaml = new Yaml();
            Map<String, Object> map = (Map<String, Object>) yaml.load(new ByteArrayInputStream(localTemplate.getBytes("UTF-8")));
            JSONObject jsonObject = new JSONObject(map);
            localTemplateJSON = jsonObject.toJSONString();
        } else {
            localTemplateJSON = localTemplate;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Write To Request  : '" + this.writeToRequestConfig + "'");
        }
        boolean writeToRequest = false;
        if (this.writeToRequestConfig != null) {
            writeToRequest = task.renderTemplate(this.writeToRequestConfig, request).equalsIgnoreCase("true");
        }
        if (writeToRequest) {
            logger.debug("Writing to secret");
            if (!os.isObjectExists(token, con, localURL, localTemplateJSON)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Url '" + localURL + "' doesn't exist");
                }
                String localPath = task.renderTemplate(this.path, request);
                String dirName;
                String fileName;
                int lastSlash = localPath.lastIndexOf('/');
                if (lastSlash == -1) {
                    dirName = "";
                    fileName = localPath;
                } else {
                    dirName = localPath.substring(0, lastSlash);
                    fileName = localPath.substring(lastSlash + 1);
                }
                JSONObject fileInfo = new JSONObject();
                fileInfo.put("fileName", fileName);
                fileInfo.put("dirName", dirName);
                fileInfo.put("data", Base64.getEncoder().encodeToString(localTemplate.getBytes("UTF-8")));
                GitFile gitFile = new GitFile(fileName, dirName, localTemplate);
                List<GitFile> gitFiles = (List<GitFile>) request.get(this.requestAttribute);
                if (gitFiles == null) {
                    gitFiles = new ArrayList<GitFile>();
                    request.put(this.requestAttribute, gitFiles);
                }
                gitFiles.add(gitFile);
            }
        } else {
            writeToAPIServer(localTemplateJSON, approvalID, localURL, con, os, token, localTarget);
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not create " + kind, e);
    } finally {
        if (con != null) {
            con.getBcm().close();
        }
    }
    return true;
}
Also used : GitFile(com.tremolosecurity.provisioning.tasks.dataobj.GitFile) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) Yaml(org.yaml.snakeyaml.Yaml) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 2 with GitFile

use of com.tremolosecurity.provisioning.tasks.dataobj.GitFile in project OpenUnison by TremoloSecurity.

the class GitUtils method applyFiles.

public void applyFiles(List<GitFile> files) throws IOException, NoFilepatternException, GitAPIException, JsonPatchException, ParseException {
    for (GitFile file : files) {
        File targetFile = new File(this.tmpdir.getAbsolutePath() + File.separator + "gitrepo" + file.getDirName() + File.separator + file.getFileName());
        if (file.isDelete()) {
            logger.info("Deleting '" + targetFile.getAbsolutePath() + "'");
            FileUtils.forceDelete(targetFile);
            git.rm().addFilepattern(file.getDirName().substring(1) + File.separator + file.getFileName()).call();
        }
        if (file.isPatch()) {
            logger.info("Patching '" + targetFile.getAbsolutePath() + "'");
            InputStream in = new FileInputStream(targetFile);
            Yaml yaml = new Yaml();
            Map<String, Object> map = (Map<String, Object>) yaml.load(in);
            JSONObject jsonObject = new JSONObject(map);
            ObjectMapper mapper = new ObjectMapper();
            JsonNode toBePatched = mapper.readValue(jsonObject.toJSONString(), JsonNode.class);
            Patch patch = null;
            if (file.getPatchType().equalsIgnoreCase("json")) {
                patch = mapper.readValue(file.getData(), JsonPatch.class);
            } else if (file.getPatchType().equalsIgnoreCase("merge")) {
                patch = mapper.readValue(file.getData(), JsonMergePatch.class);
            } else {
                throw new IOException("Unsupported merge strategy " + file.getPatchType());
            }
            JsonNode patched = patch.apply(toBePatched);
            String patchedJson = patched.toString();
            String newYaml = yaml.dump(new JSONParser().parse(patchedJson));
            FileOutputStream out = new FileOutputStream(targetFile);
            out.write(newYaml.getBytes("UTF-8"));
            out.flush();
            out.close();
        } else {
            logger.info("Creating '" + targetFile.getAbsolutePath() + "'");
            Files.createParentDirs(targetFile);
            FileOutputStream out = new FileOutputStream(targetFile);
            out.write(file.getData().getBytes("UTF-8"));
            out.flush();
            out.close();
        }
    }
    for (GitFile file : files) {
        File targetFile = new File(this.tmpdir.getAbsolutePath() + File.separator + "gitrepo" + file.getDirName() + File.separator + file.getFileName());
        if (file.isDelete()) {
            if (file.isNamespace()) {
                logger.info("Deleting namespace, removing directory '" + file.getDirName() + "'");
                // git.rm().addFilepattern("." + file.getDirName()).call();
                FileUtils.forceDelete(new File(this.tmpdir.getAbsolutePath() + File.separator + "gitrepo" + file.getDirName()));
                git.rm().addFilepattern(file.getDirName().substring(1)).call();
            }
        }
    }
}
Also used : GitFile(com.tremolosecurity.provisioning.tasks.dataobj.GitFile) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonPatch(com.github.fge.jsonpatch.JsonPatch) FileInputStream(java.io.FileInputStream) Yaml(org.yaml.snakeyaml.Yaml) JSONObject(org.json.simple.JSONObject) FileOutputStream(java.io.FileOutputStream) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) File(java.io.File) GitFile(com.tremolosecurity.provisioning.tasks.dataobj.GitFile) Map(java.util.Map) JsonMergePatch(com.github.fge.jsonpatch.mergepatch.JsonMergePatch) Patch(com.github.fge.jsonpatch.Patch) JsonPatch(com.github.fge.jsonpatch.JsonPatch) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with GitFile

use of com.tremolosecurity.provisioning.tasks.dataobj.GitFile in project OpenUnison by TremoloSecurity.

the class DeleteK8sObject method doTask.

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    String localURL = task.renderTemplate(this.url, request);
    HttpCon con = null;
    String localTarget = task.renderTemplate(this.targetName, request);
    OpenShiftTarget os = (OpenShiftTarget) task.getConfigManager().getProvisioningEngine().getTarget(localTarget).getProvider();
    try {
        String token = os.getAuthToken();
        con = os.createClient();
        boolean writeToRequest = false;
        if (this.writeToRequestConfig != null) {
            writeToRequest = task.renderTemplate(this.writeToRequestConfig, request).equalsIgnoreCase("true");
        }
        if (writeToRequest) {
            logger.debug("Writing to secret");
            String localPath = task.renderTemplate(this.path, request);
            String dirName;
            String fileName;
            int lastSlash = localPath.lastIndexOf('/');
            if (lastSlash == -1) {
                dirName = "";
                fileName = localPath;
            } else {
                dirName = localPath.substring(0, lastSlash);
                fileName = localPath.substring(lastSlash + 1);
            }
            JSONObject fileInfo = new JSONObject();
            fileInfo.put("fileName", fileName);
            fileInfo.put("dirName", dirName);
            fileInfo.put("delete", true);
            GitFile gitFile = new GitFile(fileName, dirName, true, kind.equalsIgnoreCase("Namespace"));
            List<GitFile> gitFiles = (List<GitFile>) request.get(this.requestAttribute);
            if (gitFiles == null) {
                gitFiles = new ArrayList<GitFile>();
                request.put(this.requestAttribute, gitFiles);
            }
            gitFiles.add(gitFile);
        } else {
            String respJSON = os.callWSDelete(token, con, localURL);
            if (logger.isDebugEnabled()) {
                logger.debug("Response for deleting object : '" + respJSON + "'");
            }
            JSONParser parser = new JSONParser();
            JSONObject resp = (JSONObject) parser.parse(respJSON);
            String kind = (String) resp.get("kind");
            String projectName = (String) ((JSONObject) resp.get("metadata")).get("name");
            if (logger.isDebugEnabled()) {
                logger.debug("kind : '" + kind + "' / '" + this.kind + "'");
            }
            if (kind.equalsIgnoreCase(this.kind)) {
                this.task.getConfigManager().getProvisioningEngine().logAction(localTarget, true, ProvisioningUtil.ActionType.Delete, approvalID, this.task.getWorkflow(), label, projectName);
            } else if (resp.get("status") != null) {
                String status = (String) resp.get("status");
                logger.info("status : '" + status + "'");
                if (status != null && status.equalsIgnoreCase("success")) {
                    this.task.getConfigManager().getProvisioningEngine().logAction(localTarget, true, ProvisioningUtil.ActionType.Delete, approvalID, this.task.getWorkflow(), label, projectName);
                } else {
                    throw new ProvisioningException("Could not delete " + kind + " with url '" + localURL + "' - '" + respJSON + "'");
                }
            } else {
                throw new ProvisioningException("Could not delete " + kind + " with url '" + localURL + "' - '" + respJSON + "'");
            }
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not delete " + kind + " - " + localURL, e);
    } finally {
        if (con != null) {
            con.getBcm().close();
        }
    }
    return true;
}
Also used : GitFile(com.tremolosecurity.provisioning.tasks.dataobj.GitFile) Workflow(com.tremolosecurity.provisioning.core.Workflow) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ArrayList(java.util.ArrayList) List(java.util.List) JSONParser(org.json.simple.parser.JSONParser)

Example 4 with GitFile

use of com.tremolosecurity.provisioning.tasks.dataobj.GitFile in project OpenUnison by TremoloSecurity.

the class PatchK8sObject method doTask.

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
    String localTemplate = task.renderTemplate(template, request);
    if (logger.isDebugEnabled()) {
        logger.debug("localTemplate : '" + localTemplate + "'");
    }
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    String localURL = task.renderTemplate(this.url, request);
    HttpCon con = null;
    String localTarget = task.renderTemplate(this.targetName, request);
    OpenShiftTarget os = (OpenShiftTarget) task.getConfigManager().getProvisioningEngine().getTarget(localTarget).getProvider();
    try {
        String token = os.getAuthToken();
        con = os.createClient();
        boolean writeToRequest = false;
        if (this.writeToRequestConfig != null) {
            writeToRequest = task.renderTemplate(this.writeToRequestConfig, request).equalsIgnoreCase("true");
        }
        if (writeToRequest) {
            logger.debug("Writing to secret");
            String localPath = task.renderTemplate(this.path, request);
            String dirName;
            String fileName;
            int lastSlash = localPath.lastIndexOf('/');
            if (lastSlash == -1) {
                dirName = "";
                fileName = localPath;
            } else {
                dirName = localPath.substring(0, lastSlash);
                fileName = localPath.substring(lastSlash + 1);
            }
            GitFile gitFile = new GitFile(fileName, dirName, false, false);
            gitFile.setData(localTemplate);
            gitFile.setPatch(true);
            gitFile.setPatchType(this.patchType);
            List<GitFile> gitFiles = (List<GitFile>) request.get(this.requestAttribute);
            if (gitFiles == null) {
                gitFiles = new ArrayList<GitFile>();
                request.put(this.requestAttribute, gitFiles);
            }
            gitFiles.add(gitFile);
        } else {
            if (this.isObjectExists(os, token, con, localURL, localTemplate)) {
                String respJSON = os.callWSPatchJson(token, con, localURL, localTemplate, this.patchContentType);
                if (logger.isDebugEnabled()) {
                    logger.debug("Response for creating project : '" + respJSON + "'");
                }
                JSONParser parser = new JSONParser();
                JSONObject resp = (JSONObject) parser.parse(respJSON);
                String kind = (String) resp.get("kind");
                String projectName = (String) ((JSONObject) resp.get("metadata")).get("name");
                if (!kind.equalsIgnoreCase(this.kind)) {
                    throw new ProvisioningException("Could not create " + kind + " with json '" + localTemplate + "' - '" + respJSON + "'");
                } else {
                    this.task.getConfigManager().getProvisioningEngine().logAction(localTarget, true, ActionType.Replace, approvalID, this.task.getWorkflow(), label, projectName);
                }
            } else {
                throw new ProvisioningException("Object '" + localURL + "' does not exist");
            }
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not create " + kind, e);
    } finally {
        if (con != null) {
            con.getBcm().close();
        }
    }
    return true;
}
Also used : GitFile(com.tremolosecurity.provisioning.tasks.dataobj.GitFile) Workflow(com.tremolosecurity.provisioning.core.Workflow) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(org.json.simple.parser.ParseException) 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) ArrayList(java.util.ArrayList) List(java.util.List) JSONParser(org.json.simple.parser.JSONParser)

Example 5 with GitFile

use of com.tremolosecurity.provisioning.tasks.dataobj.GitFile in project OpenUnison by TremoloSecurity.

the class PushToApiServer method doTask.

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
    String localTarget = task.renderTemplate(this.target, request);
    OpenShiftTarget target = (OpenShiftTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(localTarget).getProvider();
    HttpCon con = null;
    try {
        con = target.createClient();
        StringBuilder sb = new StringBuilder();
        List<GitFile> files = (List<GitFile>) request.get(requestObject);
        if (files == null) {
            throw new Exception("No gitfiles stored in '" + requestObject + "'");
        }
        for (GitFile f : files) {
            Yaml yaml = new Yaml();
            Map<String, Object> map = (Map<String, Object>) yaml.load(new ByteArrayInputStream(f.getData().getBytes("UTF-8")));
            JSONObject jsonObject = new JSONObject(map);
            String localTemplateJSON = jsonObject.toJSONString();
            if (!target.isObjectExistsByName(target.getAuthToken(), con, f.getDirName(), f.getFileName())) {
                logger.info(new StringBuilder().append("Writing ").append(f.getDirName()).append('/').append(f.getFileName()).toString());
                target.callWSPost(target.getAuthToken(), con, f.getDirName(), localTemplateJSON);
            }
        }
    } 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();
        }
    }
    return true;
}
Also used : GitFile(com.tremolosecurity.provisioning.tasks.dataobj.GitFile) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) IOException(java.io.IOException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) List(java.util.List) JSONObject(org.json.simple.JSONObject) Map(java.util.Map)

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