/
backupClient.ts
276 lines (255 loc) · 9.41 KB
/
backupClient.ts
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { PollerLike } from "@azure/core-lro";
import { KeyVaultClient } from "./generated/keyVaultClient";
import {
KeyVaultBackupClientOptions,
KeyVaultBackupResult,
KeyVaultBeginBackupOptions,
KeyVaultBeginRestoreOptions,
KeyVaultBeginSelectiveKeyRestoreOptions,
KeyVaultRestoreResult,
KeyVaultSelectiveKeyRestoreResult
} from "./backupClientModels";
import { LATEST_API_VERSION, SDK_VERSION, authenticationScopes } from "./constants";
import { logger } from "./log";
import { KeyVaultBackupPoller } from "./lro/backup/poller";
import { KeyVaultRestorePoller } from "./lro/restore/poller";
import { KeyVaultSelectiveKeyRestorePoller } from "./lro/selectiveKeyRestore/poller";
import { KeyVaultBackupOperationState } from "./lro/backup/operation";
import { KeyVaultRestoreOperationState } from "./lro/restore/operation";
import { KeyVaultAdminPollOperationState } from "./lro/keyVaultAdminPoller";
import { KeyVaultSelectiveKeyRestoreOperationState } from "./lro/selectiveKeyRestore/operation";
import { mappings } from "./mappings";
import { TokenCredential } from "@azure/core-auth";
import { bearerTokenAuthenticationPolicy } from "@azure/core-rest-pipeline";
import { createChallengeCallbacks } from "./challengeAuthenticationCallbacks";
export {
KeyVaultBackupOperationState,
KeyVaultRestoreOperationState,
KeyVaultSelectiveKeyRestoreOperationState,
KeyVaultAdminPollOperationState
};
/**
* The KeyVaultBackupClient provides methods to generate backups
* and restore backups of any given Azure Key Vault instance.
* This client supports generating full backups, selective restores of specific keys
* and full restores of Key Vault instances.
*/
export class KeyVaultBackupClient {
/**
* The base URL to the vault
*/
public readonly vaultUrl: string;
/**
* @internal
* A reference to the auto-generated Key Vault HTTP client.
*/
private readonly client: KeyVaultClient;
/**
* Creates an instance of the KeyVaultBackupClient.
*
* Example usage:
* ```ts
* import { KeyVaultBackupClient } from "@azure/keyvault-admin";
* import { DefaultAzureCredential } from "@azure/identity";
*
* let vaultUrl = `https://<MY KEY VAULT HERE>.vault.azure.net`;
* let credentials = new DefaultAzureCredential();
*
* let client = new KeyVaultBackupClient(vaultUrl, credentials);
* ```
* @param vaultUrl - the URL of the Key Vault. It should have this shape: `https://${your-key-vault-name}.vault.azure.net`
* @param credential - An object that implements the `TokenCredential` interface used to authenticate requests to the service. Use the \@azure/identity package to create a credential that suits your needs.
* @param options - options used to configure Key Vault API requests.
*/
constructor(
vaultUrl: string,
credential: TokenCredential,
options: KeyVaultBackupClientOptions = {}
) {
this.vaultUrl = vaultUrl;
const libInfo = `azsdk-js-keyvault-admin/${SDK_VERSION}`;
const userAgentOptions = options.userAgentOptions;
options.userAgentOptions = {
userAgentPrefix:
userAgentOptions && userAgentOptions.userAgentPrefix
? `${userAgentOptions.userAgentPrefix} ${libInfo}`
: libInfo
};
const apiVersion = options.serviceVersion || LATEST_API_VERSION;
const clientOptions = {
...options,
loggingOptions: {
logger: logger.info,
additionalAllowedHeaderNames: [
"x-ms-keyvault-region",
"x-ms-keyvault-network-info",
"x-ms-keyvault-service-version"
]
}
};
this.client = new KeyVaultClient(apiVersion, clientOptions);
this.client.pipeline.addPolicy(
bearerTokenAuthenticationPolicy({
credential,
scopes: authenticationScopes,
challengeCallbacks: createChallengeCallbacks()
})
);
}
/**
* Starts generating a backup of an Azure Key Vault on the specified Storage Blob account.
*
* This function returns a Long Running Operation poller that allows you to wait indefinitely until the Key Vault backup is generated.
*
* Example usage:
* ```ts
* const client = new KeyVaultBackupClient(url, credentials);
*
* const blobStorageUri = "<blob-storage-uri>"; // <Blob storage URL>/<folder name>
* const sasToken = "<sas-token>";
* const poller = await client.beginBackup(blobStorageUri, sasToken);
*
* // Serializing the poller
* //
* // const serialized = poller.toString();
* //
* // A new poller can be created with:
* //
* // await client.beginBackup(blobStorageUri, sasToken, { resumeFrom: serialized });
* //
*
* // Waiting until it's done
* const backupUri = await poller.pollUntilDone();
* console.log(backupUri);
* ```
* Starts a full backup operation.
* @param blobStorageUri - The URL of the blob storage resource, including the path to the container where the backup will end up being stored.
* @param sasToken - The SAS token.
* @param options - The optional parameters.
*/
public async beginBackup(
blobStorageUri: string,
sasToken: string,
options: KeyVaultBeginBackupOptions = {}
): Promise<PollerLike<KeyVaultBackupOperationState, KeyVaultBackupResult>> {
const poller = new KeyVaultBackupPoller({
blobStorageUri,
sasToken,
client: this.client,
vaultUrl: this.vaultUrl,
intervalInMs: options.intervalInMs,
resumeFrom: options.resumeFrom,
requestOptions: options
});
// This will initialize the poller's operation (the generation of the backup).
await poller.poll();
return poller;
}
/**
* Starts restoring all key materials using the SAS token pointing to a previously stored Azure Blob storage
* backup folder.
*
* This function returns a Long Running Operation poller that allows you to wait indefinitely until the Key Vault restore operation is complete.
*
* Example usage:
* ```ts
* const client = new KeyVaultBackupClient(url, credentials);
*
* const blobStorageUri = "<blob-storage-uri>"; // <Blob storage URL>/<folder name>
* const sasToken = "<sas-token>";
* const poller = await client.beginRestore(blobStorageUri, sasToken);
*
* // The poller can be serialized with:
* //
* // const serialized = poller.toString();
* //
* // A new poller can be created with:
* //
* // await client.beginRestore(blobStorageUri, sasToken, { resumeFrom: serialized });
* //
*
* // Waiting until it's done
* const backupUri = await poller.pollUntilDone();
* console.log(backupUri);
* ```
* Starts a full restore operation.
* @param folderUri - The URL of the blob storage resource where the previous successful full backup was stored.
* @param sasToken - The SAS token.
* @param options - The optional parameters.
*/
public async beginRestore(
folderUri: string,
sasToken: string,
options: KeyVaultBeginRestoreOptions = {}
): Promise<PollerLike<KeyVaultRestoreOperationState, KeyVaultRestoreResult>> {
const poller = new KeyVaultRestorePoller({
...mappings.folderUriParts(folderUri),
sasToken,
client: this.client,
vaultUrl: this.vaultUrl,
intervalInMs: options.intervalInMs,
resumeFrom: options.resumeFrom,
requestOptions: options
});
// This will initialize the poller's operation (the generation of the backup).
await poller.poll();
return poller;
}
/**
* Starts restoring all key versions of a given key using user supplied SAS token pointing to a previously
* stored Azure Blob storage backup folder.
*
* This function returns a Long Running Operation poller that allows you to wait indefinitely until the Key Vault selective restore is complete.
*
* Example usage:
* ```ts
* const client = new KeyVaultBackupClient(url, credentials);
*
* const blobStorageUri = "<blob-storage-uri>";
* const sasToken = "<sas-token>";
* const keyName = "<key-name>";
* const poller = await client.beginSelectiveKeyRestore(keyName, blobStorageUri, sasToken);
*
* // Serializing the poller
* //
* // const serialized = poller.toString();
* //
* // A new poller can be created with:
* //
* // await client.beginSelectiveKeyRestore(keyName, blobStorageUri, sasToken, { resumeFrom: serialized });
* //
*
* // Waiting until it's done
* await poller.pollUntilDone();
* ```
* Creates a new role assignment.
* @param keyName - The name of the key that wants to be restored.
* @param folderUri - The URL of the blob storage resource, with the folder name of the blob where the previous successful full backup was stored.
* @param sasToken - The SAS token.
* @param options - The optional parameters.
*/
public async beginSelectiveKeyRestore(
keyName: string,
folderUri: string,
sasToken: string,
options: KeyVaultBeginSelectiveKeyRestoreOptions = {}
): Promise<
PollerLike<KeyVaultSelectiveKeyRestoreOperationState, KeyVaultSelectiveKeyRestoreResult>
> {
const poller = new KeyVaultSelectiveKeyRestorePoller({
...mappings.folderUriParts(folderUri),
keyName,
sasToken,
client: this.client,
vaultUrl: this.vaultUrl,
intervalInMs: options.intervalInMs,
resumeFrom: options.resumeFrom,
requestOptions: options
});
// This will initialize the poller's operation (the generation of the backup).
await poller.poll();
return poller;
}
}