Skip to content

Commit

Permalink
[FCXPINFRA-76] Added cache for token
Browse files Browse the repository at this point in the history
  • Loading branch information
w00x committed Feb 22, 2024
1 parent 7cd3868 commit 3c78364
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
little_monster (0.1.30)
activesupport (= 6.1.7.6)
moneta (= 1.6.0)
multi_json (= 1.15.0)
thor (= 1.2.1)
tilt (= 2.1.0)
Expand Down Expand Up @@ -112,6 +113,7 @@ GEM
jwt (2.7.1)
method_source (1.0.0)
minitest (5.22.2)
moneta (1.6.0)
multi_json (1.15.0)
newrelic_rpm (9.0.0)
oj (3.14.2)
Expand Down
3 changes: 2 additions & 1 deletion lib/little_monster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def default_config_values
heartbeat_execution_interval: 10,
default_job_retries: -1,
tiger_api_url: 'http://tiger',
shark_login_file_path: '/var/run/secrets/kubernetes.io/serviceaccount/token'
shark_login_file_path: '/var/run/secrets/kubernetes.io/serviceaccount/token',
enable_tiger_token: true
}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/little_monster/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Config
attr_accessor :api_url, :worker_concurrency, :worker_queue, :worker_provider, :formatter, :request_timeout,
:default_request_retries, :default_request_retry_wait, :task_requests_retries, :task_requests_retry_wait,
:job_requests_retries, :job_requests_retry_wait, :heartbeat_execution_interval, :default_job_retries,
:tiger_api_url, :shark_login_file_path
:tiger_api_url, :shark_login_file_path, :enable_tiger_token

def initialize(params = {})
params.to_hash.each do |key, value|
Expand Down
1 change: 1 addition & 0 deletions lib/little_monster/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'little_monster/core/errors/task_not_found_error'

require 'little_monster/tiger/auth'
require 'little_monster/tiger/cache'
require 'little_monster/core/tagged_logger'
require 'little_monster/core/loggable' # must be required first to satisfy job and task dependencies
require 'little_monster/core/api'
Expand Down
13 changes: 12 additions & 1 deletion lib/little_monster/tiger/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ module API
module_function

def bearer_token
token = new_shark_token
token = cached_shark_token
"Bearer #{token}" if token
end

def cached_shark_token
shark_token = Cache.instance.get(:shark_token)
return shark_token if shark_token

shark_token = new_shark_token
return nil if shark_token.nil?
Cache.instance.set(:shark_token, shark_token, 60 * 60)
shark_token
end

def new_shark_token
return nil unless LittleMonster.enable_tiger_token
shark_token = File.read(LittleMonster.shark_login_file_path)
response = make_call(:post, 'login/shark', body: { token: shark_token }.to_json)
return nil if response.failure?
Expand Down
27 changes: 27 additions & 0 deletions lib/little_monster/tiger/cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'moneta'

module LittleMonster
module Tiger
class Cache
include Singleton

attr_reader :cache

def initialize
@cache = Moneta.new(:Memory, expires: true)
end

def set(key, value, expires = 0)
@cache.store(key, value, expires: expires)
end

def get(key)
@cache[key]
end

def clear
@cache.clear
end
end
end
end
1 change: 1 addition & 0 deletions little_monster.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'tilt', '2.1.0'
spec.add_runtime_dependency 'toiler', '0.7.1'
spec.add_runtime_dependency 'typhoeus', '1.4.0'
spec.add_runtime_dependency 'moneta', '1.6.0'

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'byebug', '11.1.3'
Expand Down
58 changes: 58 additions & 0 deletions spec/lib/little_monster/tiger/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
subject(:api) { described_class }

before do
LittleMonster::Tiger::Cache.instance.cache.clear
allow(File).to receive(:read).with(LittleMonster.shark_login_file_path).and_return('')
end

Expand Down Expand Up @@ -32,6 +33,63 @@
end
end

