Search in sources :

Example 41 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class K8sWatcher method initalRun.

public void initalRun() throws ProvisioningException {
    OpenShiftTarget k8s = (OpenShiftTarget) provisioningEngine.getTarget(k8sTarget).getProvider();
    if (k8s == null) {
        throw new ProvisioningException("Target " + k8sTarget + " does not exist");
    }
    HttpCon http;
    try {
        http = k8s.createClient();
    } catch (Exception e1) {
        throw new ProvisioningException("Could not create http connection", e1);
    }
    this.resourceVersions = new HashSet<String>();
    try {
        String token = k8s.getAuthToken();
        String json = null;
        try {
            json = k8s.callWS(token, http, uri);
        } catch (HttpResponseException e) {
            logger.warn("Could not retrieve urls, dynamic urls will not be supported", e);
            return;
        }
        JSONObject list = (JSONObject) new JSONParser().parse(json);
        JSONArray items = (JSONArray) list.get("items");
        if (items == null) {
            logger.error("Invalid JSON Response : '" + json + "'");
            return;
        }
        for (Object o : items) {
            JSONObject jsonObj = (JSONObject) o;
            String strjson = jsonObj.toString();
            if (logger.isDebugEnabled())
                logger.debug("json before includes : " + strjson);
            StringBuffer b = new StringBuffer();
            b.setLength(0);
            OpenUnisonConfigLoader.integrateIncludes(b, strjson);
            if (logger.isDebugEnabled())
                logger.debug("json after includes : " + b.toString());
            jsonObj = (JSONObject) new JSONParser().parse(b.toString());
            JSONObject metadata = (JSONObject) jsonObj.get("metadata");
            String resourceVersion = (String) metadata.get("resourceVersion");
            if (this.resourceVersions.contains(resourceVersion)) {
                logger.info("Resource " + resourceVersion + " already processed, skipping");
            } else {
                this.resourceVersions.add(resourceVersion);
                this.watchee.addObject(cfgMgr.getCfg(), jsonObj);
            }
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not get urls", e);
    } finally {
        try {
            http.getHttp().close();
        } catch (IOException e) {
            logger.warn(e);
        }
        http.getBcm().close();
    }
    this.keepRunning = true;
    logger.info("Adding stoppable thread");
    GlobalEntries.getGlobalEntries().getConfigManager().addThread(this);
    logger.info("Starting watch");
    new Thread(this).start();
}
Also used : JSONArray(org.json.simple.JSONArray) OpenShiftTarget(com.tremolosecurity.unison.openshiftv3.OpenShiftTarget) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpResponseException(org.apache.http.client.HttpResponseException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) StopableThread(com.tremolosecurity.server.StopableThread) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Example 42 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class K8sWatcher method runWatch.

private void runWatch(OpenShiftTarget k8s) {
    HttpCon http;
    try {
        http = k8s.createClient();
    } catch (Exception e1) {
        logger.error("Could not create connection", e1);
        return;
    }
    try {
        String url = new StringBuilder().append(k8s.getUrl()).append(this.uri).append("?watch=true&timeoutSecond=25").toString();
        logger.info("watching " + url);
        HttpGet get = new HttpGet(url);
        get.setHeader("Authorization", new StringBuilder().append("Bearer ").append(k8s.getAuthToken()).toString());
        HttpResponse resp = http.getHttp().execute(get);
        BufferedReader in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        String line = null;
        HttpCon nonwatchHttp = k8s.createClient();
        while ((line = in.readLine()) != null) {
            JSONObject event = (JSONObject) new JSONParser().parse(line);
            String action = (String) event.get("type");
            JSONObject jsonObject = (JSONObject) event.get("object");
            String strjson = jsonObject.toString();
            if (logger.isDebugEnabled())
                logger.debug("json before includes : " + strjson);
            StringBuffer b = new StringBuffer();
            b.setLength(0);
            OpenUnisonConfigLoader.integrateIncludes(b, strjson);
            if (logger.isDebugEnabled())
                logger.debug("json after includes : " + b.toString());
            jsonObject = (JSONObject) new JSONParser().parse(b.toString());
            JSONObject metadata = (JSONObject) jsonObject.get("metadata");
            String resourceVersion = (String) metadata.get("resourceVersion");
            if (this.resourceVersions.contains(resourceVersion)) {
                logger.info("Resource " + resourceVersion + " already processed, skipping");
            } else {
                this.resourceVersions.add(resourceVersion);
                if (action.equalsIgnoreCase("ADDED")) {
                    this.watchee.addObject(this.cfgMgr.getCfg(), jsonObject);
                } else if (action.equalsIgnoreCase("MODIFIED")) {
                    this.watchee.modifyObject(this.cfgMgr.getCfg(), jsonObject);
                } else {
                    // deleted
                    this.watchee.deleteObject(this.cfgMgr.getCfg(), jsonObject);
                }
            }
        }
        nonwatchHttp.getHttp().close();
        nonwatchHttp.getBcm().close();
    } catch (Exception e) {
        logger.error("Could not run watch, waiting 10 seconds", e);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e1) {
        // do nothing
        }
        return;
    } finally {
        if (http != null) {
            try {
                http.getHttp().close();
            } catch (IOException e) {
            }
            http.getBcm().close();
        }
    }
    return;
}
Also used : InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpResponseException(org.apache.http.client.HttpResponseException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) BufferedReader(java.io.BufferedReader) JSONParser(org.json.simple.parser.JSONParser)

