|
| 1 | +--- |
| 2 | +description: Reference for the 'secret' DSC configuration document function |
| 3 | +ms.date: 08/22/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: secret |
| 6 | +--- |
| 7 | + |
| 8 | +# secret |
| 9 | + |
| 10 | +## Synopsis |
| 11 | + |
| 12 | +Retrieves secrets from registered secret management extensions. |
| 13 | + |
| 14 | +## Syntax |
| 15 | + |
| 16 | +```Syntax |
| 17 | +secret(<name>) |
| 18 | +secret(<name>, <vault>) |
| 19 | +``` |
| 20 | + |
| 21 | +## Description |
| 22 | + |
| 23 | +The `secret()` function retrieves secrets from extensions that support the |
| 24 | +secret capability. It queries all registered extensions that implement secret |
| 25 | +management and returns the requested secret value. If multiple extensions |
| 26 | +return different values for the same secret name, an error is returned unless |
| 27 | +a vault is specified to disambiguate. |
| 28 | + |
| 29 | +The function supports two calling patterns: |
| 30 | + |
| 31 | +- Single argument: Retrieves a secret by name from any available vault |
| 32 | +- Two arguments: Retrieves a secret by name from a specific vault |
| 33 | + |
| 34 | +If multiple extensions return the same secret value, the function succeeds and |
| 35 | +returns that value. This allows for redundancy across secret management |
| 36 | +systems. |
| 37 | + |
| 38 | +## Examples |
| 39 | + |
| 40 | +### Example 1 - Retrieve a secret by name |
| 41 | + |
| 42 | +The following example retrieves a secret named 'DatabasePassword' from any |
| 43 | +available vault. |
| 44 | + |
| 45 | +```yaml |
| 46 | +# secret.example.1.dsc.config.yaml |
| 47 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 48 | +resources: |
| 49 | +- name: Database Connection |
| 50 | + type: Microsoft.DSC.Debug/Echo |
| 51 | + properties: |
| 52 | + output: "Connection string with password: [secret('DatabasePassword')]" |
| 53 | +``` |
| 54 | +
|
| 55 | +```bash |
| 56 | +dsc config get --file secret.example.1.dsc.config.yaml |
| 57 | +``` |
| 58 | + |
| 59 | +```yaml |
| 60 | +results: |
| 61 | +- name: Database Connection |
| 62 | + type: Microsoft.DSC.Debug/Echo |
| 63 | + result: |
| 64 | + actualState: |
| 65 | + output: "Connection string with password: MySecretPassword123" |
| 66 | +messages: [] |
| 67 | +hadErrors: false |
| 68 | +``` |
| 69 | +
|
| 70 | +### Example 2 - Retrieve a secret from a specific vault |
| 71 | +
|
| 72 | +The following example retrieves a secret from a specific vault to avoid |
| 73 | +ambiguity when the same secret name exists in multiple vaults. |
| 74 | +
|
| 75 | +```yaml |
| 76 | +# secret.example.2.dsc.config.yaml |
| 77 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 78 | +resources: |
| 79 | +- name: API Configuration |
| 80 | + type: Microsoft.DSC.Debug/Echo |
| 81 | + properties: |
| 82 | + output: "API Key: [secret('ApiKey', 'ProductionVault')]" |
| 83 | +``` |
| 84 | +
|
| 85 | +```bash |
| 86 | +dsc config get --file secret.example.2.dsc.config.yaml |
| 87 | +``` |
| 88 | + |
| 89 | +```yaml |
| 90 | +results: |
| 91 | +- name: API Configuration |
| 92 | + type: Microsoft.DSC.Debug/Echo |
| 93 | + result: |
| 94 | + actualState: |
| 95 | + output: "API Key: prod-api-key-xyz789" |
| 96 | +messages: [] |
| 97 | +hadErrors: false |
| 98 | +``` |
| 99 | +
|
| 100 | +### Example 3 - Multiple secrets in configuration |
| 101 | +
|
| 102 | +The following example demonstrates using multiple secrets within a single |
| 103 | +resource configuration. |
| 104 | +
|
| 105 | +```yaml |
| 106 | +# secret.example.3.dsc.config.yaml |
| 107 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 108 | +resources: |
| 109 | +- name: Service Configuration |
| 110 | + type: Microsoft.DSC.Debug/Echo |
| 111 | + properties: |
| 112 | + output: |
| 113 | + username: "[secret('ServiceUsername')]" |
| 114 | + password: "[secret('ServicePassword')]" |
| 115 | + connectionString: "Server=myserver;User=[secret('DbUser')];Password=[secret('DbPassword')]" |
| 116 | +``` |
| 117 | +
|
| 118 | +```bash |
| 119 | +dsc config get --file secret.example.3.dsc.config.yaml |
| 120 | +``` |
| 121 | + |
| 122 | +```yaml |
| 123 | +results: |
| 124 | +- name: Service Configuration |
| 125 | + type: Microsoft.DSC.Debug/Echo |
| 126 | + result: |
| 127 | + actualState: |
| 128 | + output: |
| 129 | + username: "serviceaccount" |
| 130 | + password: "SecurePassword456" |
| 131 | + connectionString: "Server=myserver;User=dbuser;Password=DbPass789" |
| 132 | +messages: [] |
| 133 | +hadErrors: false |
| 134 | +``` |
| 135 | +
|
| 136 | +### Example 4 - Conditional secret usage |
| 137 | +
|
| 138 | +The following example shows using secrets conditionally based on environment |
| 139 | +parameters. |
| 140 | +
|
| 141 | +```yaml |
| 142 | +# secret.example.4.dsc.config.yaml |
| 143 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 144 | +parameters: |
| 145 | + environment: |
| 146 | + type: string |
| 147 | + defaultValue: production |
| 148 | +resources: |
| 149 | +- name: Environment-specific config |
| 150 | + type: Microsoft.DSC.Debug/Echo |
| 151 | + properties: |
| 152 | + output: |
| 153 | + apiKey: >- |
| 154 | + [if(equals(parameters('environment'), 'production'), |
| 155 | + secret('ProdApiKey', 'ProdVault'), |
| 156 | + secret('DevApiKey', 'DevVault'))] |
| 157 | +``` |
| 158 | +
|
| 159 | +```bash |
| 160 | +dsc config get --file secret.example.4.dsc.config.yaml |
| 161 | +``` |
| 162 | + |
| 163 | +```yaml |
| 164 | +results: |
| 165 | +- name: Environment-specific config |
| 166 | + type: Microsoft.DSC.Debug/Echo |
| 167 | + result: |
| 168 | + actualState: |
| 169 | + output: |
| 170 | + apiKey: "prod-secure-key-abc123" |
| 171 | +messages: [] |
| 172 | +hadErrors: false |
| 173 | +``` |
| 174 | +
|
| 175 | +## Parameters |
| 176 | +
|
| 177 | +### name |
| 178 | +
|
| 179 | +The name of the secret to retrieve. |
| 180 | +
|
| 181 | +```yaml |
| 182 | +Type: string |
| 183 | +Required: true |
| 184 | +Position: 1 |
| 185 | +``` |
| 186 | +
|
| 187 | +### vault |
| 188 | +
|
| 189 | +The name of the vault or secret store to retrieve the secret from. When |
| 190 | +specified, only the named vault is queried for the secret, which helps |
| 191 | +disambiguate when multiple vaults contain secrets with the same name. |
| 192 | +
|
| 193 | +```yaml |
| 194 | +Type: string |
| 195 | +Required: false |
| 196 | +Position: 2 |
| 197 | +``` |
| 198 | +
|
| 199 | +## Output |
| 200 | +
|
| 201 | +The `secret()` function returns the secret value as a string. |
| 202 | + |
| 203 | +```yaml |
| 204 | +Type: string |
| 205 | +``` |
| 206 | + |
| 207 | +## Error conditions |
| 208 | + |
| 209 | +The `secret()` function can return errors in the following situations: |
| 210 | + |
| 211 | +- **No extensions available**: No secret management extensions are registered |
| 212 | + or available |
| 213 | +- **Secret not found**: The specified secret name does not exist in any |
| 214 | + available vault |
| 215 | +- **Multiple different values**: Multiple extensions return different values |
| 216 | + for the same secret name (specify a vault to disambiguate) |
| 217 | +- **Vault not found**: The specified vault does not exist or is not accessible |
| 218 | +- **Extension error**: An underlying secret management extension returns an |
| 219 | + error |
| 220 | + |
| 221 | +## Security considerations |
| 222 | + |
| 223 | +- Secret values are retrieved at runtime and should be handled securely |
| 224 | +- Secrets are not cached by DSC and are retrieved fresh on each function call |
| 225 | +- Secret values are not logged in trace output for security reasons |
| 226 | +- Extensions should implement appropriate authentication and authorization for |
| 227 | + secret access |
| 228 | + |
| 229 | +## Extension requirements |
| 230 | + |
| 231 | +To support the `secret()` function, extensions must: |
| 232 | + |
| 233 | +1. Declare the `Secret` capability in their manifest |
| 234 | +2. Implement a secret retrieval method that accepts name and optional vault |
| 235 | + parameters |
| 236 | +3. Return secret values as single-line strings (multi-line values are not |
| 237 | + supported) |
| 238 | +4. Handle authentication and authorization according to their secret |
| 239 | + management system |
| 240 | + |
| 241 | +## Related functions |
| 242 | + |
| 243 | +- [`if()`][00] - Returns values based on a condition for conditional secret |
| 244 | + usage |
| 245 | +- [`equals()`][01] - Compares values for conditional logic |
| 246 | +- [`parameters()`][02] - Access configuration parameters that may influence |
| 247 | + secret selection |
| 248 | + |
| 249 | +<!-- Link reference definitions --> |
| 250 | +[00]: ./if.md |
| 251 | +[01]: ./equals.md |
| 252 | +[02]: ./parameters.md |
0 commit comments