Bylaw

Gallery › Kubernetes › Azure built-in policies

Configure Kubernetes clusters with specified GitOps configuration using SSH secrets

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.1.0

Deploy a 'sourceControlConfiguration' to Kubernetes clusters to assure that the clusters get their source of truth for workloads and configurations from the defined git repo. This definition requires a SSH private key secret in Key Vault. For instructions, visit https://aka.ms/K8sGitOpsPolicy.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONDeploy-GitOps-to-Kubernetes-cluster-SSH-secrets_DINE.json · 368 lines
{
  "properties": {
    "displayName": "Configure Kubernetes clusters with specified GitOps configuration using SSH secrets",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Deploy a 'sourceControlConfiguration' to Kubernetes clusters to assure that the clusters get their source of truth for workloads and configurations from the defined git repo. This definition requires a SSH private key secret in Key Vault. For instructions, visit https://aka.ms/K8sGitOpsPolicy.",
    "metadata": {
      "version": "1.1.0",
      "category": "Kubernetes"
    },
    "version": "1.1.0",
    "parameters": {
      "configurationResourceName": {
        "type": "String",
        "metadata": {
          "displayName": "Configuration resource name",
          "description": "The name for the sourceControlConfiguration.  Learn more about setting up GitOps configuration: https://aka.ms/AzureArcK8sUsingGitOps."
        }
      },
      "operatorInstanceName": {
        "type": "String",
        "metadata": {
          "displayName": "Operator instance name",
          "description": "Name used in the operator instances. Maximum of 23 lowercase alphanumeric characters or hyphen. Must start and end with an alphanumeric character."
        }
      },
      "operatorNamespace": {
        "type": "String",
        "metadata": {
          "displayName": "Operator namespace",
          "description": "Namespace within which the operators will be installed. Maximum of 23 lowercase alphanumeric characters or hyphen. Must start and end with an alphanumeric character."
        }
      },
      "operatorScope": {
        "type": "String",
        "metadata": {
          "displayName": "Operator scope",
          "description": "The permission scope for the operator. Possible values are 'cluster' (full access) or 'namespace' (restricted access)."
        },
        "allowedValues": [
          "cluster",
          "namespace"
        ],
        "defaultValue": "namespace"
      },
      "operatorType": {
        "type": "String",
        "metadata": {
          "displayName": "Operator type",
          "description": "The type of operator to install. Currently, 'Flux' is supported."
        },
        "allowedValues": [
          "Flux"
        ],
        "defaultValue": "Flux"
      },
      "operatorParams": {
        "type": "String",
        "metadata": {
          "displayName": "Operator parameters",
          "description": "Parameters to set on the Flux operator, separated by spaces.  For example, --git-readonly --sync-garbage-collection.  Learn more: http://aka.ms/AzureArcK8sFluxOperatorParams."
        },
        "defaultValue": ""
      },
      "repositoryUrl": {
        "type": "String",
        "metadata": {
          "displayName": "Repository Url",
          "description": "The URL for the source control repository. Learn more about URL formats: https://aka.ms/GitOpsRepoUrlParameters"
        }
      },
      "enableHelmOperator": {
        "type": "String",
        "metadata": {
          "displayName": "Enable Helm",
          "description": "Indicate whether to enable Helm for this instance of Flux. Learn more: http://aka.ms/AzureArcK8sGitOpsWithHelm."
        },
        "allowedValues": [
          "true",
          "false"
        ],
        "defaultValue": "true"
      },
      "chartVersion": {
        "type": "String",
        "metadata": {
          "displayName": "Helm chart version for installing Flux Helm",
          "description": "The version of the Helm chart for installing Flux Helm. For example, 1.2.0"
        },
        "defaultValue": "1.2.0"
      },
      "chartValues": {
        "type": "String",
        "metadata": {
          "displayName": "Helm chart parameters for installing Flux Helm",
          "description": "Parameters for the Helm chart for installing Flux Helm, separated by spaces. For example, --set helm.versions=v3"
        },
        "defaultValue": ""
      },
      "sshKnownHostsContents": {
        "type": "String",
        "metadata": {
          "displayName": "Base64-encoded known hosts content",
          "description": "The base64-encoded known hosts content."
        },
        "defaultValue": ""
      },
      "keyVaultResourceId": {
        "type": "String",
        "metadata": {
          "displayName": "Key Vault resource id",
          "description": "The resource id for the Key Vault that holds the SSH or HTTPS secrets. For example: '/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.KeyVault/vaults/<key-vault-name>'",
          "strongType": "Microsoft.KeyVault/vaults",
          "assignPermissions": true
        },
        "defaultValue": ""
      },
      "sshPrivateKeyKeyVaultSecretName": {
        "type": "String",
        "metadata": {
          "displayName": "SSH private key Key Vault secret",
          "description": "The name of the Key Vault secret that holds the base64-encoded SSH private key."
        },
        "defaultValue": ""
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "auditIfNotExists",
          "AuditIfNotExists",
          "deployIfNotExists",
          "DeployIfNotExists",
          "disabled",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "in": [
          "Microsoft.Kubernetes/connectedClusters",
          "Microsoft.ContainerService/managedClusters"
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.KubernetesConfiguration/sourceControlConfigurations",
          "name": "[parameters('configurationResourceName')]",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "deploymentScope": "ResourceGroup",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.KubernetesConfiguration/sourceControlConfigurations/operatorParams",
                "in": [
                  "[parameters('operatorParams')]",
                  "[concat('--git-readonly ',parameters('operatorParams'))]"
                ]
              },
              {
                "field": "Microsoft.KubernetesConfiguration/sourceControlConfigurations/repositoryUrl",
                "equals": "[parameters('repositoryUrl')]"
              },
              {
                "field": "Microsoft.KubernetesConfiguration/sourceControlConfigurations/sshKnownHostsContents",
                "equals": "[parameters('sshKnownHostsContents')]"
              },
              {
                "anyOf": [
                  {
                    "field": "Microsoft.KubernetesConfiguration/sourceControlConfigurations/enableHelmOperator",
                    "equals": "false"
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.KubernetesConfiguration/sourceControlConfigurations/enableHelmOperator",
                        "equals": "true"
                      },
                      {
                        "field": "Microsoft.KubernetesConfiguration/sourceControlConfigurations/helmOperatorProperties.chartVersion",
                        "equals": "[parameters('chartVersion')]"
                      },
                      {
                        "field": "Microsoft.KubernetesConfiguration/sourceControlConfigurations/helmOperatorProperties.chartValues",
                        "equals": "[parameters('chartValues')]"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "configurationResourceName": {
                    "type": "string"
                  },
                  "clusterLocation": {
                    "type": "string"
                  },
                  "clusterName": {
                    "type": "string"
                  },
                  "operatorInstanceName": {
                    "type": "string"
                  },
                  "operatorNamespace": {
                    "type": "string"
                  },
                  "operatorScope": {
                    "type": "string"
                  },
                  "operatorType": {
                    "type": "string"
                  },
                  "operatorParams": {
                    "type": "string"
                  },
                  "repositoryUrl": {
                    "type": "string"
                  },
                  "enableHelmOperator": {
                    "type": "string"
                  },
                  "chartVersion": {
                    "type": "string"
                  },
                  "chartValues": {
                    "type": "string"
                  },
                  "sshKnownHostsContents": {
                    "type": "string"
                  },
                  "sshPrivateKey": {
                    "type": "securestring"
                  },
                  "clusterResourceType": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "condition": "[contains(toLower(parameters('clusterResourceType')), toLower('connectedclusters'))]",
                    "type": "Microsoft.Kubernetes/connectedClusters/providers/sourceControlConfigurations",
                    "name": "[concat(parameters('clusterName'), '/Microsoft.KubernetesConfiguration/', parameters('configurationResourceName'))]",
                    "apiVersion": "2021-03-01",
                    "properties": {
                      "operatorInstanceName": "[parameters('operatorInstanceName')]",
                      "operatorNamespace": "[parameters('operatorNamespace')]",
                      "operatorScope": "[parameters('operatorScope')]",
                      "operatorType": "[parameters('operatorType')]",
                      "operatorParams": "[parameters('operatorParams')]",
                      "repositoryUrl": "[parameters('repositoryUrl')]",
                      "enableHelmOperator": "[parameters('enableHelmOperator')]",
                      "helmOperatorProperties": {
                        "chartVersion": "[parameters('chartVersion')]",
                        "chartValues": "[parameters('chartValues')]"
                      },
                      "sshKnownHostsContents": "[parameters('sshKnownHostsContents')]",
                      "configurationProtectedSettings": {
                        "sshPrivateKey": "[parameters('sshPrivateKey')]"
                      }
                    }
                  },
                  {
                    "condition": "[contains(toLower(parameters('clusterResourceType')), toLower('managedclusters'))]",
                    "type": "Microsoft.ContainerService/managedClusters/providers/sourceControlConfigurations",
                    "name": "[concat(parameters('clusterName'), '/Microsoft.KubernetesConfiguration/', parameters('configurationResourceName'))]",
                    "apiVersion": "2021-03-01",
                    "properties": {
                      "operatorInstanceName": "[parameters('operatorInstanceName')]",
                      "operatorNamespace": "[parameters('operatorNamespace')]",
                      "operatorScope": "[parameters('operatorScope')]",
                      "operatorType": "[parameters('operatorType')]",
                      "operatorParams": "[parameters('operatorParams')]",
                      "repositoryUrl": "[parameters('repositoryUrl')]",
                      "enableHelmOperator": "[parameters('enableHelmOperator')]",
                      "helmOperatorProperties": {
                        "chartVersion": "[parameters('chartVersion')]",
                        "chartValues": "[parameters('chartValues')]"
                      },
                      "sshKnownHostsContents": "[parameters('sshKnownHostsContents')]",
                      "configurationProtectedSettings": {
                        "sshPrivateKey": "[parameters('sshPrivateKey')]"
                      }
                    }
                  }
                ]
              },
              "parameters": {
                "clusterLocation": {
                  "value": "[field('location')]"
                },
                "clusterName": {
                  "value": "[field('name')]"
                },
                "configurationResourceName": {
                  "value": "[parameters('configurationResourceName')]"
                },
                "operatorInstanceName": {
                  "value": "[parameters('operatorInstanceName')]"
                },
                "operatorNamespace": {
                  "value": "[parameters('operatorNamespace')]"
                },
                "operatorScope": {
                  "value": "[parameters('operatorScope')]"
                },
                "operatorType": {
                  "value": "[parameters('operatorType')]"
                },
                "operatorParams": {
                  "value": "[parameters('operatorParams')]"
                },
                "repositoryUrl": {
                  "value": "[parameters('repositoryUrl')]"
                },
                "enableHelmOperator": {
                  "value": "[parameters('enableHelmOperator')]"
                },
                "chartVersion": {
                  "value": "[parameters('chartVersion')]"
                },
                "chartValues": {
                  "value": "[parameters('chartValues')]"
                },
                "sshKnownHostsContents": {
                  "value": "[parameters('sshKnownHostsContents')]"
                },
                "sshPrivateKey": {
                  "reference": {
                    "keyVault": {
                      "id": "[parameters('keyVaultResourceId')]"
                    },
                    "secretName": "[parameters('sshPrivateKeyKeyVaultSecretName')]"
                  }
                },
                "clusterResourceType": {
                  "value": "[field('type')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.1.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/c050047b-b21b-4822-8a2d-c1e37c3c0c6a",
  "name": "c050047b-b21b-4822-8a2d-c1e37c3c0c6a"
}
Bylawc050047b-b21b-4822-8a2d-c1e37c3c0c6a.bylaw.ps1 · 132 lines
# Converted from Deploy-GitOps-to-Kubernetes-cluster-SSH-secrets_DINE.json by ConvertTo-Bylaw.
Policy 'c050047b-b21b-4822-8a2d-c1e37c3c0c6a' {

    Properties @{
        DisplayName   = 'Configure Kubernetes clusters with specified GitOps configuration using SSH secrets'
        Description   = 'Deploy a ''sourceControlConfiguration'' to Kubernetes clusters to assure that the clusters get their source of truth for workloads and configurations from the defined git repo. This definition requires a SSH private key secret in Key Vault. For instructions, visit https://aka.ms/K8sGitOpsPolicy.'
        Version       = '1.1.0'
        Category      = 'Kubernetes'
        PolicyVersion = '1.1.0'
        Mode          = 'Indexed'
    }

    Parameter ConfigurationResourceName @{
        Type        = 'String'
        DisplayName = 'Configuration resource name'
        Description = 'The name for the sourceControlConfiguration.  Learn more about setting up GitOps configuration: https://aka.ms/AzureArcK8sUsingGitOps.'
    }
    Parameter OperatorInstanceName @{
        Type        = 'String'
        DisplayName = 'Operator instance name'
        Description = 'Name used in the operator instances. Maximum of 23 lowercase alphanumeric characters or hyphen. Must start and end with an alphanumeric character.'
    }
    Parameter OperatorNamespace @{
        Type        = 'String'
        DisplayName = 'Operator namespace'
        Description = 'Namespace within which the operators will be installed. Maximum of 23 lowercase alphanumeric characters or hyphen. Must start and end with an alphanumeric character.'
    }
    Parameter OperatorScope @{
        AllowedValues = @('cluster', 'namespace')
        Default       = 'namespace'
        DisplayName   = 'Operator scope'
        Description   = 'The permission scope for the operator. Possible values are ''cluster'' (full access) or ''namespace'' (restricted access).'
    }
    Parameter OperatorType @{
        AllowedValues = @('Flux')
        Default       = 'Flux'
        DisplayName   = 'Operator type'
        Description   = 'The type of operator to install. Currently, ''Flux'' is supported.'
    }
    Parameter OperatorParams @{
        Default     = ''
        DisplayName = 'Operator parameters'
        Description = 'Parameters to set on the Flux operator, separated by spaces.  For example, --git-readonly --sync-garbage-collection.  Learn more: http://aka.ms/AzureArcK8sFluxOperatorParams.'
    }
    Parameter RepositoryUrl @{
        Type        = 'String'
        DisplayName = 'Repository Url'
        Description = 'The URL for the source control repository. Learn more about URL formats: https://aka.ms/GitOpsRepoUrlParameters'
    }
    Parameter EnableHelmOperator @{
        AllowedValues = @('true', 'false')
        Default       = 'true'
        DisplayName   = 'Enable Helm'
        Description   = 'Indicate whether to enable Helm for this instance of Flux. Learn more: http://aka.ms/AzureArcK8sGitOpsWithHelm.'
    }
    Parameter ChartVersion @{
        Default     = '1.2.0'
        DisplayName = 'Helm chart version for installing Flux Helm'
        Description = 'The version of the Helm chart for installing Flux Helm. For example, 1.2.0'
    }
    Parameter ChartValues @{
        Default     = ''
        DisplayName = 'Helm chart parameters for installing Flux Helm'
        Description = 'Parameters for the Helm chart for installing Flux Helm, separated by spaces. For example, --set helm.versions=v3'
    }
    Parameter SshKnownHostsContents @{
        Default     = ''
        DisplayName = 'Base64-encoded known hosts content'
        Description = 'The base64-encoded known hosts content.'
    }
    Parameter KeyVaultResourceId @{
        Default     = ''
        DisplayName = 'Key Vault resource id'
        Description = 'The resource id for the Key Vault that holds the SSH or HTTPS secrets. For example: ''/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.KeyVault/vaults/<key-vault-name>'''
        Metadata    = [ordered]@{ strongType = 'Microsoft.KeyVault/vaults'; assignPermissions = $true }
    }
    Parameter SshPrivateKeyKeyVaultSecretName @{
        Default     = ''
        DisplayName = 'SSH private key Key Vault secret'
        Description = 'The name of the Key Vault secret that holds the base64-encoded SSH private key.'
    }
    Parameter Effect @{
        AllowedValues = @('auditIfNotExists', 'AuditIfNotExists', 'deployIfNotExists', 'DeployIfNotExists', 'disabled', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource * {

        Require 'type is not one of 2 values' {
            Field type | Must -Not -BeIn @('Microsoft.Kubernetes/connectedClusters', 'Microsoft.ContainerService/managedClusters') -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.KubernetesConfiguration/sourceControlConfigurations' -Role 'Contributor' -Name (Param ConfigurationResourceName) -Effect (Param Effect) {
        Require 'operatorParams, repositoryUrl, sshKnownHostsContents and 3 more are as required' {
            Field operatorParams | Must -BeIn @('[parameters(''operatorParams'')]', '[concat(''--git-readonly '',parameters(''operatorParams''))]')
            Field repositoryUrl | Must -Be (Param RepositoryUrl)
            Field sshKnownHostsContents | Must -Be (Param SshKnownHostsContents)
            AnyOf {
                Field enableHelmOperator | Must -Be 'false'
                AllOf {
                    Field enableHelmOperator | Must -Be 'true'
                    Field helmOperatorProperties.chartVersion | Must -Be (Param ChartVersion)
                    Field helmOperatorProperties.chartValues | Must -Be (Param ChartValues)
                }
            }
        }
        Deployment @{
            TemplateFile = './templates/c050047b-b21b-4822-8a2d-c1e37c3c0c6a.json'
            Scope        = 'ResourceGroup'
            Parameters   = [ordered]@{
                clusterLocation           = (Arm '[field(''location'')]')
                clusterName               = (Arm '[field(''name'')]')
                configurationResourceName = (Param ConfigurationResourceName)
                operatorInstanceName      = (Param OperatorInstanceName)
                operatorNamespace         = (Param OperatorNamespace)
                operatorScope             = (Param OperatorScope)
                operatorType              = (Param OperatorType)
                operatorParams            = (Param OperatorParams)
                repositoryUrl             = (Param RepositoryUrl)
                enableHelmOperator        = (Param EnableHelmOperator)
                chartVersion              = (Param ChartVersion)
                chartValues               = (Param ChartValues)
                sshKnownHostsContents     = (Param SshKnownHostsContents)
                sshPrivateKey             = (KeyVaultSecret -VaultId (Param KeyVaultResourceId) -SecretName (Param SshPrivateKeyKeyVaultSecretName))
                clusterResourceType       = (Arm '[field(''type'')]')
            }
        }
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Kubernetes/Deploy-GitOps-to-Kubernetes-cluster-SSH-secrets_DINE.json in Azure/azure-policy, under the MIT licence.