From e416df3ef8f5d9f511e481cdc83f7b3380b3c082 Mon Sep 17 00:00:00 2001 From: Francesco Ilario Date: Tue, 30 Jul 2024 17:44:27 +0200 Subject: [PATCH 1/2] add public-viewer support to pkg/context (#447) this commit is part of PR #443 Signed-off-by: Francesco Ilario --- pkg/context/keys.go | 4 +++ pkg/context/public_viewer.go | 12 +++++++ pkg/context/public_viewer_test.go | 53 +++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 pkg/context/public_viewer.go create mode 100644 pkg/context/public_viewer_test.go diff --git a/pkg/context/keys.go b/pkg/context/keys.go index 9477de21..a6eba535 100644 --- a/pkg/context/keys.go +++ b/pkg/context/keys.go @@ -25,4 +25,8 @@ const ( WorkspaceKey = "workspace" // RequestReceivedTime is the context key for the starting time of a request made RequestReceivedTime = "requestReceivedTime" + // PublicViewerEnabled is a boolean value indicating whether PublicViewer support is enabled + PublicViewerEnabled = "publicViewerEnabled" + // ImpersonateUser is the context key for the impersonated user in proxied call + ImpersonateUser = "impersonateUser" ) diff --git a/pkg/context/public_viewer.go b/pkg/context/public_viewer.go new file mode 100644 index 00000000..d7f11b23 --- /dev/null +++ b/pkg/context/public_viewer.go @@ -0,0 +1,12 @@ +package context + +import ( + "github.com/labstack/echo/v4" +) + +// IsPublicViewerEnabled retrieves from the context the boolean value associated to the PublicViewerEnabled key. +// If the key is not set it returns false, otherwise it returns the boolean value stored in the context. +func IsPublicViewerEnabled(ctx echo.Context) bool { + publicViewerEnabled, _ := ctx.Get(PublicViewerEnabled).(bool) + return publicViewerEnabled +} diff --git a/pkg/context/public_viewer_test.go b/pkg/context/public_viewer_test.go new file mode 100644 index 00000000..9b86d62f --- /dev/null +++ b/pkg/context/public_viewer_test.go @@ -0,0 +1,53 @@ +package context_test + +import ( + "testing" + + "github.com/codeready-toolchain/registration-service/pkg/context" + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/require" +) + +func TestIsPublicViewerEnabled(t *testing.T) { + tt := map[string]struct { + data map[string]interface{} + expected bool + }{ + "context value is true": { + data: map[string]interface{}{context.PublicViewerEnabled: true}, + expected: true, + }, + "context value is false": { + data: map[string]interface{}{context.PublicViewerEnabled: false}, + expected: false, + }, + "value not set in context": { + data: map[string]interface{}{}, + expected: false, + }, + "value set to a not castable value": { + data: map[string]interface{}{context.PublicViewerEnabled: struct{}{}}, + expected: false, + }, + "value set to nil": { + data: map[string]interface{}{context.PublicViewerEnabled: nil}, + expected: false, + }, + } + + for k, tc := range tt { + t.Run(k, func(t *testing.T) { + // given + ctx := echo.New().NewContext(nil, nil) + for k, v := range tc.data { + ctx.Set(k, v) + } + + // when + actual := context.IsPublicViewerEnabled(ctx) + + // then + require.Equal(t, tc.expected, actual, "IsPublicViewerEnabled returned a value different from expected") + }) + } +} From 0eb4c1f5006cc190f2c4d6a1e6f78859b1f53283 Mon Sep 17 00:00:00 2001 From: Francesco Ilario Date: Wed, 31 Jul 2024 10:21:35 +0200 Subject: [PATCH 2/2] add public-viewer support to pkg/configuration (#448) * add public-viewer support to pkg/configuration this commit is part of PR #443 Signed-off-by: Francesco Ilario * Update pkg/configuration/configuration_test.go Co-authored-by: Francisc Munteanu --------- Signed-off-by: Francesco Ilario Co-authored-by: Francisc Munteanu --- pkg/configuration/configuration.go | 4 +++ pkg/configuration/configuration_test.go | 39 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/pkg/configuration/configuration.go b/pkg/configuration/configuration.go index a6611d6f..3e49fa68 100644 --- a/pkg/configuration/configuration.go +++ b/pkg/configuration/configuration.go @@ -121,6 +121,10 @@ func (r RegistrationServiceConfig) Print() { logger.Info("Registration Service Configuration", "config", r.cfg.Host.RegistrationService) } +func (r RegistrationServiceConfig) PublicViewerEnabled() bool { + return r.cfg.Host.PublicViewerConfig != nil && r.cfg.Host.PublicViewerConfig.Enabled +} + func (r RegistrationServiceConfig) Environment() string { return commonconfig.GetString(r.cfg.Host.RegistrationService.Environment, prodEnvironment) } diff --git a/pkg/configuration/configuration_test.go b/pkg/configuration/configuration_test.go index 226e0004..409c21cf 100644 --- a/pkg/configuration/configuration_test.go +++ b/pkg/configuration/configuration_test.go @@ -3,6 +3,7 @@ package configuration_test import ( "testing" + "github.com/codeready-toolchain/api/api/v1alpha1" "github.com/codeready-toolchain/registration-service/pkg/configuration" "github.com/codeready-toolchain/registration-service/test" commonconfig "github.com/codeready-toolchain/toolchain-common/pkg/configuration" @@ -68,6 +69,7 @@ func TestRegistrationService(t *testing.T) { assert.InDelta(t, float32(0), regServiceCfg.Verification().CaptchaRequiredScore(), 0.01) assert.True(t, regServiceCfg.Verification().CaptchaAllowLowScoreReactivation()) assert.Empty(t, regServiceCfg.Verification().CaptchaServiceAccountFileContents()) + assert.False(t, regServiceCfg.PublicViewerEnabled()) }) t.Run("non-default", func(t *testing.T) { // given @@ -151,5 +153,42 @@ func TestRegistrationService(t *testing.T) { assert.InDelta(t, float32(0.5), regServiceCfg.Verification().CaptchaRequiredScore(), 0.01) assert.False(t, regServiceCfg.Verification().CaptchaAllowLowScoreReactivation()) assert.Equal(t, "example-content", regServiceCfg.Verification().CaptchaServiceAccountFileContents()) + assert.False(t, regServiceCfg.PublicViewerEnabled()) }) } + +func TestPublicViewerConfiguration(t *testing.T) { + tt := map[string]struct { + name string + expectedValue bool + publicViewerConfig *v1alpha1.PublicViewerConfiguration + }{ + "public-viewer is explicitly enabled": { + expectedValue: true, + publicViewerConfig: &v1alpha1.PublicViewerConfiguration{Enabled: true}, + }, + "public-viewer is explicitly disabled": { + expectedValue: false, + publicViewerConfig: &v1alpha1.PublicViewerConfiguration{Enabled: false}, + }, + "public-viewer config not set, assume disabled": { + expectedValue: false, + publicViewerConfig: nil, + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + // given + cfg := commonconfig.NewToolchainConfigObjWithReset(t) + cfg.Spec.Host.PublicViewerConfig = tc.publicViewerConfig + secrets := make(map[string]map[string]string) + + // when + regServiceCfg := configuration.NewRegistrationServiceConfig(cfg, secrets) + + // then + assert.Equal(t, tc.expectedValue, regServiceCfg.PublicViewerEnabled()) + }) + } +}