Search in sources :

Example 66 with HttpCon

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

the class KeystoneProvisioningTarget method createUser.

@Override
public void createUser(User user, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    if (rolesOnly) {
        throw new ProvisioningException("Unsupported");
    }
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    KSUser newUser = new KSUser();
    newUser.setDomain_id(this.usersDomain);
    newUser.setName(user.getUserID());
    newUser.setEnabled(true);
    if (attributes.contains("email") && user.getAttribs().containsKey("email")) {
        newUser.setEmail(user.getAttribs().get("email").getValues().get(0));
    }
    if (attributes.contains("description") && user.getAttribs().containsKey("description")) {
        newUser.setEmail(user.getAttribs().get("description").getValues().get(0));
    }
    HttpCon con = null;
    KSUser fromKS = null;
    try {
        con = this.createClient();
        KSToken token = this.getToken(con);
        Gson gson = new Gson();
        UserHolder userHolder = new UserHolder();
        userHolder.setUser(newUser);
        String json = gson.toJson(userHolder);
        StringBuffer b = new StringBuffer();
        b.append(this.url).append("/users");
        json = this.callWSPost(token.getAuthToken(), con, b.toString(), json);
        if (json == null) {
            throw new Exception("Could not create user");
        }
        UserHolder createdUser = gson.fromJson(json, UserHolder.class);
        if (createdUser.getUser() == null) {
            throw new ProvisioningException("Could not create user :" + json);
        }
        this.cfgMgr.getProvisioningEngine().logAction(user.getUserID(), true, ActionType.Add, approvalID, workflow, "name", user.getUserID());
        this.cfgMgr.getProvisioningEngine().logAction(user.getUserID(), false, ActionType.Add, approvalID, workflow, "name", user.getUserID());
        this.cfgMgr.getProvisioningEngine().logAction(user.getUserID(), false, ActionType.Add, approvalID, workflow, "domain_id", this.usersDomain);
        this.cfgMgr.getProvisioningEngine().logAction(user.getUserID(), false, ActionType.Add, approvalID, workflow, "enabled", "true");
        if (attributes.contains("email")) {
            this.cfgMgr.getProvisioningEngine().logAction(user.getUserID(), false, ActionType.Add, approvalID, workflow, "email", user.getAttribs().get("email").getValues().get(0));
        }
        if (attributes.contains("description")) {
            this.cfgMgr.getProvisioningEngine().logAction(user.getUserID(), false, ActionType.Add, approvalID, workflow, "description", user.getAttribs().get("description").getValues().get(0));
        }
        for (String group : user.getGroups()) {
            String groupID = this.getGroupID(token.getAuthToken(), con, group);
            b.setLength(0);
            b.append(this.url).append("/groups/").append(groupID).append("/users/").append(createdUser.getUser().getId());
            if (this.callWSPutNoData(token.getAuthToken(), con, b.toString())) {
                this.cfgMgr.getProvisioningEngine().logAction(user.getUserID(), false, ActionType.Add, approvalID, workflow, "group", group);
            } else {
                throw new ProvisioningException("Could not add group " + group);
            }
        }
        if (attributes.contains("roles")) {
            Attribute roles = user.getAttribs().get("roles");
            for (String roleJSON : roles.getValues()) {
                Role role = gson.fromJson(roleJSON, Role.class);
                if (role.getScope().equalsIgnoreCase("project")) {
                    String projectid = this.getProjectID(token.getAuthToken(), con, role.getProject());
                    if (projectid == null) {
                        throw new ProvisioningException("Project " + role.getDomain() + " does not exist");
                    }
                    String roleid = this.getRoleID(token.getAuthToken(), con, role.getName());
                    if (roleid == null) {
                        throw new ProvisioningException("Role " + role.getName() + " does not exist");
                    }
                    b.setLength(0);
                    b.append(this.url).append("/projects/").append(projectid).append("/users/").append(createdUser.getUser().getId()).append("/roles/").append(roleid);
                    if (this.callWSPutNoData(token.getAuthToken(), con, b.toString())) {
                        this.cfgMgr.getProvisioningEngine().logAction(user.getUserID(), false, ActionType.Add, approvalID, workflow, "role", roleJSON);
                    } else {
                        throw new ProvisioningException("Could not add role " + roleJSON);
                    }
                } else {
                    String domainid = this.getDomainID(token.getAuthToken(), con, role.getDomain());
                    if (domainid == null) {
                        throw new ProvisioningException("Domain " + role.getDomain() + " does not exist");
                    }
                    String roleid = this.getRoleID(token.getAuthToken(), con, role.getName());
                    if (roleid == null) {
                        throw new ProvisioningException("Role " + role.getName() + " does not exist");
                    }
                    b.setLength(0);
                    b.append(this.url).append("/domains/").append(domainid).append("/users/").append(createdUser.getUser().getId()).append("/roles/").append(roleid);
                    if (this.callWSPutNoData(token.getAuthToken(), con, b.toString())) {
                        this.cfgMgr.getProvisioningEngine().logAction(user.getUserID(), false, ActionType.Add, approvalID, workflow, "role", roleJSON);
                    } else {
                        throw new ProvisioningException("Could not add role " + roleJSON);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not work with keystone", e);
    } finally {
        if (con != null) {
            con.getBcm().shutdown();
        }
    }
}
Also used : Attribute(com.tremolosecurity.saml.Attribute) KSUser(com.tremolosecurity.unison.openstack.model.KSUser) Workflow(com.tremolosecurity.provisioning.core.Workflow) Gson(com.google.gson.Gson) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) KSRole(com.tremolosecurity.unison.openstack.model.KSRole) Role(com.tremolosecurity.unison.openstack.model.Role) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) KSToken(com.tremolosecurity.unison.openstack.util.KSToken) UserHolder(com.tremolosecurity.unison.openstack.model.UserHolder)

Example 67 with HttpCon

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

the class KeystoneProvisioningTarget method listRoleObjs.

public List<KSRole> listRoleObjs() throws ProvisioningException {
    HttpCon con = null;
    try {
        con = this.createClient();
        KSToken token = this.getToken(con);
        StringBuffer b = new StringBuffer();
        b.append(this.url).append("/roles");
        String json = this.callWS(token.getAuthToken(), con, b.toString());
        Gson gson = new Gson();
        return gson.fromJson(json, RoleResponse.class).getRoles();
    } 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) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) KSToken(com.tremolosecurity.unison.openstack.util.KSToken) Gson(com.google.gson.Gson) RoleResponse(com.tremolosecurity.unison.openstack.model.RoleResponse) LoadRoleResponse(com.tremolosecurity.unison.openstack.model.LoadRoleResponse) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException)

