ようへいの日々精進XP

よかろうもん

(俺の Orbs) AWS セキュリティグループの Ingress ルールに CircleCI の IP を追加 or 削除する Orbs を作りました

あまりニーズがないかもしれないけど...

CircleCI で AWS 環境 (例えば、EC2 とか ECS ) に、デプロイする流れで AWS セキュリティグループで保護された URL に対して、CircleCI のジョブからアクセスしたい時があります。

そんな時に、アクセスする直前に、一時的にアクセス元となる CircleCI の IP アドレスをセキュリティグループの Ingress ルールに追加して、処理が終了したら、その IP アドレスを削除する CircleCI Orbs を作ったので宣伝させて下さい。

circleci.com

ちなみに、似たような機能を提供してくれる Orbs はいくつか提供されています。

circleci.com

circleci.com

他にもあるかもしれませんが、これらの Orbs には無い付加価値を付けて世の中に出したいなーと、パラメータを追加したり、テストを追加したり頑張ってみました。

例えば、他の Orbs には、セキュリティグループ ID を指定するパラメータがありません (タグで絞り込んでセキュリティグループ ID を取得している) が、拙作にはセキュリティグループ ID を指定するパラメータを付けました。また、moto server で立てたダミーの EC2 を利用したインテグレーションテストを追加したので、思わぬデグレの心配が少し減りました。

デモ

本物のリソースをイジるのは止めておきたいので、moto server で立てたダミーの EC2 の API 環境でデモってみました。

github.com

使い方は、以下のような感じです。(一部、抜粋しています)

version: 2.1

orbs:
  aws-sg-white-list-circleci-ip: inokappa/aws-sg-white-list-circleci-ip@0.0.1

executors:
  aws-sg-white-list-circleci-ip-demo:
    docker:
      - image: cimg/python:3.9
        environment:
          AWS_ACCESS_KEY_ID: EXAMPLEKEY123
          AWS_SECRET_ACCESS_KEY: EXAMPLESECRET123456
          AWS_DEFAULT_REGION: us-east-1
          AWS_REGION: us-east-1
          AWS_ENDPOINT: http://moto-server:5000
          AWS_DISABLE_SSL: true
      - image: motoserver/moto
        name: moto-server
        command: ["ec2", "-H", "0.0.0.0"]

jobs:
  aws-sg-white-list-circleci-ip-demo:
    environment:
      AWS_PAGER: ''
      GROUP_ID: ''
    executor: aws-sg-white-list-circleci-ip-demo
    steps:
      - checkout
      - install-awscli
      - create-security-group
      - aws-sg-white-list-circleci-ip/add:
          tag-key: 'sg-white-list'
          tag-value: 'true'
          description: 'Test-Permission'
      - check-exists-ip-permission
      - aws-sg-white-list-circleci-ip/del:
          tag-key: 'sg-white-list'
          tag-value: 'true'
          description: 'Test-Permission'
      - check-not-exists-ip-permission

workflows:
  version: 2

  aws-sg-white-list-circleci-ip-demo:
    jobs:
      - aws-sg-white-list-circleci-ip-demo:
          filters:
            branches:
              only:
                - aws-sg-white-list-circleci-ip-demo

苦労したところとか

circleci orb init

CircleCI が提供するコマンドラインツール circleci に Orbs を作る為のコマンドが用意されています。

$ circleci orb --help
Operate on orbs

See a full explanation and documentation on orbs here: https://circleci.com/docs/2.0/orb-intro/


Usage:
  circleci orb [command]

Available Commands:
  add-to-category      Add an orb to a category
  create               Create an orb in the specified namespace
  info                 Show the meta-data of an orb
  init                 Initialize a new orb.
  list                 List orbs
  list-categories      List orb categories
  pack                 Pack an Orb with local scripts.
  process              Validate an orb and print its form after all pre-registration processing
  publish              Publish an orb to the registry
  remove-from-category Remove an orb from a category
  source               Show the source of an orb
  unlist               Disable or enable an orb's listing in the registry
  validate             Validate an orb.yml

