use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class CreateProject 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");
HttpCon con = null;
OpenShiftTarget os = (OpenShiftTarget) task.getConfigManager().getProvisioningEngine().getTarget("openshift").getProvider();
try {
String token = os.getAuthToken();
con = os.createClient();
if (!os.isObjectExists(token, con, "/apis/project.openshift.io/v1/projects", localTemplate)) {
String respJSON = os.callWSPost(token, con, "/apis/project.openshift.io/v1/projectrequests", localTemplate);
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("Project")) {
throw new ProvisioningException("Could not create project with json '" + localTemplate + "' - '" + respJSON + "'");
} else {
this.task.getConfigManager().getProvisioningEngine().logAction(this.targetName, true, ActionType.Add, approvalID, this.task.getWorkflow(), "openshift-project", projectName);
}
}
} catch (Exception e) {
throw new ProvisioningException("Could not create project", e);
} finally {
if (con != null) {
con.getBcm().close();
}
}
return true;
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class OpenShiftWorkflows method generateWorkflows.
@Override
public List<Map<String, String>> generateWorkflows(WorkflowType wf, ConfigManager cfg, HashMap<String, Attribute> params) throws ProvisioningException {
ArrayList<Map<String, String>> wfData = new ArrayList<Map<String, String>>();
String targetName = params.get("target").getValues().get(0);
HashSet<String> nameFilter = new HashSet<String>();
Attribute attr = params.get("filter");
if (attr != null) {
nameFilter.addAll(attr.getValues());
}
OpenShiftTarget target = (OpenShiftTarget) cfg.getProvisioningEngine().getTarget(targetName).getProvider();
String kind = params.get("kind").getValues().get(0);
try {
String token = target.getAuthToken();
HttpCon con = target.createClient();
try {
String json = target.callWS(token, con, kind);
Gson gson = new Gson();
TypeToken<com.tremolosecurity.unison.openshiftv3.model.List<Item>> tokenType = new TypeToken<com.tremolosecurity.unison.openshiftv3.model.List<Item>>() {
};
com.tremolosecurity.unison.openshiftv3.model.List<Item> list = gson.fromJson(json, tokenType.getType());
for (Item item : list.getItems()) {
HashMap<String, String> wfParams = new HashMap<String, String>();
String name = (String) item.getMetadata().get("name");
if (nameFilter.contains(name)) {
continue;
}
wfParams.put("name", name);
if (item.getMetadata().containsKey("annotations")) {
com.google.gson.internal.LinkedTreeMap annotations = (com.google.gson.internal.LinkedTreeMap) item.getMetadata().get("annotations");
for (Object key : annotations.keySet()) {
String keyName = (String) key;
keyName = keyName.replace("-", "_");
keyName = keyName.replace(".", "_");
wfParams.put((String) keyName, (String) annotations.get(key));
}
}
if (item.getMetadata().containsKey("labels")) {
com.google.gson.internal.LinkedTreeMap annotations = (com.google.gson.internal.LinkedTreeMap) item.getMetadata().get("labels");
for (Object key : annotations.keySet()) {
String keyName = (String) key;
keyName = keyName.replace("-", "_");
keyName = keyName.replace(".", "_");
wfParams.put((String) keyName, (String) annotations.get(key));
}
}
wfData.add(wfParams);
}
} finally {
con.getBcm().close();
con.getHttp().close();
}
} catch (Exception e) {
throw new ProvisioningException("Could not load", e);
}
return wfData;
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class OpenShiftTarget method deleteGroup.
@Override
public void deleteGroup(String name, User user, Map<String, Object> request) throws ProvisioningException {
HttpCon con = null;
int approvalID = 0;
if (request.containsKey("APPROVAL_ID")) {
approvalID = (Integer) request.get("APPROVAL_ID");
}
Workflow workflow = (Workflow) request.get("WORKFLOW");
try {
String token = this.getAuthToken();
con = this.createClient();
Gson gson = new Gson();
StringBuffer b = new StringBuffer();
b.append("/apis/user.openshift.io/v1/groups/").append(name);
String json = this.callWSDelete(token, con, b.toString());
Response resp = gson.fromJson(json, Response.class);
if (resp.getStatus().equalsIgnoreCase("Success")) {
this.cfgMgr.getProvisioningEngine().logAction(name, true, ActionType.Delete, approvalID, workflow, "group-object", name);
} else {
throw new ProvisioningException("Unknown response : '" + json + "'");
}
} catch (Exception e) {
throw new ProvisioningException("Could not load group", e);
} finally {
if (con != null) {
con.getBcm().close();
}
}
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class OpenShiftTarget method deleteUser.
@Override
public void deleteUser(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");
user = this.findUser(user.getUserID(), new HashSet<String>(), request);
try {
String token = this.getAuthToken();
HttpCon con = this.createClient();
Gson gson = new Gson();
try {
StringBuffer b = new StringBuffer();
b.append("/apis/user.openshift.io/v1/users/").append(user.getUserID());
String json = this.callWSDelete(token, con, b.toString());
Response resp = gson.fromJson(json, Response.class);
if (resp.getStatus() != null && !resp.getStatus().equalsIgnoreCase("success")) {
throw new Exception("Unable to delete " + user.getUserID() + " - " + resp.getReason());
}
this.cfgMgr.getProvisioningEngine().logAction(name, true, ActionType.Delete, approvalID, workflow, "name", user.getUserID());
for (String group : user.getGroups()) {
this.removeUserFromGroup(token, con, user.getUserID(), group, approvalID, workflow);
}
} finally {
if (con != null) {
con.getBcm().shutdown();
con.getHttp().close();
}
}
} catch (Exception e) {
throw new ProvisioningException("Could not delete user " + user.getUserID());
}
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class OpenShiftTarget method setFullName.
private String setFullName(User user, int approvalID, Workflow workflow, Gson gson, StringBuffer b) throws Exception, IOException, ClientProtocolException, ProvisioningException {
String token;
token = this.getAuthToken();
HttpCon con = this.createClient();
try {
b.append("/apis/user.openshift.io/v1/users/").append(user.getUserID());
String json = callWS(token, con, b.toString());
com.tremolosecurity.unison.openshiftv3.model.users.User osUser = gson.fromJson(json, com.tremolosecurity.unison.openshiftv3.model.users.User.class);
osUser.setFullName(user.getAttribs().get("fullName").getValues().get(0));
json = gson.toJson(osUser);
json = callWSPut(token, con, b.toString(), json);
osUser = gson.fromJson(json, com.tremolosecurity.unison.openshiftv3.model.users.User.class);
if (osUser.getKind().equals("User")) {
this.cfgMgr.getProvisioningEngine().logAction(name, false, ActionType.Replace, approvalID, workflow, "fullName", osUser.getFullName());
} else {
throw new Exception("Could not set fullName for " + user.getUserID() + " - " + osUser.getReason());
}
} finally {
con.getHttp().close();
con.getBcm().shutdown();
}
return token;
}
Aggregations