describe '.cached_shark_token' do
context 'when the shark login success' do
let(:body_str) { File.open('./spec/mock/responses/tiger_token.json', 'r').read }
let(:body) { JSON.parse(body_str) }

before do
LoginSharkMock.new(self).login_request_success(body_str)
end

it 'return correct token' do
expect(api.cached_shark_token).to eq(body['token'])
end
end

context 'when the shark login fail' do
before do
LoginSharkMock.new(self).login_request_failure
end

it 'return nil' do
expect(api.cached_shark_token).to be_nil
end
end

context 'when called cached_shark_token the next call' do
let(:body_str) { File.open('./spec/mock/responses/tiger_token.json', 'r').read }
let(:body) { JSON.parse(body_str) }

before do
LoginSharkMock.new(self).login_request_success(body_str)
api.cached_shark_token
allow(Typhoeus::Request).to receive(:new).and_call_original
end

it 'dont request to login endpoint' do

api.cached_shark_token
expect(Typhoeus::Request).not_to have_received(:new)
end
end

context 'when called cached_shark_token for first time' do
let(:body_str) { File.open('./spec/mock/responses/tiger_token.json', 'r').read }
let(:body) { JSON.parse(body_str) }

before do
LoginSharkMock.new(self).login_request_success(body_str)
allow(Typhoeus::Request).to receive(:new).and_call_original
end

it 'request to login endpoint' do
api.cached_shark_token
expect(Typhoeus::Request).to have_received(:new)
end
end
end

describe '.new_shark_token' do
context 'when the shark login success' do
let(:body_str) { File.open('./spec/mock/responses/tiger_token.json', 'r').read }
Expand Down
59 changes: 59 additions & 0 deletions spec/lib/little_monster/tiger/cache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe LittleMonster::Tiger::Cache do
subject(:cache) { described_class }

let(:key) { SecureRandom.uuid }

before do
LittleMonster::Tiger::Cache.instance.cache.clear
cache.instance.set(:key, key)
end

describe '.set' do
context 'when set a key and value' do
before do
cache.instance.set(:key, key)
end

it 'store that value for that key' do
expect(cache.instance.get(:key)).to eq(key)
end
end

context 'when set a key, value and expires' do
let(:expires) { 1 }

before do
cache.instance.set(:key, key, expires)
end

it 'store that value for that key for `expires` seconds' do
expect(cache.instance.get(:key)).to eq(key)
Kernel.sleep(expires + 1)
expect(cache.instance.get(:key)).to be_nil
end
end
end

describe '.get' do
context 'when get from a not exist key' do
it 'return nil' do
expect(cache.instance.get(:foo)).to be_nil
end
end
end

describe '.clear' do
context 'when get a exist key after clear cache' do

before do
LittleMonster::Tiger::Cache.instance.cache.clear
end

it 'return nil' do
expect(cache.instance.get(:key)).to be_nil
end
end
end
end
2 changes: 1 addition & 1 deletion spec/mock/responses/tiger_token.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"token": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImFkNGYyMWRkLTgwZTItNGY2ZC1iOGY0LWE4OTE2ZDY0YTFhZiIsInR5cCI6IkpXVCJ9.eyJhZGRpdGlvbmFsX2luZm8iOnsiZW1haWwiOiJleGFtcGxlIiwiZnVsbF9uYW1lIjoiZXhhbXBsZSIsInVzZXJuYW1lIjoiZXhhbXBsZSJ9LCJleHAiOjQ3OTM4NzEyNzUsImlhdCI6MTY0MDI3MTI3NSwiaWRlbnRpdHkiOiJtcm46c2VnaW5mOmFkOnVzZXIvZXhhbXBsZSIsImlzcyI6ImZ1cnlfdGlnZXIiLCJzdWIiOiJleGFtcGxlIn0.OsfqgNjn52j_xxMRKNssDJQIgdVU4yVWVDnisI6yRGDyYw-hQ2-WnaHN9e5Obwpy6E0WDtdnt4Hk8nzk4QvMSQ"
"token": "foo"
}

0 comments on commit 3c78364

Please sign in to comment.