| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Core; |
| | 8 | | using Azure.Core.Pipeline; |
| | 9 | | using Azure.Security.KeyVault.Administration.Models; |
| | 10 | |
|
| | 11 | | namespace Azure.Security.KeyVault.Administration |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// The KeyVaultAccessControlClient provides synchronous and asynchronous methods to view and manage Role Based Acce |
| | 15 | | /// The client supports creating, listing, updating, and deleting <see cref="RoleAssignment"/>. |
| | 16 | | /// The client also supports listing <see cref="RoleDefinition" />. |
| | 17 | | /// </summary> |
| | 18 | | public class KeyVaultAccessControlClient |
| | 19 | | { |
| | 20 | | private readonly ClientDiagnostics _diagnostics; |
| | 21 | | private readonly RoleDefinitionsRestClient _definitionsRestClient; |
| | 22 | | private readonly RoleAssignmentsRestClient _assignmentsRestClient; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// The vault Uri. |
| | 26 | | /// </summary> |
| | 27 | | /// <value></value> |
| 88 | 28 | | public virtual Uri VaultUri { get; } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Initializes a new instance of the <see cref="KeyVaultAccessControlClient"/> class for mocking. |
| | 32 | | /// </summary> |
| 38 | 33 | | protected KeyVaultAccessControlClient() |
| 38 | 34 | | { } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Initializes a new instance of the <see cref="KeyVaultAccessControlClient"/> class for the specified vault. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="vaultUri">A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" |
| | 40 | | /// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as |
| | 41 | | /// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null |
| | 42 | | public KeyVaultAccessControlClient(Uri vaultUri, TokenCredential credential) |
| 4 | 43 | | : this(vaultUri, credential, null) |
| | 44 | | { |
| | 45 | |
|
| 4 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Initializes a new instance of the <see cref="KeyVaultAccessControlClient"/> class for the specified vault. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="vaultUri">A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" |
| | 52 | | /// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as |
| | 53 | | /// <param name="options"><see cref="KeyVaultAccessControlClientOptions"/> that allow to configure the managemen |
| | 54 | | /// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null |
| 42 | 55 | | public KeyVaultAccessControlClient(Uri vaultUri, TokenCredential credential, KeyVaultAccessControlClientOptions |
| | 56 | | { |
| 42 | 57 | | Argument.AssertNotNull(vaultUri, nameof(vaultUri)); |
| 42 | 58 | | Argument.AssertNotNull(credential, nameof(credential)); |
| | 59 | |
|
| 42 | 60 | | VaultUri = vaultUri; |
| | 61 | |
|
| 42 | 62 | | options ??= new KeyVaultAccessControlClientOptions(); |
| 42 | 63 | | string apiVersion = options.GetVersionString(); |
| | 64 | |
|
| 42 | 65 | | HttpPipeline pipeline = HttpPipelineBuilder.Build(options, |
| 42 | 66 | | new ChallengeBasedAuthenticationPolicy(credential)); |
| | 67 | |
|
| 42 | 68 | | _diagnostics = new ClientDiagnostics(options); |
| 42 | 69 | | _definitionsRestClient = new RoleDefinitionsRestClient(_diagnostics, pipeline, apiVersion); |
| 42 | 70 | | _assignmentsRestClient = new RoleAssignmentsRestClient(_diagnostics, pipeline, apiVersion); |
| 42 | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Get all role definitions that are applicable at scope and above. |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="roleScope"> The scope of the role assignments. </param> |
| | 77 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 78 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 79 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> is null.</exception> |
| | 80 | | public virtual Pageable<RoleDefinition> GetRoleDefinitions(RoleAssignmentScope roleScope, CancellationToken canc |
| | 81 | | { |
| 18 | 82 | | return PageableHelpers.CreateEnumerable(_ => |
| 18 | 83 | | { |
| 36 | 84 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G |
| 36 | 85 | | scope.Start(); |
| 18 | 86 | | try |
| 18 | 87 | | { |
| 36 | 88 | | var response = _definitionsRestClient.List(vaultBaseUrl: VaultUri.AbsoluteUri, scope: roleScope.ToSt |
| 36 | 89 | | return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()); |
| 18 | 90 | | } |
| 0 | 91 | | catch (Exception ex) |
| 18 | 92 | | { |
| 0 | 93 | | scope.Failed(ex); |
| 0 | 94 | | throw; |
| 18 | 95 | | } |
| 36 | 96 | | }, (nextLink, _) => |
| 18 | 97 | | { |
| 0 | 98 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G |
| 0 | 99 | | scope.Start(); |
| 18 | 100 | | try |
| 18 | 101 | | { |
| 0 | 102 | | var response = _definitionsRestClient.ListNextPage(nextLink: nextLink, vaultBaseUrl: VaultUri.Absolu |
| 0 | 103 | | return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()); |
| 18 | 104 | | } |
| 0 | 105 | | catch (Exception ex) |
| 18 | 106 | | { |
| 0 | 107 | | scope.Failed(ex); |
| 0 | 108 | | throw; |
| 18 | 109 | | } |
| 0 | 110 | | }); |
| | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Get all role definitions that are applicable at scope and above. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="roleScope"> The scope of the role definition. </param> |
| | 117 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 118 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 119 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> is null.</exception> |
| | 120 | | public virtual AsyncPageable<RoleDefinition> GetRoleDefinitionsAsync(RoleAssignmentScope roleScope, Cancellation |
| | 121 | | { |
| 16 | 122 | | return PageableHelpers.CreateAsyncEnumerable(async _ => |
| 16 | 123 | | { |
| 32 | 124 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G |
| 32 | 125 | | scope.Start(); |
| 16 | 126 | | try |
| 16 | 127 | | { |
| 32 | 128 | | var response = await _definitionsRestClient.ListAsync(vaultBaseUrl: VaultUri.AbsoluteUri, scope: rol |
| 32 | 129 | | .ConfigureAwait(false); |
| 32 | 130 | | return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()); |
| 16 | 131 | | } |
| 0 | 132 | | catch (Exception ex) |
| 16 | 133 | | { |
| 0 | 134 | | scope.Failed(ex); |
| 0 | 135 | | throw; |
| 16 | 136 | | } |
| 32 | 137 | | }, async (nextLink, _) => |
| 16 | 138 | | { |
| 0 | 139 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G |
| 0 | 140 | | scope.Start(); |
| 16 | 141 | | try |
| 16 | 142 | | { |
| 0 | 143 | | var response = await _definitionsRestClient.ListNextPageAsync(nextLink: nextLink, vaultBaseUrl: Vaul |
| 0 | 144 | | .ConfigureAwait(false); |
| 0 | 145 | | return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()); |
| 16 | 146 | | } |
| 0 | 147 | | catch (Exception ex) |
| 16 | 148 | | { |
| 0 | 149 | | scope.Failed(ex); |
| 0 | 150 | | throw; |
| 16 | 151 | | } |
| 0 | 152 | | }); |
| | 153 | | } |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// Gets the <see cref="RoleAssignment"/>s for a scope. |
| | 157 | | /// </summary> |
| | 158 | | /// <param name="roleScope"> The scope of the role assignments. </param> |
| | 159 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 160 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 161 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> is null.</exception> |
| | 162 | | public virtual Pageable<RoleAssignment> GetRoleAssignments(RoleAssignmentScope roleScope, CancellationToken canc |
| | 163 | | { |
| 4 | 164 | | return PageableHelpers.CreateEnumerable(_ => |
| 4 | 165 | | { |
| 8 | 166 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G |
| 8 | 167 | | scope.Start(); |
| 4 | 168 | | try |
| 4 | 169 | | { |
| 8 | 170 | | var response = _assignmentsRestClient.ListForScope(vaultBaseUrl: VaultUri.AbsoluteUri, scope: roleSc |
| 8 | 171 | | return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()); |
| 4 | 172 | | } |
| 0 | 173 | | catch (Exception ex) |
| 4 | 174 | | { |
| 0 | 175 | | scope.Failed(ex); |
| 0 | 176 | | throw; |
| 4 | 177 | | } |
| 8 | 178 | | }, (nextLink, _) => |
| 4 | 179 | | { |
| 0 | 180 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G |
| 0 | 181 | | scope.Start(); |
| 4 | 182 | | try |
| 4 | 183 | | { |
| 0 | 184 | | var response = _assignmentsRestClient.ListForScopeNextPage(nextLink: nextLink, vaultBaseUrl: VaultUr |
| 0 | 185 | | return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()); |
| 4 | 186 | | } |
| 0 | 187 | | catch (Exception ex) |
| 4 | 188 | | { |
| 0 | 189 | | scope.Failed(ex); |
| 0 | 190 | | throw; |
| 4 | 191 | | } |
| 0 | 192 | | }); |
| | 193 | | } |
| | 194 | |
|
| | 195 | | /// <summary>0 |
| | 196 | | /// Gets the <see cref="RoleAssignment"/>s for a scope. |
| | 197 | | /// </summary> |
| | 198 | | /// <param name="roleScope"> The scope of the role assignments. </param> |
| | 199 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 200 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 201 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> is null.</exception> |
| | 202 | | public virtual AsyncPageable<RoleAssignment> GetRoleAssignmentsAsync(RoleAssignmentScope roleScope, Cancellation |
| | 203 | | { |
| 2 | 204 | | return PageableHelpers.CreateAsyncEnumerable(async _ => |
| 2 | 205 | | { |
| 4 | 206 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G |
| 4 | 207 | | scope.Start(); |
| 2 | 208 | | try |
| 2 | 209 | | { |
| 4 | 210 | | var response = await _assignmentsRestClient.ListForScopeAsync(vaultBaseUrl: VaultUri.AbsoluteUri, sc |
| 4 | 211 | | .ConfigureAwait(false); |
| 4 | 212 | | return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()); |
| 2 | 213 | | } |
| 0 | 214 | | catch (Exception ex) |
| 2 | 215 | | { |
| 0 | 216 | | scope.Failed(ex); |
| 0 | 217 | | throw; |
| 2 | 218 | | } |
| 4 | 219 | | }, async (nextLink, _) => |
| 2 | 220 | | { |
| 0 | 221 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G |
| 0 | 222 | | scope.Start(); |
| 2 | 223 | | try |
| 2 | 224 | | { |
| 0 | 225 | | var response = await _assignmentsRestClient.ListForScopeNextPageAsync(nextLink: nextLink, vaultBaseU |
| 0 | 226 | | .ConfigureAwait(false); |
| 0 | 227 | | return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()); |
| 2 | 228 | | } |
| 0 | 229 | | catch (Exception ex) |
| 2 | 230 | | { |
| 0 | 231 | | scope.Failed(ex); |
| 0 | 232 | | throw; |
| 2 | 233 | | } |
| 0 | 234 | | }); |
| | 235 | | } |
| | 236 | |
|
| | 237 | | /// <summary> |
| | 238 | | /// Creates a <see cref="RoleAssignment"/>. |
| | 239 | | /// </summary> |
| | 240 | | /// <param name="roleScope"> The scope of the role assignment to create. </param> |
| | 241 | | /// <param name="properties"> Properties for the role assignment. </param> |
| | 242 | | /// <param name="name">The Name used to create the role assignment.</param> |
| | 243 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 244 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 245 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="properties"/> is nul |
| | 246 | | public virtual Response<RoleAssignment> CreateRoleAssignment(RoleAssignmentScope roleScope, RoleAssignmentProper |
| | 247 | | { |
| 14 | 248 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(Creat |
| 14 | 249 | | scope.Start(); |
| | 250 | | try |
| | 251 | | { |
| 14 | 252 | | var _name = name == default ? Guid.NewGuid().ToString() : name.ToString(); |
| 14 | 253 | | return _assignmentsRestClient.Create(VaultUri.AbsoluteUri, roleScope.ToString(), _name, new RoleAssignme |
| | 254 | | } |
| 0 | 255 | | catch (Exception ex) |
| | 256 | | { |
| 0 | 257 | | scope.Failed(ex); |
| 0 | 258 | | throw; |
| | 259 | | } |
| 14 | 260 | | } |
| | 261 | |
|
| | 262 | | /// <summary> |
| | 263 | | /// Creates a <see cref="RoleAssignment"/>. |
| | 264 | | /// </summary> |
| | 265 | | /// <param name="roleScope"> The scope of the role assignment to create. </param> |
| | 266 | | /// <param name="properties"> Properties for the role assignment. </param> |
| | 267 | | /// <param name="name">The name used to create the role assignment.</param> |
| | 268 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 269 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 270 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="properties"/> is nul |
| | 271 | | public virtual async Task<Response<RoleAssignment>> CreateRoleAssignmentAsync(RoleAssignmentScope roleScope, Rol |
| | 272 | | { |
| 12 | 273 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(Creat |
| 12 | 274 | | scope.Start(); |
| | 275 | | try |
| | 276 | | { |
| 12 | 277 | | var _name = name == default ? Guid.NewGuid().ToString() : name.ToString(); |
| 12 | 278 | | return await _assignmentsRestClient.CreateAsync(VaultUri.AbsoluteUri, roleScope.ToString(), _name, new R |
| 12 | 279 | | .ConfigureAwait(false); |
| | 280 | | } |
| 0 | 281 | | catch (Exception ex) |
| | 282 | | { |
| 0 | 283 | | scope.Failed(ex); |
| 0 | 284 | | throw; |
| | 285 | | } |
| 12 | 286 | | } |
| | 287 | |
|
| | 288 | | /// <summary> |
| | 289 | | /// Get the specified role assignment. |
| | 290 | | /// </summary> |
| | 291 | | /// <param name="roleScope"> The scope of the role assignment. </param> |
| | 292 | | /// <param name="roleAssignmentName"> The name of the role assignment to get. </param> |
| | 293 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 294 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 295 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="roleAssignmentName"/ |
| | 296 | | public virtual Response<RoleAssignment> GetRoleAssignment(RoleAssignmentScope roleScope, string roleAssignmentNa |
| | 297 | | { |
| 8 | 298 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(GetRo |
| 8 | 299 | | scope.Start(); |
| | 300 | | try |
| | 301 | | { |
| 8 | 302 | | return _assignmentsRestClient.Get(VaultUri.AbsoluteUri, roleScope.ToString(), roleAssignmentName, cancel |
| | 303 | | } |
| 2 | 304 | | catch (Exception ex) |
| | 305 | | { |
| 2 | 306 | | scope.Failed(ex); |
| 2 | 307 | | throw; |
| | 308 | | } |
| 6 | 309 | | } |
| | 310 | |
|
| | 311 | | /// <summary> |
| | 312 | | /// Get the specified role assignment. |
| | 313 | | /// </summary> |
| | 314 | | /// <param name="roleScope"> The scope of the role assignment. </param> |
| | 315 | | /// <param name="roleAssignmentName"> The name of the role assignment to get. </param> |
| | 316 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 317 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 318 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="roleAssignmentName"/ |
| | 319 | | public virtual async Task<Response<RoleAssignment>> GetRoleAssignmentAsync(RoleAssignmentScope roleScope, string |
| | 320 | | { |
| 4 | 321 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(GetRo |
| 4 | 322 | | scope.Start(); |
| | 323 | | try |
| | 324 | | { |
| 4 | 325 | | return await _assignmentsRestClient.GetAsync(VaultUri.AbsoluteUri, roleScope.ToString(), roleAssignmentN |
| 4 | 326 | | .ConfigureAwait(false); |
| | 327 | | } |
| 0 | 328 | | catch (Exception ex) |
| | 329 | | { |
| 0 | 330 | | scope.Failed(ex); |
| 0 | 331 | | throw; |
| | 332 | | } |
| 4 | 333 | | } |
| | 334 | |
|
| | 335 | | /// <summary> |
| | 336 | | /// Delete the specified role assignment. |
| | 337 | | /// </summary> |
| | 338 | | /// <param name="roleScope"> The scope of the role assignment. </param> |
| | 339 | | /// <param name="roleAssignmentName"> The name of the role assignment to get. </param> |
| | 340 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 341 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 342 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="roleAssignmentName"/ |
| | 343 | | public virtual Response<RoleAssignment> DeleteRoleAssignment(RoleAssignmentScope roleScope, string roleAssignmen |
| | 344 | | { |
| 6 | 345 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(Delet |
| 6 | 346 | | scope.Start(); |
| | 347 | | try |
| | 348 | | { |
| 6 | 349 | | return _assignmentsRestClient.Delete(VaultUri.AbsoluteUri, roleScope.ToString(), roleAssignmentName, can |
| | 350 | | } |
| 0 | 351 | | catch (Exception ex) |
| | 352 | | { |
| 0 | 353 | | scope.Failed(ex); |
| 0 | 354 | | throw; |
| | 355 | | } |
| 6 | 356 | | } |
| | 357 | |
|
| | 358 | | /// <summary> |
| | 359 | | /// Delete the specified role assignment. |
| | 360 | | /// </summary> |
| | 361 | | /// <param name="roleScope"> The scope of the role assignment. </param> |
| | 362 | | /// <param name="roleAssignmentName"> The name of the role assignment to get. </param> |
| | 363 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 364 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 365 | | /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="roleAssignmentName"/ |
| | 366 | | public virtual async Task<Response<RoleAssignment>> DeleteRoleAssignmentAsync(RoleAssignmentScope roleScope, str |
| | 367 | | { |
| 4 | 368 | | using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(Delet |
| 4 | 369 | | scope.Start(); |
| | 370 | | try |
| | 371 | | { |
| 4 | 372 | | return await _assignmentsRestClient.DeleteAsync(VaultUri.AbsoluteUri, roleScope.ToString(), roleAssignme |
| 4 | 373 | | .ConfigureAwait(false); |
| | 374 | | } |
| 0 | 375 | | catch (Exception ex) |
| | 376 | | { |
| 0 | 377 | | scope.Failed(ex); |
| 0 | 378 | | throw; |
| | 379 | | } |
| 4 | 380 | | } |
| | 381 | | } |
| | 382 | | } |