Skip to content

Commit

Permalink
- Fix base64 inline certificates handling for import
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Nov 27, 2023
1 parent 14d8480 commit 662141e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ public void saveAPILocally(ExportAPI exportAPI, APIResultHandler apiResultHandle
try {
targetFile = localFolder.getCanonicalPath() + "/" + exportAPI.getName() + apiDef.getAPIDefinitionType().getFileExtension();
if (!(apiDef instanceof WSDLSpecification && EnvironmentProperties.RETAIN_BACKEND_URL) && (!EnvironmentProperties.PRINT_CONFIG_CONSOLE)) {
writeBytesToFile(apiDef.getApiSpecificationContent(), targetFile);
exportAPI.getAPIDefinition().setApiSpecificationFile(exportAPI.getName() + apiDef.getAPIDefinitionType().getFileExtension());

writeBytesToFile(apiDef.getApiSpecificationContent(), targetFile);
exportAPI.getAPIDefinition().setApiSpecificationFile(exportAPI.getName() + apiDef.getAPIDefinitionType().getFileExtension());
}
} catch (IOException e) {
throw new AppException("Can't save API-Definition locally to file: " + targetFile, ErrorCode.UNXPECTED_ERROR, e);
Expand All @@ -104,8 +103,7 @@ public void saveAPILocally(ExportAPI exportAPI, APIResultHandler apiResultHandle
}
Image image = exportAPI.getAPIImage();
if (image != null && (!EnvironmentProperties.PRINT_CONFIG_CONSOLE)) {
writeBytesToFile(image.getImageContent(), localFolder + File.separator + image.getBaseFilename());

writeBytesToFile(image.getImageContent(), localFolder + File.separator + image.getBaseFilename());
}
if (exportAPI.getCaCerts() != null && !exportAPI.getCaCerts().isEmpty()) {
storeCaCerts(localFolder, exportAPI.getCaCerts());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private void validateDescription(API apiConfig) throws AppException {
if (!markdownFile.exists()) { // The file isn't provided with an absolute path, try to read it relative to the config file
LOG.trace("Error reading markdown description file (absolute): {}", markdownFile.getCanonicalPath());
String baseDir = this.apiConfigFile.getCanonicalFile().getParent();
markdownFile = new File(baseDir + "/" + markdownFilename);
markdownFile = new File(baseDir, markdownFilename);
}
if (!markdownFile.exists()) {
LOG.trace("Error reading markdown description file (relative): {}", markdownFile.getCanonicalPath());
Expand Down Expand Up @@ -455,6 +455,7 @@ public void completeCaCerts(API apiConfig) throws AppException {
throw new AppException("Can't initialize given certificate.", ErrorCode.CANT_READ_CONFIG_FILE, e);
}
}

}
apiConfig.getCaCerts().clear();
apiConfig.getCaCerts().addAll(completedCaCerts);
Expand All @@ -463,9 +464,13 @@ public void completeCaCerts(API apiConfig) throws AppException {

private InputStream getInputStreamForCertFile(CaCert cert) throws AppException {
InputStream is;
File file;
// Handel base64 encoded inline certificate
if (cert.getCertFile().startsWith("data:")) {
byte[] data = Base64.getDecoder().decode(cert.getCertFile().replaceFirst("data:.+,", ""));
return new ByteArrayInputStream(data);
}
// Certificates might be stored somewhere else, so try to load them directly
file = new File(cert.getCertFile());
File file = new File(cert.getCertFile());
if (file.exists()) {
try {
is = new FileInputStream(file);
Expand Down Expand Up @@ -672,7 +677,7 @@ private void handleOutboundSSLAuthN(AuthenticationProfile authnProfile) throws A
if (!clientCertFile.exists()) {
// Try to find file using a relative path to the config file
String baseDir = this.apiConfigFile.getCanonicalFile().getParent();
clientCertFile = new File(baseDir + "/" + keystore);
clientCertFile = new File(baseDir, keystore);
}
if (!clientCertFile.exists()) {
// If not found absolute & relative - Try to load it from ClassPath
Expand Down Expand Up @@ -721,7 +726,7 @@ private void addImageContent(API importApi) throws AppException {
file = new File(importApi.getImage().getFilename());
if (!file.exists()) { // The image isn't provided with an absolute path, try to read it relative to the config file
String baseDir = this.apiConfigFile.getCanonicalFile().getParent();
file = new File(baseDir + "/" + importApi.getImage().getFilename());
file = new File(baseDir, importApi.getImage().getFilename());
}
importApi.getImage().setBaseFilename(file.getName());
if (file.exists()) {
Expand Down

0 comments on commit 662141e

Please sign in to comment.