Commit 6da8db40 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'services-usage-1' into 'master'

Record Audit Event when creating new SSH keys via the API

See merge request gitlab-org/gitlab!33859
parents 358f18e0 d6e5b443
......@@ -2,6 +2,14 @@
module Keys
class CreateService < ::Keys::BaseService
attr_accessor :current_user
def initialize(current_user, params = {})
@current_user, @params = current_user, params
@ip_address = @params.delete(:ip_address)
@user = params.delete(:user) || current_user
end
def execute
key = user.keys.create(params)
notification_service.new_key(key) if key.persisted?
......
---
title: Record audit event when an admin creates a new SSH Key for a user via the API
merge_request: 33859
author: Rajendra Kadam
type: fixed
......@@ -14,7 +14,7 @@ module EE
end
def audit_event_service
::AuditEventService.new(user,
::AuditEventService.new(current_user,
user,
action: :custom,
custom_message: 'Added SSH key',
......
......@@ -3,14 +3,20 @@
require 'spec_helper'
RSpec.describe Keys::CreateService do
let(:admin) { create(:admin) }
let(:user) { create(:user) }
let(:params) { attributes_for(:key) }
let(:params) { attributes_for(:key).merge(user: user) }
subject { described_class.new(user, params) }
subject { described_class.new(admin, params) }
it 'creates' do
stub_licensed_features(extended_audit_events: true)
expect { subject.execute }.to change { SecurityEvent.count }.by(1)
event = SecurityEvent.last
expect(event.author_name).to eq(admin.name)
expect(event.entity_id).to eq(user.id)
end
end
......@@ -264,9 +264,9 @@ module API
user = User.find_by(id: params.delete(:id))
not_found!('User') unless user
key = user.keys.new(declared_params(include_missing: false))
key = ::Keys::CreateService.new(current_user, declared_params(include_missing: false).merge(user: user)).execute
if key.save
if key.persisted?
present key, with: Entities::SSHKey
else
render_validation_error!(key)
......
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