Search in sources :

Example 1 with TargetConfigType

use of com.tremolosecurity.config.xml.TargetConfigType in project OpenUnison by TremoloSecurity.

the class LoadTargetsFromK8s method createTarget.

private TargetType createTarget(JSONObject item, String name) throws ProvisioningException {
    TargetType target = new TargetType();
    target.setName(name);
    target.setParams(new TargetConfigType());
    HttpCon nonwatchHttp = null;
    JSONObject spec = (JSONObject) item.get("spec");
    try {
        nonwatchHttp = this.k8sWatch.getK8s().createClient();
        String token = this.k8sWatch.getK8s().getAuthToken();
        StringBuffer b = new StringBuffer();
        b.setLength(0);
        OpenUnisonConfigLoader.integrateIncludes(b, (String) spec.get("className"));
        target.setClassName(b.toString());
        JSONArray params = (JSONArray) spec.get("params");
        for (Object o : params) {
            JSONObject param = (JSONObject) o;
            ParamType pt = new ParamType();
            b.setLength(0);
            OpenUnisonConfigLoader.integrateIncludes(b, (String) param.get("name"));
            pt.setName(b.toString());
            b.setLength(0);
            OpenUnisonConfigLoader.integrateIncludes(b, (String) param.get("value"));
            pt.setValue(b.toString());
            target.getParams().getParam().add(pt);
        }
        JSONArray secretParams = (JSONArray) spec.get("secretParams");
        for (Object o : secretParams) {
            JSONObject secretParam = (JSONObject) o;
            String paramName = (String) secretParam.get("name");
            String secretName = (String) secretParam.get("secretName");
            String secretKey = (String) secretParam.get("secretKey");
            String secretValue = this.k8sWatch.getSecretValue(secretName, secretKey, token, nonwatchHttp);
            ParamType pt = new ParamType();
            pt.setName(paramName);
            pt.setValue(secretValue);
            target.getParams().getParam().add(pt);
        }
        JSONArray attrs = (JSONArray) spec.get("targetAttributes");
        for (Object o : attrs) {
            JSONObject attr = (JSONObject) o;
            TargetAttributeType ta = new TargetAttributeType();
            b.setLength(0);
            OpenUnisonConfigLoader.integrateIncludes(b, (String) attr.get("name"));
            ta.setName(b.toString());
            b.setLength(0);
            OpenUnisonConfigLoader.integrateIncludes(b, (String) attr.get("source"));
            ta.setSource(b.toString());
            ta.setSourceType((String) attr.get("sourceType"));
            ta.setTargetType((String) attr.get("targetType"));
            target.getTargetAttribute().add(ta);
        }
        synchronized (this.tremolo.getProvisioning().getTargets().getTarget()) {
            int found = -1;
            int ii = 0;
            for (TargetType tt : this.tremolo.getProvisioning().getTargets().getTarget()) {
                if (tt.getName().equals(target.getName())) {
                    found = ii;
                    break;
                }
                ii++;
            }
            if (found >= 0) {
                this.tremolo.getProvisioning().getTargets().getTarget().remove(found);
            }
            this.tremolo.getProvisioning().getTargets().getTarget().add(target);
        }
        return target;
    } catch (Exception e) {
        throw new ProvisioningException("Could not add target '" + name + "'", e);
    } finally {
        if (nonwatchHttp != null) {
            try {
                nonwatchHttp.getHttp().close();
            } catch (IOException e) {
            }
            nonwatchHttp.getBcm().close();
        }
    }
}
Also used : JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) ParamType(com.tremolosecurity.config.xml.ParamType) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) TargetAttributeType(com.tremolosecurity.config.xml.TargetAttributeType) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) TargetType(com.tremolosecurity.config.xml.TargetType) TargetConfigType(com.tremolosecurity.config.xml.TargetConfigType) JSONObject(org.json.simple.JSONObject)

Aggregations

ParamType (com.tremolosecurity.config.xml.ParamType)1 TargetAttributeType (com.tremolosecurity.config.xml.TargetAttributeType)1 TargetConfigType (com.tremolosecurity.config.xml.TargetConfigType)1 TargetType (com.tremolosecurity.config.xml.TargetType)1 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)1 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)1 IOException (java.io.IOException)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1