Bicep linter/static-analysis is not recognizing error scenarios related to resource name
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 21m
- Merged PRs (30d)
- 79
Description
**Bicep version**
Bicep CLI version 0.4.1124 (66c84c8ee5)
**Describe the bug**
I was recently attempting to instantiate a bicep resource with a randomly generated name for the purpose of my ci testing process. The name of the resource is constrained (as a Azure policy) to start with a letter.
I inadvertently set the name using `uniqueString()`, however it turns out this function doesn't guarantee that the result will start with a letter, and the bicep vscode extension didn't warn me about this fact while authoring the resource.
This results in me creating a deployment that will periodically fail introducing flakiness to my CI environment since every now and then (given that I am recreating the environment on each build) I may get a uniqueString that starts with a number resulting in a bad request error.
I am curious if there's a way to have the vscode linter/static-analyzer detect this during authoring since its unreasonable to expect authors to "just know" what are the constraints on a particular azure resource name.
Here is the resource I was setting:
```bicep
resource key_vault 'Microsoft.KeyVault/vaults@2021-04-01-preview' = {
name: uniqueString('kv', resourceGroup().id)
location: resourceGroup().location
properties: {
enabledForTemplateDeployment: true
tenantId: subscription().tenantId
enableRbacAuthorization:true
sku: {
name: 'standard'
family: 'A'
}
}
```
Here is the sporadic error I would get every now and then:
```
Error: Code="DeploymentFailed" Message="At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details." Details=[{"code":"BadRequest","message":"{\r\n \"error\": {\r\n \"code\": \"VaultNameNotValid\",\r\n \"message\": \"The vault name '2d6lducplqzgu' is invalid. A vault's name must be between 3-24 alphanumeric characters. The name must begin with a letter, end with a letter or digit, and not contain consecutive hyphens. Follow this link for more information: https://go.microsoft.com/fwlink/?linkid=2147742\"\r\n }\r\n}"}]
```
Here is how I ended up fixing the issue:
```bicep
resource key_vault 'Microsoft.KeyVault/vaults@2021-04-01-preview' = {
name: 'kv-${uniqueString('kv', resourceGroup().id)}'
location: resourceGroup().location
properties: {
enabledForTemplateDeployment: true
tenantId: subscription().tenantId
enableRbacAuthorization:true
sku: {
name: 'standard'
family: 'A'
}
}
```
** Expected Behavior **
I would expect the Bicep VSCode extension to underline the stanza `name: uniqueString('foo')` with a warning or an error that indicates that doing this is unsafe.
Contributor guide
Research direction
Start with the Bicep VSCode extension's linter/static-analysis behavior and the reported resource example using name: uniqueString('kv', resourceGroup().id). Determine how resource name constraints are represented and whether this scenario can be diagnosed; done means the extension warns about unsafe names such as an unprefixed uniqueString result without warning on the prefixed form.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, vscode
- Domain
- cloud, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100