pg-aws_rds_iam 0.6.1 → 0.7.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: 8de2c77e449ab425d3294a78bfb769377b96e33ef415144066c442103bdca6db
4
- data.tar.gz: a10dcfe36a489d448cfaaa5a598af4bf99d1f29cb9abc7b0f27fb85de00c08c7
3
+ metadata.gz: abc5ffd5784f5ad5c2fc33e510cd99d9f02ad9a0cf6a231427abf2b8593dbdf2
4
+ data.tar.gz: 6552886d708ea4a2e819d4cb6fa6db9d1c59eec9dffeb09b1a38d135bbde1da1
5
5
  SHA512:
6
- metadata.gz: e07d05f1b3ba640764c8493e0b27bc94722c8b7afb2688a9168551821dc2518eccc4d74f241a6a91bec51ea67a16e93a72924100014c6643bc7e4c1ddc0fc0aa
7
- data.tar.gz: 8a3b8388bac508791363b4252283bc48d6b8412c3b99337dad6640ea1475005062bfb1528a82f042b0fd76f46f97afa85d41b221881fee9520f2e4bf2064a9df
6
+ metadata.gz: 5d1c3a0f5d0e292ec0199ed8e420644cc1574ef80b2fbf2b71a0c9b8656e6f757aeb575cc0198c47dbdb92b1013e46c61ab5428d142cca650d35063ea2c2a7ad
7
+ data.tar.gz: 629e158616fc8319beb73ea54010bee5f6eb41adc30fecaf9ff0480aa693706b026572cb72597a62bd7b4fd765a83d831687f8ee9da7385b4c5e92bbd95b6295
data/CHANGELOG.md CHANGED
@@ -8,6 +8,16 @@ 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.7.0] - 2024-12-04
12
+
13
+ ### Changed
14
+ * Reuse tokens ([#690](https://github.com/haines/pg-aws_rds_iam/pull/690))
15
+
16
+ ## [0.6.2] - 2024-11-12
17
+
18
+ ### Changed
19
+ * Fix broken link in changelog ([#687](https://github.com/haines/pg-aws_rds_iam/pull/687))
20
+
11
21
  ## [0.6.1] - 2024-11-12
12
22
 
13
23
  ### Changed
@@ -91,7 +101,10 @@ No notable changes.
91
101
  * 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))
92
102
  * ActiveRecord support. ([#3](https://github.com/haines/pg-aws_rds_iam/pull/3))
93
103
 
94
- [Unreleased]: https://github.com/haines/pg-aws_rds_iam/compare/v0.6.0...HEAD
104
+ [Unreleased]: https://github.com/haines/pg-aws_rds_iam/compare/v0.7.0...HEAD
105
+ [0.7.0]: https://github.com/haines/pg-aws_rds_iam/compare/v0.6.2...v0.7.0
106
+ [0.6.2]: https://github.com/haines/pg-aws_rds_iam/compare/v0.6.1...v0.6.2
107
+ [0.6.1]: https://github.com/haines/pg-aws_rds_iam/compare/v0.6.0...v0.6.1
95
108
  [0.6.0]: https://github.com/haines/pg-aws_rds_iam/compare/v0.5.0...v0.6.0
96
109
  [0.5.0]: https://github.com/haines/pg-aws_rds_iam/compare/v0.4.2...v0.5.0
97
110
  [0.4.2]: https://github.com/haines/pg-aws_rds_iam/compare/v0.4.1...v0.4.2
@@ -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,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.1"
6
+ VERSION = "0.7.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"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg-aws_rds_iam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Haines
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-12 00:00:00.000000000 Z
11
+ date: 2024-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-rds
@@ -55,6 +55,7 @@ files:
55
55
  - lib/pg/aws_rds_iam/active_record_postgresql_database_tasks.rb
56
56
  - lib/pg/aws_rds_iam/active_record_postgresql_database_tasks/psql_env.rb
57
57
  - lib/pg/aws_rds_iam/active_record_postgresql_database_tasks/set_psql_env.rb
58
+ - lib/pg/aws_rds_iam/auth_token.rb
58
59
  - lib/pg/aws_rds_iam/auth_token_generator.rb
59
60
  - lib/pg/aws_rds_iam/auth_token_generator_registry.rb
60
61
  - lib/pg/aws_rds_iam/auth_token_injector.rb
@@ -73,7 +74,7 @@ licenses:
73
74
  metadata:
74
75
  bug_tracker_uri: https://github.com/haines/pg-aws_rds_iam/issues
75
76
  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.1
77
+ documentation_uri: https://rubydoc.info/gems/pg-aws_rds_iam/0.7.0
77
78
  homepage_uri: https://github.com/haines/pg-aws_rds_iam
78
79
  source_code_uri: https://github.com/haines/pg-aws_rds_iam
79
80
  rubygems_mfa_required: 'true'