diff --git a/.changelog/pr-171.txt b/.changelog/pr-171.txt new file mode 100644 index 00000000..52d302f1 --- /dev/null +++ b/.changelog/pr-171.txt @@ -0,0 +1,3 @@ +```release-note:bug +Add missing PingOne Authentication Environment ID option to request command +``` diff --git a/cmd/request/request.go b/cmd/request/request.go index 1b843b9f..35a73603 100644 --- a/cmd/request/request.go +++ b/cmd/request/request.go @@ -82,6 +82,7 @@ The command offers a cURL-like experience to interact with the Ping platform ser } func initPingOneRequestFlags(cmd *cobra.Command) { + cmd.Flags().AddFlag(options.PingOneAuthenticationAPIEnvironmentIDOption.Flag) cmd.Flags().AddFlag(options.PingOneAuthenticationWorkerEnvironmentIDOption.Flag) cmd.Flags().AddFlag(options.PingOneAuthenticationWorkerClientIDOption.Flag) cmd.Flags().AddFlag(options.PingOneAuthenticationWorkerClientSecretOption.Flag) diff --git a/cmd/request/request_test.go b/cmd/request/request_test.go index 616f6a45..b7841bb8 100644 --- a/cmd/request/request_test.go +++ b/cmd/request/request_test.go @@ -8,12 +8,15 @@ import ( "io" "os" "regexp" + "strings" "testing" "github.com/pingidentity/pingcli/cmd/common" + auth_internal "github.com/pingidentity/pingcli/internal/commands/auth" request_internal "github.com/pingidentity/pingcli/internal/commands/request" "github.com/pingidentity/pingcli/internal/configuration/options" "github.com/pingidentity/pingcli/internal/customtypes" + "github.com/pingidentity/pingcli/internal/profiles" "github.com/pingidentity/pingcli/internal/testing/testutils_cobra" "github.com/pingidentity/pingcli/internal/testing/testutils_koanf" "github.com/stretchr/testify/assert" @@ -108,6 +111,10 @@ func Test_RequestCommand_Validation(t *testing.T) { t.Run(tc.name, func(t *testing.T) { testutils_koanf.InitKoanfs(t) + if tc.name == "Happy Path - with header" && (os.Getenv("CI") != "" || os.Getenv("GITHUB_ACTIONS") != "") { + t.Skip("Skipping live request test in CI environment due to keychain warning") + } + err := testutils_cobra.ExecutePingcli(t, append([]string{"request"}, tc.args...)...) if !tc.expectErr { @@ -131,6 +138,7 @@ func Test_RequestCommand_Validation(t *testing.T) { // making a real API call and validating the JSON output. func Test_RequestCommand_E2E(t *testing.T) { testutils_koanf.InitKoanfs(t) + t.Setenv(options.AuthStorageOption.EnvVar, customtypes.ENUM_STORAGE_TYPE_NONE) originalStdout := os.Stdout r, w, err := os.Pipe() @@ -160,3 +168,32 @@ func Test_RequestCommand_E2E(t *testing.T) { assert.NotEmpty(t, bodyJSON, "Response JSON body is empty") assert.True(t, json.Valid(bodyJSON), "Output JSON is not valid") } + +func Test_RequestCommand_HelpIncludesPingOneEnvironmentFlag(t *testing.T) { + testutils_koanf.InitKoanfs(t) + + output, err := testutils_cobra.ExecutePingcliCaptureCobraOutput(t, "request", "--help") + require.NoError(t, err) + assert.Contains(t, output, "--"+options.PingOneAuthenticationAPIEnvironmentIDOption.CobraParamName) +} + +func Test_RequestCommand_ClientCredentialsEnvironmentErrorUsesSupportedConfigKey(t *testing.T) { + testutils_koanf.InitKoanfs(t) + + t.Setenv(options.PingOneAuthenticationAPIEnvironmentIDOption.EnvVar, "") + t.Setenv(options.PingOneAuthenticationClientCredentialsClientIDOption.EnvVar, "00000000-0000-0000-0000-000000000001") + t.Setenv(options.PingOneAuthenticationClientCredentialsClientSecretOption.EnvVar, "test-secret") + + koanfCfg, err := profiles.GetKoanfConfig() + require.NoError(t, err) + require.NoError(t, koanfCfg.KoanfInstance().Set("default."+options.PingOneAuthenticationAPIEnvironmentIDOption.KoanfKey, "")) + + message := auth_internal.ErrClientCredentialsEnvironmentIDNotConfigured.Error() + assert.Contains(t, message, "service.pingOne.authentication.environmentID") + assert.NotContains(t, message, "service.pingone.authentication.clientCredentials.environmentID") + + _, err = auth_internal.GetClientCredentialsConfiguration() + require.Error(t, err) + assert.ErrorContains(t, err, "service.pingOne.authentication.environmentID") + assert.False(t, strings.Contains(err.Error(), "service.pingone.")) +} diff --git a/internal/commands/auth/errors.go b/internal/commands/auth/errors.go index 7f6781eb..af6e5dae 100644 --- a/internal/commands/auth/errors.go +++ b/internal/commands/auth/errors.go @@ -13,31 +13,31 @@ var ( ErrUnsupportedAuthMethod = errors.New("unsupported grant type") ErrTokenKeyGenerationRequirements = errors.New("environment ID and client ID are required for token key generation") ErrGrantTypeNotSet = errors.New("configuration does not have grant type set") - ErrRegionCodeRequired = errors.New("region code is required and must be valid. Please run 'pingcli config set service.pingone.regionCode='") - ErrEnvironmentIDNotConfigured = errors.New("environment ID is not configured. Please run 'pingcli config set service.pingone.authentication.environmentID='") + ErrRegionCodeRequired = errors.New("region code is required and must be valid. Please run 'pingcli config set service.pingOne.regionCode='") + ErrEnvironmentIDNotConfigured = errors.New("environment ID is not configured. Please run 'pingcli config set service.pingOne.authentication.environmentID='") ErrTokenStorageDisabled = errors.New("token storage is disabled") ErrInvalidAuthMethod = errors.New("invalid authentication method flag provided") // Device code errors - ErrDeviceCodeClientIDNotConfigured = errors.New("device code client ID is not configured. Please run 'pingcli config set service.pingone.authentication.deviceCode.clientID='") - ErrDeviceCodeEnvironmentIDNotConfigured = errors.New("device code environment ID is not configured. Please run 'pingcli config set service.pingone.authentication.deviceCode.environmentID='") + ErrDeviceCodeClientIDNotConfigured = errors.New("device code client ID is not configured. Please run 'pingcli config set service.pingOne.authentication.deviceCode.clientID='") + ErrDeviceCodeEnvironmentIDNotConfigured = errors.New("device code environment ID is not configured. Please run 'pingcli config set service.pingOne.authentication.environmentID='") // Auth code errors - ErrAuthorizationCodeClientIDNotConfigured = errors.New("authorization code client ID is not configured. Please run 'pingcli config set service.pingone.authentication.authorizationCode.clientID='") - ErrAuthorizationCodeEnvironmentIDNotConfigured = errors.New("authorization code environment ID is not configured. Please run 'pingcli config set service.pingone.authentication.authorizationCode.environmentID='") - ErrAuthorizationCodeRedirectURINotConfigured = errors.New("authorization code redirect URI is not configured. Please run 'pingcli config set service.pingone.authentication.authorizationCode.redirectURI='") - ErrAuthorizationCodeRedirectURIPathNotConfigured = errors.New("authorization code redirect URI path is not configured. Please run 'pingcli config set service.pingone.authentication.authorizationCode.redirectURIPath='") - ErrAuthorizationCodeRedirectURIPortNotConfigured = errors.New("authorization code redirect URI port is not configured. Please run 'pingcli config set service.pingone.authentication.authorizationCode.redirectURIPort='") + ErrAuthorizationCodeClientIDNotConfigured = errors.New("authorization code client ID is not configured. Please run 'pingcli config set service.pingOne.authentication.authorizationCode.clientID='") + ErrAuthorizationCodeEnvironmentIDNotConfigured = errors.New("authorization code environment ID is not configured. Please run 'pingcli config set service.pingOne.authentication.environmentID='") + ErrAuthorizationCodeRedirectURINotConfigured = errors.New("authorization code redirect URI is not configured. Please run 'pingcli config set service.pingOne.authentication.authorizationCode.redirectURI='") + ErrAuthorizationCodeRedirectURIPathNotConfigured = errors.New("authorization code redirect URI path is not configured. Please run 'pingcli config set service.pingOne.authentication.authorizationCode.redirectURIPath='") + ErrAuthorizationCodeRedirectURIPortNotConfigured = errors.New("authorization code redirect URI port is not configured. Please run 'pingcli config set service.pingOne.authentication.authorizationCode.redirectURIPort='") // Client credentials errors - ErrClientCredentialsClientIDNotConfigured = errors.New("client credentials client ID is not configured. Please run 'pingcli config set service.pingone.authentication.clientCredentials.clientID='") - ErrClientCredentialsClientSecretNotConfigured = errors.New("client credentials client secret is not configured. Please run 'pingcli config set service.pingone.authentication.clientCredentials.clientSecret='") - ErrClientCredentialsEnvironmentIDNotConfigured = errors.New("client credentials environment ID is not configured. Please run 'pingcli config set service.pingone.authentication.clientCredentials.environmentID='") + ErrClientCredentialsClientIDNotConfigured = errors.New("client credentials client ID is not configured. Please run 'pingcli config set service.pingOne.authentication.clientCredentials.clientID='") + ErrClientCredentialsClientSecretNotConfigured = errors.New("client credentials client secret is not configured. Please run 'pingcli config set service.pingOne.authentication.clientCredentials.clientSecret='") + ErrClientCredentialsEnvironmentIDNotConfigured = errors.New("client credentials environment ID is not configured. Please run 'pingcli config set service.pingOne.authentication.environmentID='") // Worker errors - ErrWorkerClientIDNotConfigured = errors.New("worker client ID is not configured. Please run 'pingcli config set service.pingone.authentication.worker.clientID='") - ErrWorkerClientSecretNotConfigured = errors.New("worker client secret is not configured. Please run 'pingcli config set service.pingone.authentication.worker.clientSecret='") - ErrWorkerEnvironmentIDNotConfigured = errors.New("worker environment ID is not configured. Please run 'pingcli config set service.pingone.authentication.worker.environmentID='") + ErrWorkerClientIDNotConfigured = errors.New("worker client ID is not configured. Please run 'pingcli config set service.pingOne.authentication.worker.clientID='") + ErrWorkerClientSecretNotConfigured = errors.New("worker client secret is not configured. Please run 'pingcli config set service.pingOne.authentication.worker.clientSecret='") + ErrWorkerEnvironmentIDNotConfigured = errors.New("worker environment ID is not configured. Please run 'pingcli config set service.pingOne.authentication.worker.environmentID='") // PingFederate errors ErrPingFederateContextNil = errors.New("failed to initialize PingFederate services. context is nil") diff --git a/internal/testing/testutils_cobra/cobra_utils.go b/internal/testing/testutils_cobra/cobra_utils.go index 5fe4916f..51f577c6 100644 --- a/internal/testing/testutils_cobra/cobra_utils.go +++ b/internal/testing/testutils_cobra/cobra_utils.go @@ -57,5 +57,7 @@ func ExecutePingcliCaptureCobraOutput(t *testing.T, args ...string) (output stri root.SetOut(&stdout) root.SetErr(&stdout) - return stdout.String(), root.Execute() + err = root.Execute() + + return stdout.String(), err }