Example 43 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class UserPrincipal method deleteUser.

public void deleteUser(User user, Map<String, Object> request) throws ProvisioningException {
    UserPrincipal principal = new UserPrincipal(user.getUserID(), multiDomain, primaryDomain);
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    try {
        HttpCon con = this.createClient();
        try {
            if (principal.isPrimaryDomain()) {
                IPACall deleteUser = new IPACall();
                deleteUser.setId(0);
                deleteUser.setMethod("user_del");
                ArrayList<String> userArray = new ArrayList<String>();
                userArray.add(principal.getUid());
                deleteUser.getParams().add(userArray);
                HashMap<String, String> additionalParams = new HashMap<String, String>();
                deleteUser.getParams().add(additionalParams);
                IPAResponse resp = this.executeIPACall(deleteUser, con);
                this.cfgMgr.getProvisioningEngine().logAction(name, true, ActionType.Delete, approvalID, workflow, "uid", user.getUserID());
            } else {
                IPACall idOveride = new IPACall();
                idOveride.setId(0);
                idOveride.setMethod("idoverrideuser_del");
                List<String> params = new ArrayList<String>();
                params.add(this.trustViewName);
                params.add(principal.getUPN());
                idOveride.getParams().add(params);
                Map<String, Object> param2 = new HashMap<String, Object>();
                idOveride.getParams().add(param2);
                try {
                    IPAResponse resp = this.executeIPACall(idOveride, con);
                } catch (IPAException e) {
                    if (!e.getMessage().equalsIgnoreCase("no modifications to be performed")) {
                        throw e;
                    }
                }
            }
        } finally {
            if (con != null) {
                con.getBcm().shutdown();
            }
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not run search", e);
    }
}
Also used : IPAResponse(com.tremolosecurity.unison.freeipa.json.IPAResponse) HashMap(java.util.HashMap) IPACall(com.tremolosecurity.unison.freeipa.json.IPACall) ArrayList(java.util.ArrayList) Workflow(com.tremolosecurity.provisioning.core.Workflow) ClientProtocolException(org.apache.http.client.ClientProtocolException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) IPAException(com.tremolosecurity.unison.freeipa.util.IPAException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IPAException(com.tremolosecurity.unison.freeipa.util.IPAException) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException)

Example 44 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class UserPrincipal method isGroupExists.

@Override
public boolean isGroupExists(String name, User user, Map<String, Object> request) throws ProvisioningException {
    IPACall groupSearch = new IPACall();
    groupSearch.setId(0);
    groupSearch.setMethod("group_show");
    ArrayList<String> groupArray = new ArrayList<String>();
    groupArray.add(name);
    groupSearch.getParams().add(groupArray);
    HashMap<String, String> additionalParams = new HashMap<String, String>();
    groupSearch.getParams().add(additionalParams);
    HttpCon con = null;
    try {
        con = this.createClient();
        IPAResponse resp = this.executeIPACall(groupSearch, con);
        return true;
    } catch (IPAException ipae) {
        if (ipae.getCode() == 4001) {
            return false;
        } else {
            throw new ProvisioningException("Could not find groups", ipae);
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not find groups", e);
    } finally {
        if (con != null) {
            con.getBcm().close();
        }
    }
}
Also used : IPAResponse(com.tremolosecurity.unison.freeipa.json.IPAResponse) IPAException(com.tremolosecurity.unison.freeipa.util.IPAException) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) HashMap(java.util.HashMap) IPACall(com.tremolosecurity.unison.freeipa.json.IPACall) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ArrayList(java.util.ArrayList) ClientProtocolException(org.apache.http.client.ClientProtocolException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) IPAException(com.tremolosecurity.unison.freeipa.util.IPAException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 45 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class UserPrincipal method addGroup.

@Override
public void addGroup(String name, Map<String, String> additionalAttributes, 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");
    IPACall groupSearch = new IPACall();
    groupSearch.setId(0);
    groupSearch.setMethod("group_add");
    ArrayList<String> groupArray = new ArrayList<String>();
    groupArray.add(name);
    groupSearch.getParams().add(groupArray);
    HashMap<String, String> additionalParams = new HashMap<String, String>();
    for (String key : additionalAttributes.keySet()) {
        additionalParams.put(key, additionalAttributes.get(key));
    }
    groupSearch.getParams().add(additionalParams);
    HttpCon con = null;
    try {
        con = this.createClient();
        IPAResponse resp = this.executeIPACall(groupSearch, con);
        this.cfgMgr.getProvisioningEngine().logAction(name, true, ActionType.Add, approvalID, workflow, "group-object", name);
    } catch (Exception e) {
        throw new ProvisioningException("Could not find groups", e);
    } finally {
        if (con != null) {
            con.getBcm().close();
        }
    }
}
Also used : IPAResponse(com.tremolosecurity.unison.freeipa.json.IPAResponse) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) HashMap(java.util.HashMap) IPACall(com.tremolosecurity.unison.freeipa.json.IPACall) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ArrayList(java.util.ArrayList) Workflow(com.tremolosecurity.provisioning.core.Workflow) ClientProtocolException(org.apache.http.client.ClientProtocolException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) IPAException(com.tremolosecurity.unison.freeipa.util.IPAException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

HttpCon (com.tremolosecurity.provisioning.util.HttpCon)104 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)82 IOException (java.io.IOException)70 ClientProtocolException (org.apache.http.client.ClientProtocolException)49 JSONObject (org.json.simple.JSONObject)43 ParseException (org.json.simple.parser.ParseException)33 Workflow (com.tremolosecurity.provisioning.core.Workflow)32 ArrayList (java.util.ArrayList)32 UnsupportedEncodingException (java.io.UnsupportedEncodingException)31 OpenShiftTarget (com.tremolosecurity.unison.openshiftv3.OpenShiftTarget)27 JSONParser (org.json.simple.parser.JSONParser)25 HashMap (java.util.HashMap)24 JSONArray (org.json.simple.JSONArray)22 User (com.tremolosecurity.provisioning.core.User)18 Attribute (com.tremolosecurity.saml.Attribute)17 Gson (com.google.gson.Gson)16 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 HashSet (java.util.HashSet)13 List (java.util.List)13 KSToken (com.tremolosecurity.unison.openstack.util.KSToken)12