use of com.tremolosecurity.provisioning.core.Workflow in project OpenUnison by TremoloSecurity.
the class ForkProject method doTask.
@Override
public boolean doTask(User user, 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");
GitlabUserProvider gitlab = (GitlabUserProvider) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.targetName).getProvider();
GitLabApi api = gitlab.getApi();
String localSourceProjectNamespace = task.renderTemplate(this.sourceProjectNamespace, request);
String localSourceProjectName = task.renderTemplate(this.sourceProjectName, request);
String localDestinationNamespace = task.renderTemplate(this.destintionNamespace, request);
try {
Project existingProject = api.getProjectApi().getProject(localSourceProjectNamespace, localSourceProjectName);
Project newProject = api.getProjectApi().forkProject(existingProject, localDestinationNamespace);
GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().logAction(gitlab.getName(), false, ActionType.Add, approvalID, workflow, "gitlab-fork-" + existingProject.getNameWithNamespace() + "-fork", localDestinationNamespace);
String gitUrl = newProject.getSshUrlToRepo();
String prefix = gitUrl.substring(0, gitUrl.indexOf("@") + 1);
String suffix = gitUrl.substring(gitUrl.indexOf(":"));
String newGitUrl = new StringBuilder().append(prefix).append(this.gitSshHost).append(suffix).toString();
request.put("gitSshInternalURL", newGitUrl);
request.put("gitSshUrl", newProject.getSshUrlToRepo());
} catch (GitLabApiException e) {
throw new ProvisioningException("Error looking up project " + localSourceProjectNamespace + "/" + localSourceProjectName, e);
}
return true;
}
use of com.tremolosecurity.provisioning.core.Workflow in project OpenUnison by TremoloSecurity.
the class CopyGroupMembers method doTask.
@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
String localWorkflowName = task.renderTemplate(this.workflowName, request);
String localCopyFrom = task.renderTemplate(this.copyFrom, request);
String localCopyTo = task.renderTemplate(this.copyTo, request);
String memberAttr = task.getConfigManager().getCfg().getGroupMemberAttribute();
String[] members = null;
try {
LDAPSearchResults rs = task.getConfigManager().getMyVD().search(localCopyFrom, 0, "(objectClass=*)", new ArrayList<String>());
rs.hasMore();
LDAPEntry group = rs.next();
while (rs.hasMore()) rs.next();
if (group != null && group.getAttribute(memberAttr) != null) {
members = group.getAttribute(memberAttr).getStringValueArray();
} else {
members = new String[0];
}
} catch (LDAPException e) {
throw new ProvisioningException("Could not load from group", e);
}
for (String member : members) {
try {
LDAPSearchResults rs = task.getConfigManager().getMyVD().search(member, 0, "(objectClass=*)", new ArrayList<String>());
rs.hasMore();
LDAPEntry ldapMember = rs.next();
TremoloUser userToUpdate = new TremoloUser();
userToUpdate.setUid(ldapMember.getAttribute(this.uidAttribute).getStringValue());
userToUpdate.getAttributes().add(new Attribute(this.uidAttribute, userToUpdate.getUid()));
userToUpdate.getGroups().add(localCopyTo);
Workflow wf = task.getConfigManager().getProvisioningEngine().getWorkFlow(localWorkflowName);
WFCall call = new WFCall();
call.setReason("auto-creating approval group " + localCopyTo);
call.setUidAttributeName(this.uidAttribute);
call.setUser(userToUpdate);
call.setRequestor(this.requestor);
call.getRequestParams().put(ProvisioningParams.UNISON_EXEC_TYPE, ProvisioningParams.UNISON_EXEC_SYNC);
wf.executeWorkflow(call);
} catch (LDAPException e) {
logger.warn("Could not load user '" + member + "'", e);
}
}
return true;
}
use of com.tremolosecurity.provisioning.core.Workflow in project OpenUnison by TremoloSecurity.
the class DeleteK8sObject method doTask.
@Override
public boolean doTask(User user, 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");
String localURL = task.renderTemplate(this.url, request);
HttpCon con = null;
String localTarget = task.renderTemplate(this.targetName, request);
OpenShiftTarget os = (OpenShiftTarget) task.getConfigManager().getProvisioningEngine().getTarget(localTarget).getProvider();
try {
String token = os.getAuthToken();
con = os.createClient();
boolean writeToRequest = false;
if (this.writeToRequestConfig != null) {
writeToRequest = task.renderTemplate(this.writeToRequestConfig, request).equalsIgnoreCase("true");
}
if (writeToRequest) {
logger.debug("Writing to secret");
String localPath = task.renderTemplate(this.path, request);
String dirName;
String fileName;
int lastSlash = localPath.lastIndexOf('/');
if (lastSlash == -1) {
dirName = "";
fileName = localPath;
} else {
dirName = localPath.substring(0, lastSlash);
fileName = localPath.substring(lastSlash + 1);
}
JSONObject fileInfo = new JSONObject();
fileInfo.put("fileName", fileName);
fileInfo.put("dirName", dirName);
fileInfo.put("delete", true);
GitFile gitFile = new GitFile(fileName, dirName, true, kind.equalsIgnoreCase("Namespace"));
List<GitFile> gitFiles = (List<GitFile>) request.get(this.requestAttribute);
if (gitFiles == null) {
gitFiles = new ArrayList<GitFile>();
request.put(this.requestAttribute, gitFiles);
}
gitFiles.add(gitFile);
} else {
String respJSON = os.callWSDelete(token, con, localURL);
if (logger.isDebugEnabled()) {
logger.debug("Response for deleting object : '" + respJSON + "'");
}
JSONParser parser = new JSONParser();
JSONObject resp = (JSONObject) parser.parse(respJSON);
String kind = (String) resp.get("kind");
String projectName = (String) ((JSONObject) resp.get("metadata")).get("name");
if (logger.isDebugEnabled()) {
logger.debug("kind : '" + kind + "' / '" + this.kind + "'");
}
if (kind.equalsIgnoreCase(this.kind)) {
this.task.getConfigManager().getProvisioningEngine().logAction(localTarget, true, ProvisioningUtil.ActionType.Delete, approvalID, this.task.getWorkflow(), label, projectName);
} else if (resp.get("status") != null) {
String status = (String) resp.get("status");
logger.info("status : '" + status + "'");
if (status != null && status.equalsIgnoreCase("success")) {
this.task.getConfigManager().getProvisioningEngine().logAction(localTarget, true, ProvisioningUtil.ActionType.Delete, approvalID, this.task.getWorkflow(), label, projectName);
} else {
throw new ProvisioningException("Could not delete " + kind + " with url '" + localURL + "' - '" + respJSON + "'");
}
} else {
throw new ProvisioningException("Could not delete " + kind + " with url '" + localURL + "' - '" + respJSON + "'");
}
}
} catch (Exception e) {
throw new ProvisioningException("Could not delete " + kind + " - " + localURL, e);
} finally {
if (con != null) {
con.getBcm().close();
}
}
return true;
}
use of com.tremolosecurity.provisioning.core.Workflow in project OpenUnison by TremoloSecurity.
the class PatchK8sObject method doTask.
@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
String localTemplate = task.renderTemplate(template, request);
if (logger.isDebugEnabled()) {
logger.debug("localTemplate : '" + localTemplate + "'");
}
int approvalID = 0;
if (request.containsKey("APPROVAL_ID")) {
approvalID = (Integer) request.get("APPROVAL_ID");
}
Workflow workflow = (Workflow) request.get("WORKFLOW");
String localURL = task.renderTemplate(this.url, request);
HttpCon con = null;
String localTarget = task.renderTemplate(this.targetName, request);
OpenShiftTarget os = (OpenShiftTarget) task.getConfigManager().getProvisioningEngine().getTarget(localTarget).getProvider();
try {
String token = os.getAuthToken();
con = os.createClient();
boolean writeToRequest = false;
if (this.writeToRequestConfig != null) {
writeToRequest = task.renderTemplate(this.writeToRequestConfig, request).equalsIgnoreCase("true");
}
if (writeToRequest) {
logger.debug("Writing to secret");
String localPath = task.renderTemplate(this.path, request);
String dirName;
String fileName;
int lastSlash = localPath.lastIndexOf('/');
if (lastSlash == -1) {
dirName = "";
fileName = localPath;
} else {
dirName = localPath.substring(0, lastSlash);
fileName = localPath.substring(lastSlash + 1);
}
GitFile gitFile = new GitFile(fileName, dirName, false, false);
gitFile.setData(localTemplate);
gitFile.setPatch(true);
gitFile.setPatchType(this.patchType);
List<GitFile> gitFiles = (List<GitFile>) request.get(this.requestAttribute);
if (gitFiles == null) {
gitFiles = new ArrayList<GitFile>();
request.put(this.requestAttribute, gitFiles);
}
gitFiles.add(gitFile);
} else {
if (this.isObjectExists(os, token, con, localURL, localTemplate)) {
String respJSON = os.callWSPatchJson(token, con, localURL, localTemplate, this.patchContentType);
if (logger.isDebugEnabled()) {
logger.debug("Response for creating project : '" + respJSON + "'");
}
JSONParser parser = new JSONParser();
JSONObject resp = (JSONObject) parser.parse(respJSON);
String kind = (String) resp.get("kind");
String projectName = (String) ((JSONObject) resp.get("metadata")).get("name");
if (!kind.equalsIgnoreCase(this.kind)) {
throw new ProvisioningException("Could not create " + kind + " with json '" + localTemplate + "' - '" + respJSON + "'");
} else {
this.task.getConfigManager().getProvisioningEngine().logAction(localTarget, true, ActionType.Replace, approvalID, this.task.getWorkflow(), label, projectName);
}
} else {
throw new ProvisioningException("Object '" + localURL + "' does not exist");
}
}
} catch (Exception e) {
throw new ProvisioningException("Could not create " + kind, e);
} finally {
if (con != null) {
con.getBcm().close();
}
}
return true;
}
use of com.tremolosecurity.provisioning.core.Workflow in project OpenUnison by TremoloSecurity.
the class K8sCrdUserProvider method deleteUser.
@Override
public void deleteUser(User user, Map<String, Object> request) throws ProvisioningException {
String k8sUserId = OpenShiftTarget.sub2uid(user.getUserID());
int approvalID = 0;
if (request.containsKey("APPROVAL_ID")) {
approvalID = (Integer) request.get("APPROVAL_ID");
}
Workflow workflow = (Workflow) request.get("WORKFLOW");
OpenShiftTarget k8s = null;
try {
k8s = (OpenShiftTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.k8sTarget).getProvider();
} catch (ProvisioningException e1) {
logger.error("Could not retrieve kubernetes target", e1);
throw new ProvisioningException("Could not connect to kubernetes", e1);
}
String url = new StringBuilder().append("/apis/openunison.tremolo.io/v1/namespaces/").append(this.nameSpace).append("/users/").append(k8sUserId).toString();
try {
HttpCon con = k8s.createClient();
try {
k8s.callWSDelete(k8s.getAuthToken(), con, url);
GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().logAction(this.name, true, ActionType.Delete, approvalID, workflow, "sub", user.getUserID());
} finally {
con.getHttp().close();
con.getBcm().close();
}
} catch (Exception e) {
logger.error("Could not search k8s", e);
throw new ProvisioningException("Error searching kubernetes", e);
}
}
Aggregations