use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class K8sWatcher method initalRun.
public void initalRun() throws ProvisioningException {
OpenShiftTarget k8s = (OpenShiftTarget) provisioningEngine.getTarget(k8sTarget).getProvider();
if (k8s == null) {
throw new ProvisioningException("Target " + k8sTarget + " does not exist");
}
HttpCon http;
try {
http = k8s.createClient();
} catch (Exception e1) {
throw new ProvisioningException("Could not create http connection", e1);
}
this.resourceVersions = new HashSet<String>();
try {
String token = k8s.getAuthToken();
String json = null;
try {
json = k8s.callWS(token, http, uri);
} catch (HttpResponseException e) {
logger.warn("Could not retrieve urls, dynamic urls will not be supported", e);
return;
}
JSONObject list = (JSONObject) new JSONParser().parse(json);
JSONArray items = (JSONArray) list.get("items");
if (items == null) {
logger.error("Invalid JSON Response : '" + json + "'");
return;
}
for (Object o : items) {
JSONObject jsonObj = (JSONObject) o;
String strjson = jsonObj.toString();
if (logger.isDebugEnabled())
logger.debug("json before includes : " + strjson);
StringBuffer b = new StringBuffer();
b.setLength(0);
OpenUnisonConfigLoader.integrateIncludes(b, strjson);
if (logger.isDebugEnabled())
logger.debug("json after includes : " + b.toString());
jsonObj = (JSONObject) new JSONParser().parse(b.toString());
JSONObject metadata = (JSONObject) jsonObj.get("metadata");
String resourceVersion = (String) metadata.get("resourceVersion");
if (this.resourceVersions.contains(resourceVersion)) {
logger.info("Resource " + resourceVersion + " already processed, skipping");
} else {
this.resourceVersions.add(resourceVersion);
this.watchee.addObject(cfgMgr.getCfg(), jsonObj);
}
}
} catch (Exception e) {
throw new ProvisioningException("Could not get urls", e);
} finally {
try {
http.getHttp().close();
} catch (IOException e) {
logger.warn(e);
}
http.getBcm().close();
}
this.keepRunning = true;
logger.info("Adding stoppable thread");
GlobalEntries.getGlobalEntries().getConfigManager().addThread(this);
logger.info("Starting watch");
new Thread(this).start();
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class K8sWatcher method runWatch.
private void runWatch(OpenShiftTarget k8s) {
HttpCon http;
try {
http = k8s.createClient();
} catch (Exception e1) {
logger.error("Could not create connection", e1);
return;
}
try {
String url = new StringBuilder().append(k8s.getUrl()).append(this.uri).append("?watch=true&timeoutSecond=25").toString();
logger.info("watching " + url);
HttpGet get = new HttpGet(url);
get.setHeader("Authorization", new StringBuilder().append("Bearer ").append(k8s.getAuthToken()).toString());
HttpResponse resp = http.getHttp().execute(get);
BufferedReader in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
String line = null;
HttpCon nonwatchHttp = k8s.createClient();
while ((line = in.readLine()) != null) {
JSONObject event = (JSONObject) new JSONParser().parse(line);
String action = (String) event.get("type");
JSONObject jsonObject = (JSONObject) event.get("object");
String strjson = jsonObject.toString();
if (logger.isDebugEnabled())
logger.debug("json before includes : " + strjson);
StringBuffer b = new StringBuffer();
b.setLength(0);
OpenUnisonConfigLoader.integrateIncludes(b, strjson);
if (logger.isDebugEnabled())
logger.debug("json after includes : " + b.toString());
jsonObject = (JSONObject) new JSONParser().parse(b.toString());
JSONObject metadata = (JSONObject) jsonObject.get("metadata");
String resourceVersion = (String) metadata.get("resourceVersion");
if (this.resourceVersions.contains(resourceVersion)) {
logger.info("Resource " + resourceVersion + " already processed, skipping");
} else {
this.resourceVersions.add(resourceVersion);
if (action.equalsIgnoreCase("ADDED")) {
this.watchee.addObject(this.cfgMgr.getCfg(), jsonObject);
} else if (action.equalsIgnoreCase("MODIFIED")) {
this.watchee.modifyObject(this.cfgMgr.getCfg(), jsonObject);
} else {
// deleted
this.watchee.deleteObject(this.cfgMgr.getCfg(), jsonObject);
}
}
}
nonwatchHttp.getHttp().close();
nonwatchHttp.getBcm().close();
} catch (Exception e) {
logger.error("Could not run watch, waiting 10 seconds", e);
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
// do nothing
}
return;
} finally {
if (http != null) {
try {
http.getHttp().close();
} catch (IOException e) {
}
http.getBcm().close();
}
}
return;
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class UserPrincipal method deleteUser.
public void deleteUser(User user, Map<String, Object> request) throws ProvisioningException {
UserPrincipal principal = new UserPrincipal(user.getUserID(), multiDomain, primaryDomain);
int approvalID = 0;
if (request.containsKey("APPROVAL_ID")) {
approvalID = (Integer) request.get("APPROVAL_ID");
}
Workflow workflow = (Workflow) request.get("WORKFLOW");
try {
HttpCon con = this.createClient();
try {
if (principal.isPrimaryDomain()) {
IPACall deleteUser = new IPACall();
deleteUser.setId(0);
deleteUser.setMethod("user_del");
ArrayList<String> userArray = new ArrayList<String>();
userArray.add(principal.getUid());
deleteUser.getParams().add(userArray);
HashMap<String, String> additionalParams = new HashMap<String, String>();
deleteUser.getParams().add(additionalParams);
IPAResponse resp = this.executeIPACall(deleteUser, con);
this.cfgMgr.getProvisioningEngine().logAction(name, true, ActionType.Delete, approvalID, workflow, "uid", user.getUserID());
} else {
IPACall idOveride = new IPACall();
idOveride.setId(0);
idOveride.setMethod("idoverrideuser_del");
List<String> params = new ArrayList<String>();
params.add(this.trustViewName);
params.add(principal.getUPN());
idOveride.getParams().add(params);
Map<String, Object> param2 = new HashMap<String, Object>();
idOveride.getParams().add(param2);
try {
IPAResponse resp = this.executeIPACall(idOveride, con);
} catch (IPAException e) {
if (!e.getMessage().equalsIgnoreCase("no modifications to be performed")) {
throw e;
}
}
}
} finally {
if (con != null) {
con.getBcm().shutdown();
}
}
} catch (Exception e) {
throw new ProvisioningException("Could not run search", e);
}
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class UserPrincipal method isGroupExists.
@Override
public boolean isGroupExists(String name, User user, Map<String, Object> request) throws ProvisioningException {
IPACall groupSearch = new IPACall();
groupSearch.setId(0);
groupSearch.setMethod("group_show");
ArrayList<String> groupArray = new ArrayList<String>();
groupArray.add(name);
groupSearch.getParams().add(groupArray);
HashMap<String, String> additionalParams = new HashMap<String, String>();
groupSearch.getParams().add(additionalParams);
HttpCon con = null;
try {
con = this.createClient();
IPAResponse resp = this.executeIPACall(groupSearch, con);
return true;
} catch (IPAException ipae) {
if (ipae.getCode() == 4001) {
return false;
} else {
throw new ProvisioningException("Could not find groups", ipae);
}
} catch (Exception e) {
throw new ProvisioningException("Could not find groups", e);
} finally {
if (con != null) {
con.getBcm().close();
}
}
}
use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.
the class UserPrincipal method addGroup.
@Override
public void addGroup(String name, Map<String, String> additionalAttributes, 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");
IPACall groupSearch = new IPACall();
groupSearch.setId(0);
groupSearch.setMethod("group_add");
ArrayList<String> groupArray = new ArrayList<String>();
groupArray.add(name);
groupSearch.getParams().add(groupArray);
HashMap<String, String> additionalParams = new HashMap<String, String>();
for (String key : additionalAttributes.keySet()) {
additionalParams.put(key, additionalAttributes.get(key));
}
groupSearch.getParams().add(additionalParams);
HttpCon con = null;
try {
con = this.createClient();
IPAResponse resp = this.executeIPACall(groupSearch, con);
this.cfgMgr.getProvisioningEngine().logAction(name, true, ActionType.Add, approvalID, workflow, "group-object", name);
} catch (Exception e) {
throw new ProvisioningException("Could not find groups", e);
} finally {
if (con != null) {
con.getBcm().close();
}
}
}
Aggregations