diff --git a/.gitignore b/.gitignore index cd4f7a7..7cedd0c 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,6 @@ build/ .vscode/ /logs/ + +# Maven shade plugin artifacts +dependency-reduced-pom.xml diff --git a/mi-sql-public-demo/src/main/java/com/example/MainSQL.java b/mi-sql-public-demo/src/main/java/com/example/MainSQL.java index 5a8f9a5..45f7af6 100644 --- a/mi-sql-public-demo/src/main/java/com/example/MainSQL.java +++ b/mi-sql-public-demo/src/main/java/com/example/MainSQL.java @@ -30,6 +30,10 @@ public static void main(String[] args) { String connString = properties.getProperty("AZURE_SQLDB_CONNECTIONSTRING"); String clientId = properties.getProperty("AZURE_CLIENT_ID"); + // Resolve environment variables in the connection string + connString = resolveEnvironmentVariables(connString); + clientId = resolveEnvironmentVariables(clientId); + connString = connString + ";msiClientId=" + clientId + ";authentication=ActiveDirectoryMSI"; System.out.print(connString); @@ -42,5 +46,29 @@ public static void main(String[] args) { } } + /** + * Resolves environment variables in the format ${VAR_NAME} with their actual values + */ + private static String resolveEnvironmentVariables(String input) { + if (input == null) { + return null; + } + + String result = input; + // Find all environment variable references like ${VAR_NAME} + java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("\\$\\{([^}]+)\\}"); + java.util.regex.Matcher matcher = pattern.matcher(result); + + while (matcher.find()) { + String varName = matcher.group(1); + String envValue = System.getenv(varName); + if (envValue != null) { + result = result.replace("${" + varName + "}", envValue); + } + } + + return result; + } + } \ No newline at end of file diff --git a/mi-sql-public-demo/src/main/resources/application.properties b/mi-sql-public-demo/src/main/resources/application.properties index ed7d015..d4813cc 100644 --- a/mi-sql-public-demo/src/main/resources/application.properties +++ b/mi-sql-public-demo/src/main/resources/application.properties @@ -1,5 +1,5 @@ AZURE_SQLDB_CONNECTIONSTRING=jdbc:sqlserver://${AZ_DATABASE_SERVER_NAME}.database.windows.net:1433;database=demo;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30; -AZURE_CLIENT_ID= +AZURE_CLIENT_ID=${AZURE_CLIENT_ID} # Enable Azure managed identity for Spring Cloud Azure spring.cloud.azure.credential.managed-identity-enabled=true