Search in sources :

Example 31 with OpenShiftTarget

use of com.tremolosecurity.unison.openshiftv3.OpenShiftTarget in project OpenUnison by TremoloSecurity.

the class K8sUtils method loadConfigMap.

public static Map<String, String> loadConfigMap(String targetName, String namespace, String configMapName) throws Exception {
    HashMap<String, String> map = new HashMap<String, String>();
    OpenShiftTarget k8s = (OpenShiftTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(targetName).getProvider();
    HttpCon con = k8s.createClient();
    try {
        StringBuilder sb = new StringBuilder();
        sb.append("/api/v1/namespaces/").append(namespace).append("/configmaps/").append(configMapName);
        String uri = sb.toString();
        String jsonData = k8s.callWS(k8s.getAuthToken(), con, uri);
        JSONObject root = (JSONObject) new JSONParser().parse(jsonData);
        for (Object key : ((JSONObject) root.get("data")).keySet()) {
            map.put((String) key, (String) ((JSONObject) root.get("data")).get(key));
        }
    } finally {
        if (con != null) {
            con.getHttp().close();
            con.getBcm().close();
        }
    }
    return map;
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Example 32 with OpenShiftTarget

use of com.tremolosecurity.unison.openshiftv3.OpenShiftTarget in project OpenUnison by TremoloSecurity.

the class K8sWatcher method run.

@Override
public void run() {
    logger.info("Starting watch");
    while (this.keepRunning) {
        OpenShiftTarget k8s;
        try {
            k8s = (OpenShiftTarget) this.provisioningEngine.getTarget(k8sTarget).getProvider();
        } catch (ProvisioningException e2) {
            logger.error("Could not load target, stopping watch", e2);
            return;
        }
        runWatch(k8s);
    }
}
Also used : ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget)

Example 33 with OpenShiftTarget

use of com.tremolosecurity.unison.openshiftv3.OpenShiftTarget in project OpenUnison by TremoloSecurity.

the class OpenShiftInsert method search.

@Override
public void search(SearchInterceptorChain chain, DistinguishedName base, Int scope, Filter filter, ArrayList<Attribute> attributes, Bool typesOnly, Results results, LDAPSearchConstraints constraints) throws LDAPException {
    OpenShiftTarget os = null;
    try {
        os = (OpenShiftTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.osTarget).getProvider();
    } catch (ProvisioningException e1) {
        logger.error("Could not retrieve kubernetes target", e1);
        throw new LDAPException("Could not connect to kubernetes", LDAPException.OPERATIONS_ERROR, LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR));
    }
    // base search
    if (scope.getValue() == 0) {
        // dir root
        if (base.getDN().equals(this.baseDN)) {
            ArrayList<Entry> ret = new ArrayList<Entry>();
            ret.add(new Entry(EntryUtil.createBaseEntry(this.baseDN)));
            chain.addResult(results, new IteratorEntrySet(ret.iterator()), base, scope, filter, attributes, typesOnly, constraints);
            return;
        } else {
            String name = ((RDN) base.getDN().getRDNs().get(0)).getValue();
            loadUserFromOpenShift(chain, base, scope, filter, attributes, typesOnly, results, constraints, os, name, base.getDN().toString(), true);
            return;
        }
    } else if (scope.getValue() == 1) {
        if (base.getDN().equals(this.baseDN)) {
            String name = userFromFilter(filter.getRoot());
            loadUserFromOpenShift(chain, base, scope, filter, attributes, typesOnly, results, constraints, os, name, new StringBuilder().append("uid=").append(name).append(",").append(base.getDN().toString()).toString(), false);
            return;
        }
    } else {
        // only subtree left
        String name = userFromFilter(filter.getRoot());
        loadUserFromOpenShift(chain, base, scope, filter, attributes, typesOnly, results, constraints, os, name, new StringBuilder().append("uid=").append(name).append(",").append(this.baseDN.toString()).toString(), false);
        return;
    }
}
Also used : IteratorEntrySet(net.sourceforge.myvd.util.IteratorEntrySet) Entry(net.sourceforge.myvd.types.Entry) LDAPEntry(com.novell.ldap.LDAPEntry) LDAPException(com.novell.ldap.LDAPException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ArrayList(java.util.ArrayList) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) RDN(com.novell.ldap.util.RDN)

Example 34 with OpenShiftTarget

use of com.tremolosecurity.unison.openshiftv3.OpenShiftTarget in project OpenUnison by TremoloSecurity.

the class AddGroupToRole 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 localProjectName = task.renderTemplate(projectName, request);
    String localGroupName = task.renderTemplate(groupName, request);
    String localPolicyName = task.renderTemplate(roleName, request);
    HttpCon con = null;
    OpenShiftTarget os = (OpenShiftTarget) task.getConfigManager().getProvisioningEngine().getTarget("openshift").getProvider();
    try {
        String token = os.getAuthToken();
        con = os.createClient();
        if (this.openShiftVersion == 3.6) {
            addTo36Role(os, token, con, localProjectName, localPolicyName, localGroupName, approvalID);
        } else {
            addToRBACRole(os, token, con, localProjectName, localPolicyName, localGroupName, approvalID);
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not add group to role", e);
    } finally {
        if (con != null) {
            con.getBcm().close();
        }
    }
    return true;
}
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) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException)

Example 35 with OpenShiftTarget

use of com.tremolosecurity.unison.openshiftv3.OpenShiftTarget in project OpenUnison by TremoloSecurity.

the class CreateProject 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");
    HttpCon con = null;
    OpenShiftTarget os = (OpenShiftTarget) task.getConfigManager().getProvisioningEngine().getTarget("openshift").getProvider();
    try {
        String token = os.getAuthToken();
        con = os.createClient();
        if (!os.isObjectExists(token, con, "/apis/project.openshift.io/v1/projects", localTemplate)) {
            String respJSON = os.callWSPost(token, con, "/apis/project.openshift.io/v1/projectrequests", localTemplate);
            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("Project")) {
                throw new ProvisioningException("Could not create project with json '" + localTemplate + "' - '" + respJSON + "'");
            } else {
                this.task.getConfigManager().getProvisioningEngine().logAction(this.targetName, true, ActionType.Add, approvalID, this.task.getWorkflow(), "openshift-project", projectName);
            }
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not create project", e);
    } finally {
        if (con != null) {
            con.getBcm().close();
        }
    }
    return true;
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) JSONParser(org.json.simple.parser.JSONParser) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException)

Aggregations

OpenShiftTarget (com.tremolosecurity.unison.openshiftv3.OpenShiftTarget)36 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)30 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)27 JSONObject (org.json.simple.JSONObject)18 ArrayList (java.util.ArrayList)13 JSONParser (org.json.simple.parser.JSONParser)13 HashMap (java.util.HashMap)12 IOException (java.io.IOException)11 Workflow (com.tremolosecurity.provisioning.core.Workflow)9 LDAPException (com.novell.ldap.LDAPException)7 List (java.util.List)7 Map (java.util.Map)7 Attribute (com.tremolosecurity.saml.Attribute)6 GitFile (com.tremolosecurity.provisioning.tasks.dataobj.GitFile)5 JSONArray (org.json.simple.JSONArray)4 ParseException (org.json.simple.parser.ParseException)4 Gson (com.google.gson.Gson)3 LDAPEntry (com.novell.ldap.LDAPEntry)3 K8sUser (com.tremolosecurity.myvd.dataObj.K8sUser)3 User (com.tremolosecurity.provisioning.core.User)3