Commit d7517cb5 authored by Matthias Käppler's avatar Matthias Käppler

Merge branch 'pedropombeiro/349540/1-split-audit-token' into 'master'

Split CiRunnerTokenAuthor class

See merge request gitlab-org/gitlab!80621
parents f9a90786 6501a435
...@@ -3,11 +3,24 @@ ...@@ -3,11 +3,24 @@
module Gitlab module Gitlab
module Audit module Audit
class CiRunnerTokenAuthor < Gitlab::Audit::NullAuthor class CiRunnerTokenAuthor < Gitlab::Audit::NullAuthor
def initialize(token:, entity_type:, entity_path:) # Represents a CI Runner token (registration or authentication)
super(id: -1, name: "Registration token: #{token}") #
# @param [AuditEvent] audit_event event representing a runner registration/un-registration operation
def initialize(audit_event)
if audit_event.details.include?(:runner_authentication_token)
token = audit_event.details[:runner_authentication_token]
name = "Authentication token: #{token}"
elsif audit_event.details.include?(:runner_registration_token)
token = audit_event.details[:runner_registration_token]
name = "Registration token: #{token}"
else
raise ArgumentError, 'Runner token missing'
end
super(id: -1, name: name)
@entity_type = entity_type @entity_type = audit_event.entity_type
@entity_path = entity_path @entity_path = audit_event.entity_path
end end
def full_path def full_path
......
...@@ -18,12 +18,8 @@ module Gitlab ...@@ -18,12 +18,8 @@ module Gitlab
def self.for(id, audit_event) def self.for(id, audit_event)
name = audit_event[:author_name] || audit_event.details[:author_name] name = audit_event[:author_name] || audit_event.details[:author_name]
if audit_event.details.include?(:runner_registration_token) if audit_event.target_type == ::Ci::Runner.name
::Gitlab::Audit::CiRunnerTokenAuthor.new( Gitlab::Audit::CiRunnerTokenAuthor.new(audit_event)
token: audit_event.details[:runner_registration_token],
entity_type: audit_event.entity_type || audit_event.details[:entity_type],
entity_path: audit_event.entity_path || audit_event.details[:entity_path]
)
elsif id == -1 elsif id == -1
Gitlab::Audit::UnauthenticatedAuthor.new(name: name) Gitlab::Audit::UnauthenticatedAuthor.new(name: name)
else else
......
...@@ -3,18 +3,50 @@ ...@@ -3,18 +3,50 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Audit::CiRunnerTokenAuthor do RSpec.describe Gitlab::Audit::CiRunnerTokenAuthor do
describe '#initialize' do describe '.initialize' do
it 'sets correct attributes' do subject { described_class.new(audit_event) }
expect(described_class.new(token: 'abc1234567', entity_type: 'Project', entity_path: 'd/e'))
.to have_attributes(id: -1, name: 'Registration token: abc1234567') let(:details) { }
let(:audit_event) { instance_double(AuditEvent, details: details, entity_type: 'Project', entity_path: 'd/e') }
context 'with runner_authentication_token' do
let(:details) do
{ runner_authentication_token: 'abc1234567' }
end
it 'returns CiRunnerTokenAuthor with expected attributes' do
is_expected.to have_attributes(id: -1, name: 'Authentication token: abc1234567')
end
end
context 'with runner_registration_token' do
let(:details) do
{ runner_registration_token: 'abc1234567' }
end
it 'returns CiRunnerTokenAuthor with expected attributes' do
is_expected.to have_attributes(id: -1, name: 'Registration token: abc1234567')
end
end
context 'with runner token missing' do
let(:details) do
{}
end
it 'raises ArgumentError' do
expect { subject }.to raise_error ArgumentError, 'Runner token missing'
end
end end
end end
describe '#full_path' do describe '#full_path' do
subject { author.full_path } subject { author.full_path }
let(:author) { described_class.new(audit_event) }
context 'with instance registration token' do context 'with instance registration token' do
let(:author) { described_class.new(token: 'abc1234567', entity_type: 'User', entity_path: nil) } let(:audit_event) { instance_double(AuditEvent, details: { runner_registration_token: 'abc1234567' }, entity_type: 'User', entity_path: nil) }
it 'returns correct url' do it 'returns correct url' do
is_expected.to eq('/admin/runners') is_expected.to eq('/admin/runners')
...@@ -22,7 +54,7 @@ RSpec.describe Gitlab::Audit::CiRunnerTokenAuthor do ...@@ -22,7 +54,7 @@ RSpec.describe Gitlab::Audit::CiRunnerTokenAuthor do
end end
context 'with group registration token' do context 'with group registration token' do
let(:author) { described_class.new(token: 'abc1234567', entity_type: 'Group', entity_path: 'a/b') } let(:audit_event) { instance_double(AuditEvent, details: { runner_registration_token: 'abc1234567' }, entity_type: 'Group', entity_path: 'a/b') }
it 'returns correct url' do it 'returns correct url' do
expect(::Gitlab::Routing.url_helpers).to receive(:group_settings_ci_cd_path) expect(::Gitlab::Routing.url_helpers).to receive(:group_settings_ci_cd_path)
...@@ -35,7 +67,7 @@ RSpec.describe Gitlab::Audit::CiRunnerTokenAuthor do ...@@ -35,7 +67,7 @@ RSpec.describe Gitlab::Audit::CiRunnerTokenAuthor do
end end
context 'with project registration token' do context 'with project registration token' do
let(:author) { described_class.new(token: 'abc1234567', entity_type: 'Project', entity_path: project.full_path) } let(:audit_event) { instance_double(AuditEvent, details: { runner_registration_token: 'abc1234567' }, entity_type: 'Project', entity_path: project.full_path) }
let(:project) { create(:project) } let(:project) { create(:project) }
it 'returns correct url' do it 'returns correct url' do
......
...@@ -11,6 +11,7 @@ RSpec.describe Gitlab::Audit::NullAuthor do ...@@ -11,6 +11,7 @@ RSpec.describe Gitlab::Audit::NullAuthor do
it 'returns an DeletedAuthor' do it 'returns an DeletedAuthor' do
allow(audit_event).to receive(:[]).with(:author_name).and_return('Old Hat') allow(audit_event).to receive(:[]).with(:author_name).and_return('Old Hat')
allow(audit_event).to receive(:details).and_return({}) allow(audit_event).to receive(:details).and_return({})
allow(audit_event).to receive(:target_type)
expect(subject.for(666, audit_event)).to be_a(Gitlab::Audit::DeletedAuthor) expect(subject.for(666, audit_event)).to be_a(Gitlab::Audit::DeletedAuthor)
end end
...@@ -18,6 +19,7 @@ RSpec.describe Gitlab::Audit::NullAuthor do ...@@ -18,6 +19,7 @@ RSpec.describe Gitlab::Audit::NullAuthor do
it 'returns an UnauthenticatedAuthor when id equals -1', :aggregate_failures do it 'returns an UnauthenticatedAuthor when id equals -1', :aggregate_failures do
allow(audit_event).to receive(:[]).with(:author_name).and_return('Frank') allow(audit_event).to receive(:[]).with(:author_name).and_return('Frank')
allow(audit_event).to receive(:details).and_return({}) allow(audit_event).to receive(:details).and_return({})
allow(audit_event).to receive(:target_type)
expect(subject.for(-1, audit_event)).to be_a(Gitlab::Audit::UnauthenticatedAuthor) expect(subject.for(-1, audit_event)).to be_a(Gitlab::Audit::UnauthenticatedAuthor)
expect(subject.for(-1, audit_event)).to have_attributes(id: -1, name: 'Frank') expect(subject.for(-1, audit_event)).to have_attributes(id: -1, name: 'Frank')
...@@ -27,12 +29,25 @@ RSpec.describe Gitlab::Audit::NullAuthor do ...@@ -27,12 +29,25 @@ RSpec.describe Gitlab::Audit::NullAuthor do
allow(audit_event).to receive(:[]).with(:author_name).and_return('cde456') allow(audit_event).to receive(:[]).with(:author_name).and_return('cde456')
allow(audit_event).to receive(:entity_type).and_return('User') allow(audit_event).to receive(:entity_type).and_return('User')
allow(audit_event).to receive(:entity_path).and_return('/a/b') allow(audit_event).to receive(:entity_path).and_return('/a/b')
allow(audit_event).to receive(:target_type).and_return(::Ci::Runner.name)
allow(audit_event).to receive(:details) allow(audit_event).to receive(:details)
.and_return({ runner_registration_token: 'cde456', author_name: 'cde456', entity_type: 'User', entity_path: '/a/b' }) .and_return({ runner_registration_token: 'cde456', author_name: 'cde456', entity_type: 'User', entity_path: '/a/b' })
expect(subject.for(-1, audit_event)).to be_a(Gitlab::Audit::CiRunnerTokenAuthor) expect(subject.for(-1, audit_event)).to be_a(Gitlab::Audit::CiRunnerTokenAuthor)
expect(subject.for(-1, audit_event)).to have_attributes(id: -1, name: 'Registration token: cde456') expect(subject.for(-1, audit_event)).to have_attributes(id: -1, name: 'Registration token: cde456')
end end
it 'returns a CiRunnerTokenAuthor when details contain runner authentication token', :aggregate_failures do
allow(audit_event).to receive(:[]).with(:author_name).and_return('cde456')
allow(audit_event).to receive(:entity_type).and_return('User')
allow(audit_event).to receive(:entity_path).and_return('/a/b')
allow(audit_event).to receive(:target_type).and_return(::Ci::Runner.name)
allow(audit_event).to receive(:details)
.and_return({ runner_authentication_token: 'cde456', author_name: 'cde456', entity_type: 'User', entity_path: '/a/b' })
expect(subject.for(-1, audit_event)).to be_a(Gitlab::Audit::CiRunnerTokenAuthor)
expect(subject.for(-1, audit_event)).to have_attributes(id: -1, name: 'Authentication token: cde456')
end
end end
describe '#current_sign_in_ip' do describe '#current_sign_in_ip' do
......
...@@ -97,8 +97,8 @@ RSpec.describe AuditEvent do ...@@ -97,8 +97,8 @@ RSpec.describe AuditEvent do
describe '#author' do describe '#author' do
subject { audit_event.author } subject { audit_event.author }
context "when a runner_registration_token's present" do context "when the target type is not Ci::Runner" do
let(:audit_event) { build(:project_audit_event, details: { target_id: 678 }) } let(:audit_event) { build(:project_audit_event, target_id: 678) }
it 'returns a NullAuthor' do it 'returns a NullAuthor' do
expect(::Gitlab::Audit::NullAuthor).to receive(:for) expect(::Gitlab::Audit::NullAuthor).to receive(:for)
...@@ -109,12 +109,12 @@ RSpec.describe AuditEvent do ...@@ -109,12 +109,12 @@ RSpec.describe AuditEvent do
end end
end end
context "when a runner_registration_token's present" do context 'when the target type is Ci::Runner and details contain runner_registration_token' do
let(:audit_event) { build(:project_audit_event, details: { target_id: 678, runner_registration_token: 'abc123' }) } let(:audit_event) { build(:project_audit_event, target_type: ::Ci::Runner.name, target_id: 678, details: { runner_registration_token: 'abc123' }) }
it 'returns a CiRunnerTokenAuthor' do it 'returns a CiRunnerTokenAuthor' do
expect(::Gitlab::Audit::CiRunnerTokenAuthor).to receive(:new) expect(::Gitlab::Audit::CiRunnerTokenAuthor).to receive(:new)
.with({ token: 'abc123', entity_type: 'Project', entity_path: audit_event.entity_path }) .with(audit_event)
.and_call_original .and_call_original
.once .once
......
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