From 7fea2ee7f76f65531bad47065cdc42e7a03fe931 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 18 Jun 2024 19:33:06 +0100 Subject: [PATCH 1/2] check if context is already cancelled when assessing viability for audit --- audit/broker.go | 8 ++++++++ audit/broker_test.go | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/audit/broker.go b/audit/broker.go index 2681ca0ede70..96cd1405f3d1 100644 --- a/audit/broker.go +++ b/audit/broker.go @@ -462,6 +462,8 @@ func (b *Broker) IsRegistered(name string) bool { // isContextViable examines the supplied context to see if its own deadline would // occur later than a newly created context with a specific timeout. +// Additionally, whether the supplied context is already cancelled, thus making it +// unviable. // If the existing context is viable it can be used 'as-is', if not, the caller // should consider creating a new context with the relevant deadline and associated // context values (e.g. namespace) in order to reduce the likelihood that the @@ -472,6 +474,12 @@ func isContextViable(ctx context.Context) bool { return false } + select { + case <-ctx.Done(): + return false + default: + } + deadline, hasDeadline := ctx.Deadline() // If there's no deadline on the context then we don't need to worry about diff --git a/audit/broker_test.go b/audit/broker_test.go index 074cb203f0e4..b6094322eafc 100644 --- a/audit/broker_test.go +++ b/audit/broker_test.go @@ -160,11 +160,14 @@ func BenchmarkAuditBroker_File_Request_DevNull(b *testing.B) { } // TestBroker_isContextViable_basics checks the expected result of isContextViable -// for basic inputs such as nil and a never-ending context. +// for basic inputs such as nil, cancelled context and a never-ending context. func TestBroker_isContextViable_basics(t *testing.T) { t.Parallel() require.False(t, isContextViable(nil)) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + require.False(t, isContextViable(ctx)) require.True(t, isContextViable(context.Background())) } From 7a9bf3b39fc0c8f8be643292447f6b1825375578 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 18 Jun 2024 19:41:06 +0100 Subject: [PATCH 2/2] changelog --- changelog/27531.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/27531.txt diff --git a/changelog/27531.txt b/changelog/27531.txt new file mode 100644 index 000000000000..3dda984d1d7d --- /dev/null +++ b/changelog/27531.txt @@ -0,0 +1,5 @@ +```release-note:bug +core/audit: Audit logging a Vault request/response checks if the existing context +is cancelled and will now use a new context with a 5 second timeout. +If the existing context is cancelled a new context, will be used. +``` \ No newline at end of file