Search in sources :

Example 26 with HttpCon

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

the class KeystoneProvisioningTarget method createClient.

public HttpCon createClient() throws Exception {
    ArrayList<Header> defheaders = new ArrayList<Header>();
    BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager(cfgMgr.getHttpClientSocketRegistry());
    RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false).build();
    CloseableHttpClient http = HttpClients.custom().setConnectionManager(bhcm).setDefaultHeaders(defheaders).setDefaultRequestConfig(rc).build();
    HttpCon con = new HttpCon();
    con.setBcm(bhcm);
    con.setHttp(http);
    return con;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) ArrayList(java.util.ArrayList) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 27 with HttpCon

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

the class KeystoneProvisioningTarget method listDomainObjs.

public List<KSDomain> listDomainObjs() throws ProvisioningException {
    HttpCon con = null;
    try {
        con = this.createClient();
        KSToken token = this.getToken(con);
        StringBuffer b = new StringBuffer();
        b.append(this.url).append("/domains?enabled");
        String json = this.callWS(token.getAuthToken(), con, b.toString());
        Gson gson = new Gson();
        return gson.fromJson(json, DomainsResponse.class).getDomains();
    } catch (Exception e) {
        throw new ProvisioningException("Could not work with keystone", e);
    } finally {
        if (con != null) {
            con.getBcm().shutdown();
        }
    }
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) DomainsResponse(com.tremolosecurity.unison.openstack.model.DomainsResponse) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) KSToken(com.tremolosecurity.unison.openstack.util.KSToken) Gson(com.google.gson.Gson) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException)

Example 28 with HttpCon

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

the class KeystoneProvisioningTarget method getDomainName.

public String getDomainName(String id) throws ProvisioningException {
    HttpCon con = null;
    try {
        con = this.createClient();
        KSToken token = this.getToken(con);
        StringBuffer b = new StringBuffer();
        b.append(this.url).append("/domains/").append(id);
        String json = this.callWS(token.getAuthToken(), con, b.toString());
        Gson gson = new Gson();
        DomainsResponse resp = gson.fromJson(json, DomainsResponse.class);
        if (resp.getDomain() == null) {
            return null;
        } else {
            return resp.getDomain().getName();
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not work with keystone", e);
    } finally {
        if (con != null) {
            con.getBcm().shutdown();
        }
    }
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) DomainsResponse(com.tremolosecurity.unison.openstack.model.DomainsResponse) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) KSToken(com.tremolosecurity.unison.openstack.util.KSToken) Gson(com.google.gson.Gson) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException)

Example 29 with HttpCon

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

the class Drupal8Target method deleteUser.

