mysql2-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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 656c485ac1f6eae9f1d5e98ae78372181954769f32058ac1c9084f8f1bbe2a4c
4
+ data.tar.gz: a0fcb35212956975bdb0e3fa4908e44eefe0bac68db5324c1e906c4470e7c5f1
5
+ SHA512:
6
+ metadata.gz: 047445c597981ce9a02af12665f1309962b24f94f9e46e95b2000c32d893ac0ad82f547f588788294c6cd2ce8be30aacb0a9cfe9e7adf0081439d4a841b2af0f
7
+ data.tar.gz: 5c10dc7fc890d1e825c308a5cf309e66043da0e938c836826cccadf718e762f8d82a083d9857a024995857a641378259a611f3605d9934570937a602efc4fc4d
data/.yardopts ADDED
@@ -0,0 +1,5 @@
1
+ --markup markdown
2
+ --markup-provider commonmarker
3
+ --no-private
4
+ -
5
+ *.md
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
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
+ ## [Unreleased](https://github.com/haines/pg-aws_rds_iam/compare/v0.1.0...HEAD)
8
+
9
+ No notable changes.
10
+
11
+ ## [0.1.0](https://github.com/haines/pg-aws_rds_iam/compare/191a63e3c0222ac05bf06faaa496da954e352bbb...v0.1.0) - 2024-01-14
12
+
13
+ ### Added
14
+ * `Mysql2::AwsRdsIam` is an extension of [mysql2](https://github.com/brianmario/mysql2) gem that adds support of [IAM authentication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html) when connecting to MySQL in Amazon RDS.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Taras Shpachenko
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
+ # Mysql2::AwsRdsIam
2
+
3
+ [![Gem](https://img.shields.io/gem/v/mysql2-aws_rds_iam)](https://rubygems.org/gems/mysql2-aws_rds_iam)
4
+  
5
+ ![CI](https://img.shields.io/github/actions/workflow/status/floor114/mysql2-aws_rds_iam/ci.yml)
6
+
7
+ `Mysql2::AwsRdsIam` is an extension of [mysql2](https://github.com/brianmario/mysql2) gem that adds support of [IAM authentication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html) when connecting to MySQL in Amazon RDS.
8
+
9
+ This gem is a powerful tool that enables seamless connection to MySQL databases using the [mysql2](https://github.com/brianmario/mysql2) gem. It leverages the dynamic password generation feature of AWS RDS [IAM authentication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html) for enhanced security and easy password management.
10
+
11
+
12
+ ## Installation
13
+
14
+ Install manually:
15
+
16
+ ```console
17
+ $ gem install mysql2-aws_rds_iam
18
+ ```
19
+
20
+ or with Bundler:
21
+
22
+ ```console
23
+ $ bundle add mysql2-aws_rds_iam
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ To leverage [IAM authentication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html) for your database connections, follow these steps:
29
+
30
+ 1. Enable [IAM authentication](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html) for your database through AWS
31
+ 2. Add IAM credentials to your application.
32
+ 3. Set up your application to generate authentication tokens.
33
+
34
+
35
+ ### Application configurations
36
+
37
+ The default algorithm is `Mysql2::AwsRdsIam`'s [default authentication token generator](https://github.com/floor114/mysql2-aws_rds_iam/blob/main/lib/mysql2/aws_rds_iam/auth_token/generator.rb). Credentials and region are extracted using [aws-sdk-rds](https://github.com/aws/aws-sdk-ruby/tree/version-3/gems/aws-sdk-rds) configurations.
38
+
39
+
40
+ #### Apply msql2 patch
41
+ To connect to your MySQL database, you need to create initializer file that applies the patch:
42
+
43
+ ```ruby
44
+ # config/initializers/tcc_rds_iam_auth.rb
45
+
46
+ Tcc::RdsIamAuth.apply_patch
47
+
48
+ ```
49
+
50
+ #### Configure `database.yml`
51
+ New rds_iam_auth_host parameter must be added to the database.yml file:
52
+
53
+ ```yaml
54
+ production:
55
+ # ...
56
+ aws_rds_iam_auth: true
57
+ ```
58
+
59
+ #### Custom token generator
60
+ If the default generator doesn't meet your needs, you can create a custom one
61
+
62
+ ```ruby
63
+ # config/initializers/tcc_rds_iam_auth.rb
64
+
65
+ Mysql2::AwsRdsIam.auth_token_registry.add(:custom, ->(host, port, username) { 'your custom logic' })
66
+
67
+ ```
68
+
69
+ and specify it in `database.yml`
70
+
71
+ ```yaml
72
+ production:
73
+ # ...
74
+ aws_rds_iam_auth: true
75
+ aws_rds_iam_auth_token_generator: custom
76
+ ```
77
+
78
+ `Mysql2::AwsRdsIam.auth_token_registry` accepts two parameters:
79
+ 1. Generator name. The same name should be specified in `database.yml`
80
+ 2. Object that responds to `call` method and accepts 3 arguments (`host, port, username`) specified in `database.yml`.
81
+
82
+ ##### Possible generator types
83
+ * Lambda
84
+ ```ruby
85
+ Mysql2::AwsRdsIam.auth_token_registry.add(:custom, ->(host, port, username) { 'your custom logic' })
86
+
87
+ ```
88
+ * Generator instance
89
+ ```ruby
90
+ class CustomGenerator
91
+ def call(host, port, username)
92
+ GenerateMyCode
93
+ end
94
+ end
95
+
96
+ Mysql2::AwsRdsIam.auth_token_registry.add(:custom, CustomGenerator.new)
97
+
98
+ ```
99
+ * Generator class
100
+ ```ruby
101
+ class CustomGenerator
102
+ def self.call(host, port, username)
103
+ GenerateMyCode
104
+ end
105
+ end
106
+
107
+ Mysql2::AwsRdsIam.auth_token_registry.add(:custom, CustomGenerator)
108
+
109
+ ```
110
+
111
+ ## Development
112
+
113
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to run the tests and linter. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
114
+
115
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
116
+
117
+ ## Contributing
118
+
119
+ Bug reports and pull requests are welcome on GitHub at https://github.com/floor114/mysql2-aws_rds_iam. 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/[USERNAME]/mysql2-aws_rds_iam/blob/main/CODE_OF_CONDUCT.md).
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
124
+
125
+ ## Special Thanks
126
+
127
+ Inspired by [Andrew Haines'](https://github.com/haines) PG version [pg-aws_rds_iam](https://github.com/haines/pg-aws_rds_iam)
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mysql2
4
+ module AwsRdsIam
5
+ module AuthToken
6
+ class Factory
7
+ DEFAULT_GENERATOR = :default
8
+
9
+ def self.call(generator, host, port, username)
10
+ AwsRdsIam.auth_token_registry.fetch(generator&.to_sym || DEFAULT_GENERATOR).call(
11
+ host: host,
12
+ port: port,
13
+ username: username
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mysql2
4
+ module AwsRdsIam
5
+ module AuthToken
6
+ class Generator
7
+ def initialize
8
+ aws_config = Aws::RDS::Client.new.config
9
+
10
+ @generator = Aws::RDS::AuthTokenGenerator.new(credentials: aws_config.credentials)
11
+ @region = aws_config.region
12
+ end
13
+
14
+ def call(host:, port:, username:)
15
+ generator.auth_token(
16
+ region: region,
17
+ endpoint: "#{host}:#{port}",
18
+ user_name: username.to_s
19
+ )
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :generator, :region
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mysql2
4
+ module AwsRdsIam
5
+ module AuthToken
6
+ class Registry < Hash
7
+ def initialize
8
+ add(:default, Generator.new)
9
+
10
+ super
11
+ end
12
+
13
+ def add(name, generator)
14
+ self[name] = generator
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mysql2
4
+ module AwsRdsIam
5
+ module ClientExtension
6
+ def initialize(opts = {})
7
+ opts = opts.dup
8
+ aws_rds_iam_auth = opts.delete(:aws_rds_iam_auth)
9
+
10
+ if aws_rds_iam_auth
11
+ raise Errors::ReconnectConfigEnabledError if opts[:reconnect]
12
+
13
+ username = opts[:username]
14
+ host = opts[:host]
15
+ port = opts[:port]
16
+
17
+ raise Errors::UsernameNotFoundError if username.nil?
18
+ raise Errors::HostNotFoundError if host.nil?
19
+
20
+ opts.delete(:password)
21
+
22
+ aws_rds_iam_auth_token_generator = opts.delete(:aws_rds_iam_auth_token_generator)
23
+
24
+ opts[:password] = AuthToken::Factory.call(aws_rds_iam_auth_token_generator, host, port, username)
25
+ opts[:enable_cleartext_plugin] = true
26
+ end
27
+
28
+ super(opts)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mysql2
4
+ module AwsRdsIam
5
+ module Errors
6
+ class Error < StandardError; end
7
+
8
+ class Mysql2ClientNotFoundError < Error
9
+ def initialize
10
+ super('Could not find class or method when patching Mysql2::Client. Please investigate.')
11
+ end
12
+ end
13
+
14
+ class ReconnectConfigEnabledError < Error
15
+ def initialize
16
+ super('reconnect config must be false if using AWS RDS IAM authentication.')
17
+ end
18
+ end
19
+
20
+ class UsernameNotFoundError < Error
21
+ def initialize
22
+ super('username must be present.')
23
+ end
24
+ end
25
+
26
+ class HostNotFoundError < Error
27
+ def initialize
28
+ super('host must be present.')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mysql2
4
+ module AwsRdsIam
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-rds'
4
+ require 'mysql2'
5
+ require 'zeitwerk'
6
+
7
+ loader = Zeitwerk::Loader.for_gem_extension(Mysql2)
8
+ loader.setup
9
+
10
+ module Mysql2
11
+ module AwsRdsIam
12
+ def self.auth_token_registry
13
+ @auth_token_registry ||= AuthToken::Registry.new
14
+ end
15
+
16
+ def self.apply_patch
17
+ const = begin
18
+ Object.const_get('Mysql2::Client')
19
+ rescue StandardError
20
+ raise Errors::Mysql2ClientNotFoundError
21
+ end
22
+
23
+ begin
24
+ const.instance_method(:initialize)
25
+ rescue StandardError
26
+ raise Errors::Mysql2ClientNotFoundError
27
+ end
28
+
29
+ const.prepend(ClientExtension)
30
+ end
31
+ end
32
+
33
+ AwsRdsIam.apply_patch
34
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mysql2/aws_rds_iam'
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/mysql2/aws_rds_iam/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'mysql2-aws_rds_iam'
7
+ spec.version = Mysql2::AwsRdsIam::VERSION
8
+ spec.authors = ['Taras Shpachenko']
9
+ spec.email = ['taras.shpachenko@gmail.com']
10
+
11
+ spec.summary = 'AWS RDS IAM authentication for MySQL'
12
+ spec.description = 'Mysql2::AwsRdsIam is an extension of mysql2 gem that adds support ' \
13
+ 'of IAM authentication when connecting to MySQL in Amazon RDS.'
14
+ spec.homepage = 'https://github.com/floor114/mysql2-aws_rds_iam'
15
+ spec.license = 'MIT'
16
+
17
+ spec.required_ruby_version = '>= 3.0.0'
18
+
19
+ spec.metadata['rubygems_mfa_required'] = 'true'
20
+
21
+ spec.metadata['homepage_uri'] = spec.homepage
22
+ spec.metadata['source_code_uri'] = spec.homepage
23
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
24
+ spec.metadata['bug_tracker_uri'] = "#{spec.homepage}/issues"
25
+ spec.metadata['documentation_uri'] = "https://rubydoc.info/gems/mysql2-aws_rds_iam/#{Mysql2::AwsRdsIam::VERSION}"
26
+
27
+ spec.files = Dir[
28
+ 'lib/**/*.rb',
29
+ '.yardopts',
30
+ 'CHANGELOG.md',
31
+ 'LICENSE.txt',
32
+ 'mysql2-aws_rds_iam.gemspec',
33
+ 'README.md'
34
+ ]
35
+ spec.require_paths = ['lib']
36
+
37
+ # Uncomment to register a new dependency of your gem
38
+ spec.add_dependency 'aws-sdk-rds', '~> 1'
39
+ spec.add_dependency 'mysql2'
40
+ spec.add_dependency 'zeitwerk', '~> 2'
41
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql2-aws_rds_iam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Taras Shpachenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-rds
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: zeitwerk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ description: Mysql2::AwsRdsIam is an extension of mysql2 gem that adds support of
56
+ IAM authentication when connecting to MySQL in Amazon RDS.
57
+ email:
58
+ - taras.shpachenko@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".yardopts"
64
+ - CHANGELOG.md
65
+ - LICENSE.txt
66
+ - README.md
67
+ - lib/mysql2-aws_rds_iam.rb
68
+ - lib/mysql2/aws_rds_iam.rb
69
+ - lib/mysql2/aws_rds_iam/auth_token/factory.rb
70
+ - lib/mysql2/aws_rds_iam/auth_token/generator.rb
71
+ - lib/mysql2/aws_rds_iam/auth_token/registry.rb
72
+ - lib/mysql2/aws_rds_iam/client_extension.rb
73
+ - lib/mysql2/aws_rds_iam/errors.rb
74
+ - lib/mysql2/aws_rds_iam/version.rb
75
+ - mysql2-aws_rds_iam.gemspec
76
+ homepage: https://github.com/floor114/mysql2-aws_rds_iam
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ rubygems_mfa_required: 'true'
81
+ homepage_uri: https://github.com/floor114/mysql2-aws_rds_iam
82
+ source_code_uri: https://github.com/floor114/mysql2-aws_rds_iam
83
+ changelog_uri: https://github.com/floor114/mysql2-aws_rds_iam/blob/main/CHANGELOG.md
84
+ bug_tracker_uri: https://github.com/floor114/mysql2-aws_rds_iam/issues
85
+ documentation_uri: https://rubydoc.info/gems/mysql2-aws_rds_iam/0.1.0
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 3.0.0
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.5.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: AWS RDS IAM authentication for MySQL
105
+ test_files: []