Search in sources :

Example 1 with GitLabApiException

use of org.gitlab4j.api.GitLabApiException in project OpenUnison by TremoloSecurity.

the class GitlabUserProvider method createUser.

@Override
public void createUser(User user, Set<String> attributes, 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");
    org.gitlab4j.api.models.User newUser = new org.gitlab4j.api.models.User();
    newUser.setUsername(user.getUserID());
    for (String attrName : attributes) {
        Attribute attr = user.getAttribs().get(attrName);
        if (attr != null) {
            try {
                this.beanUtils.setProperty(newUser, attrName, attr.getValues().get(0));
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new ProvisioningException("Could not set " + attrName + " for " + user.getUserID(), e);
            }
        }
    }
    try {
        this.userApi.createUser(newUser, new GenPasswd(50).getPassword(), false);
    } catch (GitLabApiException e) {
        throw new ProvisioningException("Could not create user", e);
    }
    newUser = this.findUserByName(user.getUserID());
    int numTries = 0;
    while (newUser == null) {
        if (numTries > 10) {
            throw new ProvisioningException("User " + user.getUserID() + " never created");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        newUser = this.findUserByName(user.getUserID());
        numTries++;
    }
    this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Add, approvalID, workflow, "id", newUser.getId().toString());
    for (String attrName : attributes) {
        Attribute attr = user.getAttribs().get(attrName);
        if (attr != null) {
            this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, attrName, attr.getValues().get(0));
        }
    }
    List<GitlabFedIdentity> ids = (List<GitlabFedIdentity>) request.get(GitlabUserProvider.GITLAB_IDENTITIES);
    if (ids != null) {
        ArrayList<Header> defheaders = new ArrayList<Header>();
        defheaders.add(new BasicHeader("Private-Token", this.token));
        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();
        try {
            for (GitlabFedIdentity id : ids) {
                HttpPut getmembers = new HttpPut(new StringBuilder().append(this.url).append("/api/v4/users/").append(newUser.getId()).append("?provider=").append(id.getProvider()).append("&extern_uid=").append(URLEncoder.encode(user.getUserID(), "UTF-8")).toString());
                CloseableHttpResponse resp = http.execute(getmembers);
                if (resp.getStatusLine().getStatusCode() != 200) {
                    throw new IOException("Invalid response " + resp.getStatusLine().getStatusCode());
                }
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "identity-provider", id.getProvider());
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "identity-externid", id.getExternalUid());
            }
        } catch (IOException e) {
            throw new ProvisioningException("Could not set identity", e);
        } finally {
            try {
                http.close();
            } catch (IOException e) {
            }
            bhcm.close();
        }
    }
    HashMap<String, Integer> groupmap = (HashMap<String, Integer>) request.get(GitlabUserProvider.GITLAB_GROUP_ENTITLEMENTS);
    if (groupmap == null) {
        groupmap = new HashMap<String, Integer>();
    }
    for (String group : user.getGroups()) {
        try {
            Group groupObj = this.findGroupByName(group);
            if (groupObj == null) {
                logger.warn("Group " + group + " does not exist");
            } else {
                int accessLevel = AccessLevel.DEVELOPER.ordinal();
                if (groupmap.containsKey(group)) {
                    accessLevel = groupmap.get(group);
                }
                this.groupApi.addMember(groupObj.getId(), newUser.getId(), accessLevel);
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "group", group);
            }
        } catch (GitLabApiException e) {
            throw new ProvisioningException("Could not find group " + group, e);
        }
    }
}
Also used : Group(org.gitlab4j.api.models.Group) UserStoreProviderWithAddGroup(com.tremolosecurity.provisioning.core.UserStoreProviderWithAddGroup) User(com.tremolosecurity.provisioning.core.User) Attribute(com.tremolosecurity.saml.Attribute) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HttpPut(org.apache.http.client.methods.HttpPut) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) GenPasswd(com.tremolosecurity.provisioning.util.GenPasswd) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Workflow(com.tremolosecurity.provisioning.core.Workflow) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) BasicHeader(org.apache.http.message.BasicHeader)

Example 2 with GitLabApiException

use of org.gitlab4j.api.GitLabApiException in project OpenUnison by TremoloSecurity.

the class GitlabUserProvider method addGroup.

