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.