From 7c06390e181a7fc7872e6ea854149fe2b3b6f24e Mon Sep 17 00:00:00 2001 From: Luke Watts Date: Tue, 26 Nov 2024 16:29:18 +0100 Subject: [PATCH] feat(auth): auto detect API Url during OAuth authentication --- cliv2/cmd/cliv2/logheader.go | 16 ++++++---------- cliv2/cmd/cliv2/main.go | 15 +++++++++++++++ cliv2/go.mod | 2 +- cliv2/go.sum | 4 ++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/cliv2/cmd/cliv2/logheader.go b/cliv2/cmd/cliv2/logheader.go index 1bbcbdae4a..ba6f45ff83 100644 --- a/cliv2/cmd/cliv2/logheader.go +++ b/cliv2/cmd/cliv2/logheader.go @@ -11,6 +11,8 @@ import ( "regexp" "strings" + "github.com/snyk/go-application-framework/pkg/local_workflows/config_utils" + "github.com/snyk/go-application-framework/pkg/auth" "github.com/snyk/go-application-framework/pkg/configuration" localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows" @@ -81,8 +83,6 @@ func getFipsStatus(config configuration.Configuration) string { } func writeLogHeader(config configuration.Configuration, networkAccess networking.NetworkAccess) { - keys := []string{configuration.API_URL, configuration.AUTHENTICATION_TOKEN, configuration.AUTHENTICATION_BEARER_TOKEN, configuration.ORGANIZATION} - authorization, _, userAgent := logHeaderAuthorizationInfo(config, networkAccess) org := config.GetString(configuration.ORGANIZATION) @@ -127,16 +127,12 @@ func writeLogHeader(config configuration.Configuration, networkAccess networking tablePrint(" fips", fipsEnabled) tablePrint("Checks", "") - checkCount := 0 - for _, key := range keys { - keysSpecified := config.GetAllKeysThatContainValues(key) - if len(keysSpecified) > 1 { - checkCount++ - tablePrint(" Configuration", fmt.Sprintf("Possible unexpected behavior, the following configuration values might override each other %s", strings.ToUpper(strings.Join(keysSpecified, ", ")))) - } + sanityCheckResults := config_utils.CheckSanity(config) + for _, result := range sanityCheckResults { + tablePrint(" Configuration", result.Description) } - if checkCount == 0 { + if len(sanityCheckResults) == 0 { tablePrint(" Configuration", "all good") } } diff --git a/cliv2/cmd/cliv2/main.go b/cliv2/cmd/cliv2/main.go index 5dda6f9fd7..c66705a017 100644 --- a/cliv2/cmd/cliv2/main.go +++ b/cliv2/cmd/cliv2/main.go @@ -232,6 +232,8 @@ func getErrorFromWorkFlowData(engine workflow.Engine, data []workflow.Data) erro func sendAnalytics(analytics analytics.Analytics, debugLogger *zerolog.Logger) { debugLogger.Print("Sending Analytics") + analytics.SetApiUrl(globalConfiguration.GetString(configuration.API_URL)) + res, err := analytics.Send() if err != nil { debugLogger.Err(err).Msg("Failed to send Analytics") @@ -312,6 +314,17 @@ func runCodeTestCommand(cmd *cobra.Command, args []string) error { return runCommand(cmd, args) } +func runAuthCommand(cmd *cobra.Command, args []string) error { + err := runCommand(cmd, args) + + reloadError := globalConfiguration.ReloadConfig() + if reloadError != nil { + globalLogger.Err(reloadError).Msg("Failed to reload the configuration after authentication.") + } + + return err +} + func getGlobalFLags() *pflag.FlagSet { globalConfigurationOptions := workflow.GetGlobalConfiguration() globalFLags := workflow.FlagsetFromConfigurationOptions(globalConfigurationOptions) @@ -374,6 +387,8 @@ func createCommandsForWorkflows(rootCommand *cobra.Command, engine workflow.Engi // use the special run command to ensure that the non-standard behavior of the command can be kept parentCommand.RunE = runCodeTestCommand + } else if currentCommandString == "auth" { + parentCommand.RunE = runAuthCommand } } } diff --git a/cliv2/go.mod b/cliv2/go.mod index c3514f0bcf..6b568a742b 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -17,7 +17,7 @@ require ( github.com/snyk/cli-extension-sbom v0.0.0-20241016065306-0df2be5b3b8f github.com/snyk/container-cli v0.0.0-20240821111304-7ca1c415a5d7 github.com/snyk/error-catalog-golang-public v0.0.0-20241030160523-0aa643bb7069 - github.com/snyk/go-application-framework v0.0.0-20241128111948-07c460117921 + github.com/snyk/go-application-framework v0.0.0-20241129161927-d5da1e7b3eb6 github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 github.com/snyk/snyk-iac-capture v0.6.5 github.com/snyk/snyk-ls v0.0.0-20241128161444-157e462bd309 diff --git a/cliv2/go.sum b/cliv2/go.sum index 0d26db83aa..03c4278def 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -760,8 +760,8 @@ github.com/snyk/container-cli v0.0.0-20240821111304-7ca1c415a5d7 h1:Zn5BcV76oFAb github.com/snyk/container-cli v0.0.0-20240821111304-7ca1c415a5d7/go.mod h1:38w+dcAQp9eG3P5t2eNS9eG0reut10AeJjLv5lJ5lpM= github.com/snyk/error-catalog-golang-public v0.0.0-20241030160523-0aa643bb7069 h1:Oj/BJAEMEuBjTAQ72UYB4tR0IZKOB2ZtdDnAnJDL1BM= github.com/snyk/error-catalog-golang-public v0.0.0-20241030160523-0aa643bb7069/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4= -github.com/snyk/go-application-framework v0.0.0-20241128111948-07c460117921 h1:9rBEB8Zfcgr3n8/1owhcrS+K3Ry+1XOcj7xlGojvr2s= -github.com/snyk/go-application-framework v0.0.0-20241128111948-07c460117921/go.mod h1:GekIT38VeZQiDw/Ot9Qn0s2kxVR1TXMw6gcaaAiKsVg= +github.com/snyk/go-application-framework v0.0.0-20241129161927-d5da1e7b3eb6 h1:hCqIX39kxgNlP3emPSvUQzT1vMm7QIQxkg926IA2mGs= +github.com/snyk/go-application-framework v0.0.0-20241129161927-d5da1e7b3eb6/go.mod h1:GekIT38VeZQiDw/Ot9Qn0s2kxVR1TXMw6gcaaAiKsVg= github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 h1:CEQuYv0Go6MEyRCD3YjLYM2u3Oxkx8GpCpFBd4rUTUk= github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg= github.com/snyk/policy-engine v0.31.3 h1:FepCg6QN/X8uvxYjF+WwB2aiBPJB+NENDgKQeI/FwLg=