From 00aee5f70f9b0e6ffffc550c51fc3e061645fba8 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Mon, 11 Jan 2021 16:13:51 +0100 Subject: [PATCH] util/log: conditionally include the server IDs on every line For CC security logging we want the ability to route the logging events from the files where they are written into a centralized logging collector. However this routing is done line-by-line. To enable log aggregation across multiple clusters, or multiple nodes, we need to disambiguate which log entries come from which cluster and which node. This patch accommodates this requirement by adding the cluster ID and, for tenant servers, the tenant and SQL instance ID, on every output line when the env var `COCKROACH_ALWAYS_LOG_SERVER_IDS` is set to a true-ish value. Note: this feature is unneeded in v21.1 because in that version JSON logging is available and that already includes the server identity bits. Release note: None --- .../interactive_tests/test_force_auth_log.tcl | 48 ++++++++++ pkg/server/testserver.go | 7 ++ pkg/util/log/clog.go | 92 ++++++++++++++++--- pkg/util/log/sync_buffer.go | 6 +- 4 files changed, 135 insertions(+), 18 deletions(-) create mode 100644 pkg/cli/interactive_tests/test_force_auth_log.tcl diff --git a/pkg/cli/interactive_tests/test_force_auth_log.tcl b/pkg/cli/interactive_tests/test_force_auth_log.tcl new file mode 100644 index 000000000000..77b659d74c25 --- /dev/null +++ b/pkg/cli/interactive_tests/test_force_auth_log.tcl @@ -0,0 +1,48 @@ +#! /usr/bin/env expect -f + +source [file join [file dirname $argv0] common.tcl] + +set ::env(COCKROACH_INSECURE) "false" +set ::env(COCKROACH_HOST) "localhost" +set certs_dir "/certs" + + +set ::env(COCKROACH_ALWAYS_LOG_CLUSTER_ID) 1 +set ::env(COCKROACH_ALWAYS_LOG_AUTHN_EVENTS) 1 + +proc start_secure_server {argv certs_dir extra} { + report "BEGIN START SECURE SERVER" + system "$argv start-single-node --host=localhost --socket-dir=. --certs-dir=$certs_dir --pid-file=server_pid -s=path=logs/db --background $extra >>expect-cmd.log 2>&1; + $argv sql --certs-dir=$certs_dir -e 'select 1'" + report "END START SECURE SERVER" +} + +proc stop_secure_server {argv certs_dir} { + report "BEGIN STOP SECURE SERVER" + system "$argv quit --certs-dir=$certs_dir" + report "END STOP SECURE SERVER" +} + + +start_secure_server $argv $certs_dir "" + +set logfile logs/db/logs/cockroach-auth.log + +# run a client command, so we have at least one authn event in the log. +system "$argv sql -e 'create user someuser' --certs-dir=$certs_dir" +system "$argv sql -e 'select 1' --user someuser --certs-dir=$certs_dir 0 { + if buf.Len() > 0 { + buf.WriteByte(',') + } + buf.WriteString("clusterID=") + buf.WriteString(clusterID) + } + if tenantID := logging.tenantID.Get(); len(tenantID) > 0 { + if buf.Len() > 0 { + buf.WriteByte(',') + } + buf.WriteString("tenantID=") + buf.WriteString(tenantID) + } + if sqlInstanceID := atomic.LoadInt32(&logging.sqlInstanceID); sqlInstanceID != 0 { + if buf.Len() > 0 { + buf.WriteByte(',') + } + buf.WriteString("instanceID=") + buf.WriteString(strconv.Itoa(int(sqlInstanceID))) + } + entry.Tags = buf.String() + } + // Mark the logger as active, so that further configuration changes // are disabled. See IsActive() and its callers for details. setActive() diff --git a/pkg/util/log/sync_buffer.go b/pkg/util/log/sync_buffer.go index c8fcdb1b773b..1e0f7bc19dec 100644 --- a/pkg/util/log/sync_buffer.go +++ b/pkg/util/log/sync_buffer.go @@ -253,11 +253,9 @@ func (l *loggerT) initializeNewOutputFile( l.makeStartLine("arguments: %s", os.Args), ) - logging.mu.Lock() - if logging.mu.clusterID != "" { - messages = append(messages, l.makeStartLine("clusterID: %s", logging.mu.clusterID)) + if clusterID := logging.clusterID.Get(); len(clusterID) > 0 { + messages = append(messages, l.makeStartLine("clusterID: %s", clusterID)) } - logging.mu.Unlock() // Including a non-ascii character in the first 1024 bytes of the log helps // viewers that attempt to guess the character encoding.