Example 68 with HttpCon

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

the class KeystoneProvisioningTarget method listProjectObjs.

public List<Project> listProjectObjs() throws ProvisioningException {
    HttpCon con = null;
    try {
        con = this.createClient();
        KSToken token = this.getToken(con);
        StringBuffer b = new StringBuffer();
        b.append(this.url).append("/projects?enabled");
        String json = this.callWS(token.getAuthToken(), con, b.toString());
        Gson gson = new Gson();
        return gson.fromJson(json, ProjectsResponse.class).getProjects();
    } 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) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) KSToken(com.tremolosecurity.unison.openstack.util.KSToken) ProjectsResponse(com.tremolosecurity.unison.openstack.model.ProjectsResponse) Gson(com.google.gson.Gson) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException)

Example 69 with HttpCon

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

the class KeystoneProvisioningTarget method findUser.

@Override
public User findUser(String userID, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    HttpCon con = null;
    try {
        con = this.createClient();
        KSToken token = this.getToken(con);
        UserAndID found = this.lookupUser(userID, attributes, request, token, con);
        if (found != null) {
            return found.getUser();
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not work with keystone", e);
    } finally {
        if (con != null) {
            con.getBcm().shutdown();
        }
    }
}
Also used : UserAndID(com.tremolosecurity.unison.openstack.model.UserAndID) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) KSToken(com.tremolosecurity.unison.openstack.util.KSToken) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException)

Example 70 with HttpCon

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

the class KeystoneProvisioningTarget method listDomains.

public List<Map<Object, Object>> listDomains() 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());
        GsonBuilder builder = new GsonBuilder();
        Object o = builder.create().fromJson(json, Object.class);
        List<Map<Object, Object>> roles = (List<Map<Object, Object>>) ((Map<Object, Object>) o).get("domains");
        return roles;
    } 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) GsonBuilder(com.google.gson.GsonBuilder) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) KSToken(com.tremolosecurity.unison.openstack.util.KSToken) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) HashMap(java.util.HashMap) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException)

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