Skip to content

Commit

Permalink
rule: adminUsername-should-not-be-literal (#4702)
Browse files Browse the repository at this point in the history
* rule: adminUsername-should-not-be-literal

* Check compile errors in all linter tests

* PR comments

* Update test baselines

* Fix file casing

* Fix completion tests

* Update test baselines

Co-authored-by: Stephen Weatherford <stephen.weatherford@microsoft.com>
Co-authored-by: Bicep Automation <bicep@noreply.github.com>
  • Loading branch information
3 people authored Oct 5, 2021
1 parent 1aa04b8 commit 4fc99de
Show file tree
Hide file tree
Showing 24 changed files with 955 additions and 260 deletions.
5 changes: 4 additions & 1 deletion docs/examples/bicepconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"analyzers": {
"core": {
"rules": {
"adminusername-should-not-be-literal": {
"level": "warning"
},
"no-hardcoded-env-urls": {
"level": "warning"
},
Expand All @@ -23,4 +26,4 @@
}
}
}
}
}
51 changes: 51 additions & 0 deletions docs/linter-rules/adminusername-should-not-be-literal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# AdminUserName should not be a literal

**Code**: adminUsername-should-not-be-literal

**Description**: When setting an adminUserName property, don't use a literal value or an expression which evaluates to a literal value.
Create a parameter for the username and use an expression to reference the parameter's value.

The following examples fail this test.

```bicep
resource vm 'Microsoft.Compute/virtualMachines@2020-12-01' = {
name: 'name'
location: location
properties: {
osProfile: {
adminUsername: 'adminUsername'
}
}
}
```

```bicep
var defaultAdmin = 'administrator'
resource vm 'Microsoft.Compute/virtualMachines@2020-12-01' = {
name: 'name'
location: location
properties: {
osProfile: {
adminUsername: defaultAdmin
}
```

The following example passes this test.

```bicep
@secure()
param adminUsername string
param location string
resource vm 'Microsoft.Compute/virtualMachines@2020-12-01' = {
name: 'name'
location: location
properties: {
osProfile: {
adminUsername: adminUsername
}
}
}
```
13 changes: 13 additions & 0 deletions src/Bicep.Core.UnitTests/Diagnostics/BicepConfigSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void RuleConfigs_ShouldBeCorrectlyDefined()
},
*/

var allOf = rule!.SelectToken("allOf");
Assert.IsNotNull(allOf, "Each rule should have a top-level allOf");
allOf.Count().Should().BeGreaterOrEqualTo(2);
Expand All @@ -88,5 +89,17 @@ public void RuleConfigs_ShouldBeCorrectlyDefined()
refString.Should().Be("#/definitions/rule", "each rule's last allOf should be a ref to the definition of a rule");
}
}

[TestMethod]
public void RuleConfigs_RuleNamesShouldUseCorrectCasing()
{
var (rules, schema) = GetRulesAndSchema();
var ruleConfigs = schema.SelectToken("properties.analyzers.properties.core.properties.rules.properties")!.ToObject<IDictionary<string, JObject>>();
Assert.IsNotNull(ruleConfigs);
foreach (var (key, rule) in ruleConfigs)
{
key.Should().MatchRegex("^[a-z][a-z-]*[a-z]$", "all rule keys should be lower-cased with hyphens, not start or end with hyphen");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void HasBuiltInRules()
[DataRow(SecureParameterDefaultRule.Code)]
[DataRow(SimplifyInterpolationRule.Code)]
[DataRow(NoUnusedVariablesRule.Code)]
[DataRow(AdminUsernameShouldNotBeLiteralRule.Code)]
public void BuiltInRulesExist(string ruleCode)
{
var linter = new LinterAnalyzer(configuration);
Expand Down
Loading

0 comments on commit 4fc99de

Please sign in to comment.