Active Directory の検証環境を30分で構築する

2022年5月26日木曜日

AD Azure Windows 検証

t f B! P L

背景

Active Directory(以下、AD) 側の設定を変更する際、

本番環境に適用する前に検証したいのですが、

AD側が本番環境しかなく検証できないといったことないですか。

 

また、ADの管理は別チームが行っており、

気軽にADの検証ができないといったこともありますよね。

 

対策

Azureを使えば、少ないステップでADの検証環境を構築でき、

検証が終わった後は環境を削除することでコストも抑えることができます。

 

今回はADの検証環境を少ないステップでAzure上に構築する方法を紹介します。

 

作成手順

 

Bicepという技術を使って、下記のような構成を作ります。



 

Bicepを使用したデプロイ環境がない場合は下記を参照してください。

https://docs.microsoft.com/ja-jp/azure/azure-resource-manager/bicep/install

 

次に下記のソースコードをmain.bicepのファイル名で保存します。

 

@description('Username for the Virtual Machine.')

param adminUsername string ='dev'

@description('Password for the Virtual Machine.')

@minLength(12)

@secure()

param adminPassword string

@description('Unique DNS Name for the Public IP used to access the Virtual Machine.')

param dnsLabelPrefix string = toLower('${vmName}-${uniqueString(resourceGroup().id, vmName)}')

@description('Name for the Public IP used to access the Virtual Machine.')

param publicIpName string = 'myPublicIP'

@description('Allocation method for the Public IP used to access the Virtual Machine.')

@allowed([

  'Dynamic'

  'Static'

])

param publicIPAllocationMethod string = 'Dynamic'

@description('SKU for the Public IP used to access the Virtual Machine.')

@allowed([

  'Basic'

  'Standard'

])

param publicIpSku string = 'Basic'

@description('The Windows version for the VM. This will pick a fully patched Gen2 image of this given Windows version.')

@allowed([

 '2019-datacenter-gensecond'

 '2019-datacenter-core-gensecond'

 '2019-datacenter-core-smalldisk-gensecond'

 '2019-datacenter-core-with-containers-gensecond'

 '2019-datacenter-core-with-containers-smalldisk-g2'

 '2019-datacenter-smalldisk-gensecond'

 '2019-datacenter-with-containers-gensecond'

 '2019-datacenter-with-containers-smalldisk-g2'

 '2016-datacenter-gensecond'

])

param OSVersion string = '2019-datacenter-gensecond'

@description('Size of the virtual machine.')

param vmSize string = 'Standard_D2s_v3'

@description('Location for all resources.')

param location string = resourceGroup().location

@description('Name of the virtual machine.')

param vmName string = 'addc'

var storageAccountName = 'bootdiags${uniqueString(resourceGroup().id)}'

var nicName = 'myVMNic'

var addressPrefix = '10.0.0.0/16'

var subnetName = 'Subnet'

var subnetPrefix = '10.0.0.0/24'

var virtualNetworkName = 'MyVNET'

var networkSecurityGroupName = 'default-NSG'

resource stg 'Microsoft.Storage/storageAccounts@2021-04-01' = {

  name: storageAccountName

  location: location

  sku: {

    name: 'Standard_LRS'

  }

  kind: 'Storage'

}

 

resource securityGroup 'Microsoft.Network/networkSecurityGroups@2021-02-01' = {

  name: networkSecurityGroupName

  location: location

  properties: {

    securityRules: [

      {

        name: 'default-allow-3389'

        properties: {

          priority: 1000

          access: 'Allow'

          direction: 'Inbound'

          destinationPortRange: '3389'

          protocol: 'Tcp'

          sourcePortRange: '*'

          sourceAddressPrefix: '*'

          destinationAddressPrefix: '*'

        }

      }

    ]

  }

}

resource pip 'Microsoft.Network/publicIPAddresses@2021-02-01' = {

  name: publicIpName

  location: location

  sku: {

    name: publicIpSku

  }

  properties: {

    publicIPAllocationMethod: publicIPAllocationMethod

    dnsSettings: {

      domainNameLabel: dnsLabelPrefix

    }

  }

}

resource vn 'Microsoft.Network/virtualNetworks@2021-02-01' = {

  name: virtualNetworkName

  location: location

  properties: {

    addressSpace: {

      addressPrefixes: [

        addressPrefix

      ]

    }

    subnets: [

      {

        name: subnetName

        properties: {

          addressPrefix: subnetPrefix

          networkSecurityGroup: {

            id: securityGroup.id

          }

        }

      }

    ]

  }

}

