pg-aws_rds_iam 0.6.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ca9285880d14457835d0b91196653cf230f6d8a269945c65b40af0031653987
4
- data.tar.gz: 1749a70b4736c35fa0a9fffff961b3d9101df73cdef9ecffc8fadad2f6401daf
3
+ metadata.gz: 033ab6c71e67b76950791800722751b6f00ac7403c0269f19a9b135875d17fec
4
+ data.tar.gz: c8e53189801821043e80c7a692c81b140ac29ad337c250434c8a00ec6f8b6464
5
5
  SHA512:
6
- metadata.gz: 65188829a0c75e07b7c7809643999a0b9d65924437240909aed32e3b7e32f8f8289e6df699724e250b57401e2a0437cdd675eddf7cc29335278e09e7d446ec6a
7
- data.tar.gz: 2818e00188696512b5dc89b513e9c2d8110af3d337ec9f0e4b229027c2f9f7d339770c91462bc7bb3a83afcd263cfec099d38c8d7736be870428358ad97492a3
6
+ metadata.gz: '097fcef5922b2485a79e26a608c546711bbcf7d6a4ee129db557b6324bb280cf06944f9008336e14c84699f99e440d7f2c300baa90b4e113b334c33fc8f58cf7'
7
+ data.tar.gz: 0ad4fa689960fc2e4f0587743033d842b6e2faf8248051877339f134cd77176ddc4379c60d7b5aec0befcd59d5c6ae7ccb169ed8500399a315846ce9743e5d1e
data/CHANGELOG.md CHANGED
@@ -8,6 +8,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8
8
 
9
9
  No notable changes.
10
10
 
