Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update alias #217

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions samples/EdgeConfig/resources/edge/env/dev/aliases.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@
},
"certValidityInDays":"90"
},
{
"alias":"testAliasCertFile",
"keystorename": "testKeyStorename",
"ignoreExpiryValidation": true,
"format": "keycertfile",
"certFilePath":"/tmp/certs/keystore.pem"
},
{
"alias":"testAliasKeyCertFileAndKey",
"keystorename": "testKeyStorename",
"ignoreExpiryValidation": true,
"format": "keycertfile",
"certFilePath":"/tmp/certs/keystore.pem",
"keyFilePath":"/tmp/certs/keystore.key",
Expand All @@ -21,14 +29,9 @@
{
"alias":"testAliasPKCS12",
"keystorename": "testKeyStorename",
"ignoreExpiryValidation": true,
"format": "pkcs12",
"filePath":"/tmp/certs/myKeystore.p12",
"password":"dummy"
},
{
"alias":"testAliasKeyCertFile",
"keystorename": "testKeyStorename",
"format": "keycertfile",
"certFilePath":"/tmp/certs/keystore.pem"
}
]
2 changes: 1 addition & 1 deletion samples/EdgeConfig/shared-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<plugin>
<groupId>com.apigee.edge.config</groupId>
<artifactId>apigee-config-maven-plugin</artifactId>
<version>2.9.1</version>
<version>2.9.2</version>
<executions>
<execution>
<id>create-config-targetserver</id>
Expand Down
66 changes: 62 additions & 4 deletions src/main/java/com/apigee/edge/config/mavenplugin/AliasMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ protected void doUpdate(List<String> aliases) throws MojoFailureException {
if (existingAliases.contains(a.alias)) {
switch (buildOption) {
case update:
logger.info("Alias \"" + a.alias +
"\" exists.");
logger.info("Please delete the alias and re-create or run it using 'sync' option");
logger.info("Alias \"" + a.alias +
"\" already exists. Updating.");
updateAlias(serverProfile, alias);
break;
case create:
logger.info("Alias \"" + a.alias +
Expand Down Expand Up @@ -300,6 +300,65 @@ public static String createAlias(ServerProfile profile, String aliasPayload)

return "";
}

public static String updateAlias(ServerProfile profile, String aliasPayload)
throws IOException, MojoFailureException {
Alias alias = getAliasObj(aliasPayload);

validateAlias(alias);

// Call rest helper
RestUtil restUtil = new RestUtil(profile);
HttpResponse response = null;
//update is not supported for selfsignedcert, keycertjar or pkcs12 format
if(alias.format!=null && (alias.format.equalsIgnoreCase("selfsignedcert") || alias.format.equalsIgnoreCase("keycertjar") || alias.format.equalsIgnoreCase("pkcs12"))) {
logger.info(____ATTENTION_MARKER____);
logger.info("NOTE: Update alias for \""+alias.format+"\" format is not supported");
logger.info(____ATTENTION_MARKER____);
return "";
}
else {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("alias", alias.alias);
parameters.put("format", alias.format);
parameters.put("ignoreNewlineValidation", (alias.ignoreNewlineValidation)?String.valueOf(alias.ignoreNewlineValidation): "true"); //default to true
parameters.put("ignoreExpiryValidation", (alias.ignoreExpiryValidation)?String.valueOf(alias.ignoreExpiryValidation): "false"); //default to false
parameters.put("privateKeyExportable", (alias.privateKeyExportable)?String.valueOf(alias.privateKeyExportable): "false"); //default to false

//password should not be passed for update
// if(alias.password!=null && !alias.password.equalsIgnoreCase(""))
// parameters.put("password", alias.password);

Map<String, String> multipartFiles = new HashMap<String, String>();
//Set param as "certFile" for keycertfile
if(alias.certFilePath!=null && !alias.certFilePath.equalsIgnoreCase(""))
multipartFiles.put("certFile", alias.certFilePath);
if(alias.keyFilePath!=null && !alias.keyFilePath.equalsIgnoreCase("")) {
logger.info(____ATTENTION_MARKER____);
logger.info("NOTE: Key files will be ignored for the update option. Only the certs are updated");
logger.info(____ATTENTION_MARKER____);
}
//Set param as "file" for keycertjar or pkcs12
if(alias.filePath!=null && !alias.filePath.equalsIgnoreCase(""))
multipartFiles.put("file", alias.filePath);
response = restUtil.updateEnvConfigUpload(profile,
"keystores", alias.keystorename, "aliases/"+alias.alias, multipartFiles, parameters);

}
try {

logger.info("Response " + response.getContentType() + "\n" +
response.parseAsString());
if (response.isSuccessStatusCode())
logger.info("Update Success.");

} catch (HttpResponseException e) {
logger.error("Alias update error " + e.getMessage());
throw new IOException(e.getMessage());
}

return "";
}

public static String deleteAlias(ServerProfile profile,
String aliasPayload)
Expand Down Expand Up @@ -388,4 +447,3 @@ private static String removeFormatFromAlias(String aliasPayload){
}



73 changes: 73 additions & 0 deletions src/main/java/com/apigee/edge/config/rest/RestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,53 @@ public HttpResponse createEnvConfigUpload(ServerProfile profile, String resource

return response;
}

//Mainly used for updating alias (for keystore)
public HttpResponse updateEnvConfigUpload(ServerProfile profile, String resource, String resourceId, String subResource,
Map<String, String> multipartFiles, Map<String, String> parameters) throws IOException {
MultipartContent payload = new MultipartContent().setMediaType(new HttpMediaType("multipart/form-data"));
payload.setBoundary(MULTIPART_BOUNDARY_PREFIX + System.currentTimeMillis());

if (multipartFiles.entrySet().size() > 0) {
for (Map.Entry<String, String> entry : multipartFiles.entrySet()) {
byte[] file = Files.readAllBytes(new File(entry.getValue()).toPath());
ByteArrayContent content = new ByteArrayContent("application/octet-stream", file);

HttpHeaders headers = new HttpHeaders().set("Content-Disposition",
"form-data; name=\"" + entry.getKey() + "\"");

payload.addPart(new MultipartContent.Part(headers, content));
}
}

if(parameters!=null && parameters.containsKey("password")) {
HttpHeaders headers = new HttpHeaders().set("Content-Disposition",
"form-data; name=\"password\"");
HttpContent partContent = ByteArrayContent.fromString(null, parameters.get("password"));
payload.addPart(new MultipartContent.Part(headers, partContent));
parameters.remove("password");
}

String importCmd = profile.getHostUrl() + "/"
+ profile.getApi_version() + "/organizations/"
+ profile.getOrg() + "/environments/"
+ profile.getEnvironment() + "/" + resource + "/"
+ URLEncoder.encode(resourceId, "UTF-8")
+ "/" + subResource;
GenericUrl url = new GenericUrl(importCmd);
url.putAll(parameters);
HttpRequest restRequest = REQUEST_FACTORY.buildPutRequest(url, payload);
restRequest.setReadTimeout(0);
HttpResponse response;
try {
response = executeAPI(profile, restRequest);
} catch (HttpResponseException e) {
logger.error("Apigee call failed " + e.getMessage());
throw new IOException(e.getMessage());
}

return response;
}

public HttpResponse updateEnvConfig(ServerProfile profile,
String resource,
Expand Down Expand Up @@ -367,6 +414,32 @@ public HttpResponse updateEnvConfigUpload(ServerProfile profile, String resource
return response;
}

public HttpResponse updateEnvConfigWithParameters(ServerProfile profile, String resource, String resourceId, String subResource, Map<String, String> parameters,
String payload) throws IOException {

String importCmd = profile.getHostUrl() + "/" + profile.getApi_version() + "/organizations/" + profile.getOrg()
+ "/environments/" + profile.getEnvironment() + "/" + resource + "/"
+ URLEncoder.encode(resourceId, "UTF-8") + "/" + subResource;

ByteArrayContent content = new ByteArrayContent("application/json", payload.getBytes());

GenericUrl url = new GenericUrl(importCmd);
url.putAll(parameters);

HttpRequest restRequest = REQUEST_FACTORY.buildPutRequest(url, content);
restRequest.setReadTimeout(0);

HttpResponse response;
try {
response = executeAPI(profile, restRequest);
} catch (HttpResponseException e) {
logger.error("Apigee call failed " + e.getMessage());
throw new IOException(e.getMessage());
}

return response;
}

public HttpResponse deleteEnvResourceFileConfig(ServerProfile profile, String resource, String resourceId)
throws IOException {

Expand Down