Skip to content

Commit 903406c

Browse files
authored
Add mTLS to README (Azure#19393)
1 parent fef3026 commit 903406c

File tree

2 files changed

+128
-28
lines changed

2 files changed

+128
-28
lines changed

sdk/spring/azure-spring-boot-starter-keyvault-certificates/README.md

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,20 @@ This starter allows you to securely manage and tightly control your certificates
102102
![Copy secrets](resource/copy-secrets.png)
103103

104104
To use the starter for server side SSL, you will need to add the following to
105-
your `application.properties` (if the application is using Spring Cloud Config
105+
your `application.yml` (if the application is using Spring Cloud Config
106106
Server for its configuration add it to the `bootstrap.yml` of the application)
107107

108-
```
109-
azure.keyvault.uri=<the URI of the Azure Key Vault to use>
110-
azure.keyvault.tenant-id=<the ID of your Azure tenant>
111-
azure.keyvault.client-id=<the client ID with access to Azure Key Vault>
112-
azure.keyvault.client-secret=<the client secret associated wit the client ID>
113-
server.ssl.key-alias=<the name of the certificate in Azure Key Vault to use>
114-
server.ssl.key-store-type=AzureKeyVault
108+
```yaml
109+
azure:
110+
keyvault:
111+
uri: <the URI of the Azure Key Vault to use>
112+
tenant-id: <the ID of your Azure tenant>
113+
client-id: <the client ID with access to Azure Key Vault>
114+
client-secret: <the client secret associated wit the client ID>
115+
server:
116+
ssl:
117+
key-alias: <the name of the certificate in Azure Key Vault to use>
118+
key-store-type: AzureKeyVault
115119
```
116120
117121
Note: make sure the client ID has access to the Azure Key Vault to access
@@ -142,31 +146,52 @@ Follow the steps below to grant a client with access to Azure Key Vault to acces
142146
#### Using a managed identity
143147
144148
To use the starter for server side SSL, you will need to add the following to
145-
your `application.properties` (if the application is using Spring Cloud Config
149+
your `application.yml` (if the application is using Spring Cloud Config
146150
Server for its configuration add it to the `bootstrap.yml` of the application)
147151

148-
```
149-
azure.keyvault.uri=<the URI of the Azure Key Vault to use>
150-
server.ssl.key-alias=<the name of the certificate in Azure Key Vault to use>
151-
server.ssl.key-store-type=AzureKeyVault
152+
```yaml
153+
azure:
154+
keyvault:
155+
uri: <the URI of the Azure Key Vault to use>
156+
server:
157+
ssl:
158+
key-alias: <the name of the certificate in Azure Key Vault to use>
159+
key-store-type: AzureKeyVault
152160
```
153161

154162
Note: make sure the managed identity has access to the Azure Key Vault to access
155163
keys, secrets and certificates.
156164

165+
### Enable mutual SSL on the server side
166+
167+
Only some minor changes need to be done to the server side SSL example
168+
mentioned above.
169+
170+
The following additional application.yml need to be added:
171+
172+
```yaml
173+
server:
174+
ssl:
175+
client-auth: need
176+
trust-store-type: AzureKeyVault
177+
```
178+
179+
157180
### Client side SSL
158181

159182
#### Using a client ID and client secret
160183

161184
To use the starter for client side SSL, you will need to add the following to
162-
your `application.properties` (if the application is using Spring Cloud Config
185+
your `application.yml` (if the application is using Spring Cloud Config
163186
Server for its configuration add it to the `bootstrap.yml` of the application)
164187

165-
```
166-
azure.keyvault.uri=<the URI of the Azure Key Vault to use>
167-
azure.keyvault.tenant-id=<the ID of your Azure tenant>
168-
azure.keyvault.client-id=<the client ID with access to Azure Key Vault>
169-
azure.keyvault.client-secret=<the client secret associated wit the client ID>
188+
```yaml
189+
azure:
190+
keyvault:
191+
uri: <the URI of the Azure Key Vault to use>
192+
tenant-id: <the ID of your Azure tenant>
193+
client-id: <the client ID with access to Azure Key Vault>
194+
client-secret: <the client secret associated wit the client ID>
170195
```
171196

172197
Note: make sure the client ID has access to the Azure Key Vault to access
@@ -203,11 +228,13 @@ public RestTemplate restTemplate() throws Exception {
203228
#### Using a managed identity
204229

205230
To use the starter for client side SSL, you will need to add the following to
206-
your `application.properties` (if the application is using Spring Cloud Config
231+
your `application.yml` (if the application is using Spring Cloud Config
207232
Server for its configuration add it to the `bootstrap.yml` of the application)
208233

209-
```
210-
azure.keyvault.uri=<the URI of the Azure Key Vault to use>
234+
```yaml
235+
azure:
236+
keyvault:
237+
uri: <the URI of the Azure Key Vault to use>
211238
```
212239
Note: make sure the managed identity has access to the Azure Key Vault to access
213240
keys, secrets and certificates.
@@ -238,6 +265,37 @@ public RestTemplate restTemplate() throws Exception {
238265
}
239266
```
240267

268+
### Enable mutual SSL on the client side
269+
270+
Only some minor changes need to be done to the client side SSL example
271+
mentioned above.
272+
273+
1. The SSL context needs to take a ClientPrivateKeyStrategy
274+
275+
An example is show below:
276+
277+
<!-- embedme ../azure-spring-boot/src/samples/java/com/azure/spring/keyvault/KeyVaultMutualTlsOnTheClientSide.java#L27-L30 -->
278+
```java
279+
SSLContext sslContext = SSLContexts.custom()
280+
.loadKeyMaterial(ks, "".toCharArray(), new ClientPrivateKeyStrategy())
281+
.loadTrustMaterial(ks, new TrustSelfSignedStrategy())
282+
.build();
283+
```
284+
285+
2. A ClientPrivateKeyStrategy needs to be defined.
286+
287+
An example is show below:
288+
289+
<!-- embedme ../azure-spring-boot/src/samples/java/com/azure/spring/keyvault/KeyVaultMutualTlsOnTheClientSide.java#L32-L37 -->
290+
```java
291+
private static class ClientPrivateKeyStrategy implements PrivateKeyStrategy {
292+
@Override
293+
public String chooseAlias(Map<String, PrivateKeyDetails> map, Socket socket) {
294+
return "self-signed";
295+
}
296+
}
297+
```
298+
241299
### Configuring Spring Cloud Gateway
242300

243301
To configure Spring Cloud Gateway for outbound SSL you will need
@@ -291,6 +349,7 @@ certificate will be added with the alias of `mycert`.
291349
2. Certificates coming from Azure Key Vault take precedence over
292350
side-loaded certificates.
293351

352+
294353
### Testing the current version under development
295354

296355
If you want to test the current version under development you will have to
@@ -309,14 +368,17 @@ To build and install the starter use the following command line:
309368
Azure SDKs for Java offers a consistent logging story to help aid in troubleshooting application errors and expedite their resolution. The logs produced will capture the flow of an application before reaching the terminal state to help locate the root issue. View the [logging][logging] wiki for guidance about enabling logging.
310369

311370
### Enable Spring logging
312-
Spring allow all the supported logging systems to set logger levels set in the Spring Environment (for example, in application.properties) by using `logging.level.<logger-name>=<level>` where level is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. The root logger can be configured by using logging.level.root.
371+
Spring allow all the supported logging systems to set logger levels set in the Spring Environment (for example, in application.yml) by using `logging.level.<logger-name>=<level>` where level is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. The root logger can be configured by using logging.level.root.
313372

314-
The following example shows potential logging settings in `application.properties`:
373+
The following example shows potential logging settings in `application.yml`:
315374

316-
```properties
317-
logging.level.root=WARN
318-
logging.level.org.springframework.web=DEBUG
319-
logging.level.org.hibernate=ERROR
375+
```yaml
376+
logging:
377+
level:
378+
root: WARN
379+
org:
380+
springframework.web: DEBUG
381+
hibernate: ERROR
320382
```
321383

322384
For more information about setting logging in spring, please refer to the [official doc](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-logging).
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure.spring.keyvault;
4+
5+
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
6+
import org.apache.http.ssl.PrivateKeyDetails;
7+
import org.apache.http.ssl.PrivateKeyStrategy;
8+
import org.apache.http.ssl.SSLContexts;
9+
10+
import javax.net.ssl.SSLContext;
11+
import java.net.Socket;
12+
import java.security.KeyManagementException;
13+
import java.security.KeyStore;
14+
import java.security.KeyStoreException;
15+
import java.security.NoSuchAlgorithmException;
16+
import java.security.UnrecoverableKeyException;
17+
import java.util.Map;
18+
19+
public class KeyVaultMutualTlsOnTheClientSide {
20+
21+
KeyStore ks = KeyStore.getInstance("AzureKeyVault");
22+
23+
public KeyVaultMutualTlsOnTheClientSide() throws KeyStoreException,
24+
UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {
25+
}
26+
27+
SSLContext sslContext = SSLContexts.custom()
28+
.loadKeyMaterial(ks, "".toCharArray(), new ClientPrivateKeyStrategy())
29+
.loadTrustMaterial(ks, new TrustSelfSignedStrategy())
30+
.build();
31+
32+
private static class ClientPrivateKeyStrategy implements PrivateKeyStrategy {
33+
@Override
34+
public String chooseAlias(Map<String, PrivateKeyDetails> map, Socket socket) {
35+
return "self-signed";
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)