Skip to content

Commit

Permalink
Update script
Browse files Browse the repository at this point in the history
Fix reviewed comments and test failure

Update script

Convert to log error

Update script

Update the script to support 17 and 21

Update capp

Update reuest url
  • Loading branch information
kalaiyarasiganeshalingam committed Nov 24, 2024
1 parent da9771a commit 063e423
Show file tree
Hide file tree
Showing 20 changed files with 400 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.commons.property.PropertyHolder;
import org.apache.synapse.commons.resolvers.ResolverFactory;
import org.apache.synapse.transport.nhttp.config.SslSenderTrustStoreHolder;
import org.wso2.micro.application.deployer.CarbonApplication;
import org.wso2.micro.application.deployer.config.ApplicationConfiguration;
Expand All @@ -32,13 +31,17 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.Enumeration;
Expand All @@ -55,7 +58,6 @@ public class ConfigDeployer implements AppDeploymentHandler {

private static final String LOCAL_CONFIG_FILE_NAME = "config.properties";
private static final String GLOBAL_CONFIG_FILE_NAME = "file.properties";
private static final String IS_CERT_DEPLOYMENT_ENABLED = "isCertDeploymentEnabled";
private Properties globalProperties;

public static final char URL_SEPARATOR_CHAR = '/';
Expand All @@ -64,7 +66,8 @@ public ConfigDeployer() {
}

@Override
public void deployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig) throws DeploymentException {
public void deployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig)
throws DeploymentException {
if (log.isDebugEnabled()) {
log.debug("Deploying properties - " + carbonApp.getAppName());
}
Expand All @@ -77,15 +80,16 @@ public void deployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisC
artifacts.add(dep.getArtifact());
}
}
deployConfigArtifacts(artifacts, carbonApp.getAppNameWithVersion());
deployConfigArtifacts(artifacts);
}

@Override
public void undeployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig) throws DeploymentException {
public void undeployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig)
throws DeploymentException {

}

