Post

Domain join Azure Policy-vel

Kizárólag Azure-ban VM-et futtatni nem túl hatékony, azonban nagyvállalati környezetben sajnos elkerülhetetlen. Mindig lesz egy olyan alkalmazás, ami csak Legacy Windows-on fut, vagy csak siman Windows-on fut jól. Ha már használni kell, akkor az legalább legyen biztonságos és automatizált. Az Azure Policy segítségével könnyedén megoldható a Domain Join automatizálása, így anélkül, hogy ki kellene adnunk a Domain Join-hoz szükséges jogosultságokat, könnyen csatlakoztathatjuk a VM-eket a Domain-hez. Mivel elég alap dologról beszélünk, így várható, hogy valaki már megírta ezt, így nekünk már nem kell ezzel vesződnünk. Az alábbi példa egy ilyen Azure Policy, ami automatikusan csatlakoztatja a VM-et a Domain-hez, ha az az előírt feltételeknek megfelel.

Forrás: https://www.azadvertizer.net/azpolicyadvertizer/Deploy-Windows-DomainJoin.html

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
{
  "mode": "Indexed",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Compute/virtualMachines"
        },
        {
          "field": "Microsoft.Compute/imagePublisher",
          "equals": "MicrosoftWindowsServer"
        },
        {
          "field": "Microsoft.Compute/imageOffer",
          "equals": "WindowsServer"
        },
        {
          "field": "Microsoft.Compute/imageSKU",
          "in": [
            "2008-R2-SP1",
            "2008-R2-SP1-smalldisk",
            "2008-R2-SP1-zhcn",
            "2012-Datacenter",
            "2012-datacenter-gensecond",
            "2012-Datacenter-smalldisk",
            "2012-datacenter-smalldisk-g2",
            "2012-Datacenter-zhcn",
            "2012-datacenter-zhcn-g2",
            "2012-R2-Datacenter",
            "2012-r2-datacenter-gensecond",
            "2012-R2-Datacenter-smalldisk",
            "2012-r2-datacenter-smalldisk-g2",
            "2012-R2-Datacenter-zhcn",
            "2012-r2-datacenter-zhcn-g2",
            "2016-Datacenter",
            "2016-datacenter-gensecond",
            "2016-datacenter-gs",
            "2016-Datacenter-Server-Core",
            "2016-datacenter-server-core-g2",
            "2016-Datacenter-Server-Core-smalldisk",
            "2016-datacenter-server-core-smalldisk-g2",
            "2016-Datacenter-smalldisk",
            "2016-datacenter-smalldisk-g2",
            "2016-Datacenter-with-Containers",
            "2016-datacenter-with-containers-g2",
            "2016-Datacenter-with-RDSH",
            "2016-Datacenter-zhcn",
            "2016-datacenter-zhcn-g2",
            "2019-Datacenter",
            "2019-Datacenter-Core",
            "2019-datacenter-core-g2",
            "2019-Datacenter-Core-smalldisk",
            "2019-datacenter-core-smalldisk-g2",
            "2019-Datacenter-Core-with-Containers",
            "2019-datacenter-core-with-containers-g2",
            "2019-Datacenter-Core-with-Containers-smalldisk",
            "2019-datacenter-core-with-containers-smalldisk-g2",
            "2019-datacenter-gensecond",
            "2019-datacenter-gs",
            "2019-Datacenter-smalldisk",
            "2019-datacenter-smalldisk-g2",
            "2019-Datacenter-with-Containers",
            "2019-datacenter-with-containers-g2",
            "2019-Datacenter-with-Containers-smalldisk",
            "2019-datacenter-with-containers-smalldisk-g2",
            "2019-Datacenter-zhcn",
            "2019-datacenter-zhcn-g2",
            "Datacenter-Core-1803-with-Containers-smalldisk",
            "datacenter-core-1803-with-containers-smalldisk-g2",
            "Datacenter-Core-1809-with-Containers-smalldisk",
            "datacenter-core-1809-with-containers-smalldisk-g2",
            "Datacenter-Core-1903-with-Containers-smalldisk",
            "datacenter-core-1903-with-containers-smalldisk-g2",
            "datacenter-core-1909-with-containers-smalldisk",
            "datacenter-core-1909-with-containers-smalldisk-g1",
            "datacenter-core-1909-with-containers-smalldisk-g2"
          ]
        }
      ]
    },
    "then": {
      "effect": "[parameters('effect')]",
      "details": {
        "evaluationDelay": "AfterProvisioningSuccess",
        "type": "Microsoft.Compute/virtualMachines/extensions",
        "roleDefinitionIds": [
          "/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c"
        ],
        "existenceCondition": {
          "allOf": [
            {
              "field": "Microsoft.Compute/virtualMachines/extensions/type",
              "equals": "JsonADDomainExtension"
            },
            {
              "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
              "equals": "Microsoft.Compute"
            }
          ]
        },
        "deployment": {
          "properties": {
            "mode": "Incremental",
            "parameters": {
              "vmName": {
                "value": "[field('name')]"
              },
              "location": {
                "value": "[field('location')]"
              },
              "domainUsername": {
                "reference": {
                  "keyVault": {
                    "id": "[parameters('keyVaultResourceId')]"
                  },
                  "secretName": "[parameters('domainUsername')]"
                }
              },
              "domainPassword": {
                "reference": {
                  "keyVault": {
                    "id": "[parameters('keyVaultResourceId')]"
                  },
                  "secretName": "[parameters('domainPassword')]"
                }
              },
              "domainOUPath": {
                "value": "[parameters('domainOUPath')]"
              },
              "domainFQDN": {
                "value": "[parameters('domainFQDN')]"
              },
              "keyVaultResourceId": {
                "value": "[parameters('keyVaultResourceId')]"
              }
            },
            "template": {
              "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
              "contentVersion": "1.0.0.0",
              "parameters": {
                "vmName": {
                  "type": "String"
                },
                "location": {
                  "type": "String"
                },
                "domainUsername": {
                  "type": "String"
                },
                "domainPassword": {
                  "type": "securestring"
                },
                "domainFQDN": {
                  "type": "String"
                },
                "domainOUPath": {
                  "type": "String"
                },
                "keyVaultResourceId": {
                  "type": "String"
                }
              },
              "variables": {
                "domainJoinOptions": 3,
                "vmName": "[parameters('vmName')]"
              },
              "resources": [
                {
                  "apiVersion": "2015-06-15",
                  "type": "Microsoft.Compute/virtualMachines/extensions",
                  "name": "[concat(variables('vmName'),'/joindomain')]",
                  "location": "[resourceGroup().location]",
                  "properties": {
                    "publisher": "Microsoft.Compute",
                    "type": "JsonADDomainExtension",
                    "typeHandlerVersion": "1.3",
                    "autoUpgradeMinorVersion": true,
                    "settings": {
                      "Name": "[parameters('domainFQDN')]",
                      "User": "[parameters('domainUserName')]",
                      "Restart": "true",
                      "Options": "[variables('domainJoinOptions')]",
                      "OUPath": "[parameters('domainOUPath')]"
                    },
                    "protectedSettings": {
                      "Password": "[parameters('domainPassword')]"
                    }
                  }
                }
              ],
              "outputs": {}
            }
          }
        }
      }
    }
  },
  "parameters": {
    "domainUsername": {
      "type": "String",
      "metadata": {
        "displayName": "domainUsername",
        "description": null
      }
    },
    "domainPassword": {
      "type": "String",
      "metadata": {
        "displayName": "domainPassword",
        "description": null
      }
    },
    "domainFQDN": {
      "type": "String",
      "metadata": {
        "displayName": "domainFQDN",
        "description": null
      }
    },
    "domainOUPath": {
      "type": "String",
      "metadata": {
        "displayName": "domainOUPath",
        "description": null
      }
    },
    "keyVaultResourceId": {
      "type": "String",
      "metadata": {
        "displayName": "keyVaultResourceId",
        "description": null
      }
    },
    "effect": {
      "type": "String",
      "metadata": {
        "displayName": "Effect",
        "description": "Enable or disable the execution of the policy"
      },
      "allowedValues": [
        "DeployIfNotExists",
        "Disabled"
      ],
      "defaultValue": "DeployIfNotExists"
    }
  }
}

Természetesen ez a Policy csak egy példa, és a saját környezetünknek megfelelően kell assign-olni. Gondolnunk kell a következőkre:

  • Milyen image-et engedünk meg a Domain-hez csatlakozni?
  • Milyen OU-ba kerüljön a VM?
  • Milyen felhasználóval csatlakozzon a VM a Domain-hez?
  • Milyen jelszóval csatlakozzon a VM a Domain-hez?
  • Milyen KeyVault-ból vegye a jelszót?

Ami viszont nincs még itt lekezelve, de egy valós környezetben fontos lehet, például a névkonvenció! Nem mindegy ugyanis, hogy a VM-nek milyen elnevezése van, hiszen egy név csak egyszer használható egy Domain-ban. Annak függvényében, hogy alakítottuk ki a névkonvenciót, azt ellenőrizhetjük ebben a policy-ban is, így csak akkor engedjük meg a Domain-hez csatlakozást, ha a név megfelelő.

Fontos, hogy megfelelő jogosultsággal kell rendelkeznie a Policy-nak, hogy a VM csatlakoztatni tudja a Domain-hez. Ami gyakori probléma, hogy még a Key Vault-ban is engedélyeni kell a deployment során a jelszó lekérdezését.

KeyVault

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