use of com.tremolosecurity.git.GitUtils in project OpenUnison by TremoloSecurity.
the class K8sProjectCheck method createTremoloUser.
@Override
public String createTremoloUser(NewUserRequest newUser, List<String> errors, AuthInfo userData) throws ProvisioningException {
if (errors.size() == 0) {
String targetName = newUser.getAttributes().get("cluster");
if (targetName == null) {
targetName = this.targetName;
}
OpenShiftTarget target = (OpenShiftTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(targetName).getProvider();
HttpCon con = null;
try {
String token = target.getAuthToken();
con = target.createClient();
if (target.isObjectExistsByName(token, con, "/api/v1/namespaces", newUser.getAttributes().get(this.projectAttributeName))) {
errors.add("Namespace name already exists");
return "";
}
} catch (Exception e) {
throw new ProvisioningException("Could not check if namespace exists", e);
} finally {
if (con != null) {
try {
con.getHttp().close();
} catch (IOException e) {
// doesn't matter
}
con.getBcm().close();
}
}
if (target.getGitUrl() != null && !target.getGitUrl().isEmpty()) {
String gitUrlForNs = newUser.getAttributes().get("gitUrl");
String sshPrivKey = newUser.getAttributes().get("gitSshKey");
if (gitUrlForNs == null || gitUrlForNs.isEmpty()) {
errors.add("Git URL is required for clusters configured to use git");
}
if (sshPrivKey == null || sshPrivKey.isEmpty()) {
errors.add("Git SSH Private Key is required for clusters configured to use git");
}
if (errors.size() > 0) {
return "";
}
GitUtils gitUtil = new GitUtils(gitUrlForNs, sshPrivKey);
try {
gitUtil.checkOut();
} catch (Throwable t) {
logger.warn("Could not checkout '" + gitUrlForNs + "'", t);
errors.add(t.getMessage());
} finally {
gitUtil.cleanup();
}
}
return this.workflowName;
} else {
return "";
}
}
use of com.tremolosecurity.git.GitUtils in project OpenUnison by TremoloSecurity.
the class PushToGit method doTask.
@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
String localSecretName = task.renderTemplate(secretName, request);
String localNameSpace = task.renderTemplate(nameSpace, request);
String localTarget = task.renderTemplate(this.target, request);
String localKeyName = task.renderTemplate(this.keyName, request);
String localGitRepo = task.renderTemplate(gitRepo, request);
String localCommitMsg = task.renderTemplate(commitMsg, request);
OpenShiftTarget target = (OpenShiftTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(localTarget).getProvider();
HttpCon con = null;
GitUtils gitUtil = null;
try {
con = target.createClient();
StringBuilder sb = new StringBuilder();
sb.append("/api/v1/namespaces/").append(localNameSpace).append("/secrets/").append(localSecretName);
String json = target.callWS(target.getAuthToken(), con, sb.toString());
JSONObject secret = (JSONObject) new JSONParser().parse(json);
JSONObject data = (JSONObject) secret.get("data");
if (data == null) {
throw new Exception("No data found for " + sb.toString());
}
String b64KeyData = (String) data.get(localKeyName);
if (b64KeyData == null) {
throw new ProvisioningException("Could not find key '" + localKeyName + "' in '" + sb.toString() + "'");
}
String privateKey = new String(java.util.Base64.getDecoder().decode(b64KeyData));
gitUtil = new GitUtils(localGitRepo, privateKey);
try {
gitUtil.checkOut();
} catch (Exception e) {
throw new Exception("Could not checkout repo");
}
List<GitFile> files = (List<GitFile>) request.get(requestObject);
if (files == null) {
throw new Exception("No gitfiles stored in '" + requestObject + "'");
}
gitUtil.applyFiles(files);
gitUtil.commitAndPush(localCommitMsg);
} catch (Exception e) {
throw new ProvisioningException("Could not push to git", e);
} finally {
if (con != null) {
try {
con.getHttp().close();
} catch (IOException e) {
}
con.getBcm().close();
}
if (gitUtil != null) {
gitUtil.cleanup();
}
}
return true;
}
Aggregations