Search in sources :

Example 61 with HttpCon

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

the class AttributeChange method deleteUser.

@Override
public void deleteUser(User user, Map<String, Object> request) throws ProvisioningException {
    HttpCon con = null;
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    try {
        con = this.createClient();
        this.callDelete(con, new StringBuilder().append("/users/").append(URLEncoder.encode(user.getUserID(), "UTf-8")).toString());
        this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Delete, approvalID, workflow, "userPrincipalName", user.getUserID());
    } catch (Exception e) {
        throw new ProvisioningException("Could not delete user", e);
    } finally {
        try {
            con.getHttp().close();
        } catch (IOException e) {
        }
        con.getBcm().close();
    }
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 62 with HttpCon

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

the class AttributeChange method findUser.

@Override
public User findUser(String userID, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    HttpCon con = null;
    Set<String> attributesLocal = new HashSet<String>();
    attributesLocal.addAll(attributes);
    attributes = attributesLocal;
    if (!attributes.contains("id")) {
        attributes.add("id");
    }
    StringBuilder select = new StringBuilder();
    for (String attr : attributes) {
        select.append(attr).append(',');
    }
    String selectAttrs = select.toString();
    selectAttrs.subSequence(0, selectAttrs.lastIndexOf(','));
    try {
        con = this.createClient();
        String json = this.callWS(con, new StringBuilder().append("/users/").append(URLEncoder.encode(userID, "UTf-8")).append("?$select=").append(URLEncoder.encode(selectAttrs, "UTF-8")).toString());
        JSONObject root = (JSONObject) new JSONParser().parse(json);
        if (root.containsKey("error")) {
            JSONObject error = (JSONObject) root.get("error");
            String code = (String) error.get("code");
            if (code.equalsIgnoreCase("Request_ResourceNotFound")) {
                return null;
            } else {
                throw new ProvisioningException("Could not lookup user " + json);
            }
        }
        User user = new User((String) root.get("userPrincipalName"));
        for (String attributeName : attributes) {
            if (root.get(attributeName) != null) {
                String val = root.get(attributeName).toString();
                user.getAttribs().put(attributeName, new Attribute(attributeName, val));
            }
        }
        json = this.callWS(con, new StringBuilder().append("/users/").append(URLEncoder.encode(userID, "UTf-8")).append("/memberOf").toString());
        root = (JSONObject) new JSONParser().parse(json);
        if (root.containsKey("error")) {
            JSONObject error = (JSONObject) root.get("error");
            String code = (String) error.get("code");
            throw new ProvisioningException("Could not lookup user " + json);
        }
        JSONArray values = (JSONArray) root.get("value");
        for (Object o : values) {
            JSONObject group = (JSONObject) o;
            if (group.get("@odata.type").equals("#microsoft.graph.group")) {
                user.getGroups().add((String) group.get("displayName"));
            }
        }
        return user;
    } catch (Exception e) {
        throw new ProvisioningException("Could not find user", e);
    } finally {
        try {
            con.getHttp().close();
        } catch (IOException e) {
        }
        con.getBcm().close();
    }
// return null;
}
Also used : User(com.tremolosecurity.provisioning.core.User) Attribute(com.tremolosecurity.saml.Attribute) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) HashSet(java.util.HashSet)

Example 63 with HttpCon

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

the class AttributeChange method synUser.

