namespace: Avoid potential panic when logging namespace name. (#26565)

When the namespace was not found in state, indicated by a nil
object, we were using the name field of the nil object for the
return error.

This code path does not currently get triggered as the call flow
ensures the namespace will always be found within state. Making
this change makes sure we do not hit this panic in the future.
This commit is contained in:
James Rasell
2025-08-19 13:51:12 +01:00
committed by GitHub
parent b8b95eb918
commit 27daa745e4
2 changed files with 37 additions and 1 deletions

View File

@@ -305,7 +305,7 @@ func (n *Namespace) namespaceNoAssociatedVarsLocally(namespace string, snap *sta
func (n *Namespace) namespaceNoAssociatedQuotasLocally(namespace string, snap *state.StateSnapshot) (bool, error) {
ns, _ := snap.NamespaceByName(nil, namespace)
if ns == nil {
return false, fmt.Errorf("namespace %s does not exist", ns.Name)
return false, fmt.Errorf("namespace %s does not exist", namespace)
}
if ns.Quota != "" {
return false, nil

View File

@@ -696,6 +696,42 @@ func TestNamespaceEndpoint_DeleteNamespaces_Default(t *testing.T) {
assert.NotNil(msgpackrpc.CallWithCodec(codec, "Namespace.DeleteNamespaces", req, &resp))
}
func TestNamespace_namespaceNoAssociatedQuotasLocally(t *testing.T) {
ci.Parallel(t)
testServer, testServerCleanupFn := TestServer(t, nil)
t.Cleanup(testServerCleanupFn)
namespaceEndpoint := &Namespace{
srv: testServer,
}
t.Run("namespace not found", func(t *testing.T) {
stateSnapshot, err := testServer.State().Snapshot()
must.NoError(t, err)
res, err := namespaceEndpoint.namespaceNoAssociatedQuotasLocally("not-found", stateSnapshot)
must.ErrorContains(t, err, "namespace not-found does not exist")
assert.False(t, res)
})
t.Run("namespace without quota", func(t *testing.T) {
mockNamespace := mock.Namespace()
mockNamespace.Quota = ""
testServer.State().UpsertNamespaces(testServer.raft.LastIndex(), []*structs.Namespace{mockNamespace})
stateSnapshot, err := testServer.State().Snapshot()
must.NoError(t, err)
res, err := namespaceEndpoint.namespaceNoAssociatedQuotasLocally(mockNamespace.Name, stateSnapshot)
must.NoError(t, err)
assert.True(t, res)
})
}
func TestNamespaceEndpoint_UpsertNamespaces(t *testing.T) {
ci.Parallel(t)
assert := assert.New(t)