private void deployConfigArtifacts(List<Artifact> artifacts, String parentAppName) {
private void deployConfigArtifacts(List<Artifact> artifacts) {
artifacts.stream().filter(artifact -> PROPERTY_TYPE.equals(artifact.getType())).forEach(artifact -> {
if (log.isDebugEnabled()) {
log.debug("Deploying config artifact: " + artifact.getName());
Expand All @@ -102,14 +106,16 @@ private void writePropertyToMap(Artifact artifact) {
Path globalPropertiesFilePath = confFolder.resolve(GLOBAL_CONFIG_FILE_NAME) ;
Path serverConfPropertyPath = confFolder.resolve(LOCAL_CONFIG_FILE_NAME);
String configFilePath = artifact.getExtractedPath() + File.separator + LOCAL_CONFIG_FILE_NAME;
processConfFile(configFilePath, globalPropertiesFilePath.toString(), serverConfPropertyPath.toString());
processConfFile(artifact.getName(), configFilePath, globalPropertiesFilePath.toString(),
serverConfPropertyPath.toString());
} else {
log.error("config/property type must have a single file which declares " +
"config. But " + files.size() + " files found.");
}
}

public void processConfFile(String configFilePath, String globalPropertiesFilePath, String serverConfPropertyPath) {
private void processConfFile(String integrationName, String configFilePath, String globalPropertiesFilePath,
String serverConfPropertyPath) {
File configFile = new File(configFilePath);
// Load capp conf property file
Properties configProperties = loadPropertiesFromFile(configFile);
Expand All @@ -119,39 +125,32 @@ public void processConfFile(String configFilePath, String globalPropertiesFilePa
Properties serverConfigProperties = loadPropertiesFromFile(new File(serverConfPropertyPath));

Properties newServerConfigProperties = new Properties();
String isCertDeploymentEnabled = getValueOfKey(IS_CERT_DEPLOYMENT_ENABLED);
if (isCertDeploymentEnabled == null) {
isCertDeploymentEnabled = "true";
}
PropertyHolder.getInstance().setProperty(IS_CERT_DEPLOYMENT_ENABLED, isCertDeploymentEnabled);
if (serverConfigProperties.size() == 0 && configProperties.size() == 0 ) {
log.info("No configuration is used in the integration");
if (log.isDebugEnabled()) {
log.debug(String.format("No configuration is used in the integration[%s]", integrationName));
}
} else {
if (serverConfigProperties.size() > 0) {
for (Map.Entry<Object, Object> entry : serverConfigProperties.entrySet()) {
String key = entry.getKey().toString();
String type = entry.getValue().toString();
if (configProperties.containsKey(key)) {
type = configProperties.getProperty(key);
configProperties.remove(key);
}
newServerConfigProperties.setProperty(key, type);
processConfigProperties(key, type);
for (Map.Entry<Object, Object> entry : serverConfigProperties.entrySet()) {
String key = entry.getKey().toString();
String type = entry.getValue().toString();
if (configProperties.containsKey(key)) {
type = configProperties.getProperty(key);
configProperties.remove(key);
}
newServerConfigProperties.setProperty(key, type);
processConfigProperties(key, type);
}
if (configProperties.size() > 0) {
for (Map.Entry<Object, Object> entry : configProperties.entrySet()) {
String key = entry.getKey().toString();
String type = entry.getValue().toString();
newServerConfigProperties.setProperty(key, type);
processConfigProperties(key, type);
}
for (Map.Entry<Object, Object> entry : configProperties.entrySet()) {
String key = entry.getKey().toString();
String type = entry.getValue().toString();
newServerConfigProperties.setProperty(key, type);
processConfigProperties(key, type);
}
writeServerConfFile(serverConfPropertyPath, newServerConfigProperties);
}
}

public void processConfigProperties(String key, String type) {
private void processConfigProperties(String key, String type) {
String value = getValueOfKey(key);
if (value != null) {
if (Objects.equals(type, "cert")) {
Expand All @@ -160,8 +159,8 @@ public void processConfigProperties(String key, String type) {
if (PropertyHolder.getInstance().hasKey(key)) {
String oldValue = PropertyHolder.getInstance().getPropertyValue(key);
if (!Objects.equals(oldValue, value)) {
log.error(String.format("The value:[%s] of the key:[%s] has been " +
"replaced with the new value:[%s].", oldValue, key, value));
log.info(String.format("The value of the key:[%s] has been " +
"replaced with the new value.", key));
}
}
PropertyHolder.getInstance().setProperty(key, value);
Expand All @@ -170,39 +169,46 @@ public void processConfigProperties(String key, String type) {
}
}

public void deployCert(String key, String path) {
if (ResolverFactory.getInstance().getResolver("$config:" + IS_CERT_DEPLOYMENT_ENABLED).resolve().
equals("true")) {
// Load the truststore properties
char[] password = SslSenderTrustStoreHolder.getInstance().getPassword().toCharArray();
String type = SslSenderTrustStoreHolder.getInstance().getType();
Path trustStorePath = Paths.get(getHome(), SslSenderTrustStoreHolder.getInstance().getLocation());
try (FileInputStream trustStoreStream = new FileInputStream(trustStorePath.toFile())) {
KeyStore trustStore = KeyStore.getInstance(type);
trustStore.load(trustStoreStream, password);
if (!trustStore.containsAlias(key)) {
// Load the certificate file
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
try (FileInputStream certStream = new FileInputStream(path)) {
Certificate cert = certFactory.generateCertificate(certStream);
// Add the certificate to the truststore
trustStore.setCertificateEntry(key, cert);
log.info("Certificate added with alias: " + key);
}
// Save the truststore with the new certificate
try (FileOutputStream outputStream = new FileOutputStream(trustStorePath.toFile())) {
trustStore.store(outputStream, password);
log.info("Truststore updated successfully at: " + trustStorePath);
}
private void deployCert(String key, String path) {
// Load the truststore properties
char[] password = SslSenderTrustStoreHolder.getInstance().getPassword().toCharArray();
String type = SslSenderTrustStoreHolder.getInstance().getType();
Path trustStorePath = Paths.get(getHome(), SslSenderTrustStoreHolder.getInstance().getLocation());
try (FileInputStream trustStoreStream = new FileInputStream(trustStorePath.toFile())) {
KeyStore trustStore = KeyStore.getInstance(type);
trustStore.load(trustStoreStream, password);
if (!trustStore.containsAlias(key)) {
// Load the certificate file
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
try (FileInputStream certStream = new FileInputStream(path)) {
Certificate cert = certFactory.generateCertificate(certStream);
// Add the certificate to the truststore
trustStore.setCertificateEntry(key, cert);
log.info("Certificate added with alias: " + key);
}
// Save the truststore with the new certificate
try (FileOutputStream outputStream = new FileOutputStream(trustStorePath.toFile())) {
trustStore.store(outputStream, password);
log.info("Truststore updated successfully at: " + trustStorePath);
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed to import certificate: " + e.getMessage());
} else {
log.info(String.format("The trust store already contains a certificate " +
"with the alias [%s].", key));
}
} catch (FileNotFoundException e) {
log.error(String.format("File not found for importing the certificate: %s", key));
} catch (IOException e) {
log.error(String.format("Certificate import failed: %s", key));
} catch (CertificateException e) {
log.error(String.format("An error occurred while processing the certificate: %s", key));
} catch (KeyStoreException e) {
log.error(String.format("An error occurred while processing the truststore: %s", key));
} catch (NoSuchAlgorithmException e) {
log.error(String.format("An error occurred while loading the certificate: %s", key));
}
}

public void writeServerConfFile(String file, Properties newServerConfigProperties) {
private void writeServerConfFile(String file, Properties newServerConfigProperties) {
try (FileWriter writer = new FileWriter(file)) {
Enumeration<?> propertyNames = newServerConfigProperties.propertyNames();
while (propertyNames.hasMoreElements()) {
Expand All @@ -211,17 +217,18 @@ public void writeServerConfFile(String file, Properties newServerConfigPropertie
writer.write(key + ":" + value + "\n");
}
} catch (IOException e) {
System.err.println("Failed to add the config.properties file to the server conf folder: " + e.getMessage());
log.error("Failed to add the config.properties file to the server conf folder: "
+ e.getMessage());
}
}

public Properties loadPropertiesFromFile(File file) {
private Properties loadPropertiesFromFile(File file) {
Properties properties = new Properties();
if (file.exists()) {
try (FileInputStream serverConfigFileReader = new FileInputStream(file)) {
properties.load(serverConfigFileReader);
} catch (IOException e) {
log.debug("Error occurred while loading properties from file:" + e.getMessage());
log.error("Error occurred while loading properties from file:" + e.getMessage());
}
}
return properties;
Expand All @@ -238,15 +245,15 @@ private String getValueOfKey(String key) {
return value;
}

public String getHome() {
private String getHome() {
String carbonHome = System.getProperty("carbon.home");
if (carbonHome == null || "".equals(carbonHome) || ".".equals(carbonHome)) {
carbonHome = getSystemDependentPath(new File(".").getAbsolutePath());
}
return carbonHome;
}

public String getSystemDependentPath(String path) {
private String getSystemDependentPath(String path) {
return path.replace(URL_SEPARATOR_CHAR, File.separatorChar);
}
}
34 changes: 21 additions & 13 deletions distribution/src/scripts/micro-integrator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,20 @@ export_env_file() {

# Read the .env file and export each variable to the environment
while IFS='=' read -r key value; do
# Ignore lines starting with '#' (comments) or empty lines
if [ ! "$key" =~ ^# ] && [ "$key" != "" ]; then
# Trim surrounding whitespace from key and value
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs)
# Export the key-value pair to the environment
export "$key=$value"
fi
# Ignore lines starting with '#' (comments) or empty lines
case "$key" in
\#*|"")
# Skip comments or empty lines
continue
;;
*)
# Trim surrounding whitespace from key and value
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs)
# Export the key-value pair to the environment
export "$key=$value"
;;
esac
done < "$file_path"

echo "Environment variables loaded from $file_path."
Expand Down Expand Up @@ -168,14 +174,16 @@ do
else
args="$args $c"
fi
echo "11111111111111111111111111 $c"
# Check if the argument starts with --env-file=
if [ "$c" = "--env-file=*" ]; then
echo "_____________________ env file __________"
# Extract the file path from the argument
case "$c" in
--env-file=*)
file_path="${c#--env-file=}"
export_env_file "$file_path"
fi
;;
*)
continue
;;
esac
done

if [ "$ARGUMENT" = "car" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -138,16 +135,6 @@ public synchronized void startServerUsingCarbonHome(String carbonHome, Map<Strin
}

cmdArray = mergePropertiesToCommandArray(parameters, cmdArray);
System.out.println("***********************_______________*****************************");
System.out.println(Arrays.toString(cmdArray));
System.out.println(commandDir.getPath());
Path filePath = Paths.get(cmdArray[1]);

// Read the file content into a string
String content = Files.readString(filePath, StandardCharsets.UTF_8);
// Print the file content
System.out.println(content);
System.out.println("****************************************************");
process = Runtime.getRuntime().exec(cmdArray, null, commandDir);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,20 @@ export_env_file() {

# Read the .env file and export each variable to the environment
while IFS='=' read -r key value; do
# Ignore lines starting with '#' (comments) or empty lines
if [ ! "$key" =~ ^# ] && [ "$key" != "" ]; then
# Trim surrounding whitespace from key and value
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs)
# Export the key-value pair to the environment
export "$key=$value"
fi
# Ignore lines starting with '#' (comments) or empty lines
case "$key" in
\#*|"")
# Skip comments or empty lines
continue
;;
*)
# Trim surrounding whitespace from key and value
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs)
# Export the key-value pair to the environment
export "$key=$value"
;;
esac
done < "$file_path"

echo "Environment variables loaded from $file_path."
Expand Down Expand Up @@ -186,11 +192,15 @@ do
args="$args $c"
fi
# Check if the argument starts with --env-file=
if [ "$c" == --env-file=* ]; then
# Extract the file path from the argument
case "$c" in
--env-file=*)
file_path="${c#--env-file=}"
export_env_file "$file_path"
fi
;;
*)
continue
;;
esac
done

if [ "$CMD" = "--debug" ]; then
Expand Down
Loading

0 comments on commit 063e423

Please sign in to comment.