private void synUser(User user, boolean addOnly, Set<String> attributes, User fromAzure, int approvalID, Workflow workflow) throws ProvisioningException {
    List<AttributeChange> changes = new ArrayList<AttributeChange>();
    JSONObject patch = new JSONObject();
    for (String attributeName : attributes) {
        if (attributeName.equals("mail") || attributeName.equals("id")) {
            continue;
        }
        Attribute fromUser = user.getAttribs().get(attributeName);
        Attribute fromAd = fromAzure.getAttribs().get(attributeName);
        if (fromUser != null && fromAd == null) {
            patch.put(attributeName, getValue(fromUser));
            changes.add(new AttributeChange(fromUser.getName(), fromUser.getValues().get(0), ActionType.Add));
        } else if (fromUser != null && fromAd != null && !(fromUser.getValues().get(0).equals(fromAd.getValues().get(0)))) {
            patch.put(attributeName, getValue(fromUser));
            changes.add(new AttributeChange(fromUser.getName(), fromUser.getValues().get(0), ActionType.Replace));
        } else if (fromUser == null && fromAd != null && !addOnly) {
            patch.put(attributeName, null);
            changes.add(new AttributeChange(fromAd.getName(), fromAd.getValues().get(0), ActionType.Delete));
        }
    }
    String id;
    if (fromAzure.getAttribs().get("id") != null) {
        id = fromAzure.getAttribs().get("id").getValues().get(0);
    } else {
        id = user.getAttribs().get("id").getValues().get(0);
    }
    HttpCon con = null;
    try {
        con = this.createClient();
        StringBuilder sb = new StringBuilder();
        this.callWSPatchJson(con, sb.append("/users/").append(URLEncoder.encode(user.getUserID(), "UTf-8")).toString(), patch.toString());
        for (AttributeChange change : changes) {
            this.cfgMgr.getProvisioningEngine().logAction(this.name, false, change.action, approvalID, workflow, change.name, change.value);
        }
        Map<String, String> groups = this.loadGroups(con);
        Set<String> curentGroups = new HashSet<String>();
        curentGroups.addAll(fromAzure.getGroups());
        for (String group : user.getGroups()) {
            if (!curentGroups.contains(group)) {
                String uri = new StringBuilder().append("/groups/").append(groups.get(group)).append("/members/$ref").toString();
                JSONObject root = new JSONObject();
                root.put("@odata.id", new StringBuilder().append("https://graph.microsoft.com/v1.0/directoryObjects/").append(id).toString());
                this.callWSPostJsonNoReesponseExpected(con, uri, root.toString());
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "group", group);
            }
        }
        if (!addOnly) {
            curentGroups = new HashSet<String>();
            curentGroups.addAll(user.getGroups());
            for (String group : fromAzure.getGroups()) {
                if (!curentGroups.contains(group)) {
                    String uri = new StringBuilder().append("/groups/").append(groups.get(group)).append("/members/").append(id).append("/$ref").toString();
                    this.callDelete(con, uri);
                    this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Delete, approvalID, workflow, "group", group);
                }
            }
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not find user", e);
    } finally {
        try {
            con.getHttp().close();
        } catch (IOException e) {
        }
        con.getBcm().close();
    }
}
Also used : Attribute(com.tremolosecurity.saml.Attribute) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) HashSet(java.util.HashSet)

Example 64 with HttpCon

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

the class AddMatterMostTeam method doTask.

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
    MatterMostProvider mm = (MatterMostProvider) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(target).getProvider();
    HttpCon con = null;
    try {
        con = mm.createClient();
        StringBuilder sb = new StringBuilder();
        sb.append("/api/v4/teams/name/").append(teamName);
        String jsonResp = mm.callWS(con, sb.toString());
        JSONObject team = (JSONObject) new JSONParser().parse(jsonResp);
        String teamId = (String) team.get("id");
        if (teamId == null) {
            throw new ProvisioningException("Team '" + teamName + "' does not exist");
        }
        JSONObject userFromMM = mm.loadUserJson(user.getUserID(), con);
        if (userFromMM == null) {
            throw new ProvisioningException("User '" + user.getUserID() + "' does not exist");
        }
        String userId = (String) userFromMM.get("id");
        if (userId == null) {
            throw new ProvisioningException("User '" + user.getUserID() + "' does not exist");
        }
        JSONObject addTeam = new JSONObject();
        addTeam.put("team_id", teamId);
        addTeam.put("user_id", userId);
        sb.setLength(0);
        sb.append("/api/v4/teams/").append(teamId).append("/members");
        mm.callWSPost(con, sb.toString(), addTeam.toString());
    } catch (Exception e) {
        throw new ProvisioningException("Could not add team", e);
    } finally {
        if (con != null) {
            try {
                con.getHttp().close();
            } catch (IOException e) {
            }
            con.getBcm().close();
        }
    }
    return true;
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) MatterMostProvider(com.tremolosecurity.provisioning.core.providers.MatterMostProvider) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException)

Example 65 with HttpCon

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

the class UserPrincipal method createClient.

private HttpCon createClient(String lusername, String lpassword) throws Exception {
    BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager(cfgMgr.getHttpClientSocketRegistry());
    RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
    CloseableHttpClient http = HttpClients.custom().setConnectionManager(bhcm).setDefaultRequestConfig(rc).build();
    http.execute(new HttpGet(this.url + "/ipa/session/login_kerberos")).close();
    doLogin(lusername, lpassword, http);
    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) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

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