Skip to content

Commit 50a00e8

Browse files
Merge pull request #224608 from KarlErickson/karler-passwordless-edits
Edits to passwordless Java content
2 parents d73c232 + a1b0364 commit 50a00e8

File tree

3 files changed

+113
-43
lines changed

3 files changed

+113
-43
lines changed

articles/key-vault/certificates/quick-create-java.md

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Quickstart for Azure Key Vault Certificate client library - Java
33
description: Learn about the the Azure Key Vault Certificate client library for Java with the steps in this quickstart.
44
author: msmbaldwin
5-
ms.custom: devx-track-java, devx-track-azurecli, devx-track-azurepowershell, mode-api
5+
ms.custom: devx-track-java, devx-track-azurecli, devx-track-azurepowershell, mode-api, passwordless-java
66
ms.author: mbaldwin
77
ms.date: 11/14/2022
88
ms.service: key-vault
@@ -12,16 +12,18 @@ ms.devlang: java
1212
---
1313

1414
# Quickstart: Azure Key Vault Certificate client library for Java (Certificates)
15+
1516
Get started with the Azure Key Vault Certificate client library for Java. Follow the steps below to install the package and try out example code for basic tasks.
1617

1718
Additional resources:
1819

19-
* [Source code](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/keyvault/azure-security-keyvault-certificates)
20-
* [API reference documentation](https://azure.github.io/azure-sdk-for-java/keyvault.html)
21-
* [Product documentation](index.yml)
22-
* [Samples](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/keyvault/azure-security-keyvault-certificates/src/samples/java/com/azure/security/keyvault/certificates)
20+
- [Source code](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/keyvault/azure-security-keyvault-certificates)
21+
- [API reference documentation](https://azure.github.io/azure-sdk-for-java/keyvault.html)
22+
- [Product documentation](index.yml)
23+
- [Samples](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/keyvault/azure-security-keyvault-certificates/src/samples/java/com/azure/security/keyvault/certificates)
2324

2425
## Prerequisites
26+
2527
- An Azure subscription - [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
2628
- [Java Development Kit (JDK)](/java/azure/jdk/) version 8 or above
2729
- [Apache Maven](https://maven.apache.org)
@@ -30,23 +32,26 @@ Additional resources:
3032
This quickstart assumes you are running [Azure CLI](/cli/azure/install-azure-cli) and [Apache Maven](https://maven.apache.org) in a Linux terminal window.
3133

3234
## Setting up
35+
3336
This quickstart is using the Azure Identity library with Azure CLI to authenticate user to Azure Services. Developers can also use Visual Studio or Visual Studio Code to authenticate their calls, for more information, see [Authenticate the client with Azure Identity client library](/java/api/overview/azure/identity-readme).
3437

3538
### Sign in to Azure
39+
3640
1. Run the `login` command.
3741

38-
```azurecli-interactive
39-
az login
40-
```
42+
```azurecli-interactive
43+
az login
44+
```
4145

4246
If the CLI can open your default browser, it will do so and load an Azure sign-in page.
4347

4448
Otherwise, open a browser page at [https://aka.ms/devicelogin](https://aka.ms/devicelogin) and enter the
4549
authorization code displayed in your terminal.
4650

47-
2. Sign in with your account credentials in the browser.
51+
1. Sign in with your account credentials in the browser.
4852

4953
### Create a new Java console app
54+
5055
In a console window, use the `mvn` command to create a new Java console app with the name `akv-certificates-java`.
5156

5257
```console
@@ -88,6 +93,7 @@ cd akv-certificates-java
8893
```
8994

9095
### Install the package
96+
9197
Open the *pom.xml* file in your text editor. Add the following dependency elements to the group of dependencies.
9298

9399
```xml
@@ -105,39 +111,49 @@ Open the *pom.xml* file in your text editor. Add the following dependency elemen
105111
```
106112

107113
### Create a resource group and key vault
114+
108115
[!INCLUDE [Create a resource group and key vault](../../../includes/key-vault-rg-kv-creation.md)]
109116

110117
#### Grant access to your key vault
118+
111119
Create an access policy for your key vault that grants certificate permissions to your user account.
112120

113121
```azurecli
114122
az keyvault set-policy --name <your-key-vault-name> --upn [email protected] --certificate-permissions delete get list create purge
115123
```
116124

117125
#### Set environment variables
126+
118127
This application is using your key vault name as an environment variable called `KEY_VAULT_NAME`.
119128

120129
Windows
130+
121131
```cmd
122132
set KEY_VAULT_NAME=<your-key-vault-name>
123133
````
134+
124135
Windows PowerShell
136+
125137
```powershell
126138
$Env:KEY_VAULT_NAME="<your-key-vault-name>"
127139
```
128140

129141
macOS or Linux
142+
130143
```cmd
131144
export KEY_VAULT_NAME=<your-key-vault-name>
132145
```
133146

134147
## Object model
148+
135149
The Azure Key Vault Certificate client library for Java allows you to manage certificates. The [Code examples](#code-examples) section shows how to create a client, create a certificate, retrieve a certificate, and delete a certificate.
136150

137151
The entire console app is [below](#sample-code).
138152

139153
## Code examples
154+
140155
### Add directives
156+
141157
Add the following directives to the top of your code:
142158

143159
```java
@@ -154,9 +170,12 @@ import com.azure.security.keyvault.certificates.models.KeyVaultCertificateWithPo
154170
```
155171

156172
### Authenticate and create a client
157-
In this quickstart, a logged in user is used to authenticate to Key Vault, which is preferred method for local development. For applications deployed to Azure, a Managed Identity should be assigned to an App Service or Virtual Machine. For more information, see [Managed Identity Overview](../../active-directory/managed-identities-azure-resources/overview.md).
158173

159-
In the example below, the name of your key vault is expanded to the key vault URI, in the format "https://\<your-key-vault-name\>.vault.azure.net". This example is using the ['DefaultAzureCredential()'](/java/api/com.azure.identity.defaultazurecredential) class, which allows to use the same code across different environments with different options to provide identity. For more information, see [Default Azure Credential Authentication](/java/api/overview/azure/identity-readme).
174+
Application requests to most Azure services must be authorized. Using the [DefaultAzureCredential](/java/api/com.azure.identity.defaultazurecredential) is the recommended approach for implementing passwordless connections to Azure services in your code. `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code.
175+
176+
In this quickstart, `DefaultAzureCredential` authenticates to key vault using the credentials of the local development user logged into the Azure CLI. When the application is deployed to Azure, the same `DefaultAzureCredential` code can automatically discover and use a managed identity that is assigned to an App Service, Virtual Machine, or other services. For more information, see [Managed Identity Overview](/azure/active-directory/managed-identities-azure-resources/overview).
177+
178+
In this example, the name of your key vault is expanded to the key vault URI, in the format `https://<your-key-vault-name>.vault.azure.net`. For more information about authenticating to key vault, see [Developer's Guide](/azure/key-vault/general/developers-guide#authenticate-to-key-vault-in-code).
160179

161180
```java
162181
String keyVaultName = System.getenv("KEY_VAULT_NAME");
@@ -169,6 +188,7 @@ CertificateClient certificateClient = new CertificateClientBuilder()
169188
```
170189

171190
### Save a secret
191+
172192
Now that your application is authenticated, you can create a certificate in your key vault using the `certificateClient.beginCreateCertificate` method. This requires a name for the certificate and a certificate policy -- we've assigned the value "myCertificate" to the `certificateName` variable in this sample and use a default policy.
173193

174194
Certificate creation is a long running operation, for which you can poll its progress or wait for it to complete.
@@ -186,6 +206,7 @@ KeyVaultCertificate createdCertificate = certificatePoller.getFinalResult();
186206
```
187207

188208
### Retrieve a certificate
209+
189210
You can now retrieve the previously created certificate with the `certificateClient.getCertificate` method.
190211

191212
```java
@@ -195,6 +216,7 @@ KeyVaultCertificate retrievedCertificate = certificateClient.getCertificate(cert
195216
You can now access the details of the retrieved certificate with operations like `retrievedCertificate.getName`, `retrievedCertificate.getProperties`, etc. As well as its contents `retrievedCertificate.getCer`.
196217

197218
### Delete a certificate
219+
198220
Finally, let's delete the certificate from your key vault with the `certificateClient.beginDeleteCertificate` method, which is also a long running operation.
199221

200222
```java
@@ -203,6 +225,7 @@ deletionPoller.waitForCompletion();
203225
```
204226

205227
## Clean up resources
228+
206229
When no longer needed, you can use the Azure CLI or Azure PowerShell to remove your key vault and the corresponding resource group.
207230

208231
```azurecli
@@ -214,6 +237,7 @@ Remove-AzResourceGroup -Name "myResourceGroup"
214237
```
215238

216239
## Sample code
240+
217241
```java
218242
package com.keyvault.certificates.quickstart;
219243

@@ -236,16 +260,16 @@ public class App {
236260
System.out.printf("key vault name = %s and kv uri = %s \n", keyVaultName, keyVaultUri);
237261

238262
CertificateClient certificateClient = new CertificateClientBuilder()
239-
.vaultUrl(keyVaultUri)
240-
.credential(new DefaultAzureCredentialBuilder().build())
241-
.buildClient();
263+
.vaultUrl(keyVaultUri)
264+
.credential(new DefaultAzureCredentialBuilder().build())
265+
.buildClient();
242266

243267
String certificateName = "myCertificate";
244268

245269
System.out.print("Creating a certificate in " + keyVaultName + " called '" + certificateName + " ... ");
246270

247271
SyncPoller<CertificateOperation, KeyVaultCertificateWithPolicy> certificatePoller =
248-
certificateClient.beginCreateCertificate(certificateName, CertificatePolicy.getDefault());
272+
certificateClient.beginCreateCertificate(certificateName, CertificatePolicy.getDefault());
249273
certificatePoller.waitForCompletion();
250274

251275
System.out.print("done.");
@@ -265,6 +289,7 @@ public class App {
265289
```
266290

267291
## Next steps
292+
268293
In this quickstart you created a key vault, created a certificate, retrieved it, and then deleted it. To learn more about Key Vault and how to integrate it with your applications, continue on to the articles below.
269294

270295
- Read an [Overview of Azure Key Vault](../general/overview.md)

articles/key-vault/keys/quick-create-java.md

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Quickstart - Azure Key Vault Key client library for Java
33
description: Provides a quickstart for the Azure Key Vault Keys client library for Java.
44
author: msmbaldwin
5-
ms.custom: devx-track-java, devx-track-azurecli, devx-track-azurepowershell, mode-api
5+
ms.custom: devx-track-java, devx-track-azurecli, devx-track-azurepowershell, mode-api, passwordless-java
66
ms.author: mbaldwin
77
ms.date: 01/04/2023
88
ms.service: key-vault
@@ -17,12 +17,13 @@ Get started with the Azure Key Vault Key client library for Java. Follow these s
1717

1818
Additional resources:
1919

20-
* [Source code](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/keyvault/azure-security-keyvault-keys)
21-
* [API reference documentation](https://azure.github.io/azure-sdk-for-java/keyvault.html)
22-
* [Product documentation](index.yml)
23-
* [Samples](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/keyvault/azure-security-keyvault-keys/src/samples/java/com/azure/security/keyvault/keys)
20+
- [Source code](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/keyvault/azure-security-keyvault-keys)
21+
- [API reference documentation](https://azure.github.io/azure-sdk-for-java/keyvault.html)
22+
- [Product documentation](index.yml)
23+
- [Samples](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/keyvault/azure-security-keyvault-keys/src/samples/java/com/azure/security/keyvault/keys)
2424

2525
## Prerequisites
26+
2627
- An Azure subscription - [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
2728
- [Java Development Kit (JDK)](/java/azure/jdk/) version 8 or above
2829
- [Apache Maven](https://maven.apache.org)
@@ -31,22 +32,25 @@ Additional resources:
3132
This quickstart assumes you're running [Azure CLI](/cli/azure/install-azure-cli) and [Apache Maven](https://maven.apache.org) in a Linux terminal window.
3233

3334
## Setting up
35+
3436
This quickstart is using the Azure Identity library with Azure CLI to authenticate user to Azure Services. Developers can also use Visual Studio or Visual Studio Code to authenticate their calls, for more information, see [Authenticate the client with Azure Identity client library](/java/api/overview/azure/identity-readme).
3537

3638
### Sign in to Azure
39+
3740
1. Run the `login` command.
3841

39-
```azurecli-interactive
40-
az login
41-
```
42+
```azurecli-interactive
43+
az login
44+
```
4245

4346
If the CLI can open your default browser, it will do so and load an Azure sign-in page.
4447

4548
Otherwise, open a browser page at [https://aka.ms/devicelogin](https://aka.ms/devicelogin) and enter the authorization code displayed in your terminal.
4649

47-
2. Sign in with your account credentials in the browser.
50+
1. Sign in with your account credentials in the browser.
4851

4952
### Create a new Java console app
53+
5054
In a console window, use the `mvn` command to create a new Java console app with the name `akv-keys-java`.
5155

5256
```console
@@ -88,6 +92,7 @@ cd akv-keys-java
8892
```
8993

9094
### Install the package
95+
9196
Open the *pom.xml* file in your text editor. Add the following dependency elements to the group of dependencies.
9297

9398
```xml
@@ -105,39 +110,49 @@ Open the *pom.xml* file in your text editor. Add the following dependency elemen
105110
```
106111

107112
### Create a resource group and key vault
113+
108114
[!INCLUDE [Create a resource group and key vault](../../../includes/key-vault-rg-kv-creation.md)]
109115

110116
#### Grant access to your key vault
117+
111118
Create an access policy for your key vault that grants key permissions to your user account.
112119

113120
```azurecli
114121
az keyvault set-policy --name <your-key-vault-name> --upn [email protected] --key-permissions delete get list create purge
115122
```
116123

117124
#### Set environment variables
125+
118126
This application is using your key vault name as an environment variable called `KEY_VAULT_NAME`.
119127

120128
Windows
129+
121130
```cmd
122131
set KEY_VAULT_NAME=<your-key-vault-name>
123132
````
133+
124134
Windows PowerShell
135+
125136
```powershell
126137
$Env:KEY_VAULT_NAME="<your-key-vault-name>"
127138
```
128139

129140
macOS or Linux
141+
130142
```cmd
131143
export KEY_VAULT_NAME=<your-key-vault-name>
132144
```
133145

134146
## Object model
147+
135148
The Azure Key Vault Key client library for Java allows you to manage keys. The [Code examples](#code-examples) section shows how to create a client, create a key, retrieve a key, and delete a key.
136149

137150
The entire console app is supplied in [Sample code](#sample-code).
138151

139152
## Code examples
153+
140154
### Add directives
155+
141156
Add the following directives to the top of your code:
142157

143158
```java
@@ -153,9 +168,11 @@ import com.azure.security.keyvault.keys.models.KeyVaultKey;
153168

154169
### Authenticate and create a client
155170

156-
In this quickstart, a logged in user is used to authenticate to Key Vault, which is preferred method for local development. For applications deployed to Azure, a Managed Identity should be assigned to an App Service or Virtual Machine. For more information, see [Managed Identity Overview](../../active-directory/managed-identities-azure-resources/overview.md).
171+
Application requests to most Azure services must be authorized. Using the [DefaultAzureCredential](/java/api/com.azure.identity.defaultazurecredential) class is the recommended approach for implementing passwordless connections to Azure services in your code. `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code.
172+
173+
In this quickstart, `DefaultAzureCredential` authenticates to key vault using the credentials of the local development user logged into the Azure CLI. When the application is deployed to Azure, the same `DefaultAzureCredential` code can automatically discover and use a managed identity that is assigned to an App Service, Virtual Machine, or other services. For more information, see [Managed Identity Overview](/azure/active-directory/managed-identities-azure-resources/overview).
157174

158-
In this example, the name of your key vault is expanded to the key vault URI, in the format `https://<your-key-vault-name>.vault.azure.net`. This example is using the ['DefaultAzureCredential()'](/java/api/com.azure.identity.defaultazurecredential) class, which allows to use the same code across different environments with different options to provide identity. For more information, see [Default Azure Credential Authentication](/java/api/overview/azure/identity-readme).
175+
In this example, the name of your key vault is expanded to the key vault URI, in the format `https://<your-key-vault-name>.vault.azure.net`. For more information about authenticating to key vault, see [Developer's Guide](/azure/key-vault/general/developers-guide#authenticate-to-key-vault-in-code).
159176

160177
```java
161178
String keyVaultName = System.getenv("KEY_VAULT_NAME");
@@ -168,6 +185,7 @@ KeyClient keyClient = new KeyClientBuilder()
168185
```
169186

170187
### Create a key
188+
171189
Now that your application is authenticated, you can create a key in your key vault using the `keyClient.createKey` method. This requires a name for the key and a key type. We've assigned the value "myKey" to the `keyName` variable and use a an RSA `KeyType` in this sample.
172190

173191
```java
@@ -181,6 +199,7 @@ az keyvault key show --vault-name <your-unique-key-vault-name> --name myKey
181199
```
182200

183201
### Retrieve a key
202+
184203
You can now retrieve the previously created key with the `keyClient.getKey` method.
185204

186205
```java
@@ -190,6 +209,7 @@ KeyVaultKey retrievedKey = keyClient.getKey(keyName);
190209
You can now access the details of the retrieved key with operations like `retrievedKey.getProperties`, `retrievedKey.getKeyOperations`, etc.
191210

192211
### Delete a key
212+
193213
Finally, let's delete the key from your key vault with the `keyClient.beginDeleteKey` method.
194214

195215
Key deletion is a long running operation, for which you can poll its progress or wait for it to complete.
@@ -206,6 +226,7 @@ az keyvault key show --vault-name <your-unique-key-vault-name> --name myKey
206226
```
207227

208228
## Clean up resources
229+
209230
When no longer needed, you can use the Azure CLI or Azure PowerShell to remove your key vault and the corresponding resource group.
210231

211232
```azurecli
@@ -217,6 +238,7 @@ Remove-AzResourceGroup -Name "myResourceGroup"
217238
```
218239

219240
## Sample code
241+
220242
```java
221243
package com.keyvault.keys.quickstart;
222244

@@ -237,9 +259,9 @@ public class App {
237259
System.out.printf("key vault name = %s and key vault URI = %s \n", keyVaultName, keyVaultUri);
238260

239261
KeyClient keyClient = new KeyClientBuilder()
240-
.vaultUrl(keyVaultUri)
241-
.credential(new DefaultAzureCredentialBuilder().build())
242-
.buildClient();
262+
.vaultUrl(keyVaultUri)
263+
.credential(new DefaultAzureCredentialBuilder().build())
264+
.buildClient();
243265

244266
String keyName = "myKey";
245267

@@ -264,6 +286,7 @@ public class App {
264286
```
265287

266288
## Next steps
289+
267290
In this quickstart, you created a key vault, created a key, retrieved it, and then deleted it. To learn more about Key Vault and how to integrate it with your applications, continue on to these articles.
268291

269292
- Read an [Overview of Azure Key Vault](../general/overview.md)

0 commit comments

Comments
 (0)