|
| 1 | +# Example showing Vault and IAM Integration |
| 2 | + |
| 3 | +This example creates a private s3 bucket resources. It then uses vault |
| 4 | +to create keys which only has access to those s3 buckets. The example |
| 5 | +code will create an IAM role with access to that bucket and will also |
| 6 | +configure vault so that we can dynamically generate credentials for |
| 7 | +accessing that bucket. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +These are the required things for this example: |
| 12 | + |
| 13 | +* A running vault server. If you just want to experiment with this, |
| 14 | + run a development server using: |
| 15 | + |
| 16 | +``` shellsession |
| 17 | +vault server -dev |
| 18 | +``` |
| 19 | + |
| 20 | +* The AWS access and secret keys for an IAM user which the AWS Secret |
| 21 | + Backend for Vault will use for issuing new credentials. If you don't |
| 22 | + have any, you can create one using [vault-iam |
| 23 | + module](../../modules/vault-iam). You need to put the access keys in |
| 24 | + [variables.tf](./variables.tf) |
| 25 | + |
| 26 | + |
| 27 | +## Environment creation and deployment |
| 28 | + |
| 29 | +``` shellsession |
| 30 | +$ make init |
| 31 | +$ make plan |
| 32 | +$ make apply |
| 33 | +module.vault_aws_backend.vault_aws_secret_backend.aws: Creating... |
| 34 | +module.vault_aws_backend.vault_aws_secret_backend.aws: Creation complete after 0s [id=fpco/aws/dev/vault] |
| 35 | +aws_iam_role.vault_bucket_role: Creating... |
| 36 | +aws_s3_bucket.vault-test-bucket: Creating... |
| 37 | +aws_iam_role.vault_bucket_role: Still creating... [10s elapsed] |
| 38 | +aws_s3_bucket.vault-test-bucket: Still creating... [10s elapsed] |
| 39 | +aws_iam_role.vault_bucket_role: Still creating... [20s elapsed] |
| 40 | +aws_s3_bucket.vault-test-bucket: Still creating... [20s elapsed] |
| 41 | +aws_iam_role.vault_bucket_role: Creation complete after 22s [id=bucket_access_role] |
| 42 | +module.vault_aws_backend.vault_aws_secret_backend_role.aws_role: Creating... |
| 43 | +module.vault_aws_backend.vault_aws_secret_backend_role.aws_role: Creation complete after 0s [id=fpco/aws/dev/vault/roles/s3_app_user] |
| 44 | +aws_s3_bucket.vault-test-bucket: Still creating... [30s elapsed] |
| 45 | +aws_s3_bucket.vault-test-bucket: Still creating... [40s elapsed] |
| 46 | +aws_s3_bucket.vault-test-bucket: Still creating... [50s elapsed] |
| 47 | +aws_s3_bucket.vault-test-bucket: Still creating... [1m0s elapsed] |
| 48 | +aws_s3_bucket.vault-test-bucket: Still creating... [1m10s elapsed] |
| 49 | +aws_s3_bucket.vault-test-bucket: Still creating... [1m20s elapsed] |
| 50 | +aws_s3_bucket.vault-test-bucket: Still creating... [1m30s elapsed] |
| 51 | +aws_s3_bucket.vault-test-bucket: Still creating... [1m40s elapsed] |
| 52 | +aws_s3_bucket.vault-test-bucket: Still creating... [1m50s elapsed] |
| 53 | +aws_s3_bucket.vault-test-bucket: Still creating... [2m0s elapsed] |
| 54 | +aws_s3_bucket.vault-test-bucket: Still creating... [2m10s elapsed] |
| 55 | +aws_s3_bucket.vault-test-bucket: Still creating... [2m20s elapsed] |
| 56 | +aws_s3_bucket.vault-test-bucket: Still creating... [2m30s elapsed] |
| 57 | +aws_s3_bucket.vault-test-bucket: Still creating... [2m40s elapsed] |
| 58 | +aws_s3_bucket.vault-test-bucket: Creation complete after 2m48s [id=vault-fpco-test-bucket] |
| 59 | +aws_iam_role_policy.vault_bucket_policy: Creating... |
| 60 | +aws_iam_role_policy.vault_bucket_policy: Still creating... [10s elapsed] |
| 61 | +aws_iam_role_policy.vault_bucket_policy: Still creating... [20s elapsed] |
| 62 | +aws_iam_role_policy.vault_bucket_policy: Creation complete after 24s [id=bucket_access_role:bucket-policy] |
| 63 | + |
| 64 | +Apply complete! Resources: 5 added, 0 changed, 0 destroyed. |
| 65 | + |
| 66 | +The state of your infrastructure has been saved to the path |
| 67 | +below. This state is required to modify and destroy your |
| 68 | +infrastructure, so keep it safe. To inspect the complete state |
| 69 | +use the `terraform show` command. |
| 70 | + |
| 71 | +State path: terraform.tfstate |
| 72 | +``` |
| 73 | + |
| 74 | +## Testing |
| 75 | + |
| 76 | +Make sure you are already authorized with the vault server. If not, |
| 77 | +use `vault login` to do it. And then, you can dynamically create AWS |
| 78 | +credentials for accessing the s3 bucket you created: |
| 79 | + |
| 80 | +``` shellsession |
| 81 | +$ vault read fpco/aws/dev/vault/creds/s3_app_user |
| 82 | +Key Value |
| 83 | +--- ----- |
| 84 | +lease_id fpco/aws/prod/vault/creds/s3_app_user/eJcLUNbpTNRFpLoTL9mEW76p |
| 85 | +lease_duration 14m59s |
| 86 | +lease_renewable false |
| 87 | +access_key xxx |
| 88 | +secret_key xxx |
| 89 | +security_token xxx |
| 90 | +``` |
| 91 | + |
| 92 | +Now let's try to see all the files in our bucket: |
| 93 | + |
| 94 | +``` shellsession |
| 95 | +$ env AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx AWS_SESSION_TOKEN=xxx aws s3 ls s3://vault-fpco-test-bucket |
| 96 | +``` |
| 97 | + |
| 98 | +It gives you no output since there are no files. But the command |
| 99 | +works, which confirms us that the generated credentials are working as |
| 100 | +expected. |
| 101 | + |
| 102 | +Now let's try to do something for which you don't have access with the |
| 103 | +same credentials: |
| 104 | + |
| 105 | +``` shellsession |
| 106 | +$ env AWS_ACCESS_KEY_ID=xxxx AWS_SECRET_ACCESS_KEY=xxxx AWS_SESSION_TOKEN=xxx aws ec2 describe-instances --region="us-east-2" |
| 107 | +An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation. |
| 108 | +``` |
| 109 | + |
| 110 | +That doesn't work, which is expected. Let's try to see if we can |
| 111 | +access files of some other buckets which is present: |
| 112 | + |
| 113 | +``` shellsession |
| 114 | +$ env AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx AWS_SESSION_TOKEN=xxx aws s3 ls s3://some-other-existing-bucket |
| 115 | +An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied |
| 116 | +``` |
| 117 | + |
| 118 | +## Destruction |
| 119 | + |
| 120 | +``` shellsession |
| 121 | +$ make destroy |
| 122 | +$ make clean |
| 123 | +``` |
| 124 | + |
| 125 | +## Notes |
| 126 | + |
| 127 | +- This example was last tested with `Terraform v0.12.3` |
| 128 | +- This example assumes AWS credentials setup with access to the **us-east-2** region. |
0 commit comments