vp-login 0.3.0 → 0.4.1

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.
@@ -25,9 +25,11 @@ module VpLogin
25
25
 
26
26
  # Without consuming it, for callers that must refuse before spending the state.
27
27
  def read_attempt(state)
28
- return nil unless state.is_a?(String) && state.match?(ID_FORMAT)
28
+ return invalid_id(:attempt, state) unless state.is_a?(String) && state.match?(ID_FORMAT)
29
29
 
30
- @config.cache.read(key(:attempt, state))
30
+ @config.cache.read(key(:attempt, state)).tap do |value|
31
+ Log.debug('state.read', config: @config, kind: :attempt, id: Log.fingerprint(state), found: !value.nil?)
32
+ end
31
33
  end
32
34
 
33
35
  def write_handoff(attempt)
@@ -43,18 +45,35 @@ module VpLogin
43
45
  private
44
46
 
45
47
  def write(kind, id, value, ttl)
46
- @config.cache.write(key(kind, id), value, expires_in: ttl)
48
+ written = @config.cache.write(key(kind, id), value, expires_in: ttl)
49
+ Log.debug('state.write', config: @config, kind:, id: Log.fingerprint(id), ttl:,
50
+ fields: value.respond_to?(:keys) ? value.keys : nil, cache: @config.cache.class.name)
51
+ # Rails cache stores return false when the write failed (e.g. Redis down, value unserializable).
52
+ Log.error('state.write_failed', config: @config, report: true, kind:, id: Log.fingerprint(id), cache: @config.cache.class.name) if written == false
53
+ written
47
54
  end
48
55
 
56
+ # A miss is an expired, already-used or never-issued id - or a cache not shared between the
57
+ # process that wrote it and this one.
49
58
  def take(kind, id)
50
- return nil unless id.is_a?(String) && id.match?(ID_FORMAT)
59
+ return invalid_id(kind, id) unless id.is_a?(String) && id.match?(ID_FORMAT)
51
60
 
52
61
  cache_key = key(kind, id)
53
62
  value = @config.cache.read(cache_key)
54
63
  @config.cache.delete(cache_key) unless value.nil?
64
+ if value.nil?
65
+ Log.warn('state.miss', config: @config, kind:, id: Log.fingerprint(id), cache: @config.cache.class.name)
66
+ else
67
+ Log.debug('state.take', config: @config, kind:, id: Log.fingerprint(id))
68
+ end
55
69
  value
56
70
  end
57
71
 
72
+ def invalid_id(kind, id)
73
+ Log.warn('state.invalid_id', config: @config, kind:, type: id.class.name, length: id.to_s.length)
74
+ nil
75
+ end
76
+
58
77
  def key(kind, id)
59
78
  "#{@config.cache_namespace}:#{kind}:#{id}"
60
79
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Bulk migration of an app's existing users into vpLogin, for models that include
4
+ # VpLogin::DeviseUser. One VpLogin::ProvisionJob per user; every step is a no-op when already done,
5
+ # so re-running after a partial run only finishes what's left.
6
+ namespace :vplogin do
7
+ vplogin_scope = lambda do
8
+ model = VpLogin.configuration.user_model
9
+ model = model.constantize if model.is_a?(String)
10
+ scope = model.vplogin_migration_scope
11
+ ids = ENV['IDS'].to_s.split(',').map(&:strip).reject(&:empty?)
12
+ ids.empty? ? scope : scope.where(id: ids)
13
+ end
14
+
15
+ desc 'Create vpLogin identities for existing users (DRY_RUN=true to only count; IDS=1,2 to limit)'
16
+ task migrate_users: :environment do
17
+ abort 'vpLogin admin API is not configured (VPLOGIN_ADMIN_API_URL / VPLOGIN_API_TOKEN)' unless VpLogin.configuration.admin_api?
18
+
19
+ scope = vplogin_scope.call
20
+ if ENV['DRY_RUN'] == 'true'
21
+ puts "DRY_RUN: would enqueue #{scope.count} #{scope.model.name} users for vpLogin migration"
22
+ next
23
+ end
24
+
25
+ count = 0
26
+ scope.find_each do |user|
27
+ VpLogin::ProvisionJob.perform_later(user.class.name, user.id)
28
+ count += 1
29
+ end
30
+ VpLogin::Log.info('task.migrate_users.enqueued', count:, queue: VpLogin.configuration.job_queue)
31
+ puts "Enqueued #{count} VpLogin::ProvisionJob jobs on #{VpLogin.configuration.job_queue}"
32
+ end
33
+
34
+ desc 'Report which users have a vpLogin identity and password (one admin API call per user; IDS=1,2 to limit)'
35
+ task migration_status: :environment do
36
+ abort 'vpLogin admin API is not configured (VPLOGIN_ADMIN_API_URL / VPLOGIN_API_TOKEN)' unless VpLogin.configuration.admin_api?
37
+
38
+ admin = VpLogin::Admin.new
39
+ counts = Hash.new(0)
40
+ missing = []
41
+ vplogin_scope.call.find_each do |user|
42
+ identity = admin.find_user(email: user.email)
43
+ if identity.nil?
44
+ counts[:missing] += 1
45
+ missing << user.email
46
+ elsif identity['has_usable_password'] == false
47
+ counts[:no_password] += 1
48
+ else
49
+ counts[:migrated] += 1
50
+ end
51
+ rescue VpLogin::Error => e
52
+ counts[:errors] += 1
53
+ VpLogin::Log.error('task.migration_status.failed', exception: e, user_id: user.id)
54
+ warn "#{user.email}: #{e.class}: #{e.message}"
55
+ end
56
+
57
+ puts "migrated (identity + password): #{counts[:migrated]}"
58
+ puts "identity without a password: #{counts[:no_password]}"
59
+ puts "no vpLogin identity: #{counts[:missing]}"
60
+ puts "errors: #{counts[:errors]}"
61
+ puts "missing: #{missing.first(20).join(', ')}#{' ...' if missing.size > 20}" if missing.any?
62
+ end
63
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module VpLogin
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.1'
5
5
  end
data/lib/vplogin.rb CHANGED
@@ -12,6 +12,7 @@ require 'jwt'
12
12
 
13
13
  require_relative 'vplogin/version'
14
14
  require_relative 'vplogin/errors'
15
+ require_relative 'vplogin/log'
15
16
  require_relative 'vplogin/memory_cache'
16
17
  require_relative 'vplogin/configuration'
17
18
  require_relative 'vplogin/http'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vp-login
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volopay
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
60
  version: '7.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '13'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '13'
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: rspec
63
77
  requirement: !ruby/object:Gem::Requirement
@@ -105,6 +119,7 @@ files:
105
119
  - lib/vplogin/discovery.rb
106
120
  - lib/vplogin/errors.rb
107
121
  - lib/vplogin/http.rb
122
+ - lib/vplogin/log.rb
108
123
  - lib/vplogin/memory_cache.rb
109
124
  - lib/vplogin/oidc.rb
110
125
  - lib/vplogin/password_check.rb
@@ -112,6 +127,7 @@ files:
112
127
  - lib/vplogin/railtie.rb
113
128
  - lib/vplogin/routes.rb
114
129
  - lib/vplogin/state_store.rb
130
+ - lib/vplogin/tasks.rake
115
131
  - lib/vplogin/version.rb
116
132
  homepage:
117
133
  licenses: