|
1 | 1 | function client = createKeyVaultClient(varargin) |
2 | | -% CREATEKEYVAULTCLIENT Convenience function for creating KeyClient and |
3 | | -% SecretClient |
4 | | -% |
5 | | -% client = CREATEKEYVAULTCLIENT('Type','Key') creates a KeyClient with |
6 | | -% default options. |
7 | | -% |
8 | | -% client = CREATEKEYVAULTCLIENT('Type','Secret') creates SecretClient with |
9 | | -% default options. |
10 | | -% |
11 | | -% By default CREATEKEYVAULTCLIENT reads Credential information and the |
12 | | -% Vault Name from a configuration file named 'keyvaultsettings.json'. The |
13 | | -% function automatically searches for this file on the MATLABPATH. It is |
14 | | -% possible to specify a different filename using 'ConfigurationFile'. It is |
15 | | -% also possible to provide 'Credentials' and 'VaultName' as inputs to the |
16 | | -% function directly in case which no configuration file may be needed. See |
17 | | -% the Name, Value pairs below for more details. |
18 | | -% |
19 | | -% Additional Name, Value pairs can be supplied to configure non-default |
20 | | -% options: |
21 | | -% |
22 | | -% 'ConfigurationFile', explicitly specify which configuration file to |
23 | | -% use. This file is used for configuring Credentials (when not |
24 | | -% supplied as input) and/or Account Name (when not supplied as input). |
25 | | -% |
26 | | -% Default Value: 'keyvaultsettings.json' |
27 | | -% |
28 | | -% 'Credentials', explicitly specify credentials to use. This for example |
29 | | -% allows building multiple clients based on the same credentials |
30 | | -% without having to go through (interactive) authentication again. If |
31 | | -% not specified, CREATEKEYVAULTCLIENT uses configureCredentials with |
32 | | -% 'ConfigurationFile' as input to first configure credentials before |
33 | | -% building the client. |
34 | | -% |
35 | | -% Hint: configureCredentials can be used to build valid Credentials. |
36 | | -% |
37 | | -% Example: |
38 | | -% credentials = configureCredentials('myCredentials.json'); |
39 | | -% client1 = CREATEKEYVAULTCLIENT('Credentials',credentials,'Type','Key') |
40 | | -% client2 = CREATEKEYVAULTCLIENT('Credentials',credentials,'Type','Secret') |
41 | | -% |
42 | | -% 'VaultName', explicitly specify the Vault name for the client. If not |
43 | | -% specified CREATEKEYVAULTCLIENT uses loadConfigurationSettings to |
44 | | -% load configuration options from 'ConfigurationFile'. This file must |
45 | | -% then contain a "VaultName" setting. |
46 | | -% |
47 | | -% See also CONFIGURECREDENTIALS, LOADCONFIGURATIONSETTINGS |
| 2 | + % CREATEKEYVAULTCLIENT Convenience function for creating KeyClient and |
| 3 | + % SecretClient |
| 4 | + % |
| 5 | + % client = createKeyVaultClient('Type','Key') creates a KeyClient with |
| 6 | + % default options. |
| 7 | + % |
| 8 | + % client = createKeyVaultClient('Type','Secret') creates SecretClient with |
| 9 | + % default options. |
| 10 | + % |
| 11 | + % By default createKeyVaultClient reads Credential information and the |
| 12 | + % Vault Name from a configuration file named 'keyvaultsettings.json'. The |
| 13 | + % function automatically searches for this file on the MATLABPATH. It is |
| 14 | + % possible to specify a different filename using 'ConfigurationFile'. It is |
| 15 | + % also possible to provide 'Credentials' and 'VaultName' as inputs to the |
| 16 | + % function directly in case which no configuration file may be needed. See |
| 17 | + % the Name, Value pairs below for more details. |
| 18 | + % |
| 19 | + % Additional Name, Value pairs can be supplied to configure non-default |
| 20 | + % options: |
| 21 | + % |
| 22 | + % 'ConfigurationFile', explicitly specify which configuration file to |
| 23 | + % use. This file is used for configuring Credentials (when not |
| 24 | + % supplied as input) and/or Account Name (when not supplied as input). |
| 25 | + % |
| 26 | + % Default Value: 'keyvaultsettings.json' |
| 27 | + % |
| 28 | + % 'Credentials', explicitly specify credentials to use. This for example |
| 29 | + % allows building multiple clients based on the same credentials |
| 30 | + % without having to go through (interactive) authentication again. If |
| 31 | + % not specified, createKeyVaultClient uses configureCredentials with |
| 32 | + % 'ConfigurationFile' as input to first configure credentials before |
| 33 | + % building the client. |
| 34 | + % |
| 35 | + % Hint: configureCredentials can be used to build valid Credentials. |
| 36 | + % |
| 37 | + % Example: |
| 38 | + % credentials = configureCredentials('myCredentials.json'); |
| 39 | + % client1 = createKeyVaultClient('Credentials',credentials,'Type','Key') |
| 40 | + % client2 = createKeyVaultClient('Credentials',credentials,'Type','Secret') |
| 41 | + % |
| 42 | + % 'VaultName', explicitly specify the Vault name for the client. If not |
| 43 | + % specified createKeyVaultClient uses loadConfigurationSettings to |
| 44 | + % load configuration options from 'ConfigurationFile'. This file must |
| 45 | + % then contain a "VaultName" setting. |
| 46 | + % |
| 47 | + % See also CONFIGURECREDENTIALS, LOADCONFIGURATIONSETTINGS |
48 | 48 |
|
49 | | -% Copyright 2022 The MathWorks, Inc. |
| 49 | + % Copyright 2022 The MathWorks, Inc. |
50 | 50 |
|
51 | 51 | initialize('displayLevel', 'debug', 'loggerPrefix', 'Azure:KeyVault'); |
52 | | - |
| 52 | + |
53 | 53 | logObj = Logger.getLogger(); |
54 | 54 |
|
55 | 55 | p = inputParser; |
56 | 56 | p.FunctionName = 'createKeyVaultClient'; |
57 | | - |
| 57 | + |
58 | 58 | p.addParameter('Credentials',[],... |
59 | 59 | @(x) isa(x, 'azure.core.credential.TokenCredential')); |
60 | | - |
| 60 | + |
61 | 61 | p.addParameter('Type',[],@(x) ischar(x) || isStringScalar(x)); |
62 | 62 | p.addParameter('ConfigurationFile','keyvaultsettings.json',@(x) ischar(x) || isStringScalar(x)) |
63 | 63 | p.addParameter('VaultName',[],@(x) ischar(x) || isStringScalar(x)); |
64 | | - |
| 64 | + |
65 | 65 | p.parse(varargin{:}); |
66 | | - |
| 66 | + |
67 | 67 | if isempty(p.Results.Credentials) |
68 | 68 | credentials = configureCredentials(p.Results.ConfigurationFile); |
69 | 69 | else |
|
90 | 90 | vaultUrl = sprintf('https://%s.vault.azure.net/',config.VaultName); |
91 | 91 | else |
92 | 92 | vaultUrl = sprintf('https://%s.vault.azure.net/',p.Results.VaultName); |
93 | | - end |
| 93 | + end |
94 | 94 | builder = builder.vaultUrl(vaultUrl); |
95 | | - |
| 95 | + |
96 | 96 | builder = builder.httpClient(); |
97 | 97 |
|
98 | 98 | client = builder.buildClient(); |
| 99 | +end |
0 commit comments