uc3-dmp-rds 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +7 -0
- data/lib/uc3-dmp-rds/adapter.rb +81 -0
- data/lib/uc3-dmp-rds/version.rb +5 -0
- data/lib/uc3-dmp-rds.rb +14 -0
- metadata +189 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8d491135d6067f7ef206b6674634897e80e305fbf8d886a3f4b1b3d32762813b
|
4
|
+
data.tar.gz: 965e807f2ecabebbcaca3a5e251bf8844e17be5dad1731dc5c6d3d4d8cfc4bd2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8bf401ad2b04e0865f304ee1bdc1d0a50bfc179dea37892bbc8207ce641e930d6e228dd38c5b472294cc31ab26f6b20a1c49735bed9611dcc76549f22ba1d712
|
7
|
+
data.tar.gz: f2bc501cc49ad107c7b61ddfcd9564ca95cded57e2b578c79945026eb76a8975606c3cad77fa35fddb6f6a82e1a03f63edb51157eb4fe43d2e4f20dcafd1dcdf
|
data/README.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# Uc3DmpRds gem
|
2
|
+
|
3
|
+
Helper class for accessing the DMPTool database
|
4
|
+
|
5
|
+
This gem uses the uc3-dmp-api-core gem to access the AWS to fetch your DB credentials
|
6
|
+
|
7
|
+
It also uses mysql2 which means that when using this gem with a Lambda function, that function will need to reside within a Docker container that has installed mysql developer tools!
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_record_simple_execute'
|
5
|
+
require 'aws-sdk-sns'
|
6
|
+
require 'aws-sdk-ssm'
|
7
|
+
require 'mysql2'
|
8
|
+
|
9
|
+
require 'uc3-dmp-api-core'
|
10
|
+
|
11
|
+
module Uc3DmpRds
|
12
|
+
# A module to interact with the RDS DB. Expects the following ENV variables to be set:
|
13
|
+
# DATABASE_HOST: The host URL
|
14
|
+
# DATABASE_PORT: The port to use
|
15
|
+
# DATABASE_NAME: The name of the database
|
16
|
+
#
|
17
|
+
# and the following from the AWS SSM parameter store:
|
18
|
+
# RDS_USERNAME: The RDS username
|
19
|
+
# RDS_PASSWORD: The RDS password
|
20
|
+
#
|
21
|
+
class Adapter
|
22
|
+
MSG_NO_CONNECTION = 'No current database connection. Call Uc3DmpRds.connect first.'
|
23
|
+
MSG_KEYWORDS_INVALID = 'The parameters specified do not match those in the SQL query.'
|
24
|
+
|
25
|
+
class << self
|
26
|
+
# Connect to the RDS instance
|
27
|
+
def connect
|
28
|
+
creds = _credentials
|
29
|
+
ActiveRecord::Base.establish_connection(
|
30
|
+
adapter: 'mysql2',
|
31
|
+
host: ENV.fetch('DATABASE_HOST', nil),
|
32
|
+
port: ENV.fetch('DATABASE_PORT', nil),
|
33
|
+
database: ENV.fetch('DATABASE_NAME', nil),
|
34
|
+
username: creds[:username],
|
35
|
+
password: creds[:password],
|
36
|
+
encoding: 'utf8mb4'
|
37
|
+
)
|
38
|
+
ActiveRecord::Base.connected?
|
39
|
+
end
|
40
|
+
|
41
|
+
# Execute the specified query using ActiveRecord's helpers to sanitize the input
|
42
|
+
def execute_query(sql:, **params)
|
43
|
+
raise StandardError, MSG_NO_CONNECTION unless ActiveRecord::Base.connected?
|
44
|
+
return [] unless sql.is_a?(String) && !sql.strip.empty? && (params.nil? || params.is_a?(Hash))
|
45
|
+
# Verify that all of the kewords are accounted for and that values were supplied
|
46
|
+
raise StandardError, MSG_KEYWORDS_INVALID unless _verify_params(sql: sql, params: params)
|
47
|
+
|
48
|
+
ActiveRecord::Base.simple_execute(sql, params)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
# Fetch the DB credentials from SSM parameter store
|
54
|
+
def _credentials
|
55
|
+
{
|
56
|
+
username: Uc3DmpApiCore::SsmReader.get_ssm_value(key: :rds_username),
|
57
|
+
password: Uc3DmpApiCore::SsmReader.get_ssm_value(key: :rds_password)
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
# Verify that all params defined in the SQL exist in the params hash and vice versa
|
62
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
63
|
+
def _verify_params(sql:, params:)
|
64
|
+
# If the :sql doesn't have any parms and there are no :params then return true
|
65
|
+
return true if !sql.to_s.include?(':') && !params.is_a?(Hash)
|
66
|
+
# If the :sql has params but there are no :params return false
|
67
|
+
return false if sql.to_s.include?(':') && !params.is_a?(Hash)
|
68
|
+
# If the :sql has no params but there are :params return false
|
69
|
+
return false if params.is_a?(Hash) && !sql.include?(':')
|
70
|
+
|
71
|
+
resolvable = true
|
72
|
+
keywords = sql.scan(/:[a-zA-Z_]+/)
|
73
|
+
|
74
|
+
params&.keys&.each { |key| resolvable = false unless sql.include?(":#{key}") }
|
75
|
+
keywords.each { |key| resolvable = false unless params.keys.map(&:to_s).include?(key.sub(':', '')) }
|
76
|
+
resolvable
|
77
|
+
end
|
78
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/uc3-dmp-rds.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# rubocop:disable Naming/FileName
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'aws-sdk-sns'
|
5
|
+
require 'aws-sdk-ssm'
|
6
|
+
|
7
|
+
require 'uc3-dmp-api-core'
|
8
|
+
|
9
|
+
require 'uc3-dmp-rds/adapter'
|
10
|
+
|
11
|
+
# RDS Database adapter
|
12
|
+
module Uc3DmpRds
|
13
|
+
end
|
14
|
+
# rubocop:enable Naming/FileName
|
metadata
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uc3-dmp-rds
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Riley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-05-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: active_record_simple_execute
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk-sns
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.60'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.60'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: aws-sdk-ssm
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.150'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.150'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.6'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mysql2
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.5.5
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.5.5
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: uc3-dmp-api-core
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.0.6
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.0.6
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: byebug
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 11.1.3
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 11.1.3
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 3.9.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 3.9.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 1.50.2
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 1.50.2
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rubocop-rspec
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - '='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 2.20.0
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - '='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 2.20.0
|
153
|
+
description: Helpers for MySQL connections and querying
|
154
|
+
email:
|
155
|
+
- brian.riley@ucop.edu
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- README.md
|
161
|
+
- lib/uc3-dmp-rds.rb
|
162
|
+
- lib/uc3-dmp-rds/adapter.rb
|
163
|
+
- lib/uc3-dmp-rds/version.rb
|
164
|
+
homepage: https://github.com/CDLUC3/dmp-hub-cfn/blob/main/src/sam/gems/uc3-dmp-rds
|
165
|
+
licenses:
|
166
|
+
- MIT
|
167
|
+
metadata:
|
168
|
+
rubygems_mfa_required: 'false'
|
169
|
+
post_install_message:
|
170
|
+
rdoc_options: []
|
171
|
+
require_paths:
|
172
|
+
- lib
|
173
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '2.7'
|
178
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
|
+
requirements:
|
180
|
+
- - ">="
|
181
|
+
- !ruby/object:Gem::Version
|
182
|
+
version: '0'
|
183
|
+
requirements: []
|
184
|
+
rubygems_version: 3.1.6
|
185
|
+
signing_key:
|
186
|
+
specification_version: 4
|
187
|
+
summary: DMPTool gem that provides general support for accessing MySQL DB hosted on
|
188
|
+
AWS RDS
|
189
|
+
test_files: []
|