uc3-dmp-rds 0.0.8 → 0.0.10
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 +4 -4
- data/lib/uc3-dmp-rds/adapter.rb +12 -12
- data/lib/uc3-dmp-rds/authenticator.rb +50 -0
- data/lib/uc3-dmp-rds/version.rb +1 -1
- data/lib/uc3-dmp-rds.rb +0 -5
- metadata +3 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3091e4b4c758fd05dd75c866ef07a3f43f13daa15c909ee32349161f74b8a32f
|
4
|
+
data.tar.gz: a173d9fa491a0fdb756ec409f8ba0f106e0d155f80a752c23fb9d89964201997
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c762ea939a7536dc10e0a551fe3d8ab6dda43944e59e5a84f32fdcdc9af06712ee4baef7b54e99ed253a99f38cdd12ec39630af990f46cc4957f7189a00c5d4b
|
7
|
+
data.tar.gz: e82053a5d6a44958f7f08d965493dfa4045f36ab7d5e6735f8cc0fabc013c7d1531af879dc4ad4fd83934602f50a6ef5c67f8a7c6ca8c25bfc88248391236e03
|
data/lib/uc3-dmp-rds/adapter.rb
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
require 'active_record'
|
4
4
|
require 'active_record_simple_execute'
|
5
|
-
require 'aws-sdk-sns'
|
6
|
-
require 'aws-sdk-ssm'
|
7
5
|
require 'mysql2'
|
8
6
|
|
9
7
|
module Uc3DmpRds
|
@@ -22,16 +20,17 @@ module Uc3DmpRds
|
|
22
20
|
class Adapter
|
23
21
|
MSG_KEYWORDS_INVALID = 'The parameters specified do not match those in the SQL query'
|
24
22
|
MSG_MISSING_CREDENTIALS = 'No username and/or password specified'
|
25
|
-
|
23
|
+
MSG_UNABLE_TO_CONNECT = 'Unable to establish a connection'
|
24
|
+
MSG_UNABLE_TO_QUERY = 'Unable to process the query'
|
25
|
+
MSG_UNAUTHORIZED = 'You are not authorized to perform that action'
|
26
26
|
|
27
27
|
class << self
|
28
28
|
# Connect to the RDS instance
|
29
|
+
# rubocop:disable Metrics/AbcSize
|
29
30
|
def connect(username:, password:)
|
30
31
|
raise AdapterError, MSG_MISSING_CREDENTIALS if username.nil? || username.to_s.strip.empty? ||
|
31
32
|
password.nil? || password.to_s.strip.empty?
|
32
33
|
|
33
|
-
puts "CONNECT - host: #{ENV['DATABASE_HOST']}, port: #{ENV['DATABASE_PORT']}, name: #{ENV['DATABASE_NAME']}, username: #{username}, password.lenght: #{password.length}"
|
34
|
-
|
35
34
|
connection = ActiveRecord::Base.establish_connection(
|
36
35
|
adapter: 'mysql2',
|
37
36
|
host: ENV.fetch('DATABASE_HOST', nil),
|
@@ -41,21 +40,22 @@ puts "CONNECT - host: #{ENV['DATABASE_HOST']}, port: #{ENV['DATABASE_PORT']}, na
|
|
41
40
|
password: password,
|
42
41
|
encoding: 'utf8mb4'
|
43
42
|
)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
ActiveRecord::Base.connected?
|
43
|
+
!connection.nil?
|
44
|
+
rescue StandardError => e
|
45
|
+
raise AdapterError, "#{MSG_UNABLE_TO_CONNECT} - #{e.message}"
|
49
46
|
end
|
47
|
+
# rubocop:enable Metrics/AbcSize
|
50
48
|
|
51
49
|
# Execute the specified query using ActiveRecord's helpers to sanitize the input
|
52
|
-
def execute_query(sql:, **params)
|
53
|
-
raise AdapterError,
|
50
|
+
def execute_query(user:, sql:, **params)
|
51
|
+
raise AdapterError, MSG_UNAUTHORIZED unless user.is_a?(User)
|
54
52
|
return [] unless sql.is_a?(String) && !sql.strip.empty? && (params.nil? || params.is_a?(Hash))
|
55
53
|
# Verify that all of the kewords are accounted for and that values were supplied
|
56
54
|
raise AdapterError, MSG_KEYWORDS_INVALID unless _verify_params(sql: sql, params: params)
|
57
55
|
|
58
56
|
ActiveRecord::Base.simple_execute(sql, params)
|
57
|
+
rescue StandardError => e
|
58
|
+
raise AdapterError, "#{MSG_UNABLE_TO_QUERY} - #{e.message}"
|
59
59
|
end
|
60
60
|
|
61
61
|
private
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_record_simple_execute'
|
5
|
+
require 'mysql2'
|
6
|
+
|
7
|
+
module Uc3DmpRds
|
8
|
+
# Error from the Rds Adapter
|
9
|
+
class AuthenticatorError < StandardError; end
|
10
|
+
|
11
|
+
# Use Rails' ActiveResource to communicate with the DMPHub REST API
|
12
|
+
class Authenticator
|
13
|
+
MSG_INVALID_TOKEN = 'Invalid user token'
|
14
|
+
MSG_INACTIVE_USER = 'User is inactive'
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def authenticate(token:)
|
18
|
+
raise AuthenticatorError, MSG_INVALID_TOKEN if token.nil? || token.to_s.strip.empty?
|
19
|
+
|
20
|
+
sql = <<~SQL.squish
|
21
|
+
SELECT users.firstname, users.surname, users.email, users.active, i.value orcid,
|
22
|
+
orgs.name org_name, ro.name ror_name, ro.ror_id
|
23
|
+
FROM users
|
24
|
+
INNER JOIN orgs ON users.org_id = orgs.id
|
25
|
+
LEFT OUTER JOIN registry_orgs ro
|
26
|
+
ON orgs.id = ro.org_id
|
27
|
+
LEFT OUTER JOIN identifiers i
|
28
|
+
ON i.identifiable_id = users.id
|
29
|
+
AND i.identifiable_type = 'User'
|
30
|
+
AND i.identifier_scheme_id IN (SELECT sch.id FROM identifier_schemes sch WHERE sch.name = 'orcid')
|
31
|
+
WHERE users.api_token = :token
|
32
|
+
LIMIT 1
|
33
|
+
SQL
|
34
|
+
users = ActiveRecord::Base.simple_execute(sql, token: token.to_s.strip)
|
35
|
+
raise AuthenticatorError, MSG_INVALID_TOKEN unless users.is_a?(Array) and users.any?
|
36
|
+
|
37
|
+
user = users.first
|
38
|
+
raise AuthenticatorError, MSG_INACTIVE_USER unless user['active']
|
39
|
+
|
40
|
+
hash = { name: [user['surname'], user['firstname']].join(', '), mbox: user['email'] }
|
41
|
+
hash[:user_id] = { type: 'orcid', identifier: user['orcid'] } unless user['orcid'].nil?
|
42
|
+
return hash.to_json if user['org_name'].nil?
|
43
|
+
|
44
|
+
hash[:affiliation] = { name: user.fetch('ror_name', user['org_name']) }
|
45
|
+
hash[:affiliation][:affiliation_id] = { type: 'ror', identifier: user['ror_id'] } unless user['ror_id'].nil?
|
46
|
+
hash.to_json
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/uc3-dmp-rds/version.rb
CHANGED
data/lib/uc3-dmp-rds.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uc3-dmp-rds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Riley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_record_simple_execute
|
@@ -24,34 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
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
27
|
- !ruby/object:Gem::Dependency
|
56
28
|
name: json
|
57
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -160,6 +132,7 @@ files:
|
|
160
132
|
- README.md
|
161
133
|
- lib/uc3-dmp-rds.rb
|
162
134
|
- lib/uc3-dmp-rds/adapter.rb
|
135
|
+
- lib/uc3-dmp-rds/authenticator.rb
|
163
136
|
- lib/uc3-dmp-rds/version.rb
|
164
137
|
homepage: https://github.com/CDLUC3/dmp-hub-cfn/blob/main/src/sam/gems/uc3-dmp-rds
|
165
138
|
licenses:
|