11
+ ## [0.8.0] - 2026-03-27
12
+
13
+ ### Changed
14
+ * Test against Ruby 3.4 ([#697](https://github.com/haines/pg-aws_rds_iam/pull/697))
15
+ * Test against Ruby 4.0 ([#779](https://github.com/haines/pg-aws_rds_iam/pull/779))
16
+ * Require Ruby ≥ 3.2, Active Record ≥ 7.0, and PG ≥ 1.3 ([#726](https://github.com/haines/pg-aws_rds_iam/pull/726))
17
+ * Test against Sequel ([#756](https://github.com/haines/pg-aws_rds_iam/pull/756))
18
+
19
+ ## [0.7.0] - 2024-12-04
20
+
21
+ ### Changed
22
+ * Reuse tokens ([#690](https://github.com/haines/pg-aws_rds_iam/pull/690))
23
+
11
24
  ## [0.6.2] - 2024-11-12
12
25
 
13
26
  ### Changed
@@ -96,7 +109,9 @@ No notable changes.
96
109
  * 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))
97
110
  * ActiveRecord support. ([#3](https://github.com/haines/pg-aws_rds_iam/pull/3))
98
111
 
99
- [Unreleased]: https://github.com/haines/pg-aws_rds_iam/compare/v0.6.2...HEAD
112
+ [Unreleased]: https://github.com/haines/pg-aws_rds_iam/compare/v0.8.0...HEAD
113
+ [0.8.0]: https://github.com/haines/pg-aws_rds_iam/compare/v0.7.0...v0.8.0
114
+ [0.7.0]: https://github.com/haines/pg-aws_rds_iam/compare/v0.6.2...v0.7.0
100
115
  [0.6.2]: https://github.com/haines/pg-aws_rds_iam/compare/v0.6.1...v0.6.2
101
116
  [0.6.1]: https://github.com/haines/pg-aws_rds_iam/compare/v0.6.0...v0.6.1
102
117
  [0.6.0]: https://github.com/haines/pg-aws_rds_iam/compare/v0.5.0...v0.6.0
data/README.md CHANGED
@@ -99,6 +99,14 @@ You can set this parameter in
99
99
  aws_rds_iam_auth_token_generator: default
100
100
  ```
101
101
 
102
+ * `driver_options`, if you're using Sequel:
103
+
104
+ ```ruby
105
+ Sequel.connect("postgresql://andrew@postgresql.example.com:5432/blog", driver_options: {
106
+ aws_rds_iam_auth_token_generator: "default"
107
+ })
108
+ ```
109
+
102
110
  If the default authentication token generator doesn't meet your needs, you can register an alternative with
103
111
 
104
112
  ```ruby
@@ -112,14 +120,26 @@ To use this alternative authentication token generator, set the `aws_rds_iam_aut
112
120
  The block you give to `add` must construct and return the authentication token generator, which can either be an instance of `PG::AWS_RDS_IAM::AuthTokenGenerator` or another object that returns a string token in response to `call(host:, port:, user:)`.
113
121
  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.
114
122
 
123
+ ### 4. Set `sslmode` to `verify-full` (recommended)
124
+
125
+ Although not required to use IAM authentication, to further improve security when connecting to your database, you should consider setting the `sslmode` connection parameter to `verify-full`.
126
+ This ensures that your application is connecting to an RDS instance, preventing man-in-the-middle attacks.
127
+
128
+ You'll need to [download the RDS certificate bundle from AWS](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html) and set the `sslrootcert` connection parameter to the path to the downloaded file.
129
+
115
130
  ## Development
116
131
 
117
132
  After checking out the repo, run `bin/setup` to install dependencies.
118
133
  Then, run `bin/rake` to run the tests.
119
134
  You can also run `bin/console` for an interactive prompt that will allow you to experiment.
120
135
 
121
- To install this gem onto your local machine, run `bin/rake install`.
122
- 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).
136
+ To release a new version:
137
+
138
+ 1. Update the version number in [version.rb](lib/pg/aws_rds_iam/version.rb), and run `bundle install` to update [Gemfile.lock](Gemfile.lock).
139
+ 2. Update [CHANGELOG.md](CHANGELOG.md).
140
+ 3. Submit the changes as a pull request.
141
+ 4. Once merged, run `bin/rake release:tag` to tag the release and push the tag to GitHub.
142
+ The gem is published to [rubygems.org](https://rubygems.org/gems/pg-aws_rds_iam) using [trusted publishing](https://guides.rubygems.org/trusted-publishing/) via GitHub Actions.
123
143
 
124
144
  ## Contributing
125
145
 
@@ -3,14 +3,15 @@
3
3
  module PG
4
4
  module AWS_RDS_IAM
5
5
  module ActiveRecordPostgreSQLDatabaseTasks
6
+ private
7
+
8
+ def psql_env
9
+ super.tap do |psql_env|
10
+ AuthTokenInjector.new.inject_into_psql_env! configuration_hash, psql_env
11
+ end
12
+ end
6
13
  end
7
14
 
8
15
  private_constant :ActiveRecordPostgreSQLDatabaseTasks
9
16
  end
10
17
  end
11
-
12
- if ActiveRecord::Tasks::PostgreSQLDatabaseTasks.private_instance_methods.include?(:psql_env)
13
- require_relative "active_record_postgresql_database_tasks/psql_env"
14
- else
15
- require_relative "active_record_postgresql_database_tasks/set_psql_env"
16
- end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PG
4
+ module AWS_RDS_IAM
5
+ class AuthToken
6
+ def initialize(token)
7
+ @token = token
8
+ @generated_at = now
9
+ @expiry = parse_expiry || 900
10
+ end
11
+
12
+ def valid?
13
+ (now - @generated_at) < (@expiry - 60)
14
+ end
15
+
16
+ def to_str
17
+ @token
18
+ end
19
+
20
+ private
21
+
22
+ def now
23
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
24
+ end
25
+
26
+ def parse_expiry
27
+ URI
28
+ .decode_www_form(URI.parse("https://#{@token}").query)
29
+ .lazy
30
+ .filter_map { |(key, value)| Integer(value, 10) if key.downcase == "x-amz-expires" }
31
+ .first
32
+ rescue StandardError
33
+ nil
34
+ end
35
+ end
36
+
37
+ private_constant :AuthToken
38
+ end
39
+ end
@@ -13,16 +13,39 @@ module PG
13
13
  def initialize(credentials:, region:)
14
14
  @generator = Aws::RDS::AuthTokenGenerator.new(credentials:)
15
15
  @region = region
16
+ @mutex = Mutex.new
17
+ @cache = {}
16
18
  end
17
19
 
18
20
  # Generates an authentication token for connecting to an Amazon RDS instance.
21
+ # Generated tokens are cached and reused until 1 minute before they are due to expire.
19
22
  #
20
23
  # @param host [String] the host name of the RDS instance that you want to access
21
24
  # @param port [String] the port number used for connecting to your RDS instance
22
25
  # @param user [String] the database account that you want to access
23
26
  # @return [String] the generated authentication token
24
27
  def call(host:, port:, user:)
25
- @generator.auth_token(region: @region, endpoint: "#{host}:#{port}", user_name: user)
28
+ endpoint = "#{host}:#{port}"
29
+ key = "#{user}@#{endpoint}"
30
+
31
+ token = cached_token(key)
32
+ return token if token
33
+
34
+ @mutex.synchronize do
35
+ token = cached_token(key)
36
+ break token if token
37
+
38
+ @generator.auth_token(region: @region, endpoint:, user_name: user).tap do |new_token|
39
+ @cache[key] = AuthToken.new(new_token)
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def cached_token(key)
47
+ token = @cache[key]
48
+ token.to_str if token&.valid?
26
49
  end
27
50
  end
28
51
  end
@@ -3,7 +3,8 @@
3
3
  module PG
4
4
  module AWS_RDS_IAM
5
5
  module ConnectionInfo
6
- ParseError = Class.new(StandardError)
6
+ class ParseError < StandardError
7
+ end
7
8
  end
8
9
  end
9
10
  end
@@ -5,14 +5,7 @@ module PG
5
5
  module ConnectionInfo
6
6
  class URI
7
7
  def self.match?(connection_string)
8
- regexp =
9
- if defined?(::URI::RFC2396_PARSER)
10
- ::URI::RFC2396_PARSER.regexp[:ABS_URI_REF]
11
- else
12
- ::URI::ABS_URI_REF
13
- end
14
-
15
- /\A#{regexp}\z/.match?(connection_string)
8
+ /\A#{::URI::RFC2396_PARSER.regexp[:ABS_URI_REF]}\z/.match?(connection_string)
16
9
  end
17
10
 
18
11
  attr_reader :auth_token_generator_name
@@ -3,6 +3,6 @@
3
3
  module PG
4
4
  module AWS_RDS_IAM
5
5
  # The current version of the gem.
6
- VERSION = "0.6.2"
6
+ VERSION = "0.8.0"
7
7
  end
8
8
  end
@@ -5,6 +5,7 @@ require "pg"
5
5
  require "strscan"
6
6
  require "uri"
7
7
 
8
+ require_relative "aws_rds_iam/auth_token"
8
9
  require_relative "aws_rds_iam/auth_token_generator"
9
10
  require_relative "aws_rds_iam/auth_token_generator_registry"
10
11
  require_relative "aws_rds_iam/auth_token_injector"
@@ -41,7 +42,7 @@ module PG
41
42
  ActiveRecord::Tasks::PostgreSQLDatabaseTasks.prepend ActiveRecordPostgreSQLDatabaseTasks
42
43
  end
43
44
 
44
- if defined?(Rails::DBConsole) && Rails::DBConsole.private_instance_methods.include?(:find_cmd_and_exec)
45
+ if defined?(Rails::DBConsole) && Rails::DBConsole.private_method_defined?(:find_cmd_and_exec)
45
46
  require_relative "aws_rds_iam/rails_dbconsole"
46
47
 
47
48
  Rails::DBConsole.prepend RailsDBConsole
@@ -34,8 +34,8 @@ Gem::Specification.new do |spec|
34
34
 
35
35
  spec.require_paths = ["lib"]
36
36
 
37
- spec.required_ruby_version = ">= 3.1"
37
+ spec.required_ruby_version = ">= 3.2"
38
38
 
39
39
  spec.add_dependency "aws-sdk-rds", "~> 1.0"
40
- spec.add_dependency "pg", "~> 1.1"
40
+ spec.add_dependency "pg", "~> 1.3"
41
41
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg-aws_rds_iam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Haines
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-11-12 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: aws-sdk-rds
@@ -30,14 +29,14 @@ dependencies:
30
29
  requirements:
31
30
  - - "~>"
32
31
  - !ruby/object:Gem::Version
33
- version: '1.1'
32
+ version: '1.3'
34
33
  type: :runtime
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - "~>"
39
38
  - !ruby/object:Gem::Version
40
- version: '1.1'
39
+ version: '1.3'
41
40
  description: PG::AWS_RDS_IAM is a plugin for the pg gem that adds support for IAM
42
41
  authentication when connecting to PostgreSQL databases hosted in Amazon RDS.
43
42
  email:
@@ -53,8 +52,7 @@ files:
53
52
  - lib/pg/aws_rds_iam.rb
54
53
  - lib/pg/aws_rds_iam/active_record_postgresql_adapter.rb
55
54
  - lib/pg/aws_rds_iam/active_record_postgresql_database_tasks.rb
56
- - lib/pg/aws_rds_iam/active_record_postgresql_database_tasks/psql_env.rb
57
- - lib/pg/aws_rds_iam/active_record_postgresql_database_tasks/set_psql_env.rb
55
+ - lib/pg/aws_rds_iam/auth_token.rb
58
56
  - lib/pg/aws_rds_iam/auth_token_generator.rb
59
57
  - lib/pg/aws_rds_iam/auth_token_generator_registry.rb
60
58
  - lib/pg/aws_rds_iam/auth_token_injector.rb
@@ -73,11 +71,10 @@ licenses:
73
71
  metadata:
74
72
  bug_tracker_uri: https://github.com/haines/pg-aws_rds_iam/issues
75
73
  changelog_uri: https://github.com/haines/pg-aws_rds_iam/blob/main/CHANGELOG.md
76
- documentation_uri: https://rubydoc.info/gems/pg-aws_rds_iam/0.6.2
74
+ documentation_uri: https://rubydoc.info/gems/pg-aws_rds_iam/0.8.0
77
75
  homepage_uri: https://github.com/haines/pg-aws_rds_iam
78
76
  source_code_uri: https://github.com/haines/pg-aws_rds_iam
79
77
  rubygems_mfa_required: 'true'
80
- post_install_message:
81
78
  rdoc_options: []
82
79
  require_paths:
83
80
  - lib
@@ -85,15 +82,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
82
  requirements:
86
83
  - - ">="
87
84
  - !ruby/object:Gem::Version
88
- version: '3.1'
85
+ version: '3.2'
89
86
  required_rubygems_version: !ruby/object:Gem::Requirement
90
87
  requirements:
91
88
  - - ">="
92
89
  - !ruby/object:Gem::Version
93
90
  version: '0'
94
91
  requirements: []
95
- rubygems_version: 3.5.23
96
- signing_key:
92
+ rubygems_version: 4.0.9
97
93
  specification_version: 4
98
94
  summary: IAM authentication for PostgreSQL on Amazon RDS
99
95
  test_files: []
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module PG
4
- module AWS_RDS_IAM
5
- module ActiveRecordPostgreSQLDatabaseTasks
6
- private
7
-
8
- def psql_env
9
- super.tap do |psql_env|
10
- AuthTokenInjector.new.inject_into_psql_env! configuration_hash, psql_env
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module PG
4
- module AWS_RDS_IAM
5
- module ActiveRecordPostgreSQLDatabaseTasks
6
- private
7
-
8
- def set_psql_env
9
- super
10
- AuthTokenInjector.new.inject_into_psql_env! configuration_hash, ENV
11
- end
12
- end
13
- end
14
- end