Amazon InspectorでProxy環境内のAmazon EKSのマネージドノードグループを検査する

こちらに続きEKSをProxy環境内で利用する方法です。

yomon.hatenablog.com

ネットワーク

eksctlで作成したPrivate ClusterにProxyを紐付けたこちらの環境で作業します。

f:id:yomon8:20201017141757p:plain

Inspector AgentにProxy設定

InspectorのAgentにProxyの設定を行うには、以下のように awsagent.env にProxy情報を書き込めばOKです。

cat <<EOF >> /etc/init.d/awsagent.env 
export https_proxy=hostname:port
export http_proxy=hostname:port
export no_proxy=169.254.169.254
EOF

docs.aws.amazon.com

EKSのマネージドノードに設定追加

起動テンプレートのUserDataでProxyを設定

UserDataに設定することでcloud-init内で上記のProxyの設定が行えます。

以下のように起動テンプレートにUserDataとしてProxyの設定処理を追加します。

f:id:yomon8:20201020081221p:plain

Cloudformation化

起動テンプレートを登録するCfnを作成してみました。

AWSTemplateFormatVersion: "2010-09-09"
Parameters:
  ClusterIp:
    Type: String
  ProxyIP:
    Type: String
  ProxyPort:
    Type: Number
  SshKeyPairName:
    Type: String

Resources:
  EKSManagedNodeInPrivateNetworkLaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    Properties: 
      LaunchTemplateName: eks-managednodes-in-private-network
      LaunchTemplateData: 
        InstanceType: t3.medium
        KeyName: !Ref SshKeyPairName
        TagSpecifications:
        - ResourceType: instance
          Tags:
          - Key: IsInspectorTarget
            Value: true
        UserData: !Base64 
          "Fn::Sub": |
            Content-Type: multipart/mixed; boundary="==BOUNDARY=="
            MIME-Version:  1.0

            --==BOUNDARY==
            Content-Type: text/cloud-boothook; charset="us-ascii"

            #Set the proxy hostname and port
            PROXY=${ProxyIP}:${ProxyPort}
            MAC=$(curl -s http://169.254.169.254/latest/meta-data/mac/)
            VPC_CIDR=$(curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC/vpc-ipv4-cidr-blocks | xargs | tr ' ' ',')

            cloud-init-per instance env_proxy_config tee <<EOF /etc/environment >/dev/null
            export https_proxy=http://$PROXY
            export http_proxy=http://$PROXY
            export no_proxy=${ClusterIp},$VPC_CIDR,localhost,127.0.0.1,169.254.169.254,.internal,s3.amazonaws.com,.s3.ap-northeast-1.amazonaws.com,api.ecr.ap-northeast-1.amazonaws.com,dkr.ecr.ap-northeast-1.amazonaws.com,ec2.ap-northeast-1.amazonaws.com,ap-northeast-1.eks.amazonaws.com
            EOF

            #Configure inspector agent with the proxy
            cloud-init-per instance awsagent_proxy_config tee <<EOF /etc/init.d/awsagent.env >/dev/null
            source /etc/environment
            EOF

            #Configure yum to use the proxy
            cloud-init-per instance yum_proxy_config cat << EOF >> /etc/yum.conf
            proxy=http://$PROXY
            EOF

            --==BOUNDARY==
            Content-Type:text/x-shellscript; charset="us-ascii"

            #!/bin/bash
            set -a -o xtrace

            source /etc/environment

            curl -o /tmp/install https://inspector-agent.amazonaws.com/linux/latest/install
            /bin/bash /tmp/install

            --==BOUNDARY==

Inspectorを実行してみる

EKSで自動で振られるタグや、上記のCfnで作成したタグを利用して対象をフィルタリングします。

ターゲットのステータスを見るとHEALTHYになっています。

後は実行するだけです。