uc3-dmp-rds 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ceba0e24d5a6a925fde46a68d7d8f1586aa29bf1687e94000d989f6ab522e3e
4
- data.tar.gz: 0d6ef838acd8ce79e5638e83b45c8bbdb72cd919863916ad0d48d5658ab079b8
3
+ metadata.gz: 3091e4b4c758fd05dd75c866ef07a3f43f13daa15c909ee32349161f74b8a32f
4
+ data.tar.gz: a173d9fa491a0fdb756ec409f8ba0f106e0d155f80a752c23fb9d89964201997
5
5
  SHA512:
6
- metadata.gz: 7aa9e392a307c55483d432c487ef5a5c0ab6c501e7e3081646f5945ba7fe5322fd29b2eb1d3b81d110f94db0ec252f624fcdcf70a4f903df0cdc434e2851b99b
7
- data.tar.gz: 2ced3df663f4dc6ee3d360625ff12c3a267c9a136c1d9535a54a41d51092691a7e8e316ec39fb8eca207b98807abc623f0a47bcaacbca3dd6c55275997211fba
6
+ metadata.gz: c762ea939a7536dc10e0a551fe3d8ab6dda43944e59e5a84f32fdcdc9af06712ee4baef7b54e99ed253a99f38cdd12ec39630af990f46cc4957f7189a00c5d4b
7
+ data.tar.gz: e82053a5d6a44958f7f08d965493dfa4045f36ab7d5e6735f8cc0fabc013c7d1531af879dc4ad4fd83934602f50a6ef5c67f8a7c6ca8c25bfc88248391236e03
@@ -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
- MSG_NO_CONNECTION = 'No current database connection. Call Uc3DmpRds.connect first'
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
- puts connection.inspect
46
- puts "CONNECTED? #{ActiveRecord::Base.connected?}"
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, MSG_NO_CONNECTION unless ActiveRecord::Base.connected?
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uc3DmpRds
4
- VERSION = '0.0.8'
4
+ VERSION = '0.0.10'
5
5
  end
data/lib/uc3-dmp-rds.rb CHANGED
@@ -1,11 +1,6 @@
1
1
  # rubocop:disable Naming/FileName
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'aws-sdk-sns'
5
- require 'aws-sdk-ssm'
6
-
7
- require 'uc3-dmp-api-core'
8
-
9
4
  require 'uc3-dmp-rds/adapter'
10
5
 
11
6
  # RDS Database adapter
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.8
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-03 00:00:00.000000000 Z
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: