From a61c928e341c621665d2f32223d08e293f3d3da9 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Wed, 9 Oct 2019 16:54:10 +0800 Subject: [PATCH] Check the existence of project before the next action Fixes #8234, check the existence of project to avoid the panic Signed-off-by: Wenkai Yin --- src/core/middlewares/util/util.go | 6 +++++- src/core/middlewares/util/util_test.go | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core/middlewares/util/util.go b/src/core/middlewares/util/util.go index 8a9624291..85a32ab4b 100644 --- a/src/core/middlewares/util/util.go +++ b/src/core/middlewares/util/util.go @@ -40,7 +40,7 @@ import ( "github.com/goharbor/harbor/src/core/config" "github.com/goharbor/harbor/src/core/promgr" "github.com/goharbor/harbor/src/pkg/scan/whitelist" - "github.com/opencontainers/go-digest" + digest "github.com/opencontainers/go-digest" ) type contextKey string @@ -346,6 +346,10 @@ func (pc PmsPolicyChecker) ContentTrustEnabled(name string) bool { log.Errorf("Unexpected error when getting the project, error: %v", err) return true } + if project == nil { + log.Debugf("project %s not found", name) + return false + } return project.ContentTrustEnabled() } diff --git a/src/core/middlewares/util/util_test.go b/src/core/middlewares/util/util_test.go index 2e6c9d609..db2f8960a 100644 --- a/src/core/middlewares/util/util_test.go +++ b/src/core/middlewares/util/util_test.go @@ -32,7 +32,7 @@ import ( notarytest "github.com/goharbor/harbor/src/common/utils/notary/test" testutils "github.com/goharbor/harbor/src/common/utils/test" "github.com/goharbor/harbor/src/core/config" - "github.com/opencontainers/go-digest" + digest "github.com/opencontainers/go-digest" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -187,6 +187,9 @@ func TestPMSPolicyChecker(t *testing.T) { assert.True(t, projectVulnerableEnabled) assert.Equal(t, projectVulnerableSeverity, models.SevLow) assert.Empty(t, wl.Items) + + contentTrustFlag = GetPolicyChecker().ContentTrustEnabled("non_exist_project") + assert.False(t, contentTrustFlag) } func TestCopyResp(t *testing.T) {