Skip to content
Draft
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ build/
.vscode/

/logs/

# Maven shade plugin generated files
dependency-reduced-pom.xml
64 changes: 63 additions & 1 deletion mi-sql-public-demo/README.md
Original file line number Diff line number Diff line change
@@ -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=<your_managed_identity_client_id>
```

**Important**: Replace `<your_managed_identity_client_id>` 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

Expand Down
32 changes: 32 additions & 0 deletions mi-sql-public-demo/src/main/java/com/example/MainSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}


}