set setting reset

脂肪と糖にはたらくやつ

ELB に登録されているインスタンスを解除 & 指定したインスタンスを ELB に登録

ELB に登録されているインスタンスを全て解除する

#!/bin/bash

aws="/usr/bin/aws --region ap-northeast-1"
elb_names=("elb-1" "elb-2")

deregister_instances_from_elb()
{
    for elb_name in ${elb_names[@]}
    do
        # get registerd instance ids per elb
        deregister_instance_ids=$(${aws} elb describe-load-balancers \
            --load-balancer-names ${elb_name} \
            --query 'LoadBalancerDescriptions[].Instances[].InstanceId' \
            --output text)

        # deregister instances from elb
        ${aws} elb deregister-instances-from-load-balancer \
            --load-balancer-name ${elb_name} \
            --instances ${deregister_instance_ids} \
            || echo "ERROR. failed to deregistered ${deregister_instance_ids} from ${elb_name}" | exit 1

        echo "OK. deregistered ${deregister_instance_ids} from ${elb_name}"
    done
}

deregister_instances_from_elb

指定したインスタンスを指定した ELB に登録

リストで持たせた NAME TAG から InstanceId を取得して一気に ELB に登録します。 また、登録した全てのインスタンスが InService になるまで待ちます。

#!/bin/bash

aws="/usr/bin/aws --region ap-northeast-1"
elb_names=("elb-1" "elb-2")
regist_servers=("web-1" "web-2" "web-3" "web-4")