@Override
public void deleteUser(User user, Map<String, Object> request) throws ProvisioningException {
    int userID = 0;
    int approvalID = 0;
    int workflowID = 0;
    if (request.containsKey("TREMOLO_USER_ID")) {
        userID = (Integer) request.get("TREMOLO_USER_ID");
    }
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    if (request.containsKey("WORKFLOW_ID")) {
        workflowID = (Integer) request.get("WORKFLOW_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    StringBuilder sb = new StringBuilder();
    sb.append(url).append("/user/").append(user.getUserID()).append("?_format=json");
    HttpCon con = null;
    try {
        con = this.createClient();
        HttpDelete req = new HttpDelete(sb.toString());
        sb.setLength(0);
        sb.append(this.user).append(":").append(this.password);
        String azHeader = java.util.Base64.getEncoder().encodeToString(sb.toString().getBytes());
        sb.setLength(0);
        req.setHeader("Authorization", sb.append("Basic ").append(azHeader).toString());
        CloseableHttpResponse resp = con.getHttp().execute(req);
        if (resp.getStatusLine().getStatusCode() != 204) {
            logger.warn("User '" + user.getUserID() + "' not found");
            return;
        }
        this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Delete, approvalID, workflow, "uid", user.getUserID());
    } catch (Exception e) {
        throw new ProvisioningException("Could not find user", e);
    } finally {
        if (con != null) {
            try {
                con.getHttp().close();
            } catch (IOException e) {
            }
            con.getBcm().close();
        }
    }
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) HttpDelete(org.apache.http.client.methods.HttpDelete) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Workflow(com.tremolosecurity.provisioning.core.Workflow) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) ParseException(org.json.simple.parser.ParseException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 30 with HttpCon

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

the class Drupal8Target method createUser.

@Override
public void createUser(User user, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    int userID = 0;
    int approvalID = 0;
    int workflowID = 0;
    if (request.containsKey("TREMOLO_USER_ID")) {
        userID = (Integer) request.get("TREMOLO_USER_ID");
    }
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    if (request.containsKey("WORKFLOW_ID")) {
        workflowID = (Integer) request.get("WORKFLOW_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    StringBuilder sb = new StringBuilder();
    JSONObject root = new JSONObject();
    HashMap<String, String> attrsForAudit = new HashMap<String, String>();
    if (attributes.contains("name") && user.getAttribs().containsKey("name")) {
        this.setJsonValue("name", user.getAttribs().get("name").getValues().get(0), root);
        attrsForAudit.put("name", user.getAttribs().get("name").getValues().get(0));
    }
    if (attributes.contains("langcode") && user.getAttribs().containsKey("langcode")) {
        this.setJsonValue("langcode", user.getAttribs().get("langcode").getValues().get(0), root);
        attrsForAudit.put("langcode", user.getAttribs().get("langcode").getValues().get(0));
    }
    if (attributes.contains("preferred_langcode") && user.getAttribs().containsKey("preferred_langcode")) {
        this.setJsonValue("preferred_langcode", user.getAttribs().get("preferred_langcode").getValues().get(0), root);
        attrsForAudit.put("preferred_langcode", user.getAttribs().get("preferred_langcode").getValues().get(0));
    }
    if (attributes.contains("preferred_admin_langcode") && user.getAttribs().containsKey("preferred_admin_langcode")) {
        this.setJsonValue("preferred_admin_langcode", user.getAttribs().get("preferred_admin_langcode").getValues().get(0), root);
        attrsForAudit.put("preferred_admin_langcode", user.getAttribs().get("preferred_admin_langcode").getValues().get(0));
    }
    if (attributes.contains("mail") && user.getAttribs().containsKey("mail")) {
        this.setJsonValue("mail", user.getAttribs().get("mail").getValues().get(0), root);
        attrsForAudit.put("mail", user.getAttribs().get("mail").getValues().get(0));
    }
    if (attributes.contains("status") && user.getAttribs().containsKey("status")) {
        this.setJsonValueBoolean("status", user.getAttribs().get("status").getValues().get(0), root);
        attrsForAudit.put("status", user.getAttribs().get("status").getValues().get(0));
    }
    for (String userAttributeName : user.getAttribs().keySet()) {
        if (attributes.contains(userAttributeName) && !defaultAttributes.contains(userAttributeName)) {
            this.setJsonValue("field_" + userAttributeName, user.getAttribs().get(userAttributeName).getValues().get(0), root);
            attrsForAudit.put("field_" + userAttributeName, user.getAttribs().get(userAttributeName).getValues().get(0));
        }
    }
    JSONArray roles = new JSONArray();
    for (String groupName : user.getGroups()) {
        JSONObject group = new JSONObject();
        group.put("target_id", groupName);
        roles.add(group);
    }
    root.put("roles", roles);
    HttpPost post = new HttpPost(this.url + "/entity/user?_format=json");
    post.setHeader(new BasicHeader("X-CSRF-Token", UUID.randomUUID().toString()));
    post.addHeader("Content-Type", "application/json");
    try {
        post.setEntity(new StringEntity(root.toJSONString()));
    } catch (UnsupportedEncodingException e) {
        throw new ProvisioningException("Couldn't create user", e);
    }
    sb.setLength(0);
    sb.append(this.user).append(":").append(this.password);
    String azHeader = java.util.Base64.getEncoder().encodeToString(sb.toString().getBytes());
    sb.setLength(0);
    post.setHeader("Authorization", sb.append("Basic ").append(azHeader).toString());
    HttpCon con = null;
    try {
        con = this.createClient();
    } catch (Exception e) {
        throw new ProvisioningException("Couldn't create user", e);
    }
    try {
        CloseableHttpResponse resp = con.getHttp().execute(post);
        if (resp.getStatusLine().getStatusCode() == 201) {
            String json = EntityUtils.toString(resp.getEntity());
            JSONParser parser = new JSONParser();
            root = (JSONObject) parser.parse(json);
            String uid = getJsonValue("uid", root);
            this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Add, approvalID, workflow, "uid", uid);
            for (String attr : attrsForAudit.keySet()) {
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, attr, attrsForAudit.get(attr));
            }
            for (String groupName : user.getGroups()) {
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "role", groupName);
            }
            user.setUserID(uid);
        } else {
            throw new ProvisioningException("Could not create user with code " + resp.getStatusLine().getStatusCode());
        }
    } catch (IOException | ParseException e) {
        throw new ProvisioningException("Couldn't create user", e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) Workflow(com.tremolosecurity.provisioning.core.Workflow) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) ParseException(org.json.simple.parser.ParseException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) StringEntity(org.apache.http.entity.StringEntity) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) BasicHeader(org.apache.http.message.BasicHeader)

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