-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathSecretsCustomizations.java
More file actions
327 lines (306 loc) · 20 KB
/
SecretsCustomizations.java
File metadata and controls
327 lines (306 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import com.azure.autorest.customization.Customization;
import com.azure.autorest.customization.Editor;
import com.azure.autorest.customization.LibraryCustomization;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.javadoc.Javadoc;
import org.slf4j.Logger;
import java.util.Arrays;
import static com.github.javaparser.javadoc.description.JavadocDescription.parseText;
/**
* Contains customizations for Azure Key Vault's Secrets code generation.
*/
public class SecretsCustomizations extends Customization {
@Override
public void customize(LibraryCustomization libraryCustomization, Logger logger) {
removeFiles(libraryCustomization.getRawEditor());
customizeServiceVersion(libraryCustomization);
customizeModuleInfo(libraryCustomization.getRawEditor());
customizePackageInfos(libraryCustomization.getRawEditor());
}
private static void removeFiles(Editor editor) {
editor.removeFile("src/main/java/com/azure/security/keyvault/secrets/KeyVaultServiceVersion.java");
editor.removeFile("src/main/java/com/azure/security/keyvault/secrets/SecretAsyncClient.java");
editor.removeFile("src/main/java/com/azure/security/keyvault/secrets/SecretClient.java");
editor.removeFile("src/main/java/com/azure/security/keyvault/secrets/SecretClientBuilder.java");
}
private static void customizeServiceVersion(LibraryCustomization customization) {
CompilationUnit compilationUnit = new CompilationUnit();
compilationUnit.addOrphanComment(new LineComment(" Copyright (c) Microsoft Corporation. All rights reserved."));
compilationUnit.addOrphanComment(new LineComment(" Licensed under the MIT License."));
compilationUnit.addOrphanComment(new LineComment(" Code generated by Microsoft (R) TypeSpec Code Generator."));
compilationUnit.setPackageDeclaration("com.azure.security.keyvault.secrets")
.addImport("com.azure.core.util.ServiceVersion");
EnumDeclaration enumDeclaration = compilationUnit.addEnum("SecretServiceVersion", Modifier.Keyword.PUBLIC)
.addImplementedType("ServiceVersion")
.setJavadocComment("The versions of Azure Key Vault Secrets supported by this client library.");
for (String version : Arrays.asList("7.0", "7.1", "7.2", "7.3", "7.4", "7.5", "7.6", "2025-07-01")) {
enumDeclaration.addEnumConstant("V" + version.replace('.', '_').replace('-', '_').toUpperCase())
.setJavadocComment("Service version {@code " + version + "}.")
.addArgument(new StringLiteralExpr(version));
}
enumDeclaration.addField("String", "version", Modifier.Keyword.PRIVATE, Modifier.Keyword.FINAL);
enumDeclaration.addConstructor().addParameter("String", "version")
.setBody(StaticJavaParser.parseBlock("{ this.version = version; }"));
enumDeclaration.addMethod("getVersion", Modifier.Keyword.PUBLIC)
.setType("String")
.setJavadocComment("{@inheritDoc}")
.addMarkerAnnotation("Override")
.setBody(StaticJavaParser.parseBlock("{ return this.version; }"));
enumDeclaration.addMethod("getLatest", Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC)
.setType("SecretServiceVersion")
.setJavadocComment(new Javadoc(parseText("Gets the latest service version supported by this client library."))
.addBlockTag("return", "The latest {@link SecretServiceVersion}."))
.setBody(StaticJavaParser.parseBlock("{ return V2025_07_01; }"));
customization.getRawEditor()
.addFile("src/main/java/com/azure/security/keyvault/secrets/SecretServiceVersion.java",
compilationUnit.toString());
String fileName = "src/main/java/com/azure/security/keyvault/secrets/implementation/SecretClientImpl.java";
String fileContent = customization.getRawEditor().getFileContent(fileName);
fileContent = fileContent.replace("KeyVaultServiceVersion", "SecretServiceVersion");
customization.getRawEditor().replaceFile(fileName, fileContent);
}
private static void customizeModuleInfo(Editor editor) {
editor.replaceFile("src/main/java/module-info.java", joinWithNewline(
"// Copyright (c) Microsoft Corporation. All rights reserved.",
"// Licensed under the MIT License.",
"",
"module com.azure.security.keyvault.secrets {",
" requires transitive com.azure.core;",
"",
" exports com.azure.security.keyvault.secrets;",
" exports com.azure.security.keyvault.secrets.models;",
"",
" opens com.azure.security.keyvault.secrets to com.azure.core;",
" opens com.azure.security.keyvault.secrets.models to com.azure.core;",
" opens com.azure.security.keyvault.secrets.implementation.models to com.azure.core;",
"}"));
}
private static void customizePackageInfos(Editor editor) {
editor.replaceFile("src/main/java/com/azure/security/keyvault/secrets/package-info.java", joinWithNewline(
"// Copyright (c) Microsoft Corporation. All rights reserved.",
"// Licensed under the MIT License.",
"",
"/**",
" * <!-- @formatter:off -->",
" * <p><a href=\"https://learn.microsoft.com/azure/key-vault/general/\">Azure Key Vault</a> is a cloud-based service",
" * provided by Microsoft Azure that allows users to store, manage, and access secrets, such as passwords, certificates,",
" * and other sensitive information, securely in the cloud. The service provides a centralized and secure location for",
" * storing secrets, which can be accessed by authorized applications and users with appropriate permissions.",
" * Azure Key Vault Secrets offers several key features, including:</p>",
" * <ul>",
" * <li>Secret management: It allows users to store, manage, and access secrets securely, and provides features such",
" * as versioning, backup, and restoration.</li>",
" * <li>Access control: It offers",
" * <a href = \"https://learn.microsoft.com/azure/key-vault/general/rbac-guide?tabs=azure-cli\">",
" * role-based access control (RBAC)</a> and enables users to grant specific permissions to access secrets to",
" * other users, applications, or services.</li>",
" * <li>Integration with other Azure services: Azure Key Vault Secrets can be integrated with other Azure services,",
" * such as Azure App Service, Azure Functions, and Azure Virtual Machines, to simplify the process of securing",
" * sensitive information.</li>",
" * <li>High availability and scalability: The service is designed to provide high availability and scalability,",
" * with the ability to handle large volumes of secrets and requests.</li>",
" * </ul>",
" *",
" * <p>The Azure Key Vault Secrets client library allows developers to interact with the Azure Key Vault service",
" * from their applications. The library provides a set of APIs that enable developers to securely store, manage, and",
" * retrieve secrets in a key vault, and supports operations such as creating, updating, deleting, and retrieving",
" * secrets.</p>",
" *",
" * <p><strong>Key Concepts:</strong></p>",
" *",
" * <p>What is a Secret Client?</p>",
" * <p>The secret client performs the interactions with the Azure Key Vault service for getting, setting, updating,",
" * deleting, and listing secrets and its versions. Asynchronous (SecretAsyncClient) and synchronous (SecretClient)",
" * clients exist in the SDK allowing for selection of a client based on an application's use case.",
" * Once you've initialized a secret, you can interact with the primary resource types in Key Vault.</p>",
" *",
" * <p>What is an Azure Key Vault Secret ?</p>",
" * <p>A secret is the fundamental resource within Azure Key Vault. From a developer's perspective, Key Vault APIs",
" * accept and return secret values as strings. In addition to the secret data, the following attributes may be",
" * specified:</p>",
" *",
" * <ol>",
" * <li>enabled: Specifies whether the secret data can be retrieved.</li>",
" * <li>notBefore: Identifies the time after which the secret will be active.</li>",
" * <li>expires: Identifies the expiration time on or after which the secret data should not be retrieved.</li>",
" * <li>created: Indicates when this version of the secret was created.</li>",
" * <li>updated: Indicates when this version of the secret was updated.</li>",
" * </ol>",
" *",
" * <h2>Getting Started</h2>",
" *",
" * <p>In order to interact with the Azure Key Vault service, you will need to create an instance of the",
" * {@link com.azure.security.keyvault.secrets.SecretClient} or",
" * {@link com.azure.security.keyvault.secrets.SecretAsyncClient} class, a vault url and a credential object.</p>",
" *",
" * <p>The examples shown in this document use a credential object named DefaultAzureCredential for authentication,",
" * which is appropriate for most scenarios, including local development and production environments. Additionally,",
" * we recommend using a",
" * <a href=\"https://learn.microsoft.com/azure/active-directory/managed-identities-azure-resources/\">",
" * managed identity</a> for authentication in production environments.",
" * You can find more information on different ways of authenticating and their corresponding credential types in the",
" * <a href=\"https://learn.microsoft.com/java/api/overview/azure/identity-readme?view=azure-java-stable\">",
" * Azure Identity documentation\"</a>.</p>",
" *",
" * <p><strong>Sample: Construct Synchronous Secret Client</strong></p>",
" *",
" * <p>The following code sample demonstrates the creation of a {@link com.azure.security.keyvault.secrets.SecretClient},",
" * using the {@link com.azure.security.keyvault.secrets.SecretClientBuilder} to configure it.</p>",
" *",
" * <!-- src_embed com.azure.security.keyvault.SecretClient.instantiation -->",
" * <pre>",
" * SecretClient secretClient = new SecretClientBuilder()",
" * .credential(new DefaultAzureCredentialBuilder().build())",
" * .vaultUrl("<your-key-vault-url>")",
" * .buildClient();",
" * </pre>",
" * <!-- end com.azure.security.keyvault.SecretClient.instantiation -->",
" *",
" * <p><strong>Sample: Construct Asynchronous Secret Client</strong></p>",
" *",
" * <p>The following code sample demonstrates the creation of a",
" * {@link com.azure.security.keyvault.secrets.SecretAsyncClient}, using the",
" * {@link com.azure.security.keyvault.secrets.SecretClientBuilder} to configure it.</p>",
" *",
" * <!-- src_embed com.azure.security.keyvault.secrets.SecretAsyncClient.instantiation -->",
" * <pre>",
" * SecretAsyncClient secretAsyncClient = new SecretClientBuilder()",
" * .credential(new DefaultAzureCredentialBuilder().build())",
" * .vaultUrl("<your-key-vault-url>")",
" * .buildAsyncClient();",
" * </pre>",
" * <!-- end com.azure.security.keyvault.secrets.SecretAsyncClient.instantiation -->",
" *",
" * <hr/>",
" *",
" * <h2>Create a Secret</h2>",
" * The {@link com.azure.security.keyvault.secrets.SecretClient} or",
" * {@link com.azure.security.keyvault.secrets.SecretAsyncClient} can be used to create a secret in the key vault.",
" *",
" * <p><strong>Synchronous Code Sample:</strong></p>",
" * <p>The following code sample demonstrates how to synchronously create and store a secret in the key vault,",
" * using the {@link com.azure.security.keyvault.secrets.SecretClient#setSecret(java.lang.String, java.lang.String)} API.",
" * </p>",
" *",
" * <!-- src_embed com.azure.security.keyvault.SecretClient.setSecret#string-string -->",
" * <pre>",
" * KeyVaultSecret secret = secretClient.setSecret("secretName", "secretValue");",
" * System.out.printf("Secret is created with name %s and value %s%n", secret.getName(), secret.getValue());",
" * </pre>",
" * <!-- end com.azure.security.keyvault.SecretClient.setSecret#string-string -->",
" *",
" * <p><strong>Asynchronous Code Sample:</strong></p>",
" * <p>The following code sample demonstrates how to asynchronously create and store a secret in the key vault,",
" * using the {@link com.azure.security.keyvault.secrets.SecretAsyncClient}.</p>",
" *",
" * <p><strong>Note:</strong> For the asynchronous sample, refer to",
" * {@link com.azure.security.keyvault.secrets.SecretAsyncClient}.</p>",
" *",
" * <hr/>",
" *",
" * <h2>Get a Secret</h2>",
" * The {@link com.azure.security.keyvault.secrets.SecretClient} or",
" * {@link com.azure.security.keyvault.secrets.SecretAsyncClient} can be used to retrieve a secret from the",
" * key vault.",
" *",
" * <p><strong>Synchronous Code Sample:</strong></p>",
" * <p>The following code sample demonstrates how to synchronously retrieve a previously stored secret from the",
" * key vault, using the {@link com.azure.security.keyvault.secrets.SecretClient#getSecret(java.lang.String)} API.</p>",
" *",
" * <!-- src_embed com.azure.security.keyvault.SecretClient.getSecret#string -->",
" * <pre>",
" * KeyVaultSecret secret = secretClient.getSecret("secretName");",
" * System.out.printf("Secret is returned with name %s and value %s%n",",
" * secret.getName(), secret.getValue());",
" * </pre>",
" * <!-- end com.azure.security.keyvault.SecretClient.getSecret#string -->",
" *",
" * <p><strong>Note:</strong> For the asynchronous sample, refer to",
" * {@link com.azure.security.keyvault.secrets.SecretAsyncClient}.</p>",
" *",
" * <hr/>",
" *",
" * <h2>Delete a Secret</h2>",
" * The {@link com.azure.security.keyvault.secrets.SecretClient} or",
" * {@link com.azure.security.keyvault.secrets.SecretAsyncClient} can be used to delete a secret from the",
" * key vault.",
" *",
" * <p><strong>Synchronous Code Sample:</strong></p>",
" * <p>The following code sample demonstrates how to synchronously delete a secret from the",
" * key vault, using the {@link com.azure.security.keyvault.secrets.SecretClient#beginDeleteSecret(java.lang.String)}",
" * API.",
" * </p>",
" *",
" * <!-- src_embed com.azure.security.keyvault.SecretClient.deleteSecret#String -->",
" * <pre>",
" * SyncPoller<DeletedSecret, Void> deleteSecretPoller = secretClient.beginDeleteSecret("secretName");",
" *",
" * // Deleted Secret is accessible as soon as polling begins.",
" * PollResponse<DeletedSecret> deleteSecretPollResponse = deleteSecretPoller.poll();",
" *",
" * // Deletion date only works for a SoftDelete-enabled Key Vault.",
" * System.out.println("Deleted Date %s" + deleteSecretPollResponse.getValue()",
" * .getDeletedOn().toString());",
" * System.out.printf("Deleted Secret's Recovery Id %s", deleteSecretPollResponse.getValue()",
" * .getRecoveryId());",
" *",
" * // Secret is being deleted on server.",
" * deleteSecretPoller.waitForCompletion();",
" * </pre>",
" * <!-- end com.azure.security.keyvault.SecretClient.deleteSecret#String -->",
" *",
" * <p><strong>Note:</strong> For the asynchronous sample, refer to",
" * {@link com.azure.security.keyvault.secrets.SecretAsyncClient}.</p>",
" *",
" * @see com.azure.security.keyvault.secrets.SecretClient",
" * @see com.azure.security.keyvault.secrets.SecretAsyncClient",
" * @see com.azure.security.keyvault.secrets.SecretClientBuilder",
" * @see com.azure.security.keyvault.secrets.models.KeyVaultSecret",
" */",
"package com.azure.security.keyvault.secrets;",
""));
editor.replaceFile("src/main/java/com/azure/security/keyvault/secrets/models/package-info.java",
joinWithNewline("// Copyright (c) Microsoft Corporation. All rights reserved.",
"// Licensed under the MIT License.",
"",
"/**",
" * <!-- @formatter:off -->",
" * Package containing the data models for Secrets clients. The Key Vault clients perform cryptographic key and vault",
" * operations against the Key Vault service.",
" */",
"package com.azure.security.keyvault.secrets.models;",
""));
editor.replaceFile("src/main/java/com/azure/security/keyvault/secrets/implementation/package-info.java",
joinWithNewline("// Copyright (c) Microsoft Corporation. All rights reserved.",
"// Licensed under the MIT License.",
"",
"/**",
" * <!-- @formatter:off -->",
" * Package containing the implementations for Secrets clients. The Key Vault clients perform cryptographic key",
" * operations and vault operations against the Key Vault service.",
" */",
"package com.azure.security.keyvault.secrets.implementation;",
""));
editor.replaceFile("src/main/java/com/azure/security/keyvault/secrets/implementation/models/package-info.java",
joinWithNewline("// Copyright (c) Microsoft Corporation. All rights reserved.",
"// Licensed under the MIT License.",
"",
"/**",
" * <!-- @formatter:off -->",
" * Package containing the implementation data models for Secrets clients. The Key Vault clients perform cryptographic",
" * key operations and vault operations against the Key Vault service.",
" */",
"package com.azure.security.keyvault.secrets.implementation.models;",
""));
}
private static String joinWithNewline(String... lines) {
return String.join("\n", lines);
}
}