Skip to content

Commit b2c77c6

Browse files
committed
Changes to LRS Basic Authentication
1 parent ed3559a commit b2c77c6

File tree

5 files changed

+35
-30
lines changed

5 files changed

+35
-30
lines changed

app/models/tenant.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
class Tenant < ApplicationRedisRecord
44
SECRETS_SEPARATOR = ':'
55

6-
define_attribute_methods :id, :name, :secrets, :lrs_endpoint, :lrs_basic_token, :kc_token_url, :kc_client_id, :kc_client_secret, :kc_username,
7-
:kc_password
6+
define_attribute_methods :id, :name, :secrets, :lrs_endpoint, :lrs_username, :lrs_password,
7+
:kc_token_url, :kc_client_id, :kc_client_secret, :kc_username, :kc_password
88

99
# Unique ID for this tenant
1010
application_redis_attr :id
@@ -17,7 +17,8 @@ class Tenant < ApplicationRedisRecord
1717

1818
# Custom LRS work
1919
application_redis_attr :lrs_endpoint
20-
application_redis_attr :lrs_basic_token
20+
application_redis_attr :lrs_username
21+
application_redis_attr :lrs_password
2122
application_redis_attr :kc_token_url
2223
application_redis_attr :kc_client_id
2324
application_redis_attr :kc_client_secret
@@ -45,7 +46,8 @@ def save!
4546
pipeline.hset(id_key, 'name', name) if name_changed?
4647
pipeline.hset(id_key, 'secrets', secrets) if secrets_changed?
4748
pipeline.hset(id_key, 'lrs_endpoint', lrs_endpoint) if lrs_endpoint_changed?
48-
pipeline.hset(id_key, 'lrs_basic_token', lrs_basic_token) if lrs_basic_token_changed?
49+
pipeline.hset(id_key, 'lrs_username', lrs_username) if lrs_username_changed?
50+
pipeline.hset(id_key, 'lrs_password', lrs_password) if lrs_password_changed?
4951
pipeline.hset(id_key, 'kc_token_url', kc_token_url) if kc_token_url_changed?
5052
pipeline.hset(id_key, 'kc_client_id', kc_client_id) if kc_client_id_changed?
5153
pipeline.hset(id_key, 'kc_client_secret', kc_client_secret) if kc_client_secret_changed?

app/services/lrs_payload_service.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ def initialize(tenant:, secret:)
77
end
88

99
def call
10-
token = @tenant.kc_token_url.present? ? fetch_token_from_keycloak : @tenant.lrs_basic_token
11-
12-
if token.nil?
13-
Rails.logger.warn("LRS Token not found")
14-
return nil
15-
end
16-
1710
lrs_payload = {
1811
lrs_endpoint: @tenant.lrs_endpoint,
19-
lrs_token: token
2012
}
2113

14+
if @tenant.lrs_username.present?
15+
lrs_payload[:lrs_username] = @tenant.lrs_username
16+
lrs_payload[:lrs_password] = @tenant.lrs_password
17+
else
18+
token = fetch_token_from_keycloak
19+
20+
if token.nil?
21+
Rails.logger.warn("LRS Token not found")
22+
return nil
23+
end
24+
25+
lrs_payload[:lrs_token] = token
26+
end
27+
2228
# Generate a random salt
2329
salt = SecureRandom.random_bytes(8)
2430

lib/tasks/tenants.rake

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ task tenants: :environment do |_t, _args|
1515
puts("\tname: #{tenant.name}")
1616
puts("\tsecrets: #{tenant.secrets}")
1717
puts("\tlrs_endpoint: #{tenant.lrs_endpoint}") if tenant.lrs_endpoint.present?
18-
puts("\tlrs_basic_token: #{tenant.lrs_basic_token}") if tenant.lrs_basic_token.present?
18+
puts("\tlrs_username: #{tenant.lrs_username}") if tenant.lrs_username.present?
19+
puts("\tlrs_password: #{tenant.lrs_password}") if tenant.lrs_password.present?
1920
puts("\tkc_token_url: #{tenant.kc_token_url}") if tenant.kc_token_url.present?
2021
puts("\tkc_client_id: #{tenant.kc_client_id}") if tenant.kc_client_id.present?
2122
puts("\tkc_client_secret: #{tenant.kc_client_secret}") if tenant.kc_client_secret.present?
@@ -68,20 +69,22 @@ namespace :tenants do
6869
end
6970

