-
Notifications
You must be signed in to change notification settings - Fork 367
Add audit events to stacks #4733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| require 'repositories/event_types' | ||
|
|
||
| module VCAP::CloudController | ||
| module Repositories | ||
| class StackEventRepository | ||
| def record_stack_create(stack, user_audit_info, request_attrs) | ||
| Event.create( | ||
| type: EventTypes::STACK_CREATE, | ||
| actee: stack.guid, | ||
| actee_type: 'stack', | ||
| actee_name: stack.name, | ||
| actor: user_audit_info.user_guid, | ||
| actor_type: 'user', | ||
| actor_name: user_audit_info.user_email, | ||
| actor_username: user_audit_info.user_name, | ||
| timestamp: Sequel::CURRENT_TIMESTAMP, | ||
| space_guid: '', | ||
| organization_guid: '', | ||
| metadata: { | ||
| request: request_attrs | ||
| } | ||
| ) | ||
| end | ||
|
|
||
| def record_stack_update(stack, user_audit_info, request_attrs) | ||
| Event.create( | ||
| type: EventTypes::STACK_UPDATE, | ||
| actee: stack.guid, | ||
| actee_type: 'stack', | ||
| actee_name: stack.name, | ||
| actor: user_audit_info.user_guid, | ||
| actor_type: 'user', | ||
| actor_name: user_audit_info.user_email, | ||
| actor_username: user_audit_info.user_name, | ||
| timestamp: Sequel::CURRENT_TIMESTAMP, | ||
| space_guid: '', | ||
| organization_guid: '', | ||
| metadata: { | ||
| request: request_attrs | ||
| } | ||
| ) | ||
| end | ||
|
|
||
| def record_stack_delete(stack, user_audit_info) | ||
| Event.create( | ||
| type: EventTypes::STACK_DELETE, | ||
| actee: stack.guid, | ||
| actee_type: 'stack', | ||
| actee_name: stack.name, | ||
| actor: user_audit_info.user_guid, | ||
| actor_type: 'user', | ||
| actor_name: user_audit_info.user_email, | ||
| actor_username: user_audit_info.user_name, | ||
| timestamp: Sequel::CURRENT_TIMESTAMP, | ||
| space_guid: '', | ||
| organization_guid: '', | ||
| metadata: {} | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,12 @@ | |
| module VCAP::CloudController | ||
| RSpec.describe StackCreate do | ||
| describe 'create' do | ||
| let(:user) { User.make } | ||
| let(:user_email) { '[email protected]' } | ||
| let(:user_audit_info) { UserAuditInfo.new(user_guid: user.guid, user_email: user_email) } | ||
|
|
||
| subject(:stack_create) { StackCreate.new(user_audit_info) } | ||
|
|
||
| it 'creates a stack' do | ||
| message = VCAP::CloudController::StackCreateMessage.new( | ||
| name: 'the-name', | ||
|
|
@@ -20,7 +26,7 @@ module VCAP::CloudController | |
| } | ||
| } | ||
| ) | ||
| stack = StackCreate.new.create(message) | ||
| stack = stack_create.create(message) | ||
|
|
||
| expect(stack.name).to eq('the-name') | ||
| expect(stack.description).to eq('the-description') | ||
|
|
@@ -35,6 +41,31 @@ module VCAP::CloudController | |
| ) | ||
| end | ||
|
|
||
| it 'creates an audit event' do | ||
| message = VCAP::CloudController::StackCreateMessage.new( | ||
| name: 'my-stack', | ||
| description: 'my-description' | ||
| ) | ||
| created_stack = stack_create.create(message) | ||
|
|
||
| expect(VCAP::CloudController::Event.count).to eq(1) | ||
| stack_create_event = VCAP::CloudController::Event.find(type: 'audit.stack.create') | ||
| expect(stack_create_event).to exist | ||
| expect(stack_create_event.values).to include( | ||
| type: 'audit.stack.create', | ||
| actor: user_audit_info.user_guid, | ||
| actor_type: 'user', | ||
| actor_name: user_audit_info.user_email, | ||
| actee: created_stack.guid, | ||
| actee_type: 'stack', | ||
| actee_name: 'my-stack', | ||
| space_guid: '', | ||
| organization_guid: '' | ||
| ) | ||
| expect(stack_create_event.metadata).to eq({ 'request' => message.audit_hash }) | ||
| expect(stack_create_event.timestamp).to be | ||
| end | ||
|
|
||
| context 'when a model validation fails' do | ||
| it 'raises an error' do | ||
| errors = Sequel::Model::Errors.new | ||
|
|
@@ -44,7 +75,7 @@ module VCAP::CloudController | |
|
|
||
| message = VCAP::CloudController::StackCreateMessage.new(name: 'foobar') | ||
| expect do | ||
| StackCreate.new.create(message) | ||
| stack_create.create(message) | ||
| end.to raise_error(StackCreate::Error, 'blork is busted') | ||
| end | ||
| end | ||
|
|
@@ -59,7 +90,7 @@ module VCAP::CloudController | |
| it 'raises a human-friendly error' do | ||
| message = VCAP::CloudController::StackCreateMessage.new(name:) | ||
| expect do | ||
| StackCreate.new.create(message) | ||
| stack_create.create(message) | ||
| end.to raise_error(StackCreate::Error, 'Name must be unique') | ||
| end | ||
| end | ||
|
|
@@ -71,15 +102,15 @@ module VCAP::CloudController | |
| message = VCAP::CloudController::StackCreateMessage.new(name:) | ||
| # First request, should succeed | ||
| expect do | ||
| StackCreate.new.create(message) | ||
| stack_create.create(message) | ||
| end.not_to raise_error | ||
|
|
||
| # Mock the validation for the second request to simulate the race condition and trigger a unique constraint violation | ||
| allow_any_instance_of(Stack).to receive(:validate).and_return(true) | ||
|
|
||
| # Second request, should fail with correct error | ||
| expect do | ||
| StackCreate.new.create(message) | ||
| stack_create.create(message) | ||
| end.to raise_error(StackCreate::Error, 'Name must be unique') | ||
| end | ||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,11 @@ | |
|
|
||
| module VCAP::CloudController | ||
| RSpec.describe StackDelete do | ||
| subject(:stack_delete) { StackDelete.new } | ||
| let(:user) { User.make } | ||
| let(:user_email) { '[email protected]' } | ||
| let(:user_audit_info) { UserAuditInfo.new(user_guid: user.guid, user_email: user_email) } | ||
|
|
||
| subject(:stack_delete) { StackDelete.new(user_audit_info) } | ||
|
|
||
| describe '#delete' do | ||
| context 'when the stack exists' do | ||
|
|
@@ -16,6 +20,30 @@ module VCAP::CloudController | |
| expect { stack.refresh }.to raise_error(Sequel::Error, 'Record not found') | ||
| end | ||
|
|
||
| it 'creates an audit event' do | ||
| stack_guid = stack.guid | ||
| stack_name = stack.name | ||
|
|
||
| stack_delete.delete(stack) | ||
|
|
||
| expect(VCAP::CloudController::Event.count).to eq(1) | ||
| stack_delete_event = VCAP::CloudController::Event.find(type: 'audit.stack.delete') | ||
| expect(stack_delete_event).to exist | ||
| expect(stack_delete_event.values).to include( | ||
| type: 'audit.stack.delete', | ||
| actor: user_audit_info.user_guid, | ||
| actor_type: 'user', | ||
| actor_name: user_audit_info.user_email, | ||
| actee: stack_guid, | ||
| actee_type: 'stack', | ||
| actee_name: stack_name, | ||
| space_guid: '', | ||
| organization_guid: '' | ||
| ) | ||
| expect(stack_delete_event.metadata).to eq({}) | ||
| expect(stack_delete_event.timestamp).to be | ||
| end | ||
|
|
||
| it 'deletes associated labels' do | ||
| label = StackLabelModel.make(resource_guid: stack.guid, key_name: 'test1', value: 'bommel') | ||
| expect do | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,11 @@ | |
|
|
||
| module VCAP::CloudController | ||
| RSpec.describe StackUpdate do | ||
| subject(:stack_update) { StackUpdate.new } | ||
| let(:user) { User.make } | ||
| let(:user_email) { '[email protected]' } | ||
| let(:user_audit_info) { UserAuditInfo.new(user_guid: user.guid, user_email: user_email) } | ||
|
|
||
| subject(:stack_update) { StackUpdate.new(user_audit_info) } | ||
|
|
||
| describe '#update' do | ||
| let(:body) do | ||
|
|
@@ -29,6 +33,27 @@ module VCAP::CloudController | |
| expect(stack).to have_labels({ key_name: 'freaky', value: 'wednesday' }) | ||
| expect(stack).to have_annotations({ key_name: 'tokyo', value: 'grapes' }) | ||
| end | ||
|
|
||
| it 'creates an audit event' do | ||
| stack_update.update(stack, message) | ||
|
|
||
| expect(VCAP::CloudController::Event.count).to eq(1) | ||
| stack_update_event = VCAP::CloudController::Event.find(type: 'audit.stack.update') | ||
| expect(stack_update_event).to exist | ||
| expect(stack_update_event.values).to include( | ||
| type: 'audit.stack.update', | ||
| actor: user_audit_info.user_guid, | ||
| actor_type: 'user', | ||
| actor_name: user_audit_info.user_email, | ||
| actee: stack.guid, | ||
| actee_type: 'stack', | ||
| actee_name: stack.name, | ||
| space_guid: '', | ||
| organization_guid: '' | ||
| ) | ||
| expect(stack_update_event.metadata).to eq({ 'request' => message.audit_hash }) | ||
| expect(stack_update_event.timestamp).to be | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.