@Override
public void addGroup(String name, Map<String, String> additionalAttributes, User user, Map<String, Object> request) throws ProvisioningException {
    if (this.isGroupExists(name, null, request)) {
        return;
    }
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    Group groupToCreate = new Group();
    groupToCreate.setName(name);
    groupToCreate.setPath(name);
    for (String prop : additionalAttributes.keySet()) {
        try {
            this.beanUtils.setProperty(groupToCreate, prop, additionalAttributes.get(prop));
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ProvisioningException("Could not set properties", e);
        }
    }
    try {
        this.groupApi.addGroup(groupToCreate);
    } catch (GitLabApiException e) {
        throw new ProvisioningException("Could not create group " + name, e);
    }
    this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Add, approvalID, workflow, "group-object", name);
}
Also used : Group(org.gitlab4j.api.models.Group) UserStoreProviderWithAddGroup(com.tremolosecurity.provisioning.core.UserStoreProviderWithAddGroup) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) GitLabApiException(org.gitlab4j.api.GitLabApiException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with GitLabApiException

use of org.gitlab4j.api.GitLabApiException in project OpenUnison by TremoloSecurity.

the class GitlabUserProvider method deleteGroup.

@Override
public void deleteGroup(String name, User user, Map<String, Object> request) throws ProvisioningException {
    if (!this.isGroupExists(name, null, request)) {
        return;
    }
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    try {
        this.groupApi.deleteGroup(name);
    } catch (GitLabApiException e) {
        throw new ProvisioningException("Could not delete group " + name, e);
    }
    this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Delete, approvalID, workflow, "group-object", name);
}
Also used : ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 4 with GitLabApiException

use of org.gitlab4j.api.GitLabApiException in project octane-gitlab-service by MicroFocus.

the class GitlabServices method addWebHookToProject.

private Boolean addWebHookToProject(Object projectId, boolean deleteOldWebHook) throws GitLabApiException {
    try {
        if (deleteOldWebHook) {
            deleteWebHooks(projectId);
        }
        ProjectHook hook = new ProjectHook();
        hook.setJobEvents(true);
        hook.setPipelineEvents(true);
        hook.setMergeRequestsEvents(true);
        gitLabApi.getProjectApi().addHook(projectId, webhookURL.toString(), hook, false, generateNewToken());
    } catch (GitLabApiException e) {
        log.warn("Failed to add web hooks to project: " + projectId, e);
        throw e;
    }
    return true;
}
Also used : ProjectHook(org.gitlab4j.api.models.ProjectHook) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 5 with GitLabApiException

use of org.gitlab4j.api.GitLabApiException in project octane-gitlab-service by MicroFocus.

the class GitlabServices method createStructure.

PipelineNode createStructure(String buildId) {
    ParsedPath project = new ParsedPath(buildId, gitLabApi, PathType.MULTI_BRUNCH);
    // add a webhook to new Octane pipeline (gitlab project) in Octane
    try {
        if (project.isMultiBranch()) {
            addWebHookToProject(project.getFullPathOfProject(), true);
            return dtoFactory.newDTO(PipelineNode.class).setJobCiId(project.getJobCiId(true)).setMultiBranchType(MultiBranchType.MULTI_BRANCH_PARENT).setName(project.getNameWithNameSpaceForDisplayName()).setParameters(getParameters(project));
        }
        project = new ParsedPath(buildId, gitLabApi, PathType.PIPELINE);
        addWebHookToProject(project.getId(), true);
        return dtoFactory.newDTO(PipelineNode.class).setJobCiId(project.getJobCiId(false)).setName(project.getNameWithNameSpaceForDisplayName()).setParameters(getParameters(project));
    } catch (GitLabApiException e) {
        log.error("unable to update webhook when create a pipeline in Octane for project:" + project.getDisplayName(), e);
        return null;
    }
}
Also used : ParsedPath(com.microfocus.octane.gitlab.helpers.ParsedPath) GitLabApiException(org.gitlab4j.api.GitLabApiException) PipelineNode(com.hp.octane.integrations.dto.pipelines.PipelineNode)

Aggregations

GitLabApiException (org.gitlab4j.api.GitLabApiException)77 IOException (java.io.IOException)35 GitLabApi (org.gitlab4j.api.GitLabApi)18 Project (org.gitlab4j.api.models.Project)18 Group (org.gitlab4j.api.models.Group)14 List (java.util.List)12 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)9 MergeRequest (org.gitlab4j.api.models.MergeRequest)9 GitMember (de.catma.repository.git.GitMember)8 ArrayList (java.util.ArrayList)8 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)8 MergeRequestApi (org.gitlab4j.api.MergeRequestApi)8 UserApi (org.gitlab4j.api.UserApi)8 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)7 Workflow (com.tremolosecurity.provisioning.core.Workflow)7 GroupApi (org.gitlab4j.api.GroupApi)7 IssuesApi (org.gitlab4j.api.IssuesApi)7 AccessLevel (org.gitlab4j.api.models.AccessLevel)7 Issue (org.gitlab4j.api.models.Issue)7 Comment (de.catma.document.comment.Comment)6