Commit f8dea2e2 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Implement migration strategy for token authenticatable

parent a7fec177
...@@ -47,6 +47,14 @@ module TokenAuthenticatableStrategies ...@@ -47,6 +47,14 @@ module TokenAuthenticatableStrategies
options[:fallback] == true options[:fallback] == true
end end
def migrating?
unless options[:migrating].in?([true, false, nil])
raise ArgumentError, 'migrating: needs to be a boolean value!'
end
options[:migrating] == true
end
def self.fabricate(model, field, options) def self.fabricate(model, field, options)
if options[:digest] && options[:encrypted] if options[:digest] && options[:encrypted]
raise ArgumentError, 'Incompatible options set!' raise ArgumentError, 'Incompatible options set!'
......
...@@ -2,14 +2,24 @@ ...@@ -2,14 +2,24 @@
module TokenAuthenticatableStrategies module TokenAuthenticatableStrategies
class Encrypted < Base class Encrypted < Base
def initialize(*)
super
if migrating? && fallback?
raise ArgumentError, '`fallback` and `migration` options are not compatible!'
end
end
def find_token_authenticatable(token, unscoped = false) def find_token_authenticatable(token, unscoped = false)
return unless token return unless token
unless migrating?
encrypted_value = Gitlab::CryptoHelper.aes256_gcm_encrypt(token) encrypted_value = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
token_authenticatable = relation(unscoped) token_authenticatable = relation(unscoped)
.find_by(encrypted_field => encrypted_value) .find_by(encrypted_field => encrypted_value)
end
if fallback? if migrating? || fallback?
token_authenticatable ||= fallback_strategy token_authenticatable ||= fallback_strategy
.find_token_authenticatable(token) .find_token_authenticatable(token)
end end
...@@ -39,6 +49,8 @@ module TokenAuthenticatableStrategies ...@@ -39,6 +49,8 @@ module TokenAuthenticatableStrategies
end end
def get_token(instance) def get_token(instance)
return fallback_strategy.get_token(instance) if migrating?
encrypted_token = instance.read_attribute(encrypted_field) encrypted_token = instance.read_attribute(encrypted_field)
token = Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token) token = Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token)
...@@ -49,6 +61,7 @@ module TokenAuthenticatableStrategies ...@@ -49,6 +61,7 @@ module TokenAuthenticatableStrategies
raise ArgumentError unless token.present? raise ArgumentError unless token.present?
instance[encrypted_field] = Gitlab::CryptoHelper.aes256_gcm_encrypt(token) instance[encrypted_field] = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
instance[token_field] = token if migrating?
instance[token_field] = nil if fallback? instance[token_field] = nil if fallback?
token token
end end
......
...@@ -3,7 +3,6 @@ require 'spec_helper' ...@@ -3,7 +3,6 @@ require 'spec_helper'
describe TokenAuthenticatableStrategies::Encrypted do describe TokenAuthenticatableStrategies::Encrypted do
let(:model) { double(:model) } let(:model) { double(:model) }
let(:instance) { double(:instance) } let(:instance) { double(:instance) }
let(:options) { { fallback: true } }
let(:encrypted) do let(:encrypted) do
Gitlab::CryptoHelper.aes256_gcm_encrypt('my-value') Gitlab::CryptoHelper.aes256_gcm_encrypt('my-value')
...@@ -13,7 +12,20 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -13,7 +12,20 @@ describe TokenAuthenticatableStrategies::Encrypted do
described_class.new(model, 'some_field', options) described_class.new(model, 'some_field', options)
end end
describe '.new' do
context 'when fallback and migration strategies are set' do
let(:options) { { fallback: true, migrating: true } }
it 'raises an error' do
expect { subject }.to raise_error ArgumentError, /not compatible/
end
end
end
describe '#find_token_authenticatable' do describe '#find_token_authenticatable' do
context 'when using fallback strategy' do
let(:options) { { fallback: true } }
it 'finds the encrypted resource by cleartext' do it 'finds the encrypted resource by cleartext' do
allow(model).to receive(:find_by) allow(model).to receive(:find_by)
.with('some_field_encrypted' => encrypted) .with('some_field_encrypted' => encrypted)
...@@ -37,7 +49,33 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -37,7 +49,33 @@ describe TokenAuthenticatableStrategies::Encrypted do
end end
end end
context 'when using migration strategy' do
let(:options) { { migrating: true } }
it 'finds the cleartext resource by cleartext' do
allow(model).to receive(:find_by)
.with('some_field' => 'my-value')
.and_return('cleartext resource')
expect(subject.find_token_authenticatable('my-value'))
.to eq 'cleartext resource'
end
it 'returns nil if resource cannot be found' do
allow(model).to receive(:find_by)
.with('some_field' => 'my-value')
.and_return(nil)
expect(subject.find_token_authenticatable('my-value'))
.to be_nil
end
end
end
describe '#get_token' do describe '#get_token' do
context 'when using fallback strategy' do
let(:options) { { fallback: true } }
it 'returns decrypted token when an encrypted token is present' do it 'returns decrypted token when an encrypted token is present' do
allow(instance).to receive(:read_attribute) allow(instance).to receive(:read_attribute)
.with('some_field_encrypted') .with('some_field_encrypted')
...@@ -59,7 +97,39 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -59,7 +97,39 @@ describe TokenAuthenticatableStrategies::Encrypted do
end end
end end
context 'when using migration strategy' do
let(:options) { { migrating: true } }
it 'returns cleartext token when an encrypted token is present' do
allow(instance).to receive(:read_attribute)
.with('some_field_encrypted')
.and_return(encrypted)
allow(instance).to receive(:read_attribute)
.with('some_field')
.and_return('my-cleartext-value')
expect(subject.get_token(instance)).to eq 'my-cleartext-value'
end
it 'returns the cleartext token when encrypted token is not present' do
allow(instance).to receive(:read_attribute)
.with('some_field_encrypted')
.and_return(nil)
allow(instance).to receive(:read_attribute)
.with('some_field')
.and_return('cleartext value')
expect(subject.get_token(instance)).to eq 'cleartext value'
end
end
end
describe '#set_token' do describe '#set_token' do
context 'when using fallback strategy' do
let(:options) { { fallback: true } }
it 'writes encrypted token and removes plaintext token and returns it' do it 'writes encrypted token and removes plaintext token and returns it' do
expect(instance).to receive(:[]=) expect(instance).to receive(:[]=)
.with('some_field_encrypted', encrypted) .with('some_field_encrypted', encrypted)
...@@ -69,4 +139,18 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -69,4 +139,18 @@ describe TokenAuthenticatableStrategies::Encrypted do
expect(subject.set_token(instance, 'my-value')).to eq 'my-value' expect(subject.set_token(instance, 'my-value')).to eq 'my-value'
end end
end end
context 'when using migration strategy' do
let(:options) { { migrating: true } }
it 'writes encrypted token and writes plaintext token' do
expect(instance).to receive(:[]=)
.with('some_field_encrypted', encrypted)
expect(instance).to receive(:[]=)
.with('some_field', 'my-value')
expect(subject.set_token(instance, 'my-value')).to eq 'my-value'
end
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment