Skip to content

Commit

Permalink
AST-26691 - CLI | Support PACKAGE_ENFORCEMENT_ENABLED feature flags |…
Browse files Browse the repository at this point in the history
… wrap JWT enforcement with feature flag

- Move tenant id handling inside feature-flags-http.go so that we can mock it in unit tests
  • Loading branch information
hmmachadocx committed Jul 25, 2023
1 parent 5913004 commit 2e202ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
14 changes: 13 additions & 1 deletion internal/wrappers/feature-flags-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wrappers
import (
"encoding/json"
"net/http"
"strings"

"github.com/spf13/viper"

Expand All @@ -19,7 +20,18 @@ func NewFeatureFlagsHTTPWrapper(path string) FeatureFlagsWrapper {
}
}

func (f FeatureFlagsHTTPWrapper) GetAll(tenantID string) (*FeatureFlagsResponseModel, error) {
func (f FeatureFlagsHTTPWrapper) GetAll() (*FeatureFlagsResponseModel, error) {
accessToken, tokenError := GetAccessToken()
if tokenError != nil {
return nil, tokenError
}

tenantIDFromClaims, extractError := extractFromTokenClaims(accessToken, tenantIDClaimKey)
if extractError != nil {
return nil, extractError
}

tenantID := strings.Split(tenantIDFromClaims, "::")[1]
clientTimeout := viper.GetUint(commonParams.ClientTimeoutKey)
params := map[string]string{
"filter": tenantID,
Expand Down
17 changes: 2 additions & 15 deletions internal/wrappers/feature-flags.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package wrappers

import (
"strings"

"github.com/checkmarx/ast-cli/internal/logger"
)

Expand All @@ -24,18 +22,7 @@ var FeatureFlagsBaseMap = []CommandFlags{
var FeatureFlags = map[string]bool{}

func HandleFeatureFlags(featureFlagsWrapper FeatureFlagsWrapper) error {
accessToken, tokenError := GetAccessToken()
if tokenError != nil {
return tokenError
}

tenantIDFromClaims, extractError := extractFromTokenClaims(accessToken, tenantIDClaimKey)
if extractError != nil {
return extractError
}

tenantID := strings.Split(tenantIDFromClaims, "::")[1]
allFlags, err := featureFlagsWrapper.GetAll(tenantID)
allFlags, err := featureFlagsWrapper.GetAll()
if err != nil {
loadFeatureFlagsDefaultValues()

Expand Down Expand Up @@ -74,7 +61,7 @@ func loadFeatureFlagsDefaultValues() {
}

type FeatureFlagsWrapper interface {
GetAll(tenantID string) (*FeatureFlagsResponseModel, error)
GetAll() (*FeatureFlagsResponseModel, error)
}

type FeatureFlagsResponseModel []struct {
Expand Down
9 changes: 1 addition & 8 deletions internal/wrappers/mock/feature-flags-mock.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package mock

import (
"strings"

"github.com/checkmarx/ast-cli/internal/wrappers"
)

type FeatureFlagsMockWrapper struct{}

func (f FeatureFlagsMockWrapper) GetAll(string) (*wrappers.FeatureFlagsResponseModel, error) {
allowedEngines := make(map[string]bool)
engines := []string{"sast", "iac-security", "sca", "api-security"}
for _, value := range engines {
allowedEngines[strings.ToLower(value)] = true
}
func (f FeatureFlagsMockWrapper) GetAll() (*wrappers.FeatureFlagsResponseModel, error) {
return &wrappers.FeatureFlagsResponseModel{}, nil
}

0 comments on commit 2e202ab

Please sign in to comment.