Post

Azure Policy-val VM-hez jogosultság hozzárendelés OS alapján

Felmerült egy olyan kérdés, hogy lehet-e automatikusan az adott VM-hez hozzárendelni AD csoportot az alapján, hogy az Windows vagy Linuxos VM.

Válasz, persze :)

Azure Policy-val mondanám, hogy minden is megoldható, de egy másik területen éppen belefutottam egy limitációba, de erről majd máskor, szóval SZINTE minden megoldható, többek között ez is… A logika mögötte elég egyszerű, amikor létrejön egy ‘Microsoft.Compute/virtualMachines’ típusú erőforrás, automatikusan elindít egy deploymentet, mely hozzá rendeli a kívánt csoportot a megadott jogosultsági szinttel (RBAC). Amennyiben szeretnénk még inkább minimálizálni a kiosztott jogosultságokat, úgy létrehozhatunk egy custom RBAC-ot, mellyel a VM-et csak restartolhatjuk pl.

Előfeltételek

A környezet létrehozásához a következőre van szükség:

  • Azure előfizetés amiben van pár $ (Policy maga ingyenes, csak amit ellenőrzünk vele, annak van költsége)

Policy létrehozása

Magát a policy létrehozását már leírtam ebben a postban, a menete pedig ugyanaz, csak ezt a kódot kell használni ;)

Windows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Compute/virtualMachines"
        },
        {
          "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.offer",
          "contains": "Windows"
        }
      ]
    },
    "then": {
      "effect": "deployIfNotExists",
      "details": {
        "type": "Microsoft.Authorization/roleAssignments",
        "evaluationDelay": "AfterProvisioning",
        "roleDefinitionIds": [
          "/providers/microsoft.authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"
        ],
        "existenceCondition": {
          "allOf": [
            {
              "field": "Microsoft.Authorization/roleAssignments/roleDefinitionId",
              "like": "[concat('*',parameters('rbacid'))]"
            },
            {
              "field": "Microsoft.Authorization/roleAssignments/principalId",
              "equals": "[parameters('groupid')]"
            }
          ]
        },
        "deployment": {
          "properties": {
            "mode": "incremental",
            "parameters": {
              "groupid": {
                "value": "[parameters('groupid')]"
              },
              "rbacid": {
                "value": "[parameters('rbacid')]"
              },
              "vmid": {
                "value": "[field('id')]"
              }
            },
            "template": {
              "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
              "contentVersion": "1.0.0.0",
              "parameters": {
                "groupid": {
                  "type": "string"
                },
                "rbacid": {
                  "type": "string"
                },
                "vmid": {
                  "type": "string"
                }
              },
              "variables": {
                "roleDefinitionId": "[resourceId('Microsoft.Authorization/roleDefinitions', parameters('rbacid'))]"
              },
              "resources": [
                {
                  "type": "Microsoft.Authorization/roleAssignments",
                  "apiVersion": "2018-09-01-preview",
                  "name": "[guid(resourceGroup().id, deployment().name)]",
                  "scope": "[parameters('vmid')]",
                  "properties": {
                    "roleDefinitionId": "[variables('roleDefinitionId')]",
                    "principalId": "[parameters('groupid')]"
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "parameters": {
    "rbacid": {
      "type": "String",
      "metadata": {
        "displayName": "rbacid",
        "description": "rbacid"
      }
    },
    "groupid": {
      "type": "String",
      "metadata": {
        "displayName": "groupid",
        "description": "groupid"
      }
    }
  }
}

Linux

Itt, mivel sok az offer lehetőség, megfordítottam, szóval minden, ami nem Windows a feltételem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Compute/virtualMachines"
        },
        {
          "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.offer",
          "notContains": "Windows"
        }
      ]
    },
    "then": {
      "effect": "deployIfNotExists",
      "details": {
        "type": "Microsoft.Authorization/roleAssignments",
        "evaluationDelay": "AfterProvisioning",
        "roleDefinitionIds": [
          "/providers/microsoft.authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"
        ],
        "existenceCondition": {
          "allOf": [
            {
              "field": "Microsoft.Authorization/roleAssignments/roleDefinitionId",
              "like": "[concat('*',parameters('rbacid'))]"
            },
            {
              "field": "Microsoft.Authorization/roleAssignments/principalId",
              "equals": "[parameters('groupid')]"
            }
          ]
        },
        "deployment": {
          "properties": {
            "mode": "incremental",
            "parameters": {
              "groupid": {
                "value": "[parameters('groupid')]"
              },
              "rbacid": {
                "value": "[parameters('rbacid')]"
              },
              "vmid": {
                "value": "[field('id')]"
              }
            },
            "template": {
              "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
              "contentVersion": "1.0.0.0",
              "parameters": {
                "groupid": {
                  "type": "string"
                },
                "rbacid": {
                  "type": "string"
                },
                "vmid": {
                  "type": "string"
                }
              },
              "variables": {
                "roleDefinitionId": "[resourceId('Microsoft.Authorization/roleDefinitions', parameters('rbacid'))]"
              },
              "resources": [
                {
                  "type": "Microsoft.Authorization/roleAssignments",
                  "apiVersion": "2018-09-01-preview",
                  "name": "[guid(resourceGroup().id, deployment().name)]",
                  "scope": "[parameters('vmid')]",
                  "properties": {
                    "roleDefinitionId": "[variables('roleDefinitionId')]",
                    "principalId": "[parameters('groupid')]"
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "parameters": {
    "rbacid": {
      "type": "String",
      "metadata": {
        "displayName": "rbacid",
        "description": "rbacid"
      }
    },
    "groupid": {
      "type": "String",
      "metadata": {
        "displayName": "groupid",
        "description": "groupid"
      }
    }
  }
}

RBAC

Minimális jogosultságra egy példa, mellyel csak elindítani, újraindítani és leállítani lehet a VM-et:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
    "properties": {
        "roleName": "VM start-stop-restart",
        "description": "",
        "assignableScopes": [
            "/providers/Microsoft.Management/managementGroups/604f5222-cd10-48f0-ae0c-585414dd4443"
        ],
        "permissions": [
            {
                "actions": [
                    "Microsoft.Compute/virtualMachines/start/action",
                    "Microsoft.Compute/virtualMachines/read",
                    "Microsoft.Compute/virtualMachines/restart/action",
                    "Microsoft.Compute/virtualMachines/deallocate/action"
                ],
                "notActions": [],
                "dataActions": [],
                "notDataActions": []
            }
        ]
    }
}

Assign

Miután létrehoztuk a a custom policy-t, azt ki kell ajánlani és két adatot meg kell adni. img-description

  • rbacid: A jogosultsági szintnek az objektum azonosítóját kell megadni. (Fontos, ha egyéni jogosultsági szintet akarunk használni, akkor annak elérhetőnek kell lennie az erőforrás szintjén is ahol felhasználnánk)
  • groupid: AzureAD-ban található csoport objektum azonosítóját kell megadni.

Végeredmény

Ha mindent jól csináltunk, (és vártunk 30 percet, hogy tényleg aktiválódjon a policy) a következő gép létrehozáskor már hozzá is fog rendelődni az új jogosultság. img-description
img-description

This post is licensed under CC BY 4.0 by the author.