Search in sources :

Example 11 with Group

use of org.gitlab4j.api.models.Group in project catma by forTEXT.

the class GitlabManagerRestricted method createRepository.

@Override
public CreateRepositoryResponse createRepository(String name, String path, String groupPath) throws IOException {
    GroupApi groupApi = restrictedGitLabApi.getGroupApi();
    ProjectApi projectApi = restrictedGitLabApi.getProjectApi();
    try {
        Group group = groupApi.getGroup(groupPath);
        Namespace namespace = new Namespace();
        namespace.setId(group.getId());
        Project project = new Project();
        project.setName(name);
        project.setNamespace(namespace);
        if (StringUtils.isNotEmpty(path)) {
            project.setPath(path);
        }
        project = projectApi.createProject(project);
        return new CreateRepositoryResponse(groupPath, project.getId(), GitlabUtils.rewriteGitLabServerUrl(project.getHttpUrlToRepo()));
    } catch (GitLabApiException e) {
        throw new IOException("Failed to create remote Git repository", e);
    }
}
Also used : Group(org.gitlab4j.api.models.Group) Project(org.gitlab4j.api.models.Project) GroupApi(org.gitlab4j.api.GroupApi) ProjectApi(org.gitlab4j.api.ProjectApi) CreateRepositoryResponse(de.catma.repository.git.CreateRepositoryResponse) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) Namespace(org.gitlab4j.api.models.Namespace)

Example 12 with Group

use of org.gitlab4j.api.models.Group in project catma by forTEXT.

the class GitlabManagerCommon method getRoleOnProject.

@Override
public final RBACRole getRoleOnProject(RBACSubject subject, String projectId) throws IOException {
    try {
        Group group = getGitLabApi().getGroupApi().getGroup(projectId);
        if (group == null) {
            throw new IOException("Project unkown " + projectId);
        }
        Member member = getGitLabApi().getGroupApi().getMember(group.getId(), subject.getUserId());
        if (member == null) {
            throw new IOException("member not found " + subject);
        }
        return RBACRole.forValue(member.getAccessLevel().value);
    } catch (GitLabApiException e) {
        throw new IOException("error getting role on project #" + projectId, e);
    }
}
Also used : Group(org.gitlab4j.api.models.Group) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) GitMember(de.catma.repository.git.GitMember) Member(org.gitlab4j.api.models.Member)

Example 13 with Group

use of org.gitlab4j.api.models.Group in project catma by forTEXT.

the class GitlabManagerCommon method unassignFromProject.

@Override
public final void unassignFromProject(RBACSubject subject, String projectId) throws IOException {
    try {
        Group group = getGitLabApi().getGroupApi().getGroup(projectId);
        if (group == null) {
            throw new IOException(String.format("CATMA-Project/git-Group unknown %1$s", projectId));
        }
        getGitLabApi().getGroupApi().removeMember(group.getId(), subject.getUserId());
    } catch (GitLabApiException e) {
        throw new IOException(String.format("Error accessing CATMA-Project/git-Group %1$s or user %2$s", projectId, subject == null ? "null" : subject.toString()), e);
    }
}
Also used : Group(org.gitlab4j.api.models.Group) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException)

Example 14 with Group

use of org.gitlab4j.api.models.Group in project OpenUnison by TremoloSecurity.

the class GitlabUserProvider method syncUser.

