diff --git a/.gitignore b/.gitignore index cd4f7a7..ba75d8d 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,6 @@ build/ .vscode/ /logs/ + +# Maven shade plugin generated files +dependency-reduced-pom.xml diff --git a/mi-sql-public-demo/README.md b/mi-sql-public-demo/README.md index 33aaf39..0331eae 100644 --- a/mi-sql-public-demo/README.md +++ b/mi-sql-public-demo/README.md @@ -1,4 +1,66 @@ -# sqldbmi +# Azure SQL Database with Managed Identity - Demo Application + +This Java application demonstrates how to connect to Azure SQL Database using Azure Managed Identity authentication instead of traditional username/password authentication. + +## Prerequisites + +1. **Azure SQL Database**: An Azure SQL Database server and database configured for Managed Identity +2. **Managed Identity**: An Azure User Assigned Managed Identity or System Assigned Managed Identity +3. **Environment Variables**: Properly configured environment variables (see Configuration section) + +## Configuration + +### Environment Variables + +Set the following environment variable before running the application: + +```bash +export AZ_DATABASE_SERVER_NAME=your-sql-server-name +``` + +### Application Properties + +The application is pre-configured in `src/main/resources/application.properties` with: + +```properties +# Azure SQL Database configuration with Managed Identity +spring.datasource.url=jdbc:sqlserver://${AZ_DATABASE_SERVER_NAME}.database.windows.net:1433;database=demo;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;authentication=ActiveDirectoryMSI + +# Azure Managed Identity configuration +spring.cloud.azure.credential.managed-identity-enabled=true +spring.cloud.azure.credential.client-id= +``` + +**Important**: Replace `` with your actual Managed Identity client ID. + +## Building and Running + +### Build the application: + +```bash +mvn clean package +``` + +### Run the application: + +```bash +export AZ_DATABASE_SERVER_NAME=your-sql-server-name +java -jar target/demo-1.0-SNAPSHOT.jar +``` + +## Features + +- **Secure Authentication**: Uses Azure Managed Identity instead of passwords +- **Environment Variable Support**: Dynamically substitutes `${AZ_DATABASE_SERVER_NAME}` from environment +- **Modern Spring Configuration**: Leverages Spring Cloud Azure for seamless integration +- **Error Handling**: Provides clear error messages for missing configuration + +## Azure Setup Requirements + +1. **Create Azure SQL Database** with Managed Identity authentication enabled +2. **Create Managed Identity** (User Assigned or use System Assigned) +3. **Grant Database Access** to the Managed Identity +4. **Deploy Application** to Azure service that supports Managed Identity (App Service, Container Apps, etc.) ## Documentation 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 bedc095..c4116f5 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 @@ -34,6 +34,9 @@ public static void main(String[] args) { return; } + // Substitute environment variables in connection string + connString = substituteEnvironmentVariables(connString); + System.out.println("Connection string: " + connString); SQLServerDataSource ds = new SQLServerDataSource(); @@ -45,5 +48,34 @@ public static void main(String[] args) { } } + /** + * Substitutes environment variables in the format ${VARIABLE_NAME} with their actual values. + * @param input the string containing environment variable placeholders + * @return the string with environment variables substituted + */ + private static String substituteEnvironmentVariables(String input) { + if (input == null) { + return null; + } + + String result = input; + // Pattern to match ${VARIABLE_NAME} + java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("\\$\\{([^}]+)\\}"); + java.util.regex.Matcher matcher = pattern.matcher(input); + + while (matcher.find()) { + String envVarName = matcher.group(1); + String envVarValue = System.getenv(envVarName); + + if (envVarValue != null) { + result = result.replace(matcher.group(0), envVarValue); + } else { + System.err.println("Warning: Environment variable " + envVarName + " is not set"); + } + } + + return result; + } + } \ No newline at end of file