项目作者: geckoboard

项目描述 :
Minimal slack slash commands for managing AWS infrastructure
高级语言: Go
项目地址: git://github.com/geckoboard/slash-infra.git
创建时间: 2018-12-18T17:06:57Z
项目社区:https://github.com/geckoboard/slash-infra

开源协议:

下载


slash-infra

This is a slack integration for “chatops”. A lot of the hubot scripts
don’t integrate natively with slack’s slash commands, or give you
unnecessarily powerful commands (e.g. launching individual ec2 instances,
or creating auto-scaling groups), or only work against one AWS account.

This tool came from a need to lookup EC2 instances by their IP/instance
ID. Our production/staging/dev environments are hosted in different AWS
accounts, and some are hosted in different regions. Switching between
accounts and regions to track down an instance can be quite laborious
and error prone, especially if you’re under pressure trying to triage a
problem.

/infra-search {query} can search multiple AWS accounts to find
resources. Currently it only supports looking up instances by their
instance ID.

Configuring Slack

  • Create a slack app
  • export the signing secret (from the App Credentials section of the
    app’s basic information) as the environment variable
    SLACK_SIGNING_SECRET
  • Configure slash commands to point at the routes specified in
    server.go

Configuring AWS access

Rather than create a user in each AWS account, the app uses a limited
IAM user to assume roles in each AWS account that should be searched.

This approach seems convoluted, but there are several benefits.

Firstly, when an IAM user “assumes” a role, AWS generates a set of temporary
credentials for the user that have the same permissions as the role.
These credentials are short-lived, and are rotated transparently by
the AWS SDK. If these temporary credentials were leaked to a
third party, they would only be usable for a short period of time.

Secondly, if the credentials for the IAM user are leaked, the attacker
will only be able to assume IAM roles. If they do not know the ARN of
your role they will not be able to assume it, and thus won’t be able to
perform actions on your account. You then only need to rotate one set of
credentials, rather credentials for all of your AWS accounts.

Note that if you’re using the role name suggested in these docs then
they will likely be able to guess the full ARN, as you can always get
the ID of the AWS account credentials belong to using aws sts get-caller-identity. If this is a concern for you, you can make things
slightly more difficult for attackers by choosing a unique name for your
roles that are different to the name of the IAM user. slash-infra only
uses the role ARN to authenticate to AWS, so you could use a different
role name for each environment.

You can configure the IAM user using the conventional environment
variables:

  1. export AWS_ACCESS_KEY_ID=...
  2. export AWS_SECRET_ACCESS_KEY=...

This IAM user should have the following permission policy:

  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Sid": "AllowAssumingSlashInfraRoles",
  6. "Effect": "Allow",
  7. "Action": "sts:AssumeRole",
  8. "Resource": "arn:aws:iam::*:role/SlashInfraInspection"
  9. }
  10. ]
  11. }

Note that the * in the ARN allows this user to assume the
SlashInfraInspection role in any AWS account that:

  • has that role
  • has marked the IAM user’s AWS account ID as a “Trusted entity” in the
    role’s “Trust relationships”

If these are new concepts for you, I’d really recommend reading AWS’
documentation on IAM
roles

Each role in each account should have a permission policy like this:

  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Sid": "AllowReadOnlyAccess",
  6. "Effect": "Allow",
  7. "Action": "ec2:DescribeInstances",
  8. "Resource": "*"
  9. }
  10. ]
  11. }

You can then configure slash-infra to use the role via environment
variables:

  1. export AWS_ROLE_{role alias}=arn:aws:iam:.....:role/SlashInfraInspection
  2. # The region defaults to `us-east-1` if left unspecified
  3. export AWS_REGION_{role alias}=eu-west-2
  4. # e.g.
  5. export AWS_ROLE_PRODUCTION=arn:aws:iam:.....:role/SlashInfraInspection
  6. export AWS_REGION_PRODUCTION=eu-west-2

If you need to search multiple regions within a single account you can
create several aliases that use the same role ARN.

Testing locally

Download ngrok, and create a slack
app
in your slack workspace. Create slash
commands in the app for the commands you want to support (see server.go).

FAQ

Why not write this in a lambda?

  • I don’t know how to write lambdas. I wrote this in 20% time and didn’t
    want to spend my day learning how to deploy lambdas
  • We don’t mind spending the few $X heroku charge to run this on a hobby
    dyno