@Override
public void syncUser(User user, boolean addOnly, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    List<GitlabFedIdentity> ids = (List<GitlabFedIdentity>) request.get(GitlabUserProvider.GITLAB_IDENTITIES);
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    User fromGitlab = this.findUser(user.getUserID(), attributes, request);
    if (fromGitlab == null) {
        this.createUser(user, attributes, request);
        return;
    }
    List<GitlabFedIdentity> idsFromGitlab = (List<GitlabFedIdentity>) request.get(GitlabUserProvider.GITLAB_IDENTITIES);
    HashMap<String, String> toSet = new HashMap<String, String>();
    HashSet<String> toDelete = new HashSet<String>();
    for (String attrName : attributes) {
        Attribute attrFromGitlab = fromGitlab.getAttribs().get(attrName);
        Attribute attrIn = user.getAttribs().get(attrName);
        if ((attrIn != null && attrFromGitlab == null) || (attrIn != null && attrFromGitlab != null && !attrIn.getValues().get(0).equals(attrFromGitlab.getValues().get(0)))) {
            toSet.put(attrName, attrIn.getValues().get(0));
        } else if (!addOnly) {
            if (attrIn == null && attrFromGitlab != null) {
                toDelete.add(attrName);
            }
        }
    }
    org.gitlab4j.api.models.User toSave = this.findUserByName(user.getUserID());
    for (String attrName : toSet.keySet()) {
        try {
            this.beanUtils.setProperty(toSave, attrName, toSet.get(attrName));
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ProvisioningException("Could not update user " + user.getUserID(), e);
        }
    }
    for (String attrName : toDelete) {
        try {
            this.beanUtils.setProperty(toSave, attrName, "");
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ProvisioningException("Could not update user " + user.getUserID(), e);
        }
    }
    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) {
                boolean found = false;
                for (GitlabFedIdentity idfromgl : idsFromGitlab) {
                    if (id.getExternalUid().equals(idfromgl.getExternalUid()) && id.getProvider().equals(idfromgl.getProvider())) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    HttpPut getmembers = new HttpPut(new StringBuilder().append(this.url).append("/api/v4/users/").append(toSave.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();
        }
    }
    try {
        this.userApi.updateUser(toSave, null);
    } catch (GitLabApiException e) {
        throw new ProvisioningException("Could not save user " + user.getUserID(), e);
    }
    for (String attrName : toSet.keySet()) {
        this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Replace, approvalID, workflow, attrName, toSet.get(attrName));
    }
    for (String attrName : toDelete) {
        this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Replace, approvalID, workflow, attrName, "");
    }
    HashMap<String, Integer> groupmap = (HashMap<String, Integer>) request.get(GitlabUserProvider.GITLAB_GROUP_ENTITLEMENTS);
    if (groupmap == null) {
        groupmap = new HashMap<String, Integer>();
    }
    for (String inGroup : user.getGroups()) {
        if (!fromGitlab.getGroups().contains(inGroup)) {
            try {
                Group groupObj = this.findGroupByName(inGroup);
                if (groupObj == null) {
                    logger.warn("Group " + inGroup + " does not exist");
                } else {
                    int accessLevel = AccessLevel.DEVELOPER.ordinal();
                    if (groupmap.containsKey(inGroup)) {
                        accessLevel = groupmap.get(inGroup);
                    }
                    this.groupApi.addMember(groupObj.getId(), toSave.getId(), accessLevel);
                    this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "group", inGroup);
                }
            } catch (GitLabApiException e) {
                if (e.getMessage().equalsIgnoreCase("Member already exists")) {
                    continue;
                } else {
                    throw new ProvisioningException("Could not find group " + inGroup, e);
                }
            }
        }
    }
    if (!addOnly) {
        for (String groupFromGitlab : fromGitlab.getGroups()) {
            if (!user.getGroups().contains(groupFromGitlab)) {
                try {
                    Group groupObj = this.findGroupByName(groupFromGitlab);
                    if (groupObj == null) {
                        logger.warn("Group " + groupFromGitlab + " does not exist");
                    } else {
                        this.groupApi.removeMember(groupObj.getId(), toSave.getId());
                        this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Delete, approvalID, workflow, "group", groupFromGitlab);
                    }
                } catch (GitLabApiException e) {
                    throw new ProvisioningException("Could not find group " + groupFromGitlab);
                }
            }
        }
    }
}
Also used : Group(org.gitlab4j.api.models.Group) UserStoreProviderWithAddGroup(com.tremolosecurity.provisioning.core.UserStoreProviderWithAddGroup) User(com.tremolosecurity.provisioning.core.User) HashMap(java.util.HashMap) Attribute(com.tremolosecurity.saml.Attribute) 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) HashSet(java.util.HashSet) 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 15 with Group

use of org.gitlab4j.api.models.Group in project OpenUnison by TremoloSecurity.

the class AddGroupToProject method doTask.

@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
    if (request.get("newProjectJSON") == null) {
        logger.warn("Project not created, skipping");
        return true;
    }
    String localGroupName = task.renderTemplate(this.groupName, request);
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    GitlabUserProvider gitlab = (GitlabUserProvider) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.targetName).getProvider();
    GitLabApi api = gitlab.getApi();
    ObjectMapper mapper = new ObjectMapper();
    Project newProject = null;
    if (this.projectName == null) {
        try {
            newProject = (Project) mapper.readValue((String) request.get("newProjectJSON"), Project.class);
        } catch (JsonProcessingException e) {
            throw new ProvisioningException("Could not parse", e);
        }
    } else {
        String localProjectName = task.renderTemplate(this.projectName, request);
        String localNamespace = task.renderTemplate(this.namespace, request);
        try {
            newProject = api.getProjectApi().getProject(localNamespace, localProjectName);
        } catch (GitLabApiException e) {
            throw new ProvisioningException("Could not find " + localNamespace + "/" + localProjectName, e);
        }
    }
    Group groupToAdd;
    try {
        groupToAdd = gitlab.findGroupByName(localGroupName);
        if (groupToAdd == null) {
            throw new ProvisioningException("Group " + localGroupName + " does not exist");
        }
        api.getProjectApi().shareProject(newProject, groupToAdd.getId(), AccessLevel.valueOf(accessLevel), null);
    } catch (GitLabApiException e) {
        throw new ProvisioningException("Could not add group " + localGroupName + " to project " + newProject.getNameWithNamespace(), e);
    }
    GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().logAction(gitlab.getName(), false, ActionType.Add, approvalID, workflow, "gitlab-project-" + newProject.getNameWithNamespace() + "-group", localGroupName);
    return true;
}
Also used : GitlabUserProvider(com.tremolosecurity.unison.gitlab.provisioning.targets.GitlabUserProvider) Project(org.gitlab4j.api.models.Project) Group(org.gitlab4j.api.models.Group) GitLabApi(org.gitlab4j.api.GitLabApi) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) GitLabApiException(org.gitlab4j.api.GitLabApiException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Group (org.gitlab4j.api.models.Group)15 GitLabApiException (org.gitlab4j.api.GitLabApiException)13 IOException (java.io.IOException)11 Project (org.gitlab4j.api.models.Project)7 GitMember (de.catma.repository.git.GitMember)6 GroupApi (org.gitlab4j.api.GroupApi)6 Member (org.gitlab4j.api.models.Member)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 CreateRepositoryResponse (de.catma.repository.git.CreateRepositoryResponse)4 Cache (com.google.common.cache.Cache)3 CacheBuilder (com.google.common.cache.CacheBuilder)3 Maps (com.google.common.collect.Maps)3 EventBus (com.google.common.eventbus.EventBus)3 Subscribe (com.google.common.eventbus.Subscribe)3 JsonObject (com.google.gson.JsonObject)3 JsonParser (com.google.gson.JsonParser)3 BackgroundService (de.catma.backgroundservice.BackgroundService)3 Comment (de.catma.document.comment.Comment)3