Search in sources :

Example 66 with Workflow

use of com.tremolosecurity.provisioning.core.Workflow in project OpenUnison by TremoloSecurity.

the class ForkProject 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");
    GitlabUserProvider gitlab = (GitlabUserProvider) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.targetName).getProvider();
    GitLabApi api = gitlab.getApi();
    String localSourceProjectNamespace = task.renderTemplate(this.sourceProjectNamespace, request);
    String localSourceProjectName = task.renderTemplate(this.sourceProjectName, request);
    String localDestinationNamespace = task.renderTemplate(this.destintionNamespace, request);
    try {
        Project existingProject = api.getProjectApi().getProject(localSourceProjectNamespace, localSourceProjectName);
        Project newProject = api.getProjectApi().forkProject(existingProject, localDestinationNamespace);
        GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().logAction(gitlab.getName(), false, ActionType.Add, approvalID, workflow, "gitlab-fork-" + existingProject.getNameWithNamespace() + "-fork", localDestinationNamespace);
        String gitUrl = newProject.getSshUrlToRepo();
        String prefix = gitUrl.substring(0, gitUrl.indexOf("@") + 1);
        String suffix = gitUrl.substring(gitUrl.indexOf(":"));
        String newGitUrl = new StringBuilder().append(prefix).append(this.gitSshHost).append(suffix).toString();
        request.put("gitSshInternalURL", newGitUrl);
        request.put("gitSshUrl", newProject.getSshUrlToRepo());
    } catch (GitLabApiException e) {
        throw new ProvisioningException("Error looking up project " + localSourceProjectNamespace + "/" + localSourceProjectName, e);
    }
    return true;
}
Also used : GitlabUserProvider(com.tremolosecurity.unison.gitlab.provisioning.targets.GitlabUserProvider) Project(org.gitlab4j.api.models.Project) GitLabApi(org.gitlab4j.api.GitLabApi) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 67 with Workflow

use of com.tremolosecurity.provisioning.core.Workflow in project OpenUnison by TremoloSecurity.

the class CopyGroupMembers method doTask.

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
    String localWorkflowName = task.renderTemplate(this.workflowName, request);
    String localCopyFrom = task.renderTemplate(this.copyFrom, request);
    String localCopyTo = task.renderTemplate(this.copyTo, request);
    String memberAttr = task.getConfigManager().getCfg().getGroupMemberAttribute();
    String[] members = null;
    try {
        LDAPSearchResults rs = task.getConfigManager().getMyVD().search(localCopyFrom, 0, "(objectClass=*)", new ArrayList<String>());
        rs.hasMore();
        LDAPEntry group = rs.next();
        while (rs.hasMore()) rs.next();
        if (group != null && group.getAttribute(memberAttr) != null) {
            members = group.getAttribute(memberAttr).getStringValueArray();
        } else {
            members = new String[0];
        }
    } catch (LDAPException e) {
        throw new ProvisioningException("Could not load from group", e);
    }
    for (String member : members) {
        try {
            LDAPSearchResults rs = task.getConfigManager().getMyVD().search(member, 0, "(objectClass=*)", new ArrayList<String>());
            rs.hasMore();
            LDAPEntry ldapMember = rs.next();
            TremoloUser userToUpdate = new TremoloUser();
            userToUpdate.setUid(ldapMember.getAttribute(this.uidAttribute).getStringValue());
            userToUpdate.getAttributes().add(new Attribute(this.uidAttribute, userToUpdate.getUid()));
            userToUpdate.getGroups().add(localCopyTo);
            Workflow wf = task.getConfigManager().getProvisioningEngine().getWorkFlow(localWorkflowName);
            WFCall call = new WFCall();
            call.setReason("auto-creating approval group " + localCopyTo);
            call.setUidAttributeName(this.uidAttribute);
            call.setUser(userToUpdate);
            call.setRequestor(this.requestor);
            call.getRequestParams().put(ProvisioningParams.UNISON_EXEC_TYPE, ProvisioningParams.UNISON_EXEC_SYNC);
            wf.executeWorkflow(call);
        } catch (LDAPException e) {
            logger.warn("Could not load user '" + member + "'", e);
        }
    }
    return true;
}
Also used : LDAPEntry(com.novell.ldap.LDAPEntry) WFCall(com.tremolosecurity.provisioning.service.util.WFCall) LDAPSearchResults(com.novell.ldap.LDAPSearchResults) LDAPException(com.novell.ldap.LDAPException) TremoloUser(com.tremolosecurity.provisioning.service.util.TremoloUser) Attribute(com.tremolosecurity.saml.Attribute) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow)

Example 68 with Workflow

use of com.tremolosecurity.provisioning.core.Workflow 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 69 with Workflow

use of com.tremolosecurity.provisioning.core.Workflow 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 70 with Workflow

use of com.tremolosecurity.provisioning.core.Workflow in project OpenUnison by TremoloSecurity.

the class K8sCrdUserProvider method deleteUser.

@Override
public void deleteUser(User user, Map<String, Object> request) throws ProvisioningException {
    String k8sUserId = OpenShiftTarget.sub2uid(user.getUserID());
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    OpenShiftTarget k8s = null;
    try {
        k8s = (OpenShiftTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.k8sTarget).getProvider();
    } catch (ProvisioningException e1) {
        logger.error("Could not retrieve kubernetes target", e1);
        throw new ProvisioningException("Could not connect to kubernetes", e1);
    }
    String url = new StringBuilder().append("/apis/openunison.tremolo.io/v1/namespaces/").append(this.nameSpace).append("/users/").append(k8sUserId).toString();
    try {
        HttpCon con = k8s.createClient();
        try {
            k8s.callWSDelete(k8s.getAuthToken(), con, url);
            GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().logAction(this.name, true, ActionType.Delete, approvalID, workflow, "sub", user.getUserID());
        } finally {
            con.getHttp().close();
            con.getBcm().close();
        }
    } catch (Exception e) {
        logger.error("Could not search k8s", e);
        throw new ProvisioningException("Error searching kubernetes", e);
    }
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) LDAPException(com.novell.ldap.LDAPException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException)

Aggregations

Workflow (com.tremolosecurity.provisioning.core.Workflow)78 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)68 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)32 IOException (java.io.IOException)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)22 ClientProtocolException (org.apache.http.client.ClientProtocolException)21 Attribute (com.tremolosecurity.saml.Attribute)19 ArrayList (java.util.ArrayList)18 LDAPException (com.novell.ldap.LDAPException)17 HashMap (java.util.HashMap)17 User (com.tremolosecurity.provisioning.core.User)16 HashSet (java.util.HashSet)15 ParseException (org.json.simple.parser.ParseException)14 LDAPSearchResults (com.novell.ldap.LDAPSearchResults)12 JSONObject (org.json.simple.JSONObject)12 Gson (com.google.gson.Gson)11 LDAPEntry (com.novell.ldap.LDAPEntry)11 LDAPAttribute (com.novell.ldap.LDAPAttribute)10 GitLabApiException (org.gitlab4j.api.GitLabApiException)10 SQLException (java.sql.SQLException)9