Skip to content

Commit

Permalink
chore(CE): added audit logs to catalogs_controller (#439)
Browse files Browse the repository at this point in the history
Co-authored-by: TivonB-AI2 <[email protected]>
  • Loading branch information
github-actions[bot] and TivonB-AI2 authored Oct 25, 2024
1 parent 7269342 commit 0348d38
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/app/controllers/api/v1/catalogs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ module Api
module V1
class CatalogsController < ApplicationController
include Catalogs
include AuditLogger
before_action :set_connector, only: %i[create update]
before_action :set_catalog, only: %i[update]

after_action :create_audit_log

def create
authorize current_workspace, policy_class: ConnectorPolicy
result = CreateCatalog.call(
Expand All @@ -16,6 +19,8 @@ def create

if result.success?
@catalog = result.catalog
@audit_resource = @catalog.catalog["streams"].first["name"]
@payload = catalog_params.to_h
render json: @catalog, status: :created
else
render_error(
Expand All @@ -36,6 +41,8 @@ def update

if result.success?
@catalog = result.catalog
@audit_resource = @catalog.catalog["streams"].first["name"]
@payload = catalog_params.to_h
render json: @catalog, status: :created
else
render_error(
Expand All @@ -61,6 +68,10 @@ def set_catalog
@catalog = @connector.catalog
end

def create_audit_log
audit!(resource_id: params[:id], resource: @audit_resource, payload: @payload)
end

def catalog_params
params.require(:catalog).permit(json_schema: {})
end
Expand Down
36 changes: 36 additions & 0 deletions server/spec/requests/api/v1/catalogs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@
expect(response_hash.dig(:data, :attributes, :catalog, :streams).first["name"]).to eq(connector.name)
expect(response_hash.dig(:data, :attributes, :catalog,
:streams).first["json_schema"]).to eq(request_body[:catalog]["json_schema"])

audit_log = AuditLog.last
expect(audit_log).not_to be_nil
expect(audit_log.user_id).to eq(user.id)
expect(audit_log.action).to eq("create")
expect(audit_log.resource_type).to eq("Catalog")
expect(audit_log.resource_id).to eq(nil)
expect(audit_log.resource).to eq(connector.name)
expect(audit_log.workspace_id).to eq(workspace.id)
end
end

Expand All @@ -105,6 +114,15 @@
expect(response_hash.dig(:data, :attributes, :catalog, :streams).first["name"]).to eq(connector.name)
expect(response_hash.dig(:data, :attributes, :catalog,
:streams).first["json_schema"]).to eq(request_body[:catalog]["json_schema"])

audit_log = AuditLog.last
expect(audit_log).not_to be_nil
expect(audit_log.user_id).to eq(user.id)
expect(audit_log.action).to eq("create")
expect(audit_log.resource_type).to eq("Catalog")
expect(audit_log.resource_id).to eq(nil)
expect(audit_log.resource).to eq(connector.name)
expect(audit_log.workspace_id).to eq(workspace.id)
end
end

Expand Down Expand Up @@ -145,6 +163,15 @@
expect(response_hash.dig(:data, :attributes, :connector_id)).to eq(connector.id)
expect(response_hash.dig(:data, :attributes, :catalog,
:streams).first["json_schema"]).to eq(update_request_body[:catalog]["json_schema"])

audit_log = AuditLog.last
expect(audit_log).not_to be_nil
expect(audit_log.user_id).to eq(user.id)
expect(audit_log.action).to eq("update")
expect(audit_log.resource_type).to eq("Catalog")
expect(audit_log.resource_id).to eq(existing_catalog.id)
expect(audit_log.resource).to eq(connector.name)
expect(audit_log.workspace_id).to eq(workspace.id)
end
end

Expand All @@ -164,6 +191,15 @@
expect(response_hash.dig(:data, :attributes, :connector_id)).to eq(connector.id)
expect(response_hash.dig(:data, :attributes, :catalog,
:streams).first["json_schema"]).to eq(update_request_body[:catalog]["json_schema"])

audit_log = AuditLog.last
expect(audit_log).not_to be_nil
expect(audit_log.user_id).to eq(user.id)
expect(audit_log.action).to eq("update")
expect(audit_log.resource_type).to eq("Catalog")
expect(audit_log.resource_id).to eq(existing_catalog.id)
expect(audit_log.resource).to eq(connector.name)
expect(audit_log.workspace_id).to eq(workspace.id)
end
end

Expand Down

0 comments on commit 0348d38

Please sign in to comment.