Class: Legate::Auth::ManagerStore::RedisStore
- Inherits:
-
Object
- Object
- Legate::Auth::ManagerStore::RedisStore
- Defined in:
- lib/legate/auth/manager_store.rb
Overview
Redis-backed implementation for storing authentication configuration
Constant Summary collapse
- SCHEMES_HASH_KEY =
Redis key prefixes
'legate:auth:schemes'- CREDENTIALS_HASH_KEY =
'legate:auth:credentials'- URL_MAPPINGS_KEY =
'legate:auth:url_mappings'
Instance Method Summary collapse
-
#add_url_mapping(mapping) ⇒ Boolean
Add a single URL mapping.
-
#available? ⇒ Boolean
Check if Redis is available.
-
#clear_url_mappings ⇒ Boolean
Clear all URL mappings.
-
#delete_credential(name) ⇒ Boolean
Delete a credential.
-
#delete_scheme(name) ⇒ Boolean
Delete a scheme.
-
#initialize(redis_client:) ⇒ RedisStore
constructor
A new instance of RedisStore.
-
#load_all_credentials ⇒ Hash
Load all credentials.
-
#load_all_schemes ⇒ Hash
Load all schemes.
-
#load_credential(name) ⇒ Hash?
Load a single credential.
-
#load_scheme(name) ⇒ Hash?
Load a single scheme.
-
#load_url_mappings ⇒ Array<Hash>
Load URL mappings.
-
#remove_url_mapping(index) ⇒ Boolean
Remove a URL mapping.
-
#save_credential(name, credential) ⇒ Boolean
Save a credential.
-
#save_scheme(name, scheme) ⇒ Boolean
Save a scheme configuration.
-
#save_url_mappings(mappings) ⇒ Boolean
Save URL mappings (replaces all).
Constructor Details
#initialize(redis_client:) ⇒ RedisStore
Returns a new instance of RedisStore.
18 19 20 21 22 23 24 25 |
# File 'lib/legate/auth/manager_store.rb', line 18 def initialize(redis_client:) @redis = redis_client @logger = Legate.logger @logger&.info('Legate::Auth::ManagerStore::RedisStore initialized.') rescue StandardError => e Legate.logger&.error("Failed to initialize Auth ManagerStore: #{e.}") @redis = nil end |
Instance Method Details
#add_url_mapping(mapping) ⇒ Boolean
Add a single URL mapping
220 221 222 223 224 225 226 |
# File 'lib/legate/auth/manager_store.rb', line 220 def add_url_mapping(mapping) return false unless available? current = load_url_mappings current << mapping save_url_mappings(current) end |
#available? ⇒ Boolean
Check if Redis is available
29 30 31 |
# File 'lib/legate/auth/manager_store.rb', line 29 def available? !@redis.nil? end |
#clear_url_mappings ⇒ Boolean
Clear all URL mappings
243 244 245 246 247 248 249 250 251 |
# File 'lib/legate/auth/manager_store.rb', line 243 def clear_url_mappings return false unless available? @redis.del(URL_MAPPINGS_KEY) true rescue StandardError => e @logger&.error("Failed to clear URL mappings: #{e.}") false end |
#delete_credential(name) ⇒ Boolean
Delete a credential
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/legate/auth/manager_store.rb', line 156 def delete_credential(name) return false unless available? @redis.hdel(CREDENTIALS_HASH_KEY, name.to_s) @logger&.debug("Deleted auth credential '#{name}' from Redis") true rescue StandardError => e @logger&.error("Failed to delete credential '#{name}': #{e.}") false end |
#delete_scheme(name) ⇒ Boolean
Delete a scheme
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/legate/auth/manager_store.rb', line 89 def delete_scheme(name) return false unless available? @redis.hdel(SCHEMES_HASH_KEY, name.to_s) @logger&.debug("Deleted auth scheme '#{name}' from Redis") true rescue StandardError => e @logger&.error("Failed to delete scheme '#{name}': #{e.}") false end |
#load_all_credentials ⇒ Hash
Load all credentials
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/legate/auth/manager_store.rb', line 138 def load_all_credentials return {} unless available? result = {} @redis.hgetall(CREDENTIALS_HASH_KEY).each do |name, data| result[name.to_sym] = JSON.parse(data, symbolize_names: true) rescue JSON::ParserError => e @logger&.warn("Failed to parse credential '#{name}': #{e.}") end result rescue StandardError => e @logger&.error("Failed to load credentials: #{e.}") {} end |
#load_all_schemes ⇒ Hash
Load all schemes
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/legate/auth/manager_store.rb', line 71 def load_all_schemes return {} unless available? result = {} @redis.hgetall(SCHEMES_HASH_KEY).each do |name, data| result[name.to_sym] = JSON.parse(data, symbolize_names: true) rescue JSON::ParserError => e @logger&.warn("Failed to parse scheme '#{name}': #{e.}") end result rescue StandardError => e @logger&.error("Failed to load schemes: #{e.}") {} end |
#load_credential(name) ⇒ Hash?
Load a single credential
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/legate/auth/manager_store.rb', line 123 def load_credential(name) return nil unless available? name = name.to_s data = @redis.hget(CREDENTIALS_HASH_KEY, name) return nil unless data JSON.parse(data, symbolize_names: true) rescue StandardError => e @logger&.error("Failed to load credential '#{name}': #{e.}") nil end |
#load_scheme(name) ⇒ Hash?
Load a single scheme
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/legate/auth/manager_store.rb', line 56 def load_scheme(name) return nil unless available? name = name.to_s data = @redis.hget(SCHEMES_HASH_KEY, name) return nil unless data JSON.parse(data, symbolize_names: true) rescue StandardError => e @logger&.error("Failed to load scheme '#{name}': #{e.}") nil end |
#load_url_mappings ⇒ Array<Hash>
Load URL mappings
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/legate/auth/manager_store.rb', line 193 def load_url_mappings return [] unless available? data = @redis.get(URL_MAPPINGS_KEY) return [] unless data mappings = JSON.parse(data, symbolize_names: true) # Reconstruct Regexp patterns mappings.map do |mapping| pattern = mapping[:pattern] pattern = Regexp.new(pattern[:regexp]) if pattern.is_a?(Hash) && pattern[:regexp] { pattern: pattern, scheme_name: mapping[:scheme_name].to_sym, credential_name: mapping[:credential_name].to_sym } end rescue StandardError => e @logger&.error("Failed to load URL mappings: #{e.}") [] end |
#remove_url_mapping(index) ⇒ Boolean
Remove a URL mapping
231 232 233 234 235 236 237 238 239 |
# File 'lib/legate/auth/manager_store.rb', line 231 def remove_url_mapping(index) return false unless available? current = load_url_mappings return false if index < 0 || index >= current.size current.delete_at(index) save_url_mappings(current) end |
#save_credential(name, credential) ⇒ Boolean
Save a credential
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/legate/auth/manager_store.rb', line 106 def save_credential(name, credential) return false unless available? name = name.to_s credential_data = serialize_credential(credential) @redis.hset(CREDENTIALS_HASH_KEY, name, credential_data.to_json) @logger&.debug("Saved auth credential '#{name}' to Redis") true rescue StandardError => e @logger&.error("Failed to save credential '#{name}': #{e.}") false end |
#save_scheme(name, scheme) ⇒ Boolean
Save a scheme configuration
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/legate/auth/manager_store.rb', line 39 def save_scheme(name, scheme) return false unless available? name = name.to_s scheme_data = serialize_scheme(scheme) @redis.hset(SCHEMES_HASH_KEY, name, scheme_data.to_json) @logger&.debug("Saved auth scheme '#{name}' to Redis") true rescue StandardError => e @logger&.error("Failed to save scheme '#{name}': #{e.}") false end |
#save_url_mappings(mappings) ⇒ Boolean
Save URL mappings (replaces all)
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/legate/auth/manager_store.rb', line 172 def save_url_mappings(mappings) return false unless available? serialized = mappings.map do |mapping| { pattern: mapping[:pattern].is_a?(Regexp) ? { regexp: mapping[:pattern].source } : mapping[:pattern], scheme_name: mapping[:scheme_name].to_s, credential_name: mapping[:credential_name].to_s } end @redis.set(URL_MAPPINGS_KEY, serialized.to_json) @logger&.debug("Saved #{mappings.size} URL mappings to Redis") true rescue StandardError => e @logger&.error("Failed to save URL mappings: #{e.}") false end |