7071
desc 'Update an existing Tenants LRS credentials with basic authentication'
71-
task :update_lrs_basic, [:id, :lrs_endpoint, :lrs_basic_token] => :environment do |_t, args|
72+
task :update_lrs_basic, [:id, :lrs_endpoint, :lrs_username, :lrs_password] => :environment do |_t, args|
7273
check_multitenancy
7374
id = args[:id]
7475
lrs_endpoint = args[:lrs_endpoint]
75-
lrs_basic_token = args[:lrs_basic_token]
76+
lrs_username = args[:lrs_username]
77+
lrs_password = args[:lrs_password]
7678

77-
if id.blank? || lrs_endpoint.blank? || lrs_basic_token.blank?
78-
puts('Error: id, LRS_ENDPOINT, LRS_BASIC_TOKEN are required to update a Tenant')
79+
if id.blank? || lrs_endpoint.blank? || lrs_username.blank? || lrs_password.blank?
80+
puts('Error: id, LRS_ENDPOINT, LRS_USERNAME, LRS_PASSWORD are required to update a Tenant')
7981
exit(1)
8082
end
8183

8284
tenant = Tenant.find(id)
8385
tenant.lrs_endpoint = lrs_endpoint
84-
tenant.lrs_basic_token = lrs_basic_token
86+
tenant.lrs_username = lrs_username
87+
tenant.lrs_password = lrs_password
8588

8689
tenant.save!
8790

spec/factories/tenant.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
name { Faker::Creature::Animal.name }
66
secrets { "#{Faker::Crypto.sha256}:#{Faker::Crypto.sha512}" }
77
lrs_endpoint { nil }
8-
lrs_basic_token { nil }
8+
lrs_username { nil }
9+
lrs_password { nil }
910
kc_token_url { nil }
1011
kc_client_id { nil }
1112
kc_client_secret { nil }

spec/services/lrs_payload_service_spec.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@
55
RSpec.describe LrsPayloadService, type: :service do
66
describe '#call' do
77
context 'Basic Auth' do
8-
it 'uses the lrs_basic_token if set' do
9-
tenant = create(:tenant, name: 'bn', lrs_endpoint: 'https://lrs_endpoint.com', lrs_basic_token: 'basic_token')
8+
it 'uses the lrs_username and lrs_password if set' do
9+
tenant = create(:tenant, name: 'bn', lrs_endpoint: 'https://lrs_endpoint.com', lrs_username: 'basic_username', lrs_password: 'basic_password')
1010

1111
encrypted_value = described_class.new(tenant: tenant, secret: 'server-secret').call
1212

13-
expect(JSON.parse(decrypt(encrypted_value, 'server-secret'))["lrs_token"]).to eq(tenant.lrs_basic_token)
14-
end
15-
16-
it 'logs a warning and returns nil if lrs_basic_token is not set' do
17-
tenant = create(:tenant, name: 'bn', lrs_endpoint: 'https://lrs_endpoint.com')
18-
19-
expect(Rails.logger).to receive(:warn)
20-
21-
expect(described_class.new(tenant: tenant, secret: 'server-secret').call).to be_nil
13+
expect(JSON.parse(decrypt(encrypted_value, 'server-secret'))["lrs_username"]).to eq(tenant.lrs_username)
14+
expect(JSON.parse(decrypt(encrypted_value, 'server-secret'))["lrs_password"]).to eq(tenant.lrs_password)
2215
end
2316
end
2417

0 commit comments

Comments
 (0)