pg-aws_rds_iam 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/pull-request.yml +90 -0
- data/.gitignore +14 -0
- data/.rubocop.yml +40 -0
- data/.ruby-version +1 -0
- data/.yardopts +2 -0
- data/CHANGELOG.md +17 -0
- data/CODE_OF_CONDUCT.md +87 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +95 -0
- data/LICENSE.txt +21 -0
- data/README.md +127 -0
- data/Rakefile +39 -0
- data/bin/bundle +105 -0
- data/bin/console +8 -0
- data/bin/install-bundler +6 -0
- data/bin/rake +29 -0
- data/bin/setup +6 -0
- data/bin/yard +29 -0
- data/lib/pg/aws_rds_iam.rb +30 -0
- data/lib/pg/aws_rds_iam/auth_token_generator.rb +29 -0
- data/lib/pg/aws_rds_iam/auth_token_generator_registry.rb +68 -0
- data/lib/pg/aws_rds_iam/auth_token_injector.rb +48 -0
- data/lib/pg/aws_rds_iam/connection.rb +25 -0
- data/lib/pg/aws_rds_iam/connection_info.rb +21 -0
- data/lib/pg/aws_rds_iam/connection_info/keyword_value_string.rb +114 -0
- data/lib/pg/aws_rds_iam/connection_info/parse_error.rb +9 -0
- data/lib/pg/aws_rds_iam/connection_info/uri.rb +44 -0
- data/lib/pg/aws_rds_iam/version.rb +8 -0
- data/pg-aws_rds_iam.gemspec +39 -0
- metadata +256 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 49df62cf1db1c3fdbfdd25f515b65cea8335dec18e050581ef45a3005f4b6ad9
|
4
|
+
data.tar.gz: 322858372f9fdc7b80b8cf7aaecc131e97f80efa44f8879bfed02445ca786633
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 76839858df3934b56f08cbb481d2d40b3e8e7993b0bed0e00b304a1fb608e21a544183a7664c32da17eb11ca5f1edae030da56bc75d6e346ae6e9ce0ed4c5009
|
7
|
+
data.tar.gz: 415384ef75576d6b0af3499d0ee8341415405b4717f63149fd8f5b5c0c0b50a268067907b9aed844e6f120fab57b10e795fa6b564c1f4e93e2e9489ca41aae70
|
@@ -0,0 +1,90 @@
|
|
1
|
+
name: pull-request
|
2
|
+
|
3
|
+
on:
|
4
|
+
- pull_request
|
5
|
+
|
6
|
+
jobs:
|
7
|
+
test:
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby:
|
11
|
+
- "2.5"
|
12
|
+
- "2.6"
|
13
|
+
- "2.7"
|
14
|
+
|
15
|
+
pg:
|
16
|
+
- "0.18"
|
17
|
+
- "0.19"
|
18
|
+
- "0.20"
|
19
|
+
- "0.21"
|
20
|
+
- "1.0"
|
21
|
+
- "1.1"
|
22
|
+
- "1.2"
|
23
|
+
|
24
|
+
activerecord:
|
25
|
+
- "5.2"
|
26
|
+
- "6.0"
|
27
|
+
|
28
|
+
name: Ruby ${{ matrix.ruby }} | PG ${{ matrix.pg }} | ActiveRecord ${{ matrix.activerecord }}
|
29
|
+
|
30
|
+
runs-on: ubuntu-latest
|
31
|
+
|
32
|
+
container:
|
33
|
+
image: ruby:${{ matrix.ruby }}-alpine
|
34
|
+
|
35
|
+
env:
|
36
|
+
BUNDLE_PATH: ${{ github.workspace }}/vendor/bundle
|
37
|
+
ACTIVERECORD_VERSION: ${{ matrix.activerecord }}
|
38
|
+
PG_VERSION: ${{ matrix.pg }}
|
39
|
+
|
40
|
+
steps:
|
41
|
+
- name: Install dependencies
|
42
|
+
run: apk add build-base git postgresql-dev
|
43
|
+
|
44
|
+
- name: Check out source code
|
45
|
+
uses: actions/checkout@v2
|
46
|
+
|
47
|
+
- name: Install Bundler
|
48
|
+
run: bin/install-bundler
|
49
|
+
|
50
|
+
- name: Update Gemfile.lock
|
51
|
+
run: bin/bundle lock --update activerecord pg
|
52
|
+
|
53
|
+
- name: Compute cache key
|
54
|
+
id: cache-key
|
55
|
+
run: |
|
56
|
+
source /etc/os-release
|
57
|
+
postgresql_version=$(apk info --installed postgresql-dev --verbose)
|
58
|
+
printf \
|
59
|
+
"::set-output name=cache-key::alpine-%s-ruby-%s-%s\n" \
|
60
|
+
"${VERSION_ID}" \
|
61
|
+
"${RUBY_VERSION}" \
|
62
|
+
"${postgresql_version}"
|
63
|
+
|
64
|
+
- name: Cache gems
|
65
|
+
uses: actions/cache@v1
|
66
|
+
with:
|
67
|
+
key: ${{ steps.cache-key.outputs.cache-key }}-gems-${{ hashFiles('Gemfile.lock') }}
|
68
|
+
path: vendor/bundle
|
69
|
+
|
70
|
+
- name: Install gems
|
71
|
+
run: bin/bundle install
|
72
|
+
env:
|
73
|
+
BUNDLE_FROZEN: true
|
74
|
+
BUNDLE_JOBS: 4
|
75
|
+
BUNDLE_RETRY: 3
|
76
|
+
|
77
|
+
- name: Run RuboCop
|
78
|
+
run: bin/rake rubocop
|
79
|
+
|
80
|
+
- name: Run unit tests
|
81
|
+
run: bin/rake test:unit
|
82
|
+
|
83
|
+
- name: Run acceptance tests
|
84
|
+
run: bin/rake test:acceptance
|
85
|
+
env:
|
86
|
+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
87
|
+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
88
|
+
AWS_REGION: ${{ secrets.AWS_REGION }}
|
89
|
+
DATABASE_URL: ${{ secrets.DATABASE_URL }}
|
90
|
+
SECURITY_GROUP_ID: ${{ secrets.SECURITY_GROUP_ID }}
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.4
|
3
|
+
Exclude:
|
4
|
+
- bin/bundle
|
5
|
+
- bin/rake
|
6
|
+
- bin/yard
|
7
|
+
- vendor/**/*
|
8
|
+
|
9
|
+
Layout/LineLength:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Metrics/AbcSize:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Naming/ClassAndModuleCamelCase:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Style/Documentation:
|
22
|
+
Exclude:
|
23
|
+
- lib/pg/aws_rds_iam/auth_token_injector.rb
|
24
|
+
- lib/pg/aws_rds_iam/connection_info.rb
|
25
|
+
- lib/pg/aws_rds_iam/connection_info/**/*.rb
|
26
|
+
- lib/pg/aws_rds_iam/connection.rb
|
27
|
+
- test/**/*.rb
|
28
|
+
|
29
|
+
Style/HashSyntax:
|
30
|
+
Exclude:
|
31
|
+
- Rakefile
|
32
|
+
|
33
|
+
Style/StringLiterals:
|
34
|
+
EnforcedStyle: double_quotes
|
35
|
+
|
36
|
+
Style/SymbolArray:
|
37
|
+
EnforcedStyle: brackets
|
38
|
+
|
39
|
+
Style/WordArray:
|
40
|
+
EnforcedStyle: brackets
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.7.0
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [0.1.0] - 2020-02-05
|
11
|
+
|
12
|
+
### Added
|
13
|
+
* A plugin for the [`pg` gem](https://rubygems.org/gems/pg) that adds support for [IAM authentication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html) when connecting to PostgreSQL databases hosted in Amazon RDS. ([#1](https://github.com/haines/pg-aws_rds_iam/pull/1))
|
14
|
+
* ActiveRecord support. ([#3](https://github.com/haines/pg-aws_rds_iam/pull/3))
|
15
|
+
|
16
|
+
[Unreleased]: https://github.com/haines/pg-aws_rds_iam/compare/v0.1.0...HEAD
|
17
|
+
[0.1.0]: https://github.com/haines/pg-aws_rds_iam/compare/64168051a8ef5f32a13632d8ef0b7da00d0056bc...v0.1.0
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or advances of any kind
|
22
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
23
|
+
* Public or private harassment
|
24
|
+
* Publishing others' private information, such as a physical or email address, without their explicit permission
|
25
|
+
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
26
|
+
|
27
|
+
## Enforcement Responsibilities
|
28
|
+
|
29
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
30
|
+
|
31
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
32
|
+
|
33
|
+
## Scope
|
34
|
+
|
35
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces.
|
36
|
+
Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
37
|
+
|
38
|
+
## Enforcement
|
39
|
+
|
40
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at andrew@haines.org.nz.
|
41
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
42
|
+
|
43
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
44
|
+
|
45
|
+
## Enforcement Guidelines
|
46
|
+
|
47
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
48
|
+
|
49
|
+
### 1. Correction
|
50
|
+
|
51
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
52
|
+
|
53
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate.
|
54
|
+
A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior.
|
61
|
+
No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time.
|
62
|
+
This includes avoiding interactions in community spaces as well as external channels like social media.
|
63
|
+
Violating these terms may lead to a temporary or permanent ban.
|
64
|
+
|
65
|
+
### 3. Temporary Ban
|
66
|
+
|
67
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
68
|
+
|
69
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time.
|
70
|
+
No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period.
|
71
|
+
Violating these terms may lead to a permanent ban.
|
72
|
+
|
73
|
+
### 4. Permanent Ban
|
74
|
+
|
75
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
76
|
+
|
77
|
+
**Consequence**: A permanent ban from any sort of public interaction within the project community.
|
78
|
+
|
79
|
+
## Attribution
|
80
|
+
|
81
|
+
This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 2.0, available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
82
|
+
|
83
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
84
|
+
|
85
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
86
|
+
https://www.contributor-covenant.org/faq.
|
87
|
+
Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pg-aws_rds_iam (0.1.0)
|
5
|
+
aws-sdk-rds (~> 1.0)
|
6
|
+
pg (>= 0.18, < 2.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (6.0.2.1)
|
12
|
+
activesupport (= 6.0.2.1)
|
13
|
+
activerecord (6.0.2.1)
|
14
|
+
activemodel (= 6.0.2.1)
|
15
|
+
activesupport (= 6.0.2.1)
|
16
|
+
activesupport (6.0.2.1)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 0.7, < 2)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
zeitwerk (~> 2.2)
|
22
|
+
ansi (1.5.0)
|
23
|
+
ast (2.4.0)
|
24
|
+
aws-eventstream (1.0.3)
|
25
|
+
aws-partitions (1.270.0)
|
26
|
+
aws-sdk-core (3.89.1)
|
27
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
28
|
+
aws-partitions (~> 1, >= 1.239.0)
|
29
|
+
aws-sigv4 (~> 1.1)
|
30
|
+
jmespath (~> 1.0)
|
31
|
+
aws-sdk-ec2 (1.138.0)
|
32
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
33
|
+
aws-sigv4 (~> 1.1)
|
34
|
+
aws-sdk-rds (1.76.0)
|
35
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
36
|
+
aws-sigv4 (~> 1.1)
|
37
|
+
aws-sigv4 (1.1.0)
|
38
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
39
|
+
builder (3.2.4)
|
40
|
+
coderay (1.1.2)
|
41
|
+
concurrent-ruby (1.1.5)
|
42
|
+
i18n (1.8.2)
|
43
|
+
concurrent-ruby (~> 1.0)
|
44
|
+
jaro_winkler (1.5.4)
|
45
|
+
jmespath (1.4.0)
|
46
|
+
method_source (0.9.2)
|
47
|
+
minitest (5.14.0)
|
48
|
+
minitest-reporters (1.4.2)
|
49
|
+
ansi
|
50
|
+
builder
|
51
|
+
minitest (>= 5.0)
|
52
|
+
ruby-progressbar
|
53
|
+
parallel (1.19.1)
|
54
|
+
parser (2.7.0.2)
|
55
|
+
ast (~> 2.4.0)
|
56
|
+
pg (1.2.2)
|
57
|
+
pry (0.12.2)
|
58
|
+
coderay (~> 1.1.0)
|
59
|
+
method_source (~> 0.9.0)
|
60
|
+
rainbow (3.0.0)
|
61
|
+
rake (13.0.1)
|
62
|
+
rubocop (0.79.0)
|
63
|
+
jaro_winkler (~> 1.5.1)
|
64
|
+
parallel (~> 1.10)
|
65
|
+
parser (>= 2.7.0.1)
|
66
|
+
rainbow (>= 2.2.2, < 4.0)
|
67
|
+
ruby-progressbar (~> 1.7)
|
68
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
69
|
+
ruby-progressbar (1.10.1)
|
70
|
+
thread_safe (0.3.6)
|
71
|
+
timecop (0.9.1)
|
72
|
+
tzinfo (1.2.6)
|
73
|
+
thread_safe (~> 0.1)
|
74
|
+
unicode-display_width (1.6.1)
|
75
|
+
yard (0.9.24)
|
76
|
+
zeitwerk (2.2.2)
|
77
|
+
|
78
|
+
PLATFORMS
|
79
|
+
ruby
|
80
|
+
|
81
|
+
DEPENDENCIES
|
82
|
+
activerecord (>= 5.2, < 7.0)
|
83
|
+
aws-sdk-ec2 (~> 1.137)
|
84
|
+
bundler (~> 2.0)
|
85
|
+
minitest (~> 5.14)
|
86
|
+
minitest-reporters (~> 1.4)
|
87
|
+
pg-aws_rds_iam!
|
88
|
+
pry (~> 0.12)
|
89
|
+
rake (~> 13.0)
|
90
|
+
rubocop (~> 0.79)
|
91
|
+
timecop (~> 0.9)
|
92
|
+
yard (~> 0.9)
|
93
|
+
|
94
|
+
BUNDLED WITH
|
95
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Andrew Haines
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
# PG::AWS_RDS_IAM
|
2
|
+
|
3
|
+
`PG::AWS_RDS_IAM` is a plugin for the [`pg` gem](https://rubygems.org/gems/pg) that adds support for [IAM authentication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html) when connecting to PostgreSQL databases hosted in Amazon RDS.
|
4
|
+
|
5
|
+
IAM authentication allows your application to connect to the database using secure, short-lived authentication tokens instead of a fixed password.
|
6
|
+
This gives you greater security and eliminates the operational overhead of rotating passwords.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Install manually:
|
11
|
+
|
12
|
+
```console
|
13
|
+
$ gem install pg-aws_rds_iam
|
14
|
+
```
|
15
|
+
|
16
|
+
or with Bundler:
|
17
|
+
|
18
|
+
```console
|
19
|
+
$ bundle add pg-aws_rds_iam
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
To use IAM authentication for your database connections, you need to
|
25
|
+
|
26
|
+
1. enable IAM authentication for your database,
|
27
|
+
2. provide your application with IAM credentials, and
|
28
|
+
3. configure your application to generate authentication tokens.
|
29
|
+
|
30
|
+
### 1. Enable IAM authentication for your database
|
31
|
+
|
32
|
+
Start by configuring your database to allow IAM authentication using either the [AWS console](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Enabling.html#UsingWithRDS.IAMDBAuth.Enabling.Console) or the [`aws` CLI](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Enabling.html#UsingWithRDS.IAMDBAuth.Enabling.CLI).
|
33
|
+
This doesn't require downtime so is safe to apply immediately, unless you already have pending modifications that require a database reboot.
|
34
|
+
|
35
|
+
Next, [grant your database user the `rds_iam` role](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.DBAccounts.html#UsingWithRDS.IAMDBAuth.DBAccounts.PostgreSQL).
|
36
|
+
|
37
|
+
### 2. Provide your application with IAM credentials
|
38
|
+
|
39
|
+
The most secure way to grant your application permission to connect to your database is to use an IAM role.
|
40
|
+
|
41
|
+
Start by [creating a service role](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-service.html) if your application doesn't already have one.
|
42
|
+
Then, create an [IAM policy granting the `rds-db:connect` permission](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.IAMPolicy.html) and attach it to the role.
|
43
|
+
|
44
|
+
If you've created a new service role, you'll need to associate it with your application.
|
45
|
+
The way to do this depends on where you are running your application:
|
46
|
+
|
47
|
+
* for EC2 instances, [set up an instance profile](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2.html);
|
48
|
+
* for EKS pods, [associate the IAM role with a Kubernetes service account](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html);
|
49
|
+
* for ECS tasks, [configure a task role](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html); or
|
50
|
+
* for Lambda functions, [set the execution role](https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html).
|
51
|
+
|
52
|
+
If you can't use a service role, then you can grant the permissions to an IAM user instead and supply the application with their access key through a [configuration profile](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) or via the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables.
|
53
|
+
However, you won't get the full security and operational benefits of RDS IAM authentication, because the access key is a long-lived secret that you need to supply to the application securely and rotate periodically.
|
54
|
+
|
55
|
+
### 3. Configure your application to generate authentication tokens
|
56
|
+
|
57
|
+
You can use `PG::AWS_RDS_IAM`'s default authentication token generator if you are using a service role as described above, or if you configure your application using the standard AWS ways to provide [credentials](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html#aws-ruby-sdk-setting-credentials) and [region](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html#aws-ruby-sdk-setting-region).
|
58
|
+
|
59
|
+
To use the default authentication token generator, set the `aws_rds_iam_auth_token_generator` connection parameter to `default`.
|
60
|
+
You can set this parameter in
|
61
|
+
|
62
|
+
* the query string of a connection URI:
|
63
|
+
|
64
|
+
```
|
65
|
+
postgresql://andrew@postgresql.example.com:5432/blog?aws_rds_iam_auth_token_generator=default
|
66
|
+
```
|
67
|
+
|
68
|
+
* a `key=value` pair in a connection string:
|
69
|
+
|
70
|
+
```
|
71
|
+
user=andrew host=postgresql.example.com port=5432 dbname=blog aws_rds_iam_auth_token_generator=default
|
72
|
+
```
|
73
|
+
|
74
|
+
* a `key: value` pair in a connection hash:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
PG.connect(
|
78
|
+
user: "andrew",
|
79
|
+
host: "postgresql.example.com",
|
80
|
+
port: 5432,
|
81
|
+
dbname: "blog",
|
82
|
+
aws_rds_iam_auth_token_generator: "default"
|
83
|
+
)
|
84
|
+
```
|
85
|
+
|
86
|
+
* `database.yml`, if you're using Rails:
|
87
|
+
|
88
|
+
```yaml
|
89
|
+
production:
|
90
|
+
adapter: postgresql
|
91
|
+
username: andrew
|
92
|
+
host: postgresql.example.com
|
93
|
+
port: 5432
|
94
|
+
database: blog
|
95
|
+
aws_rds_iam_auth_token_generator: default
|
96
|
+
```
|
97
|
+
|
98
|
+
If the default authentication token generator doesn't meet your needs, you can register an alternative with
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
PG::AWS_IAM_RDS.auth_token_generators.add :custom do
|
102
|
+
PG::AWS_IAM_RDS::AuthTokenGenerator.new(credentials: ..., region: ...)
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
To use this alternative authentication token generator, set the `aws_rds_iam_auth_token_generator` connection parameter to the name you registered it with (`custom`, in this example).
|
107
|
+
|
108
|
+
The block you give to `add` must construct and return the authentication token generator, which can either be an instance of `PG::AWS_IAM_RDS::AuthTokenGenerator` or another object that returns a string token in response to `call(host:, port:, user:)`.
|
109
|
+
The block will be called once, when the first token is generated, and the returned authentication token generator will be re-used to generate all future tokens.
|
110
|
+
|
111
|
+
## Development
|
112
|
+
|
113
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
114
|
+
Then, run `bin/rake` to run the tests.
|
115
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
116
|
+
|
117
|
+
To install this gem onto your local machine, run `bin/rake install`.
|
118
|
+
To release a new version, update the version number in `version.rb`, and then run `bin/rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
119
|
+
|
120
|
+
## Contributing
|
121
|
+
|
122
|
+
Bug reports and pull requests are welcome [on GitHub](https://github.com/haines/pg-aws_rds_iam).
|
123
|
+
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/haines/pg-aws_rds_iam/blob/master/CODE_OF_CONDUCT.md).
|
124
|
+
|
125
|
+
## License
|
126
|
+
|
127
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|