Bylaw

Gallery › Kubernetes › Azure built-in policies

Kubernetes cluster pods and containers should only run with approved user and group IDs

Doesn't convert yet Policy Audit Deny Disabled Mode Microsoft.Kubernetes.Data Version 6.2.0

Control the user, primary group, supplemental group and file system group IDs that pods and containers can use to run in a Kubernetes Cluster. This policy is generally available for Kubernetes Service (AKS), and preview for Azure Arc enabled Kubernetes. For more information, see https://aka.ms/kubepolicydoc.

ConvertTo-Bylaw can't convert this policy yet:

Azure Policy JSONAllowedUsersGroups.json · 417 lines
{
  "properties": {
    "displayName": "Kubernetes cluster pods and containers should only run with approved user and group IDs",
    "policyType": "BuiltIn",
    "mode": "Microsoft.Kubernetes.Data",
    "description": "Control the user, primary group, supplemental group and file system group IDs that pods and containers can use to run in a Kubernetes Cluster. This policy is generally available for Kubernetes Service (AKS), and preview for Azure Arc enabled Kubernetes. For more information, see https://aka.ms/kubepolicydoc.",
    "metadata": {
      "version": "6.2.0",
      "category": "Kubernetes"
    },
    "version": "6.2.0",
    "parameters": {
      "source": {
        "type": "String",
        "metadata": {
          "displayName": "Source",
          "description": "The source k8s object for constraint evaluation. 'Original' means only evaluate against the specific GroupVersionKind specified in the policy definition. 'Generated' means only evaluate against k8s objects generated by Gatekeeper ExpansionTemplates. 'All' means evaluate against both the original object and any generated ones."
        },
        "defaultValue": "Original",
        "allowedValues": [
          "All",
          "Generated",
          "Original"
        ]
      },
      "warn": {
        "type": "Boolean",
        "metadata": {
          "displayName": "Warn",
          "description": "Whether or not to return warnings back to the user in the kubectl cli"
        },
        "defaultValue": false
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "'Audit' allows a non-compliant resource to be created or updated, but flags it as non-compliant. 'Deny' blocks the non-compliant resource creation or update. 'Disabled' turns off the policy.",
          "portalReview": true
        },
        "allowedValues": [
          "audit",
          "Audit",
          "deny",
          "Deny",
          "disabled",
          "Disabled"
        ],
        "defaultValue": "Audit"
      },
      "excludedNamespaces": {
        "type": "Array",
        "metadata": {
          "displayName": "Namespace exclusions",
          "description": "List of Kubernetes namespaces to exclude from policy evaluation. System namespaces \"kube-system\", \"gatekeeper-system\" and \"azure-arc\" are always excluded by design. \"azure-extensions-usage-system\" is optional to remove."
        },
        "defaultValue": [
          "kube-system",
          "gatekeeper-system",
          "azure-arc",
          "azure-extensions-usage-system"
        ]
      },
      "namespaces": {
        "type": "Array",
        "metadata": {
          "displayName": "Namespace inclusions",
          "description": "List of Kubernetes namespaces to only include in policy evaluation. An empty list means the policy is applied to all resources in all namespaces."
        },
        "defaultValue": []
      },
      "labelSelector": {
        "type": "object",
        "metadata": {
          "displayName": "Kubernetes label selector",
          "description": "Label query to select Kubernetes resources for policy evaluation. An empty label selector matches all Kubernetes resources."
        },
        "defaultValue": {},
        "schema": {
          "description": "A label selector is a label query over a set of resources. The result of matchLabels and matchExpressions are ANDed. An empty label selector matches all resources.",
          "type": "object",
          "properties": {
            "matchLabels": {
              "description": "matchLabels is a map of {key,value} pairs.",
              "type": "object",
              "additionalProperties": {
                "type": "string"
              },
              "minProperties": 1
            },
            "matchExpressions": {
              "description": "matchExpressions is a list of values, a key, and an operator.",
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "key": {
                    "description": "key is the label key that the selector applies to.",
                    "type": "string"
                  },
                  "operator": {
                    "description": "operator represents a key's relationship to a set of values.",
                    "type": "string",
                    "enum": [
                      "In",
                      "NotIn",
                      "Exists",
                      "DoesNotExist"
                    ]
                  },
                  "values": {
                    "description": "values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty.",
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                },
                "required": [
                  "key",
                  "operator"
                ],
                "additionalProperties": false
              },
              "minItems": 1
            }
          },
          "additionalProperties": false
        }
      },
      "runAsUserRule": {
        "type": "String",
        "metadata": {
          "displayName": "Run as user rule",
          "description": "The 'RunAsUser' rule that containers are allowed to run with. MustRunAs requires at least one range to be specified. MustRunAsNonRoot requires the pod be submitted with non-zero runAsUser or have USER directive defined (using a numeric UID) in the image. RunAsAny allows any runAsUser to be specified",
          "portalReview": true
        },
        "allowedValues": [
          "MustRunAs",
          "MustRunAsNonRoot",
          "RunAsAny"
        ],
        "defaultValue": "MustRunAsNonRoot"
      },
      "runAsUserRanges": {
        "type": "Object",
        "metadata": {
          "displayName": "Allowed user ID ranges",
          "description": "The user ID ranges that are allowed for containers to use. Set 'max' as '-1' to skip max limit evaluation. Empty array blocks every defined value for 'MustRunAs'.",
          "portalReview": true
        },
        "defaultValue": {
          "ranges": []
        },
        "schema": {
          "type": "object",
          "properties": {
            "ranges": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "min": {
                    "type": "integer"
                  },
                  "max": {
                    "type": "integer"
                  }
                },
                "required": [
                  "min",
                  "max"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "ranges"
          ],
          "additionalProperties": false
        }
      },
      "runAsGroupRule": {
        "type": "String",
        "metadata": {
          "displayName": "Run as group rule",
          "description": "The 'RunAsGroup' rule that containers are allowed to run with. MustRunAs requires at least one range to be specified. MayRunAs does not require that 'RunAsGroup' be specified. RunAsAny allows any",
          "portalReview": true
        },
        "allowedValues": [
          "MustRunAs",
          "MayRunAs",
          "RunAsAny"
        ],
        "defaultValue": "RunAsAny"
      },
      "runAsGroupRanges": {
        "type": "Object",
        "metadata": {
          "displayName": "Allowed group ID ranges",
          "description": "The group ID ranges that are allowed for containers to use. Set 'max' as '-1' to skip max limit evaluation. Empty array blocks every defined value for 'MustRunAs' and 'MayRunAs'.",
          "portalReview": true
        },
        "defaultValue": {
          "ranges": []
        },
        "schema": {
          "type": "object",
          "properties": {
            "ranges": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "min": {
                    "type": "integer"
                  },
                  "max": {
                    "type": "integer"
                  }
                },
                "required": [
                  "min",
                  "max"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "ranges"
          ],
          "additionalProperties": false
        }
      },
      "supplementalGroupsRule": {
        "type": "String",
        "metadata": {
          "displayName": "Supplemental group rule",
          "description": "The 'SupplementalGroups' rule that containers are allowed to run with. MustRunAs requires at least one range to be specified. MayRunAs does not require that 'SupplementalGroups' be specified. RunAsAny allows any",
          "portalReview": true
        },
        "allowedValues": [
          "MustRunAs",
          "MayRunAs",
          "RunAsAny"
        ],
        "defaultValue": "RunAsAny"
      },
      "supplementalGroupsRanges": {
        "type": "Object",
        "metadata": {
          "displayName": "Allowed supplemental group ID ranges",
          "description": "The supplemental group ID ranges that are allowed for containers to use. Set 'max' as '-1' to skip max limit evaluation. Empty array blocks every defined value for 'MustRunAs' and 'MayRunAs'.",
          "portalReview": true
        },
        "defaultValue": {
          "ranges": []
        },
        "schema": {
          "type": "object",
          "properties": {
            "ranges": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "min": {
                    "type": "integer"
                  },
                  "max": {
                    "type": "integer"
                  }
                },
                "required": [
                  "min",
                  "max"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "ranges"
          ],
          "additionalProperties": false
        }
      },
      "fsGroupRule": {
        "type": "String",
        "metadata": {
          "displayName": "File system group rule",
          "description": "The 'FSGroup' rule that containers are allowed to run with. MustRunAs requires at least one range to be specified. MayRunAs does not require that 'FSGroup' be specified. RunAsAny allows any",
          "portalReview": true
        },
        "allowedValues": [
          "MustRunAs",
          "MayRunAs",
          "RunAsAny"
        ],
        "defaultValue": "RunAsAny"
      },
      "fsGroupRanges": {
        "type": "Object",
        "metadata": {
          "displayName": "Allowed file system group ID ranges",
          "description": "The file system group ranges that are allowed for pods to use. Set 'max' as '-1' to skip max limit evaluation. Empty array blocks every defined value for 'MustRunAs' and 'MayRunAs'.",
          "portalReview": true
        },
        "defaultValue": {
          "ranges": []
        },
        "schema": {
          "type": "object",
          "properties": {
            "ranges": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "min": {
                    "type": "integer"
                  },
                  "max": {
                    "type": "integer"
                  }
                },
                "required": [
                  "min",
                  "max"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "ranges"
          ],
          "additionalProperties": false
        }
      },
      "excludedContainers": {
        "type": "Array",
        "metadata": {
          "displayName": "Containers exclusions",
          "description": "The list of InitContainers and Containers to exclude from policy evaluation. The identify is the name of container. Use an empty list to apply this policy to all containers in all namespaces."
        },
        "defaultValue": []
      },
      "excludedImages": {
        "type": "Array",
        "metadata": {
          "displayName": "Image exclusions",
          "description": "The list of InitContainers and Containers to exclude from policy evaluation. The identifier is the image of container. Prefix-matching can be signified with `*`. For example: `myregistry.azurecr.io/istio:*`. It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name) in order to avoid unexpectedly exempting images from an untrusted repository.",
          "portalReview": true
        },
        "defaultValue": []
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "in": [
          "Microsoft.Kubernetes/connectedClusters",
          "Microsoft.ContainerService/managedClusters"
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "source": "[parameters('source')]",
          "warn": "[parameters('warn')]",
          "templateInfo": {
            "sourceType": "PublicURL",
            "url": "https://store.policy.core.windows.net/kubernetes/allowed-users-groups/v3/template.yaml"
          },
          "apiGroups": [
            ""
          ],
          "kinds": [
            "Pod"
          ],
          "excludedNamespaces": "[parameters('excludedNamespaces')]",
          "namespaces": "[parameters('namespaces')]",
          "labelSelector": "[parameters('labelSelector')]",
          "values": {
            "runAsUser": {
              "rule": "[parameters('runAsUserRule')]",
              "ranges": "[parameters('runAsUserRanges').ranges]"
            },
            "runAsGroup": {
              "rule": "[parameters('runAsGroupRule')]",
              "ranges": "[parameters('runAsGroupRanges').ranges]"
            },
            "supplementalGroups": {
              "rule": "[parameters('supplementalGroupsRule')]",
              "ranges": "[parameters('supplementalGroupsRanges').ranges]"
            },
            "fsGroup": {
              "rule": "[parameters('fsGroupRule')]",
              "ranges": "[parameters('fsGroupRanges').ranges]"
            },
            "excludedContainers": "[parameters('excludedContainers')]",
            "excludedImages": "[parameters('excludedImages')]"
          }
        }
      }
    },
    "versions": [
      "6.2.0",
      "6.1.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/f06ddb64-5fa3-4b77-b166-acb36f7f6042",
  "name": "f06ddb64-5fa3-4b77-b166-acb36f7f6042"
}

Source: built-in-policies/policyDefinitions/Kubernetes/AllowedUsersGroups.json in Azure/azure-policy, under the MIT licence.