resource nic 'Microsoft.Network/networkInterfaces@2021-02-01' = {

  name: nicName

  location: location

  properties: {

    ipConfigurations: [

      {

        name: 'ipconfig1'

        properties: {

          privateIPAddress: '10.0.0.4'

          privateIPAllocationMethod: 'Static'

          publicIPAddress: {

            id: pip.id

          }

          subnet: {

            id: resourceId('Microsoft.Network/virtualNetworks/subnets', vn.name, subnetName)

          }

        }

      }

    ]

  }

}

resource vm 'Microsoft.Compute/virtualMachines@2021-03-01' = {

  name: vmName

  location: location

  properties: {

    hardwareProfile: {

      vmSize: vmSize

    }

    osProfile: {

      computerName: vmName

      adminUsername: adminUsername

      adminPassword: adminPassword

    }

    storageProfile: {

      imageReference: {

        publisher: 'MicrosoftWindowsServer'

        offer: 'WindowsServer'

        sku: OSVersion

        version: 'latest'

      }

      osDisk: {

        createOption: 'FromImage'

        managedDisk: {

          storageAccountType: 'StandardSSD_LRS'

        }

      }

      dataDisks: [

        {

          diskSizeGB: 100

          lun: 0

          createOption: 'Empty'

        }

      ]

    }

    networkProfile: {

      networkInterfaces: [

        {

          id: nic.id

        }

      ]

    }

    diagnosticsProfile: {

      bootDiagnostics: {

        enabled: true

        storageUri: stg.properties.primaryEndpoints.blob

      }

    }

  }

}

resource virtualMachines_ad_vm_name_CreateADForest 'Microsoft.Compute/virtualMachines/extensions@2021-11-01' = {

  location: location

  name: '${vmName}/CreateADForest'

  properties: {

    autoUpgradeMinorVersion: true

    publisher: 'Microsoft.Powershell'

    settings: {

      ConfigurationFunction: 'CreateADPDC.ps1\\CreateADPDC'

      ModulesUrl: 'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/modules/active-directory-new-domain/0.9/DSC/CreateADPDC.zip'

      Properties: {

        AdminCreds: {

          Password: 'PrivateSettingsRef:AdminPassword'

          UserName: 'dev'

        }

        DomainName: 'contoso.com'

      }

    }

    protectedSettings: {

      Items: {

        AdminPassword: adminPassword

      }

    }

    type: 'DSC'

    typeHandlerVersion: '2.19'

  }

  dependsOn: [

    vm

  ]

}

module updateVNet './nested_updateVNet.bicep' = {

  name: 'updateVNet'

  params: {

    reference_firstVNet_addressSpace: vn.properties

    reference_firstVNet_subnets_0_name: vn.properties

    reference_firstVNet_subnets_0_properties_addressPrefix: vn.properties

    vnname:virtualNetworkName

    location:location

    securityGroup:securityGroup.id

  }

  dependsOn: [

    virtualMachines_ad_vm_name_CreateADForest

  ]

}

output hostname string = pip.properties.dnsSettings.fqdn

 

保存できたら、下記のコマンドでリソースグループを作成します。

New-AzResourceGroup -Name exampleRG -Location japaneast

 

最後に下記のコマンドでデプロイを開始します。(ドメイン管理者のユーザ名はdevになります。)

New-AzResourceGroupDeployment -ResourceGroupName exampleRG -TemplateFile ./main.bicep -adminUsername "dev"

 

開始直後にadminPasswordの入力を求められます。

これはADの管理者ユーザのパスワードとなります。

 

デプロイが完了するとoutputsの項目にPublic IPURLが表示されます。



 

このURLを使用して、リモートデスクトップで接続できます。

 

まとめ

Bicepを使用することで、30分程度でドメイン環境を作成することができました。

急なドメインコントローラのセキュリティポリシーの変更にも

事前に検証ができますね。

 

 

 


PV

PVアクセスランキング にほんブログ村

ブログ村

このブログを検索

自己紹介

システムエンジニアとして12年ほど勤めたあと、社内SEに転職しました。 2017年に転職して、2019年に中古マンションを買いました。

リモートデスクトップのプロキシ越え

社内ネットワークからクラウド上のサーバにリモートデスクトップしたい Azureなどのクラウド環境にWindowsOSを立ち上げると、インターネット経由でリモートデスクトップ接続することになります。会社のネットワークからインターネットにアクセスする場合はプロキシサーバーやファイ...

QooQ