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);
}
}
}
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);
}
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);
}
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;
}
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;
}
}
Aggregations