Linter rule - no hardcoded environment URL

This rule finds any hard-coded URLs that vary by the cloud environment.

Linter rule code

Use the following value in the Bicep configuration file to customize rule settings:

no-hardcoded-env-urls

Solution

Instead of hard-coding URLs in your Bicep file, use the environment function to dynamically get these URLs during deployment. The environment function returns different URLs based on the cloud environment you're deploying to.

The following example fails this test because the URL is hardcoded.

var managementURL = 'https://management.chinacloudapi.cn'

The test also fails when used with concat or uri.

var galleryURL1 = concat('https://','gallery.azure.com')
var galleryURL2 = uri('gallery.azure.com','test')

You can fix it by replacing the hard-coded URL with the environment() function.

var galleryURL = environment().gallery

In some cases, you can fix it by getting a property from a resource you've deployed. For example, instead of constructing the endpoint for your storage account, retrieve it with .properties.primaryEndpoints.

param storageAccountName string

resource sa 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  name: storageAccountName
  location: 'chinanorth'
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  properties: {
    accessTier: 'Hot'
  }
}

output endpoint string = sa.properties.primaryEndpoints.web

Configuration

By default, this rule uses the following settings for determining which URLs are disallowed in Azure China Cloud.

"analyzers": {
  "core": {
    "verbose": false,
    "enabled": true,
    "rules": {
      "no-hardcoded-env-urls": {
        "level": "warning",
        "disallowedhosts": [
          "gallery.azure.com",
          "management.core.chinacloudapi.cn",
          "management.chinacloudapi.cn",
          "database.chinacloudapi.cn",
          "core.chinacloudapi.cn",
          "login.chinacloudapi.cn",
          "microsoftgraph.chinacloudapi.cn",
          "trafficmanager.cn",
          "datalake.chinacloudapi.cn",
          "azuredatalakestore.net",
          "azuredatalakeanalytics.net",
          "vault.azure.cn",
          "api.loganalytics.io",
          "asazure.chinacloudapi.cn",
          "region.asazure.chinacloudapi.cn",
          "batch.core.chinacloudapi.cn"
        ],
        "excludedhosts": [
          "schema.management.azure.com"
        ]
      }
    }
  }
}

You can customize it by adding a bicepconfig.json file and applying new settings.

Next steps

For more information about the linter, see Use Bicep linter.