From 712b54f697421efbe1dc3a0740089ed29316198e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wa=C5=82ejko?= Date: Mon, 4 Mar 2024 11:12:06 +0000 Subject: [PATCH] Add IP address and user agent to audit log --- R/audit-trail.R | 22 +++++++++++++++---- ...dress_and_user_agent_to_audit_log.down.sql | 2 ++ ...address_and_user_agent_to_audit_log.up.sql | 2 ++ 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 inst/db/migrations/20240304105844_add_ip_address_and_user_agent_to_audit_log.down.sql create mode 100644 inst/db/migrations/20240304105844_add_ip_address_and_user_agent_to_audit_log.up.sql diff --git a/R/audit-trail.R b/R/audit-trail.R index b8457d6..fd80f67 100644 --- a/R/audit-trail.R +++ b/R/audit-trail.R @@ -31,6 +31,12 @@ AuditLog <- R6::R6Class( # nolint: object_name_linter. ) private$response_body <- response_body }, + set_ip_address = function(ip_address) { + private$ip_address <- ip_address + }, + set_user_agent = function(user_agent) { + private$user_agent <- user_agent + }, set_event_type = function(event_type) { private$event_type <- event_type }, @@ -69,7 +75,9 @@ AuditLog <- R6::R6Class( # nolint: object_name_linter. private$request_method, private$request_body, private$response_code, - private$response_body + private$response_body, + private$ip_address, + private$user_agent ) values <- purrr::map(values, \(x) ifelse(is.null(x), NA, x)) @@ -84,9 +92,11 @@ AuditLog <- R6::R6Class( # nolint: object_name_linter. request_method, request_body, response_code, - response_body + response_body, + ip_address, + user_agent ) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", values ) } @@ -100,7 +110,9 @@ AuditLog <- R6::R6Class( # nolint: object_name_linter. request_method = NULL, response_code = NULL, request_body = NULL, - response_body = NULL + response_body = NULL, + ip_address = NULL, + user_agent = NULL ) ) @@ -146,6 +158,8 @@ setup_audit_trail <- function(pr, endpoints = list()) { audit_log$set_response_code(res$status) audit_log$set_request_body(req$body) audit_log$set_response_body(res$body) + audit_log$set_ip_address(req$REMOTE_ADDR) + audit_log$set_user_agent(req$HTTP_USER_AGENT) log_valid <- audit_log$validate_log() diff --git a/inst/db/migrations/20240304105844_add_ip_address_and_user_agent_to_audit_log.down.sql b/inst/db/migrations/20240304105844_add_ip_address_and_user_agent_to_audit_log.down.sql new file mode 100644 index 0000000..d4baee8 --- /dev/null +++ b/inst/db/migrations/20240304105844_add_ip_address_and_user_agent_to_audit_log.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE audit_log DROP COLUMN ip_address; +ALTER TABLE audit_log DROP COLUMN user_agent; \ No newline at end of file diff --git a/inst/db/migrations/20240304105844_add_ip_address_and_user_agent_to_audit_log.up.sql b/inst/db/migrations/20240304105844_add_ip_address_and_user_agent_to_audit_log.up.sql new file mode 100644 index 0000000..aa15654 --- /dev/null +++ b/inst/db/migrations/20240304105844_add_ip_address_and_user_agent_to_audit_log.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE audit_log ADD COLUMN ip_address VARCHAR(255); +ALTER TABLE audit_log ADD COLUMN user_agent TEXT; \ No newline at end of file