use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class KeystoneProvisioningTarget method createClient.
public HttpCon createClient() throws Exception {
ArrayList<Header> defheaders = new ArrayList<Header>();
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();
HttpCon con = new HttpCon();
con.setBcm(bhcm);
con.setHttp(http);
return con;
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class KeystoneProvisioningTarget method listDomainObjs.
public List<KSDomain> listDomainObjs() throws ProvisioningException {
HttpCon con = null;
try {
con = this.createClient();
KSToken token = this.getToken(con);
StringBuffer b = new StringBuffer();
b.append(this.url).append("/domains?enabled");
String json = this.callWS(token.getAuthToken(), con, b.toString());
Gson gson = new Gson();
return gson.fromJson(json, DomainsResponse.class).getDomains();
} catch (Exception e) {
throw new ProvisioningException("Could not work with keystone", e);
} finally {
if (con != null) {
con.getBcm().shutdown();
}
}
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class KeystoneProvisioningTarget method getDomainName.
public String getDomainName(String id) throws ProvisioningException {
HttpCon con = null;
try {
con = this.createClient();
KSToken token = this.getToken(con);
StringBuffer b = new StringBuffer();
b.append(this.url).append("/domains/").append(id);
String json = this.callWS(token.getAuthToken(), con, b.toString());
Gson gson = new Gson();
DomainsResponse resp = gson.fromJson(json, DomainsResponse.class);
if (resp.getDomain() == null) {
return null;
} else {
return resp.getDomain().getName();
}
} catch (Exception e) {
throw new ProvisioningException("Could not work with keystone", e);
} finally {
if (con != null) {
con.getBcm().shutdown();
}
}
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class Drupal8Target method deleteUser.
@Override
public void deleteUser(User user, Map<String, Object> request) throws ProvisioningException {
int userID = 0;
int approvalID = 0;
int workflowID = 0;
if (request.containsKey("TREMOLO_USER_ID")) {
userID = (Integer) request.get("TREMOLO_USER_ID");
}
if (request.containsKey("APPROVAL_ID")) {
approvalID = (Integer) request.get("APPROVAL_ID");
}
if (request.containsKey("WORKFLOW_ID")) {
workflowID = (Integer) request.get("WORKFLOW_ID");
}
Workflow workflow = (Workflow) request.get("WORKFLOW");
StringBuilder sb = new StringBuilder();
sb.append(url).append("/user/").append(user.getUserID()).append("?_format=json");
HttpCon con = null;
try {
con = this.createClient();
HttpDelete req = new HttpDelete(sb.toString());
sb.setLength(0);
sb.append(this.user).append(":").append(this.password);
String azHeader = java.util.Base64.getEncoder().encodeToString(sb.toString().getBytes());
sb.setLength(0);
req.setHeader("Authorization", sb.append("Basic ").append(azHeader).toString());
CloseableHttpResponse resp = con.getHttp().execute(req);
if (resp.getStatusLine().getStatusCode() != 204) {
logger.warn("User '" + user.getUserID() + "' not found");
return;
}
this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Delete, approvalID, workflow, "uid", user.getUserID());
} catch (Exception e) {
throw new ProvisioningException("Could not find user", e);
} finally {
if (con != null) {
try {
con.getHttp().close();
} catch (IOException e) {
}
con.getBcm().close();
}
}
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class Drupal8Target method createUser.
@Override
public void createUser(User user, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
int userID = 0;
int approvalID = 0;
int workflowID = 0;
if (request.containsKey("TREMOLO_USER_ID")) {
userID = (Integer) request.get("TREMOLO_USER_ID");
}
if (request.containsKey("APPROVAL_ID")) {
approvalID = (Integer) request.get("APPROVAL_ID");
}
if (request.containsKey("WORKFLOW_ID")) {
workflowID = (Integer) request.get("WORKFLOW_ID");
}
Workflow workflow = (Workflow) request.get("WORKFLOW");
StringBuilder sb = new StringBuilder();
JSONObject root = new JSONObject();
HashMap<String, String> attrsForAudit = new HashMap<String, String>();
if (attributes.contains("name") && user.getAttribs().containsKey("name")) {
this.setJsonValue("name", user.getAttribs().get("name").getValues().get(0), root);
attrsForAudit.put("name", user.getAttribs().get("name").getValues().get(0));
}
if (attributes.contains("langcode") && user.getAttribs().containsKey("langcode")) {
this.setJsonValue("langcode", user.getAttribs().get("langcode").getValues().get(0), root);
attrsForAudit.put("langcode", user.getAttribs().get("langcode").getValues().get(0));
}
if (attributes.contains("preferred_langcode") && user.getAttribs().containsKey("preferred_langcode")) {
this.setJsonValue("preferred_langcode", user.getAttribs().get("preferred_langcode").getValues().get(0), root);
attrsForAudit.put("preferred_langcode", user.getAttribs().get("preferred_langcode").getValues().get(0));
}
if (attributes.contains("preferred_admin_langcode") && user.getAttribs().containsKey("preferred_admin_langcode")) {
this.setJsonValue("preferred_admin_langcode", user.getAttribs().get("preferred_admin_langcode").getValues().get(0), root);
attrsForAudit.put("preferred_admin_langcode", user.getAttribs().get("preferred_admin_langcode").getValues().get(0));
}
if (attributes.contains("mail") && user.getAttribs().containsKey("mail")) {
this.setJsonValue("mail", user.getAttribs().get("mail").getValues().get(0), root);
attrsForAudit.put("mail", user.getAttribs().get("mail").getValues().get(0));
}
if (attributes.contains("status") && user.getAttribs().containsKey("status")) {
this.setJsonValueBoolean("status", user.getAttribs().get("status").getValues().get(0), root);
attrsForAudit.put("status", user.getAttribs().get("status").getValues().get(0));
}
for (String userAttributeName : user.getAttribs().keySet()) {
if (attributes.contains(userAttributeName) && !defaultAttributes.contains(userAttributeName)) {
this.setJsonValue("field_" + userAttributeName, user.getAttribs().get(userAttributeName).getValues().get(0), root);
attrsForAudit.put("field_" + userAttributeName, user.getAttribs().get(userAttributeName).getValues().get(0));
}
}
JSONArray roles = new JSONArray();
for (String groupName : user.getGroups()) {
JSONObject group = new JSONObject();
group.put("target_id", groupName);
roles.add(group);
}
root.put("roles", roles);
HttpPost post = new HttpPost(this.url + "/entity/user?_format=json");
post.setHeader(new BasicHeader("X-CSRF-Token", UUID.randomUUID().toString()));
post.addHeader("Content-Type", "application/json");
try {
post.setEntity(new StringEntity(root.toJSONString()));
} catch (UnsupportedEncodingException e) {
throw new ProvisioningException("Couldn't create user", e);
}
sb.setLength(0);
sb.append(this.user).append(":").append(this.password);
String azHeader = java.util.Base64.getEncoder().encodeToString(sb.toString().getBytes());
sb.setLength(0);
post.setHeader("Authorization", sb.append("Basic ").append(azHeader).toString());
HttpCon con = null;
try {
con = this.createClient();
} catch (Exception e) {
throw new ProvisioningException("Couldn't create user", e);
}
try {
CloseableHttpResponse resp = con.getHttp().execute(post);
if (resp.getStatusLine().getStatusCode() == 201) {
String json = EntityUtils.toString(resp.getEntity());
JSONParser parser = new JSONParser();
root = (JSONObject) parser.parse(json);
String uid = getJsonValue("uid", root);
this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Add, approvalID, workflow, "uid", uid);
for (String attr : attrsForAudit.keySet()) {
this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, attr, attrsForAudit.get(attr));
}
for (String groupName : user.getGroups()) {
this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "role", groupName);
}
user.setUserID(uid);
} else {
throw new ProvisioningException("Could not create user with code " + resp.getStatusLine().getStatusCode());
}
} catch (IOException | ParseException e) {
throw new ProvisioningException("Couldn't create user", e);
}
}
Aggregations