pg-aws_rds_iam 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PG
4
+ module AWS_RDS_IAM
5
+ module ConnectionInfo
6
+ class KeywordValueString
7
+ attr_reader :auth_token_generator_name
8
+
9
+ def initialize(connection_string)
10
+ @params = Parser.new(connection_string).parse
11
+ @auth_token_generator_name = @params.delete(:aws_rds_iam_auth_token_generator)
12
+ end
13
+
14
+ def user
15
+ @params[:user]
16
+ end
17
+
18
+ def host
19
+ @params[:host]
20
+ end
21
+
22
+ def port
23
+ @params[:port]
24
+ end
25
+
26
+ def password=(value)
27
+ @params[:password] = value
28
+ end
29
+
30
+ def to_s
31
+ @params.map { |key, value| "#{key}=#{PG::Connection.quote_connstr(value)}" }.join(" ")
32
+ end
33
+
34
+ def to_h
35
+ @params.dup
36
+ end
37
+
38
+ class Parser
39
+ def initialize(connection_string)
40
+ @buffer = StringScanner.new(connection_string)
41
+ end
42
+
43
+ # @see https://github.com/postgres/postgres/blob/REL_12_1/src/interfaces/libpq/fe-connect.c#L5344-L5457
44
+ def parse
45
+ result = {}
46
+
47
+ skip_whitespace
48
+ until @buffer.eos?
49
+ key = parse_key
50
+ skip_whitespace
51
+ assert_followed_by_equals_sign key
52
+ skip_whitespace
53
+ value = parse_value
54
+ skip_whitespace
55
+
56
+ result[key] = value
57
+ end
58
+
59
+ @buffer.reset
60
+ result
61
+ end
62
+
63
+ private
64
+
65
+ def skip_whitespace
66
+ @buffer.skip(/\s+/)
67
+ end
68
+
69
+ def parse_key
70
+ key = @buffer.scan(/[^=\s]+/)
71
+ raise ParseError, %(Missing parameter name before "#{@buffer.rest}") unless key
72
+
73
+ key.to_sym
74
+ end
75
+
76
+ def assert_followed_by_equals_sign(key)
77
+ raise ParseError, %(Missing "=" after "#{key}") unless @buffer.getch == "="
78
+ end
79
+
80
+ def parse_value
81
+ if @buffer.peek(1) == "'"
82
+ parse_quoted_value
83
+ else
84
+ parse_unquoted_value
85
+ end
86
+ end
87
+
88
+ def parse_quoted_value
89
+ value = +""
90
+ @buffer.skip(/'/)
91
+ loop do
92
+ value << (@buffer.scan(/[^'\\]+/) || "")
93
+ case @buffer.getch
94
+ when "'" then return value
95
+ when "\\" then value << (@buffer.getch || "")
96
+ else raise ParseError, "Unterminated quoted value"
97
+ end
98
+ end
99
+ end
100
+
101
+ def parse_unquoted_value
102
+ value = +""
103
+ loop do
104
+ value << (@buffer.scan(/[^\s\\]+/) || "")
105
+ return value unless @buffer.getch == "\\"
106
+
107
+ value << (@buffer.getch || "")
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PG
4
+ module AWS_RDS_IAM
5
+ module ConnectionInfo
6
+ ParseError = Class.new(StandardError)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PG
4
+ module AWS_RDS_IAM
5
+ module ConnectionInfo
6
+ class URI
7
+ def self.match?(connection_string)
8
+ /\A#{::URI::ABS_URI_REF}\z/.match?(connection_string)
9
+ end
10
+
11
+ attr_reader :auth_token_generator_name
12
+
13
+ def initialize(connection_string)
14
+ @uri = ::URI.parse(connection_string)
15
+ @query = ::URI.decode_www_form(@uri.query).to_h
16
+ @auth_token_generator_name = @query.delete("aws_rds_iam_auth_token_generator")
17
+ end
18
+
19
+ def user
20
+ @uri.user || @query["user"]
21
+ end
22
+
23
+ def host
24
+ @uri.host || @query["host"]
25
+ end
26
+
27
+ def port
28
+ @uri.port || @query["port"]
29
+ end
30
+
31
+ def password=(value)
32
+ @uri.password = nil
33
+ @query["password"] = value
34
+ end
35
+
36
+ def to_s
37
+ @uri.query = ::URI.encode_www_form(@query)
38
+
39
+ @uri.to_s.sub(%r{^#{@uri.scheme}:(?!//)}, "#{@uri.scheme}://")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PG
4
+ module AWS_RDS_IAM
5
+ # The current version of the gem.
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "pg/aws_rds_iam/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "pg-aws_rds_iam"
9
+ spec.version = PG::AWS_RDS_IAM::VERSION
10
+ spec.authors = ["Andrew Haines"]
11
+ spec.email = ["andrew@haines.org.nz"]
12
+
13
+ spec.summary = "IAM authentication for PostgreSQL on Amazon RDS"
14
+ spec.description = "PG::AWS_RDS_IAM is a plugin for the pg gem that adds support for IAM authentication when connecting to PostgreSQL databases hosted in Amazon RDS."
15
+ spec.homepage = "https://github.com/haines/pg-aws_rds_iam"
16
+ spec.license = "MIT"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"
21
+
22
+ spec.files = Dir.chdir(__dir__) { `git ls-files -z`.split("\x0").reject { |path| path.start_with?("test/") } }
23
+
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "aws-sdk-rds", "~> 1.0"
27
+ spec.add_dependency "pg", ">= 0.18", "< 2.0"
28
+
29
+ spec.add_development_dependency "activerecord", ">= 5.2", "< 7.0"
30
+ spec.add_development_dependency "aws-sdk-ec2", "~> 1.137"
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "minitest", "~> 5.14"
33
+ spec.add_development_dependency "minitest-reporters", "~> 1.4"
34
+ spec.add_development_dependency "pry", "~> 0.12"
35
+ spec.add_development_dependency "rake", "~> 13.0"
36
+ spec.add_development_dependency "rubocop", "~> 0.79"
37
+ spec.add_development_dependency "timecop", "~> 0.9"
38
+ spec.add_development_dependency "yard", "~> 0.9"
39
+ end
metadata ADDED
@@ -0,0 +1,256 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg-aws_rds_iam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Haines
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-05 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.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.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.18'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '2.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0.18'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: activerecord
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '5.2'
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '7.0'
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '5.2'
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '7.0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: aws-sdk-ec2
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.137'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.137'
81
+ - !ruby/object:Gem::Dependency
82
+ name: bundler
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '2.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '2.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: minitest
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '5.14'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '5.14'
109
+ - !ruby/object:Gem::Dependency
110
+ name: minitest-reporters
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '1.4'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '1.4'
123
+ - !ruby/object:Gem::Dependency
124
+ name: pry
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '0.12'
130
+ type: :development
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - "~>"
135
+ - !ruby/object:Gem::Version
136
+ version: '0.12'
137
+ - !ruby/object:Gem::Dependency
138
+ name: rake
139
+ requirement: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: '13.0'
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - "~>"
149
+ - !ruby/object:Gem::Version
150
+ version: '13.0'
151
+ - !ruby/object:Gem::Dependency
152
+ name: rubocop
153
+ requirement: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '0.79'
158
+ type: :development
159
+ prerelease: false
160
+ version_requirements: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - "~>"
163
+ - !ruby/object:Gem::Version
164
+ version: '0.79'
165
+ - !ruby/object:Gem::Dependency
166
+ name: timecop
167
+ requirement: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - "~>"
170
+ - !ruby/object:Gem::Version
171
+ version: '0.9'
172
+ type: :development
173
+ prerelease: false
174
+ version_requirements: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - "~>"
177
+ - !ruby/object:Gem::Version
178
+ version: '0.9'
179
+ - !ruby/object:Gem::Dependency
180
+ name: yard
181
+ requirement: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - "~>"
184
+ - !ruby/object:Gem::Version
185
+ version: '0.9'
186
+ type: :development
187
+ prerelease: false
188
+ version_requirements: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - "~>"
191
+ - !ruby/object:Gem::Version
192
+ version: '0.9'
193
+ description: PG::AWS_RDS_IAM is a plugin for the pg gem that adds support for IAM
194
+ authentication when connecting to PostgreSQL databases hosted in Amazon RDS.
195
+ email:
196
+ - andrew@haines.org.nz
197
+ executables: []
198
+ extensions: []
199
+ extra_rdoc_files: []
200
+ files:
201
+ - ".github/workflows/pull-request.yml"
202
+ - ".gitignore"
203
+ - ".rubocop.yml"
204
+ - ".ruby-version"
205
+ - ".yardopts"
206
+ - CHANGELOG.md
207
+ - CODE_OF_CONDUCT.md
208
+ - Gemfile
209
+ - Gemfile.lock
210
+ - LICENSE.txt
211
+ - README.md
212
+ - Rakefile
213
+ - bin/bundle
214
+ - bin/console
215
+ - bin/install-bundler
216
+ - bin/rake
217
+ - bin/setup
218
+ - bin/yard
219
+ - lib/pg/aws_rds_iam.rb
220
+ - lib/pg/aws_rds_iam/auth_token_generator.rb
221
+ - lib/pg/aws_rds_iam/auth_token_generator_registry.rb
222
+ - lib/pg/aws_rds_iam/auth_token_injector.rb
223
+ - lib/pg/aws_rds_iam/connection.rb
224
+ - lib/pg/aws_rds_iam/connection_info.rb
225
+ - lib/pg/aws_rds_iam/connection_info/keyword_value_string.rb
226
+ - lib/pg/aws_rds_iam/connection_info/parse_error.rb
227
+ - lib/pg/aws_rds_iam/connection_info/uri.rb
228
+ - lib/pg/aws_rds_iam/version.rb
229
+ - pg-aws_rds_iam.gemspec
230
+ homepage: https://github.com/haines/pg-aws_rds_iam
231
+ licenses:
232
+ - MIT
233
+ metadata:
234
+ homepage_uri: https://github.com/haines/pg-aws_rds_iam
235
+ source_code_uri: https://github.com/haines/pg-aws_rds_iam
236
+ changelog_uri: https://github.com/haines/pg-aws_rds_iam/blob/master/CHANGELOG.md
237
+ post_install_message:
238
+ rdoc_options: []
239
+ require_paths:
240
+ - lib
241
+ required_ruby_version: !ruby/object:Gem::Requirement
242
+ requirements:
243
+ - - ">="
244
+ - !ruby/object:Gem::Version
245
+ version: '0'
246
+ required_rubygems_version: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - ">="
249
+ - !ruby/object:Gem::Version
250
+ version: '0'
251
+ requirements: []
252
+ rubygems_version: 3.1.2
253
+ signing_key:
254
+ specification_version: 4
255
+ summary: IAM authentication for PostgreSQL on Amazon RDS
256
+ test_files: []