register_instances_with_elb()
{
    for elb_name in ${elb_names[@]}
    do
        # get instance ids from Name Tag
        for regist_server in ${regist_servers[@]}
        do
            regist_instance_id=$(${aws} ec2 describe-instances \
                --filters Name=tag-value,Values=${regist_server} \
                --query 'Reservations[].Instances[].[InstanceId]' \
                --output text)

            regist_instance_ids+=("$regist_instance_id")
        done

        # register instance with elb
        ${aws} elb register-instances-with-load-balancer \
            --load-balancer-name ${elb_name} \
            --instances ${regist_instance_ids[@]} \
            || echo "ERROR. failed to registered ${regist_instance_id[@]} with ${elb_name}" | exit 1

        ## check in service
        regist_num=${#regist_instance_ids[@]}
        i=0
        while true
        do
            in_service=$(${aws} elb describe-instance-health --load-balancer-name ${elb_name} \
                --query 'length(InstanceStates[?State==`InService`].[State])')

            if [ ${in_service} = "${regist_num}" ]; then
                echo "OK. ${regist_servers[@]} is InService at ${elb_name}"
                unset regist_instance_ids
                break
            fi

            i=`expr $i + 1`
            sleep 5
        done
    done
}

register_instances_with_elb

elb_names と attach_servers の持たせ方を工夫するとメンテナンスとかで使えそうな気がしたので晒してみました。

administrator 以外のユーザから Web Deploy 3.5 でデプロイする

環境

OS IIS Version Web Deploy Version
Windows Server 2012R2 IIS 8.5 Web Deploy 3.5

結論

IIS マネージャーユーザに登録することで可能になりました。ただしグループは指定できません。
IIS マネージャーのユーザ追加なので、OS的には把握されず、ここで登録されたユーザでリモートデスクトップ接続などはできません。
デプロイ専用のユーザということで administrator と切り離せるので、ある意味合理的かなと思います。

設定

こちらで紹介されていた方法で設定可能でした。

blog.livedoor.jp

設定すると c:¥Windows¥System32¥inetsrv¥config¥administration.config にエントリが追加されます。

            <authentication defaultProvider="ConfigurationAuthenticationProvider">
                <credentials>
                    <add name="user2" password="暗号化されたパスワード" />
                </credentials>
            </authentication>

でもやっぱりグループで管理できたら「誰が」デプロイしたかがわかるようになるのでいいなぁと思います。
ご存知のかた教えて下さい!

EC2(windows server 2012R2)でVirtual Box VMを動かす

VMWare Player on Windows 7 で動かしていた vm (CentOS5) を Virtual Box がサポートしている形式に変換して EC2 上で動かしてみました。

virtual box への移行

イメージの変換

Windows 7 上の vmx ファイルを ovf ファイルに変換します。
変換はコマンドラインから実施します。

PS C:\Program Files (x86)\VMware\VMware Player\OVFTool> .\ovftool.exe C:\Users\hoge\Documents\vm.vmx C:\Users\hoge\vm.ovf

この時、vm イメージの指定があらぬとこを見ていて変換に失敗したので、 vmware player の設定を以下の様に変更しました。

[仮想マシン設定の編集] - [ハードウェア] - [CD/DVD] - [接続] で [物理ドライブを使用する] にチェックが入っていること

転送

C:\Users\hoge にできた ovf ファイルと vmdk ファイルを普通にEC2に転送します。

Virtual Box のセットアップ

EC2 に Virtual Box がインストール済みであることが前提です。
インストール時にインスタンスの NW 接続が瞬断するので注意します。

  • virtual box の [仮想アプライアンスの追加] から ovf ファイルを指定してインポート
  • NW は NAT とする
  • ポートフォワードを適宜追加する
    • http なら 80 -> 80 など
  • ポートフォワードに追加した内容を windows firewall に許可設定する
  • ゲストから外向きの通信ができない場合は、ゲストにデフォルトゲートウェイを追加する
$ sudo echo "0.0.0.0/0 via 10.0.2.2" >> /etc/sysconfig/network-scripts/route-eth0
$ sudo service network restart
  • VM を起動し、動作確認を行う

vm自動起動

以下の 1 行を bat とかにして、タスクスケジューラに登録します。

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm --type headless vm

おわり

t2.medium インスタンスで動かしているのですが、CPU を常時 40% ほど使っているので、ec2-import-instance 使えと言われている気がします。 今回は事情によりリソースが余っているインスタンスがあったのでこのようなことになりました。 二重仮想化ということでゲストのパフォーマンス劣化が噂されていましたが、いまのところ問題なさそうです。 (大したものが動いていないということもありそうですが)

terraform をバージョンアップしてみる(0.4.2 -> 0.5.3)

以前の記事で手元の macbrew でインストールした terraform ですが、
brew upgrade でなかなか降ってこないので手動でバージョンアップしてみます。 手動バージョンアップと言ってもシンボリックリンクの書き換えしただけなので、メモとして残します。

さて、私の環境では terraform は以下の様にインストールされています。

$ ls -l /usr/local/bin/terraform*
lrwxr-xr-x  1 hoge  hage  39 Apr 27 14:21 /usr/local/bin/terraform -> ../Cellar/terraform/0.4.2/bin/terraform
lrwxr-xr-x  1 hoge  hage  54 Apr 27 14:21 /usr/local/bin/terraform-provider-atlas -> ../Cellar/terraform/0.4.2/bin/terraform-provider-atlas
lrwxr-xr-x  1 hoge  hage  52 Apr 27 14:21 /usr/local/bin/terraform-provider-aws -> ../Cellar/terraform/0.4.2/bin/terraform-provider-aws
lrwxr-xr-x  1 hoge  hage  59 Apr 27 14:21 /usr/local/bin/terraform-provider-cloudflare -> ../Cellar/terraform/0.4.2/bin/terraform-provider-cloudflare
lrwxr-xr-x  1 hoge  hage  59 Apr 27 14:21 /usr/local/bin/terraform-provider-cloudstack -> ../Cellar/terraform/0.4.2/bin/terraform-provider-cloudstack
lrwxr-xr-x  1 hoge  hage  55 Apr 27 14:21 /usr/local/bin/terraform-provider-consul -> ../Cellar/terraform/0.4.2/bin/terraform-provider-consul
lrwxr-xr-x  1 hoge  hage  61 Apr 27 14:21 /usr/local/bin/terraform-provider-digitalocean -> ../Cellar/terraform/0.4.2/bin/terraform-provider-digitalocean
lrwxr-xr-x  1 hoge  hage  52 Apr 27 14:21 /usr/local/bin/terraform-provider-dme -> ../Cellar/terraform/0.4.2/bin/terraform-provider-dme
lrwxr-xr-x  1 hoge  hage  57 Apr 27 14:21 /usr/local/bin/terraform-provider-dnsimple -> ../Cellar/terraform/0.4.2/bin/terraform-provider-dnsimple
lrwxr-xr-x  1 hoge  hage  55 Apr 27 14:21 /usr/local/bin/terraform-provider-docker -> ../Cellar/terraform/0.4.2/bin/terraform-provider-docker
lrwxr-xr-x  1 hoge  hage  55 Jun 29 16:30 /usr/local/bin/terraform-provider-google -> ../Cellar/terraform/0.5.3/bin/terraform-provider-google
lrwxr-xr-x  1 hoge  hage  55 Apr 27 14:21 /usr/local/bin/terraform-provider-heroku -> ../Cellar/terraform/0.4.2/bin/terraform-provider-heroku
lrwxr-xr-x  1 hoge  hage  56 Apr 27 14:21 /usr/local/bin/terraform-provider-mailgun -> ../Cellar/terraform/0.4.2/bin/terraform-provider-mailgun
lrwxr-xr-x  1 hoge  hage  53 Apr 27 14:21 /usr/local/bin/terraform-provider-null -> ../Cellar/terraform/0.4.2/bin/terraform-provider-null
lrwxr-xr-x  1 hoge  hage  58 Apr 27 14:21 /usr/local/bin/terraform-provider-openstack -> ../Cellar/terraform/0.4.2/bin/terraform-provider-openstack
lrwxr-xr-x  1 hoge  hage  58 Apr 27 14:21 /usr/local/bin/terraform-provider-terraform -> ../Cellar/terraform/0.4.2/bin/terraform-provider-terraform
lrwxr-xr-x  1 hoge  hage  56 Apr 27 14:21 /usr/local/bin/terraform-provisioner-file -> ../Cellar/terraform/0.4.2/bin/terraform-provisioner-file
lrwxr-xr-x  1 hoge  hage  62 Apr 27 14:21 /usr/local/bin/terraform-provisioner-local-exec -> ../Cellar/terraform/0.4.2/bin/terraform-provisioner-local-exec
lrwxr-xr-x  1 hoge  hage  63 Apr 27 14:21 /usr/local/bin/terraform-provisioner-remote-exec -> ../Cellar/terraform/0.4.2/bin/terraform-provisioner-remote-exec

あまり詳しくないのですが、/usr/local/bin 配下はシンボリックリンクが多いのですね。

最新版の terraform を公式からダウンロードします。今回は 0.5.3 です。

https://dl.bintray.com/mitchellh/terraform/terraform_0.5.3_darwin_amd64.zip

zip を解凍するとバイナリが詰まっています。

~/Downloads$  ll terraform*
-rwxr-xr-x@ 1 hoge  hage  15202736 Jun  2 03:36 terraform
-rwxr-xr-x@ 1 hoge  hage  10354576 Jun  2 03:36 terraform-provider-atlas
-rwxr-xr-x@ 1 hoge  hage  19750112 Jun  2 03:36 terraform-provider-aws
-rwxr-xr-x@ 1 hoge  hage  10258784 Jun  2 03:36 terraform-provider-cloudflare
-rwxr-xr-x@ 1 hoge  hage  20105264 Jun  2 03:36 terraform-provider-cloudstack
-rwxr-xr-x@ 1 hoge  hage  10543600 Jun  2 03:36 terraform-provider-consul
-rwxr-xr-x@ 1 hoge  hage  10634496 Jun  2 03:36 terraform-provider-digitalocean
-rwxr-xr-x@ 1 hoge  hage  10275520 Jun  2 03:36 terraform-provider-dme
-rwxr-xr-x@ 1 hoge  hage  10267264 Jun  2 03:36 terraform-provider-dnsimple
-rwxr-xr-x@ 1 hoge  hage  11398336 Jun  2 03:36 terraform-provider-docker
-rwxr-xr-x@ 1 hoge  hage  13571952 Jun  2 03:36 terraform-provider-google
-rwxr-xr-x@ 1 hoge  hage  10825968 Jun  2 03:36 terraform-provider-heroku
-rwxr-xr-x@ 1 hoge  hage  10237248 Jun  2 03:36 terraform-provider-mailgun
-rwxr-xr-x@ 1 hoge  hage  10181808 Jun  2 03:36 terraform-provider-null
-rwxr-xr-x@ 1 hoge  hage  12746256 Jun  2 03:37 terraform-provider-openstack
-rwxr-xr-x@ 1 hoge  hage  10211632 Jun  2 03:37 terraform-provider-template
-rwxr-xr-x@ 1 hoge  hage  12143264 Jun  2 03:37 terraform-provider-terraform
-rwxr-xr-x@ 1 hoge  hage  11443376 Jun  2 03:37 terraform-provisioner-chef
-rwxr-xr-x@ 1 hoge  hage  11425600 Jun  2 03:37 terraform-provisioner-file
-rwxr-xr-x@ 1 hoge  hage   9806128 Jun  2 03:37 terraform-provisioner-local-exec
-rwxr-xr-x@ 1 hoge  hage  11408416 Jun  2 03:37 terraform-provisioner-remote-exec

これらを移動します。

~/Downloads$ mkdir -p /usr/local/Celler/terraform/0.5.3/bin
~/Downloads$ cp terraform* /usr/local/Celler/terraform/0.5.3/bin

シンボリックリンクを張り替えます。

#!/bin/bash

ls -l /usr/local/bin/terraform* | while read i
do
    bin=`echo $i | awk {'print $9'}`
    new_link=`echo $i | awk {'print $11'} | sed 's/0.4.2/0.5.3/'` 
    ln -sf ${new_link} ${bin}
done
$ ls -l /usr/local/bin/terraform*
lrwxr-xr-x  1 hoge  hage     39 Jun 29 16:45 terraform -> ../Cellar/terraform/0.5.3/bin/terraform
lrwxr-xr-x  1 hoge  hage     54 Jun 29 16:45 terraform-provider-atlas -> ../Cellar/terraform/0.5.3/bin/terraform-provider-atlas
lrwxr-xr-x  1 hoge  hage     52 Jun 29 16:45 terraform-provider-aws -> ../Cellar/terraform/0.5.3/bin/terraform-provider-aws
lrwxr-xr-x  1 hoge  hage     59 Jun 29 16:45 terraform-provider-cloudflare -> ../Cellar/terraform/0.5.3/bin/terraform-provider-cloudflare
lrwxr-xr-x  1 hoge  hage     59 Jun 29 16:45 terraform-provider-cloudstack -> ../Cellar/terraform/0.5.3/bin/terraform-provider-cloudstack
lrwxr-xr-x  1 hoge  hage     55 Jun 29 16:45 terraform-provider-consul -> ../Cellar/terraform/0.5.3/bin/terraform-provider-consul
lrwxr-xr-x  1 hoge  hage     61 Jun 29 16:45 terraform-provider-digitalocean -> ../Cellar/terraform/0.5.3/bin/terraform-provider-digitalocean
lrwxr-xr-x  1 hoge  hage     52 Jun 29 16:45 terraform-provider-dme -> ../Cellar/terraform/0.5.3/bin/terraform-provider-dme
lrwxr-xr-x  1 hoge  hage     57 Jun 29 16:45 terraform-provider-dnsimple -> ../Cellar/terraform/0.5.3/bin/terraform-provider-dnsimple
lrwxr-xr-x  1 hoge  hage     55 Jun 29 16:45 terraform-provider-docker -> ../Cellar/terraform/0.5.3/bin/terraform-provider-docker
lrwxr-xr-x  1 hoge  hage     55 Jun 29 16:45 terraform-provider-google -> ../Cellar/terraform/0.5.3/bin/terraform-provider-google
lrwxr-xr-x  1 hoge  hage     55 Jun 29 16:45 terraform-provider-heroku -> ../Cellar/terraform/0.5.3/bin/terraform-provider-heroku
lrwxr-xr-x  1 hoge  hage     56 Jun 29 16:45 terraform-provider-mailgun -> ../Cellar/terraform/0.5.3/bin/terraform-provider-mailgun
lrwxr-xr-x  1 hoge  hage     53 Jun 29 16:45 terraform-provider-null -> ../Cellar/terraform/0.5.3/bin/terraform-provider-null
lrwxr-xr-x  1 hoge  hage     58 Jun 29 16:45 terraform-provider-openstack -> ../Cellar/terraform/0.5.3/bin/terraform-provider-openstack
lrwxr-xr-x  1 hoge  hage     58 Jun 29 16:45 terraform-provider-terraform -> ../Cellar/terraform/0.5.3/bin/terraform-provider-terraform
lrwxr-xr-x  1 hoge  hage     56 Jun 29 16:45 terraform-provisioner-file -> ../Cellar/terraform/0.5.3/bin/terraform-provisioner-file
lrwxr-xr-x  1 hoge  hage     62 Jun 29 16:45 terraform-provisioner-local-exec -> ../Cellar/terraform/0.5.3/bin/terraform-provisioner-local-exec
lrwxr-xr-x  1 hoge  hage     63 Jun 29 16:45 terraform-provisioner-remote-exec -> ../Cellar/terraform/0.5.3/bin/terraform-provisioner-remote-exec

以上でバージョンアップ完了です。
その後 plan apply destroy は問題なく動作しています。 というか、そもそも実行環境を vagrant とか docker とかを使えばいいのではと思いますが、手が回らず。。

最後に、今回バージョンアップしたくなったのは route53 の private hosted zone と、
elb の connection draining がサポートされた(嬉し涙)からなのですが、CHANGELOG を見ると他にもかなりたくさんの機能追加がされていて開発の早さを感じます。

terraform は今後も勉強していきたいと思います。

chef-server からの bootstrap で ERROR: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

ぐぐると多数ヒットしますが、備忘のため。

chef-server から bootstrap すると表題のエラーが出ました。 bootstrap 先のサーバから knife client list とか色々やっても client として登録されないので全部ダメ。

ERROR: Your private key could not be loaded from /etc/chef/client.pem
Check your configuration file and ensure that your private key is readable

下記サイトなどによると knife ssl fetch をしなさいとあったので、chef-server 上で試してみるもまだうまくいきません。

www.creationline.com

bootstrap したいサーバ => 10.0.0.5 , chef-server => 10.0.1.5 です。

Doing old-style registration with the validation key at /etc/chef-server/chef-validator.pem...
Delete your validation key in order to use your user credentials instead

Connecting to 10.0.0.5
10.0.0.5 Starting first Chef Client run...
10.0.0.5 Starting Chef Client, version 12.3.0
#log_level                   :info
10.0.0.5 Creating a new client identity for infra-amazon-linux-test-1 using the validator key.
10.0.0.5 [2015-06-24T02:54:22+00:00] ERROR: SSL Validation failure connecting to host: 10.0.1.5 - hostname "10.0.1.5" does not match the server certificate
10.0.0.5
10.0.0.5 ================================================================================
10.0.0.5 Chef encountered an error attempting to create the client "infra-amazon-linux-test-1"
10.0.0.5 ================================================================================
10.0.0.5
10.0.0.5 [2015-06-24T02:54:22+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
10.0.0.5 Chef Client failed. 0 resources updated in 0.998938885 seconds
10.0.0.5 [2015-06-24T02:54:22+00:00] ERROR: hostname "10.0.1.5" does not match the server certificate
10.0.0.5 [2015-06-24T02:54:22+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

knife ssl check というコマンドもあるので試してみると

Connecting to host 10.0.1.5:443
ERROR: The SSL cert is signed by a trusted authority but is not valid for the given hostname
ERROR: You are attempting to connect to:   '10.0.1.5'
ERROR: The server's certificate belongs to 'chef-server'

TO FIX THIS ERROR:

The solution for this issue depends on your networking configuration. If you
are able to connect to this server using the hostname chef-server
instead of 10.0.1.5, then you can resolve this issue by updating chef_server_url
in your configuration file.

If you are not able to connect to the server using the hostname chef-server
you will have to update the certificate on the server to use the correct hostname.

IP で接続しようとしてエラーになっているようです。 chef-server という名前で接続させなければならないようなので、設定を探すと knife.rb が犯人でした。

#chef_server_url             'https://10.0.1.5'
chef_server_url             'https://chef-server'

上記の様に編集した後に bootstrap したらうまくいきました。 chef 初心者丸出しな内容で我ながら辟易します。。。

powershell で DNS 条件付きフォワーダを設定する

環境は以下の通りです。

  • windows server 2012R2
  • ADDS + DNS がインストール済みのドメインコントローラ

SE'S BOOK: dnscmdコマンドでDNS条件付きフォワーダーを作成、設定

上記サイトによると dnscmd というのを使うと簡単とのことなので試してみます。

PS C:\Users\Administrator> dnscmd /?

使用法: DnsCmd <サーバー> /ZoneInfo <ゾーン名> [<プロパティ>]
  <プロパティ> -- 表示するゾーン プロパティ
  例:
    AllowUpdate
    DsIntegrated
    Aging
    RefreshInterval
    NoRefreshInterval
    IsSigned
    Keymaster
    IsKeymaster
    SignWithNSEC3
    NSEC3HashAlgorithm
    NSEC3Iterations
    NSEC3RandomSaltLength
    NSEC3UserSalt
    NSEC3CurrentSalt
    NSEC3OptOut
    MaintainTrustAnchor
    SignatureInceptionOffset
    DNSKEYRecordSetTTL
    DSRecordSetTTL
    SecureDelegationPollingPeriod
    DsRecordAlgorithms
    RFC5011KeyRollovers
    SigningKeyDescriptors
    PropagationTime
    ParentHasSecureDelegation

今後のバージョンの Windows では、dnscmd.exe が削除される可能性があります。

現在 dnscmd.exe を使用して DNS サーバーを構成および管理している場合は、
Windows PowerShell に移行することをお勧めします。

DNS サーバー管理のコマンドの一覧を表示するには、Windows PowerShell プロンプト
で、「Get-Command -Module DnsServer」と入力します。DNS の Windows PowerShell
コマンドの詳細については、
http://go.microsoft.com/fwlink/?LinkId=217627 を参照してください。

使うなと言われたのと、dnscmd は結果をオブジェクトとして扱えないので DnsServer モジュールを使ってみることにします。

PS C:\Users\Administrator> get-command -module dnsserver

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Alias           Export-DnsServerTrustAnchor                        dnsserver
Function        Add-DnsServerConditionalForwarderZone              dnsserver
Function        Add-DnsServerDirectoryPartition                    dnsserver
Function        Add-DnsServerForwarder                             dnsserver
Function        Add-DnsServerPrimaryZone                           dnsserver
Function        Add-DnsServerResourceRecord                        dnsserver
Function        Add-DnsServerResourceRecordA                       dnsserver
Function        Add-DnsServerResourceRecordAAAA                    dnsserver
Function        Add-DnsServerResourceRecordCName                   dnsserver
Function        Add-DnsServerResourceRecordDnsKey                  dnsserver
Function        Add-DnsServerResourceRecordDS                      dnsserver
Function        Add-DnsServerResourceRecordMX                      dnsserver
Function        Add-DnsServerResourceRecordPtr                     dnsserver
Function        Add-DnsServerRootHint                              dnsserver
Function        Add-DnsServerSecondaryZone                         dnsserver
Function        Add-DnsServerSigningKey                            dnsserver
Function        Add-DnsServerStubZone                              dnsserver
Function        Add-DnsServerTrustAnchor                           dnsserver
Function        Add-DnsServerZoneDelegation                        dnsserver
Function        Clear-DnsServerCache                               dnsserver
Function        Clear-DnsServerStatistics                          dnsserver
Function        ConvertTo-DnsServerPrimaryZone                     dnsserver
Function        ConvertTo-DnsServerSecondaryZone                   dnsserver
Function        Disable-DnsServerSigningKeyRollover                dnsserver
Function        Enable-DnsServerSigningKeyRollover                 dnsserver
Function        Export-DnsServerDnsSecPublicKey                    dnsserver
Function        Export-DnsServerZone                               dnsserver
Function        Get-DnsServer                                      dnsserver
Function        Get-DnsServerCache                                 dnsserver
Function        Get-DnsServerDiagnostics                           dnsserver
Function        Get-DnsServerDirectoryPartition                    dnsserver
Function        Get-DnsServerDnsSecZoneSetting                     dnsserver
Function        Get-DnsServerDsSetting                             dnsserver
Function        Get-DnsServerEDns                                  dnsserver
Function        Get-DnsServerForwarder                             dnsserver
Function        Get-DnsServerGlobalNameZone                        dnsserver
Function        Get-DnsServerGlobalQueryBlockList                  dnsserver
Function        Get-DnsServerRecursion                             dnsserver
Function        Get-DnsServerResourceRecord                        dnsserver
Function        Get-DnsServerRootHint                              dnsserver
Function        Get-DnsServerScavenging                            dnsserver
Function        Get-DnsServerSetting                               dnsserver
Function        Get-DnsServerSigningKey                            dnsserver
Function        Get-DnsServerStatistics                            dnsserver
Function        Get-DnsServerTrustAnchor                           dnsserver
Function        Get-DnsServerTrustPoint                            dnsserver
Function        Get-DnsServerZone                                  dnsserver
Function        Get-DnsServerZoneAging                             dnsserver
Function        Get-DnsServerZoneDelegation                        dnsserver
Function        Import-DnsServerResourceRecordDS                   dnsserver
Function        Import-DnsServerRootHint                           dnsserver
Function        Import-DnsServerTrustAnchor                        dnsserver
Function        Invoke-DnsServerSigningKeyRollover                 dnsserver
Function        Invoke-DnsServerZoneSign                           dnsserver
Function        Invoke-DnsServerZoneUnsign                         dnsserver
Function        Register-DnsServerDirectoryPartition               dnsserver
Function        Remove-DnsServerDirectoryPartition                 dnsserver
Function        Remove-DnsServerForwarder                          dnsserver
Function        Remove-DnsServerResourceRecord                     dnsserver
Function        Remove-DnsServerRootHint                           dnsserver
Function        Remove-DnsServerSigningKey                         dnsserver
Function        Remove-DnsServerTrustAnchor                        dnsserver
Function        Remove-DnsServerZone                               dnsserver
Function        Remove-DnsServerZoneDelegation                     dnsserver
Function        Reset-DnsServerZoneKeyMasterRole                   dnsserver
Function        Restore-DnsServerPrimaryZone                       dnsserver
Function        Restore-DnsServerSecondaryZone                     dnsserver
Function        Resume-DnsServerZone                               dnsserver
Function        Set-DnsServer                                      dnsserver
Function        Set-DnsServerCache                                 dnsserver
Function        Set-DnsServerConditionalForwarderZone              dnsserver
Function        Set-DnsServerDiagnostics                           dnsserver
Function        Set-DnsServerDnsSecZoneSetting                     dnsserver
Function        Set-DnsServerDsSetting                             dnsserver
Function        Set-DnsServerEDns                                  dnsserver
Function        Set-DnsServerForwarder                             dnsserver
Function        Set-DnsServerGlobalNameZone                        dnsserver
Function        Set-DnsServerGlobalQueryBlockList                  dnsserver
Function        Set-DnsServerPrimaryZone                           dnsserver
Function        Set-DnsServerRecursion                             dnsserver
Function        Set-DnsServerResourceRecord                        dnsserver
Function        Set-DnsServerResourceRecordAging                   dnsserver
Function        Set-DnsServerRootHint                              dnsserver
Function        Set-DnsServerScavenging                            dnsserver
Function        Set-DnsServerSecondaryZone                         dnsserver
Function        Set-DnsServerSetting                               dnsserver
Function        Set-DnsServerSigningKey                            dnsserver
Function        Set-DnsServerStubZone                              dnsserver
Function        Set-DnsServerZoneAging                             dnsserver
Function        Set-DnsServerZoneDelegation                        dnsserver
Function        Show-DnsServerCache                                dnsserver
Function        Show-DnsServerKeyStorageProvider                   dnsserver
Function        Start-DnsServerScavenging                          dnsserver
Function        Start-DnsServerZoneTransfer                        dnsserver
Function        Step-DnsServerSigningKeyRollover                   dnsserver
Function        Suspend-DnsServerZone                              dnsserver
Function        Sync-DnsServerZone                                 dnsserver
Function        Test-DnsServer                                     dnsserver
Function        Test-DnsServerDnsSecZoneSetting                    dnsserver
Function        Unregister-DnsServerDirectoryPartition             dnsserver
Function        Update-DnsServerTrustPoint                         dnsserver

DnsShell はインストールが必要なようなので、インストール不要なこちらを利用することにします。

条件付きフォワーダの設定は Add-DnsServerConditionalForwarderZone を使えばよさそうです。 今回は Active Directory 環境の DNS サーバなので、以下の様に設定してみました。

Add-DnsServerConditionalForwarderZone `
    -Name "hogehoge.com" `
    -MasterServers 10.0.0.2,10.0.0.3 `
    -ReplicationScope "Forest"

これでフォレスト内のDNSサーバ間でレプリケーションもされます。
また、確認は Get-DnsServerZone を使うとよさそうです。ZoneType が Forwarder になっています。

PS C:\Users\Administrator> Get-DnsServerZone

ZoneName                            ZoneType        IsAutoCreated   IsDsIntegrated  IsReverseLookupZone  IsSigned
--------                            --------        -------------   --------------  -------------------  --------
_msdcs.example.local                Primary         False           True            False                False
0.in-addr.arpa                      Primary         True            False           True                 False
127.in-addr.arpa                    Primary         True            False           True                 False
255.in-addr.arpa                    Primary         True            False           True                 False
hogehoge.com                        Forwarder       False           True            False
example.local                       Primary         False           True            False                False
TrustAnchors                        Primary         False           True            False                False

dnsmgmt.msc でも追加されていることが確認できました。

f:id:rriifftt:20150617181057p:plain

Add-DnsServerConditionalForwarderZone

IIS マネージャーのアクセス許可を Active Directory のグループで制御する

概要

IIS による web サイトへのアクセス許可ではなく、IIS マネージャーの操作へのアクセス許可設定を Active Directory のグループで実施します。

Active Directory ユーザはデフォルトで IIS マネージャーの起動はできますが、設定をするためには administrator のパスワーを入力しなければなりませんので、これを一般ユーザでもできるようにします。

今回は WEB サーバへリモートデスクトップ接続が可能なユーザを IIS マネージャーを操作できるようにします。 この時、ユーザでの管理はしんどいので、グループで管理できるようにします。

環境

役割 OS
ドメインコントローラ(ADDS) Windows Server 2012 R2
WEBサーバ(IIS) Windows Server 2012 R2

なお、AWS VPCで構築したドメインネットワークです。

前提

  • WEBサーバはドメインに参加済み
  • ローカルの Remote Desktop Users グループにドメイン内のグループ(SecurityGroup)が追加済み
net localgroup "Remote Desktop Users" example.local\SecurityGroup /add
  • example.local\SecurityGroup のユーザがWEBサーバにRDPログオンできること

注意事項

  • サーバレベルでのアクセス許可は一般ユーザに付与できず、administrator のみが接続できる
  • 一般ユーザは各サーバのサイトごとにしかアクセスできない
  • 一般ユーザの IIS マネージャーでの操作は administrator 権限より限定的になる
    • ex) IISマネージャーのアクセス許可は設定できない

手順

  1. administrator 権限でIISマネージャを開く
  2. サイトレベルのIISマネージャーのアクセス許可を開く
  3. ユーザの許可をクリック
  4. ユーザーの種類を windows にチェックを入れ、Remote Desktop Users と入力する

コマンドラインでやる

Remote Desktop Users グループメンバーが IIS マネージャーの全てのサイトに接続できるようにするスクリプトを書いてみました。

[xml]$sites = C:\Windows\System32\inetsrv\appcmd.exe list config -section:sites
$siteNames = $sites.'system.applicationHost'.sites.site | foreach-object { 
    $site = [System.String]$_.name
    $file = "C:\Windows\System32\inetsrv\config\administration.config"
    $config = [xml](Get-Content -Path $file)

    $authorizationRules = $config.configuration.'system.webServer'.management.authorization.authorizationRules.scope
    $RuleEntry = ($authorizationRules | Where-Object {$_.path -eq "/$site"}).path.count
    
    if ( $RuleEntry -eq "0" )
    {
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Management") | Out-Null
        [Microsoft.Web.Management.Server.ManagementAuthorization]::Grant("Remote Desktop Users","$site", 1)
    }
}

すると、C:¥Windows¥System32¥inetsrv¥config¥administration.config にエントリが追加されます。

<configuration>
    <system.webServer>
        <management>
            <authorization defaultProvider="ConfigurationAuthorizationProvider">
                <authorizationRules>
                    <scope path="/サイト名">
                        <add name="BUILTIN\Remote Desktop Users" isRole="true" />
                    </scope>
                </authorizationRules>
            </authorization>
        </management>
    </system.webServer>
</configuration>

Adding IIS Manager Users and Permissions using Powershell

バグ

このコマンドを chef の powershell_script で実行したところ、例外で落ちることがあります。

問題の署名:
  問題イベント名:    PowerShell
  NameOfExe:    powershell.exe
  FileVersionOfSystemManagementAutomation:  6.3.9600.17400
  InnermostExceptionType:   Runtime.InteropServices.InvalidComObject
  OutermostExceptionType:   Runtime.InteropServices.InvalidComObject
  DeepestPowerShellFrame:   unknown
  DeepestFrame: System.StubHelpers.StubHelpers.GetCOMIPFromRCW
  ThreadName:   unknown
  OS バージョン:   6.3.9600.2.0.0.272.7
  ロケール ID:  1041

オンラインのプライバシーに関する声明をお読みください:
  http://go.microsoft.com/fwlink/?linkid=280262

オンラインのプライバシーに関する声明が利用できない場合は、プライバシーに関する声明をオフラインでお読みください:
  C:\Windows\system32\ja-JP\erofflps.txt

ぐぐるとバグとして報告されている模様。

http://serverfault.com/questions/587305/powershell-has-stopped-working-on-ps-exit-after-creating-iis-user