activerecord-aws-secret-connector 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ede52301fb5baadbaec680812560494af3ebde837bd361eb78c1ff08da8a42a0
4
+ data.tar.gz: b01c4a66519fcc1a7f6dce66e8994f35e01852e5f5ad6a24dbf285717894eba8
5
+ SHA512:
6
+ metadata.gz: c67c45b27406f82c1a56a31f5cce4770d3cec41436e6c82849eb2db982ed728914fd8b0225140e5831c7283c730d1c68922cc7e8be5d323171601ba163fa659c
7
+ data.tar.gz: 9d2b6299ac6277b064f78eac2cb5271fdd50e81aca5cdf70e47204f0d035b3b275d70ec0566bf5b1b8c12a9e3fa5c22786a7dfefbffd0e63a63cb55f79e0b65d
@@ -0,0 +1,11 @@
1
+ # Contributing
2
+
3
+ 1. Fork the repository.
4
+ 2. Make your change with new passing tests, following the existing style.
5
+ 3. Write a [good commit message], push your fork, and submit a pull request.
6
+
7
+ [good commit message]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
8
+
9
+ Others will give constructive feedback. This is a time for discussion and
10
+ improvements, and making the necessary changes will be required before we can
11
+ merge the contribution.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-dotenv.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Zygo Team Repository
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # ActiveRecord AWS Secret Connector
2
+ An adapter to make possibly to use active record and database.yml to connect to database using aws secrets manager
3
+
4
+ ## Features
5
+
6
+ This adapter makes possible to store all database connection information on aws secrets manager, and even configure it to rotate the password from time to time,
7
+ and configures directly database.yml to connect to aws secret and easily use it to connect to the database.
8
+
9
+ This gem also uses rails can cache feature to store the database connection informations and avoid to connect and request those informations from ass secret every time. By default, it will expire the cache in 60 minutes.
10
+
11
+ ## Installation
12
+
13
+ Add this line to gemfile
14
+
15
+ ```ruby
16
+ gem "activerecord-aws-secret-connector"
17
+ ```
18
+
19
+ And then run
20
+ ```bash
21
+ bundle
22
+ ```
23
+
24
+ After that, configure the database.yml, specifiyng the aws secret key that
25
+ ```yaml
26
+ # config/database.yml
27
+
28
+ production:
29
+ aws_secret: YOUR_AWS_SECRET_KAY_FOR_DATABASE_CONNECTION
30
+ ```
31
+
32
+ This gem will connect to aws secret only for database config environments that has a `aws_secret` key on database.yml, working as default for other environments.
33
+
34
+ When `aws_secret` is present, it will ignore the keys `host`, `port`, `database`, `username` and `password`, even if they are passed on database.yml too. It will override the database.yml values with values from aws secret in that case.
35
+
36
+ ## Options
37
+
38
+ ### Cache expiration
39
+
40
+ By default, the gem will not use cache to store the database connection informations from aws secret. If you want to use cache and save some requests for aws secret, you need to set `cache_secret` key as true on database.yml, like below:
41
+
42
+ ```yaml
43
+ production:
44
+ aws_secret: YOUR_AWS_SECRET_KAY_FOR_DATABASE_CONNECTION
45
+ cache_secret: true
46
+ ```
47
+
48
+ When you set `cache_key` as true, the gem will use a default value for the cache key as DATABASE_SECRET_FOR_ENVIRONMENT and 60 minutes as expiration. Both attributes can be customized directly on database.yml too using `cache_key` and `cache_expires_in` keys. The value for `cache_expires_in` must be in minutes.
49
+
50
+ ```yaml
51
+ production:
52
+ aws_secret: YOUR_AWS_SECRET_KAY_FOR_DATABASE_CONNECTION
53
+ cache_secret: true
54
+ cache_key: CUSTOMIZED_CACHE_KEY
55
+ cache_expires_in: 360
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zygotecnologia/activerecord-aws-secret-connector.
61
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the (Contributor Covenant)[http://contributor-covenant.org/] code of conduct.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the (MIT License)[https://opensource.org/licenses/MIT].
66
+
67
+ ## TODO:
68
+
69
+ - [ ] Adds tests
70
+ - [ ] Adds configuration to connect to a different aws region from the default application aws region
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/aws_secret_connector/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'activerecord-aws-secret-connector'
8
+ spec.version = ActiveRecord::AwsSecretConnector::VERSION
9
+ spec.authors = ['João Paulo Lethier']
10
+ spec.email = ['jplethier@gmail.com']
11
+ spec.description = %q{Adds ability to active record connect to database using aws secret to store database connection informations}
12
+ spec.summary = %q{Adds ability to active record connect to database using aws secret to store database connection informations}
13
+ spec.homepage = 'https://github.com/zygotecnologia/activerecord-aws-secret-connector'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'aws-sdk-secretsmanager', '~> 1.43.0'
22
+
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module AwsSecretConnector
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,65 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class ConnectionPool
4
+ private
5
+
6
+ DEFAULT_DATABASE_SECRET_CACHE_KEY = "DATABASE_SECRET_FOR_#{Rails.env.upcase}"
7
+
8
+ # Overrides default new_connection method from active record
9
+ # to verify if database.yml has an aws_secret key
10
+ # It uses the aws_secret from database.yml to get
11
+ # all database connection informations from aws secret
12
+ # and possibly cache it to use it later easier
13
+ # when this key is present.
14
+ # Otherwise, just work as normal connection for activerecord default feature
15
+ def new_connection
16
+ config = spec.config
17
+
18
+ if spec.config.key? :aws_secret
19
+ database_info = database_config_with_cache if spec.config.try(:cache_secret)
20
+
21
+ database_info ||= database_config_from_secret
22
+
23
+ config.merge!(
24
+ host: database_info["host"],
25
+ port: database_info["port"],
26
+ database: database_info["dbname"],
27
+ username: database_info["username"],
28
+ password: database_info["password"]
29
+ )
30
+ end
31
+
32
+ Base.send(spec.adapter_method, config).tap do |conn|
33
+ conn.check_version
34
+ end
35
+ end
36
+
37
+ # Returns database config from cache if it is there, otherwise
38
+ # gets it from aws secret, stores it on cache and returns
39
+ #
40
+ # @return { "host" => "SOME_VALUE", "port" => "SOME_VALUE", "dbname" => "SOME_VALUE", "username" => "SOME_VALUE", "password" => "SOME_VALUE" }
41
+ def database_config_with_cache
42
+ Rails.cache.fetch(database_secret_cache_key, expire_in: database_secret_cache_expiration) do
43
+ database_config_from_secret
44
+ end
45
+ end
46
+
47
+ # Returns database config from secret directly
48
+ #
49
+ # @return { "host" => "SOME_VALUE", "port" => "SOME_VALUE", "dbname" => "SOME_VALUE", "username" => "SOME_VALUE", "password" => "SOME_VALUE" }
50
+ def database_config_from_secret
51
+ client = Aws::SecretsManager::Client.new
52
+
53
+ JSON.parse(client.get_secret_value(secret_id: spec.config[:aws_secret]).secret_string)
54
+ end
55
+
56
+ def database_secret_cache_key
57
+ spec.config.try(:cache_key).presence || DEFAULT_DATABASE_SECRET_CACHE_KEY
58
+ end
59
+
60
+ def database_secret_cache_expiration
61
+ (spec.config.try(:cache_expires_in).presence || 60).minutes
62
+ end
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-aws-secret-connector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - João Paulo Lethier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-secretsmanager
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.43.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.43.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Adds ability to active record connect to database using aws secret to
56
+ store database connection informations
57
+ email:
58
+ - jplethier@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - CONTRIBUTING.md
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - activerecord-aws-secret-connector.gemspec
68
+ - lib/active_record/aws_secret_connector/version.rb
69
+ - lib/active_record/connection_adapters/connection_pool.rb
70
+ homepage: https://github.com/zygotecnologia/activerecord-aws-secret-connector
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.0.6
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Adds ability to active record connect to database using aws secret to store
93
+ database connection informations
94
+ test_files: []