-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
KeyVaultSettingsClient.cs
208 lines (189 loc) · 10.4 KB
/
KeyVaultSettingsClient.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Security.KeyVault.Administration.Models;
namespace Azure.Security.KeyVault.Administration
{
/// <summary>
/// The KeyVaultSettingsClient provides synchronous and asynchronous methods to get and update Managed HSM settings.
/// </summary>
public class KeyVaultSettingsClient
{
private readonly ClientDiagnostics _diagnostics;
private readonly SettingsRestClient _restClient;
/// <summary>
/// Initializes a new instance of the <see cref="KeyVaultSettingsClient"/> class for the specified vault.
/// </summary>
/// <param name="vaultUri">A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" in the Azure portal. You should validate that this URI references a valid Managed HSM resource. See <see href="https://aka.ms/azsdk/blog/vault-uri"/> for details.</param>
/// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as DefaultAzureCredential.</param>
/// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null.</exception>
public KeyVaultSettingsClient(Uri vaultUri, TokenCredential credential)
: this(vaultUri, credential, null)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="KeyVaultSettingsClient"/> class for the specified vault.
/// </summary>
/// <param name="vaultUri">A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" in the Azure portal You should validate that this URI references a valid Managed HSM resource. See <see href="https://aka.ms/azsdk/blog/vault-uri"/> for details..</param>
/// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as DefaultAzureCredential.</param>
/// <param name="options"><see cref="KeyVaultAdministrationClientOptions"/> that allow to configure the management of the request sent to Key Vault.</param>
/// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null.</exception>
public KeyVaultSettingsClient(Uri vaultUri, TokenCredential credential, KeyVaultAdministrationClientOptions options)
{
Argument.AssertNotNull(vaultUri, nameof(vaultUri));
Argument.AssertNotNull(credential, nameof(credential));
VaultUri = vaultUri;
options ??= new KeyVaultAdministrationClientOptions();
string apiVersion = options.GetVersionString();
HttpPipeline pipeline = HttpPipelineBuilder.Build(options,
new ChallengeBasedAuthenticationPolicy(credential, options.DisableChallengeResourceVerification));
_diagnostics = new ClientDiagnostics(options);
_restClient = new SettingsRestClient(_diagnostics, pipeline, apiVersion);
}
/// <summary>
/// Initializes a new instance of the <see cref="KeyVaultBackupClient"/> class for mocking.
/// </summary>
protected KeyVaultSettingsClient() { }
/// <summary>
/// The vault Uri.
/// </summary>
/// <value></value>
public virtual Uri VaultUri { get; }
/// <summary>
/// Gets the specified account setting.
/// </summary>
/// <param name="name">The name of the account setting. Must be a valid settings option.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>A response containing the specified account setting.</returns>
/// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
public virtual Response<KeyVaultSetting> GetSetting(string name, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(name, nameof(name));
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultSettingsClient)}.{nameof(GetSetting)}");
scope.Start();
try
{
return _restClient.GetSetting(VaultUri.AbsoluteUri, name, cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Gets the specified account setting.
/// </summary>
/// <param name="name">The name of the account setting. Must be a valid settings option.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>A response containing the specified account setting.</returns>
/// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
public virtual async Task<Response<KeyVaultSetting>> GetSettingAsync(string name, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(name, nameof(name));
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultSettingsClient)}.{nameof(GetSetting)}");
scope.Start();
try
{
return await _restClient.GetSettingAsync(VaultUri.AbsoluteUri, name, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Gets all account settings.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An array of all account settings.</returns>
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
public virtual Response<GetSettingsResult> GetSettings(CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultSettingsClient)}.{nameof(GetSettings)}");
scope.Start();
try
{
return _restClient.GetSettings(VaultUri.AbsoluteUri, cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Gets all account settings.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An array of all account settings.</returns>
/// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
public virtual async Task<Response<GetSettingsResult>> GetSettingsAsync(CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultSettingsClient)}.{nameof(GetSettings)}");
scope.Start();
try
{
return await _restClient.GetSettingsAsync(VaultUri.AbsoluteUri, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Updates the named account setting.
/// </summary>
/// <param name="setting">A <see cref="KeyVaultSetting"/> to update. Must be a valid settings option.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns></returns>
[CallerShouldAudit(KeyVaultAdministrationClientOptions.CallerShouldAuditReason)]
public virtual Response<KeyVaultSetting> UpdateSetting(KeyVaultSetting setting, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(setting, nameof(setting));
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultSettingsClient)}.{nameof(UpdateSetting)}");
scope.Start();
try
{
return _restClient.UpdateSetting(VaultUri.AbsoluteUri, setting.Name, setting.Value.ToString(), cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Updates the named account setting.
/// </summary>
/// <param name="setting">A <see cref="KeyVaultSetting"/> to update. Must be a valid settings option.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns></returns>
[CallerShouldAudit(KeyVaultAdministrationClientOptions.CallerShouldAuditReason)]
public virtual async Task<Response<KeyVaultSetting>> UpdateSettingAsync(KeyVaultSetting setting, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(setting, nameof(setting));
using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultSettingsClient)}.{nameof(UpdateSetting)}");
scope.Start();
try
{
return await _restClient.UpdateSettingAsync(VaultUri.AbsoluteUri, setting.Name, setting.Value.ToString(), cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
}
}