Flags:
  -h, --help   help for orb

Global Flags:
      --host string         URL to your CircleCI host, also CIRCLECI_CLI_HOST (default "https://circleci.com")
      --skip-update-check   Skip the check for updates check run before every command.
      --token string        your token for using CircleCI, also CIRCLECI_CLI_TOKEN
Use "circleci orb [command] --help" for more information about a command.

circleci orb init ${path} を実行することで、ウィザード形式で Orbs を開発、リリースする為の各種設定が自動生成されます。

以下、実行例です。

$ circleci orb init my-sample-orb
Note: This command is in preview. Please report any bugs! https://github.com/CircleCI-Public/circleci-cli/issues/new/choose
? Would you like to perform an automated setup of this orb? Yes, walk me through the process.
Downloading Orb Project Template into my-sample-orb
A few questions to get you up and running.
? Are you using GitHub or Bitbucket? GitHub
? Enter your github username or organization inokappa
? Enter the namespace to use for this orb inokappa
Saving namespace inokappa as default
? Orb name my-sample-orb
? Automatically set up a publishing context for your orb? No, I'll do this later.
? Would you like to set up your git project? Yes
? Enter the remote git repository my-sample-orb
Thank you! Setting up your orb...
Error: author field is required

最後の Error: author field is required というエラーが気になりますが、以下のようなファイルが生成されます。

$ tree -I .git -a my-sample-orb -L 3
my-sample-orb
├── .circleci
│   ├── README.md
│   └── config.yml
├── .github
│   ├── ISSUE_TEMPLATE
│   │   ├── BUG.md
│   │   ├── FEATURE_REQUEST.md
│   │   └── config.yml
│   └── PULL_REQUEST_TEMPLATE
│       └── PULL_REQUEST.md
├── .gitignore
├── .yamllint
├── CHANGELOG.md
├── LICENSE
├── README.md
└── src
    ├── @orb.yml
    ├── README.md
    ├── commands
    │   ├── README.md
    │   └── greet.yml
    ├── examples
    │   ├── README.md
    │   └── example.yml
    ├── executors
    │   ├── README.md
    │   └── default.yml
    ├── jobs
    │   ├── README.md
    │   └── hello.yml
    ├── scripts
    │   ├── README.md
    │   └── greet.sh
    └── tests
        ├── README.md
        └── greet.bats

11 directories, 25 files

Orbs 自身も CircleCI で CI/CD 出来るように .circleci/config.yml が用意されていることが判ります。

詳細は、以下のドキュメントに記載されています。

circleci.com

Orbs の開発流れについて、下図のようなフローも掲示されているので勉強になります。

引用: https://circleci.com/docs/ja/2.0/creating-orbs/#github-%E3%81%B8%E3%81%AE%E3%83%90%E3%83%BC%E3%82%B8%E3%83%A7%E3%83%B3-%E3%82%BF%E3%82%B0%E3%81%AE%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%B7%E3%83%A5

command にスクリプトをベタ書き

例えば、

github.com

add.yml の中に、以下のようにスクリプトをベタ書きしている点が残念。

description: >-
  This command will add a rule in the SG of the AWS Security Group to allow the
  ingress of the CircleCI Machine
parameters:
  description:
    description: Description to identify the rule. Spaces cannot be included.
    type: string

... 略 ...
steps:
  - run:
      name: Add CircleCI's IP address to the Security Group
      command: |
        AWS_COMMAND="aws"
        if [ -n "${AWS_ENDPOINT}" ];then
          AWS_COMMAND="aws --endpoint ${AWS_ENDPOINT}"
        fi
... 略 ...

当初は、scripts ディレクトリ以下にシェルスクリプトを用意していたんだけど、パラメータを環境変数に代入して引き回せなくて、試行錯誤を重ねたものの、妥協して command 内にスクリプトをベタ書きすることにした。うーん、もう少し、頑張ってみようかな。

以上

宣伝でございました。