This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackup_restore_sample.js
154 lines (118 loc) · 5.93 KB
/
backup_restore_sample.js
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
// --------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
// --------------------------------------------------------------------------
'use strict;'
const KeyVaultSampleBase = require('./key_vault_sample_base');
class BackupRestoreSample extends KeyVaultSampleBase {
async runSample() {
var self = this;
// Authenticate to Key Vault and set up our Key Vault Client and Management Client
await self._authenticate();
// Create two key vaults for sample purposes
self._firstVault = await self._createVault();
self._secondVault = await self._createVault();
// Run our individual backup and restore samples now that setup is complete
await self.backupRestoreKey();
await self.backupRestoreSecret();
await self.backupRestoreCertificate();
}
async backupRestoreKey() {
var self = this;
console.log('************************************');
console.log(' Key backup and restore sample. ');
console.log('************************************');
var keyName = self._getName('key');
const sourceVaultClient = self._getKeyClient(self._firstVault.properties.vaultUri);
var key = await sourceVaultClient.createKey(keyName, 'RSA');
console.log('Created key ' + keyName);
console.log('Backing up key.');
var keyBackup = await sourceVaultClient.backupKey(keyName);
console.log('Backed up key ' + keyName);
console.log('Restoring');
const targetVaultClient = self._getKeyClient(self._secondVault.properties.vaultUri);
var restored = await targetVaultClient.restoreKeyBackup(keyBackup)
console.log('Restored key ' + keyName);
var keys = await targetVaultClient.listPropertiesOfKeys();
console.log('Vault ' + self._secondVault.name + ' keys:');
for await (const keyProperties of keys) {
console.log(' kid: ' + keyProperties.kid);
}
}
async backupRestoreSecret() {
var self = this;
console.log('************************************');
console.log(' Secret backup and restore sample. ');
console.log('************************************');
var secretName = self._getName('secret');
const sourceVaultClient = self._getSecretClient(self._firstVault.properties.vaultUri);
var secret = await sourceVaultClient.setSecret(secretName, 'AValue');
console.log('Created secret: ' + secretName);
console.log(secret);
console.log('Backing up secret');
var secretBackup = await sourceVaultClient.backupSecret(secretName);
console.log('Backed up secret ' + secretName);
console.log('Restoring.');
const targetVaultClient = self._getSecretClient(self._secondVault.properties.vaultUri);
var restored = await targetVaultClient.restoreSecretBackup(secretBackup);
console.log('Restored secret ' + secretName);
var secrets = await targetVaultClient.listPropertiesOfSecrets();
console.log('Vault ' + self._secondVault.name + ' secrets:');
for await (const secretProperties of secrets) {
console.log(' Secret ID: ' + secretProperties.id);
}
}
async backupRestoreCertificate() {
var self = this;
var certPolicyOptions = {
'certificatePolicy': {
'keyProperties': {
'keySize': 4096,
'reuseKey': false
}
},
'issuerName': 'Self',
'subject': 'CN=www.contoso.com',
'x509CertificateProperties': {
'validityInMonths': 12
},
'certificateAttributes': {
'enabled': true
}
};
console.log('******************************************');
console.log(' Certificate backup and restore sample. ');
console.log('******************************************');
var certificateName = self._getName('certificate');
const sourceVaultClient = self._getCertificateClient(self._firstVault.properties.vaultUri);
console.log('Creating certificate: ' + certificateName);
var certificate = await sourceVaultClient.beginCreateCertificate(certificateName, certPolicyOptions);
await certificate.pollUntilDone();
console.log('Created certificate ' + certificateName);
var certOp = await sourceVaultClient.getCertificateOperation(certificateName);
// wait for cert to actually be created
while( certOp.status == 'inProgress' ) {
certOp = await sourceVaultClient.getCertificateOperation(certificateName);
await self._sleep(1000);
}
console.log('Backing up certificate.');
var certificateBackup = await sourceVaultClient.backupCertificate(certificateName);
console.log('Backed up certificate ' + certificateName);
console.log('Restoring.');
const targetVaultClient = self._getCertificateClient(self._secondVault.properties.vaultUri);
var restored = await targetVaultClient.restoreCertificateBackup(certificateBackup);
console.log(restored);
console.log('Restored certificate ' + certificateName);
var certificates = await targetVaultClient.listPropertiesOfCertificates();
console.log('Vault ' + self._secondVault.name + ' certificates:');
for await (const certificateProperties of certificates) {
console.log(' ID: ' + certificateProperties.id);
}
}
}
if (require.main === module) {
var backupRestoreSample = new BackupRestoreSample();
backupRestoreSample.runSample()
.catch( (err) => { console.log(err.stack); });
}