Skip to content

Commit 66cce26

Browse files
osaidwtdCopilot
andauthored
RTDEV-63004 - Fix key enter (#8)
* RTDEV-63004 - Fix key enter * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add debug output and test cases for key trimming behavior - Add debug logging to TestEnsureKeyExists_TrimsWhitespace to help understand trimming behavior differences - Add test cases to illustrate flag vs environment variable behavior and precedence - Provides visibility into input/output values and their lengths for debugging Addresses dortam888's request to debug the difference in trimming behavior. * RTDEV-63004 - Fix key enter * RTDEV-63004 - Fix key enter --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent e2aeae3 commit 66cce26

2 files changed

Lines changed: 121 additions & 11 deletions

File tree

evidence/cli/command_cli.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import (
77
"slices"
88
"strings"
99

10+
"github.com/jfrog/jfrog-cli-artifactory/evidence/cli/docs/create"
11+
"github.com/jfrog/jfrog-cli-artifactory/evidence/cli/docs/get"
12+
"github.com/jfrog/jfrog-cli-artifactory/evidence/cli/docs/verify"
13+
sonarhelper "github.com/jfrog/jfrog-cli-artifactory/evidence/sonar"
14+
evidenceUtils "github.com/jfrog/jfrog-cli-artifactory/evidence/utils"
1015
commonCliUtils "github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
1116
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
1217
pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common"
1318
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
1419
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
1520
coreUtils "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
16-
"github.com/jfrog/jfrog-cli-evidence/evidence/cli/docs/create"
17-
"github.com/jfrog/jfrog-cli-evidence/evidence/cli/docs/get"
18-
"github.com/jfrog/jfrog-cli-evidence/evidence/cli/docs/verify"
19-
sonarhelper "github.com/jfrog/jfrog-cli-evidence/evidence/sonar"
20-
evidenceUtils "github.com/jfrog/jfrog-cli-evidence/evidence/utils"
2121
"github.com/jfrog/jfrog-client-go/utils"
2222
"github.com/jfrog/jfrog-client-go/utils/errorutils"
2323
"github.com/jfrog/jfrog-client-go/utils/log"
@@ -207,7 +207,7 @@ func validateCreateEvidenceCommonContext(ctx *components.Context) error {
207207
}
208208
}
209209

210-
if err := ensureKeyExists(ctx, key); err != nil {
210+
if err := resolveAndNormalizeKey(ctx, key); err != nil {
211211
return err
212212
}
213213

@@ -240,15 +240,31 @@ func validateSigstoreBundleArgsConflicts(ctx *components.Context) error {
240240
return nil
241241
}
242242

243-
func ensureKeyExists(ctx *components.Context, key string) error {
243+
func resolveAndNormalizeKey(ctx *components.Context, key string) error {
244244
if assertValueProvided(ctx, key) == nil {
245+
// Trim whitespace and newlines from the flag value
246+
keyValue := ctx.GetStringFlagValue(key)
247+
log.Debug(fmt.Sprintf("Flag '%s' original value: %q (length: %d)", key, keyValue, len(keyValue)))
248+
249+
trimmedKeyValue := strings.TrimSpace(keyValue)
250+
log.Debug(fmt.Sprintf("Flag '%s' trimmed value: %q (length: %d)", key, trimmedKeyValue, len(trimmedKeyValue)))
251+
252+
// Always update the flag value with the trimmed version
253+
ctx.AddStringFlag(key, trimmedKeyValue)
245254
return nil
246255
}
247256

248257
signingKeyValue, _ := evidenceUtils.GetEnvVariable(coreUtils.SigningKey)
249258
if signingKeyValue == "" {
250259
return errorutils.CheckErrorf("JFROG_CLI_SIGNING_KEY env variable or --%s flag must be provided when creating evidence", key)
251260
}
261+
262+
log.Debug(fmt.Sprintf("Environment variable '%s' original value: %q (length: %d)", coreUtils.SigningKey, signingKeyValue, len(signingKeyValue)))
263+
264+
// Trim whitespace and newlines from the environment variable
265+
signingKeyValue = strings.TrimSpace(signingKeyValue)
266+
log.Debug(fmt.Sprintf("Environment variable '%s' trimmed value: %q (length: %d)", coreUtils.SigningKey, signingKeyValue, len(signingKeyValue)))
267+
252268
ctx.AddStringFlag(key, signingKeyValue)
253269
return nil
254270
}

evidence/cli/command_cli_test.go

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ func TestCreateEvidence_Context(t *testing.T) {
2020
assert.NoError(t, os.Setenv(coreUtils.SigningKey, "PGP"), "Failed to set env: "+coreUtils.SigningKey)
2121
assert.NoError(t, os.Setenv(coreUtils.BuildName, buildName), "Failed to set env: JFROG_CLI_BUILD_NAME")
2222
defer func() {
23-
assert.NoError(t, os.Unsetenv(coreUtils.SigningKey))
23+
assert.NoError(t, os.Unsetenv(coreUtils.SigningKey), "Failed to unset env: "+coreUtils.SigningKey)
2424
}()
2525
defer func() {
26-
assert.NoError(t, os.Unsetenv(coreUtils.BuildName))
26+
assert.NoError(t, os.Unsetenv(coreUtils.BuildName), "Failed to unset env: JFROG_CLI_BUILD_NAME")
2727
}()
2828

2929
app := cli.NewApp()
@@ -221,10 +221,10 @@ func TestVerifyEvidence_Context(t *testing.T) {
221221
assert.NoError(t, os.Setenv(coreUtils.SigningKey, "PGP"), "Failed to set env: "+coreUtils.SigningKey)
222222
assert.NoError(t, os.Setenv(coreUtils.BuildName, buildName), "Failed to set env: JFROG_CLI_BUILD_NAME")
223223
defer func() {
224-
assert.NoError(t, os.Unsetenv(coreUtils.SigningKey))
224+
assert.NoError(t, os.Unsetenv(coreUtils.SigningKey), "Failed to unset env: "+coreUtils.SigningKey)
225225
}()
226226
defer func() {
227-
assert.NoError(t, os.Unsetenv(coreUtils.BuildName))
227+
assert.NoError(t, os.Unsetenv(coreUtils.BuildName), "Failed to unset env: JFROG_CLI_BUILD_NAME")
228228
}()
229229

230230
app := cli.NewApp()
@@ -614,6 +614,100 @@ func setDefaultValue(flag string, defaultValue string) components.Flag {
614614
return f
615615
}
616616

617+
func TestResolveAndNormalizeKey_TrimsWhitespace(t *testing.T) {
618+
tests := []struct {
619+
name string
620+
envKeyValue string
621+
flagKeyValue string
622+
setFlag bool
623+
expectedResult string
624+
}{
625+
{
626+
name: "Env key with trailing newline",
627+
envKeyValue: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE\n-----END PRIVATE KEY-----\n",
628+
setFlag: false,
629+
expectedResult: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE\n-----END PRIVATE KEY-----",
630+
},
631+
{
632+
name: "Env key with trailing spaces and newlines",
633+
envKeyValue: " -----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE\n-----END PRIVATE KEY----- \n\n",
634+
setFlag: false,
635+
expectedResult: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE\n-----END PRIVATE KEY-----",
636+
},
637+
{
638+
name: "Flag key with trailing newline",
639+
flagKeyValue: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE\n-----END PRIVATE KEY-----\n",
640+
setFlag: true,
641+
expectedResult: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE\n-----END PRIVATE KEY-----",
642+
},
643+
{
644+
name: "Flag key with carriage return and newline",
645+
flagKeyValue: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE\n-----END PRIVATE KEY-----\r\n",
646+
setFlag: true,
647+
expectedResult: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQE\n-----END PRIVATE KEY-----",
648+
},
649+
{
650+
name: "Flag vs Env behavior difference - flag takes precedence and gets trimmed",
651+
envKeyValue: " env-key-value \n",
652+
flagKeyValue: " flag-key-value \n",
653+
setFlag: true,
654+
expectedResult: "flag-key-value",
655+
},
656+
{
657+
name: "Env fallback when no flag - env key gets trimmed",
658+
envKeyValue: " env-key-value \n",
659+
setFlag: false,
660+
expectedResult: "env-key-value",
661+
},
662+
}
663+
664+
for _, tt := range tests {
665+
t.Run(tt.name, func(t *testing.T) {
666+
// Setup
667+
if tt.envKeyValue != "" {
668+
assert.NoError(t, os.Setenv(coreUtils.SigningKey, tt.envKeyValue))
669+
defer func() {
670+
if err := os.Unsetenv(coreUtils.SigningKey); err != nil {
671+
t.Errorf("failed to unset env %q: %v", coreUtils.SigningKey, err)
672+
}
673+
}()
674+
}
675+
676+
// Create context
677+
app := cli.NewApp()
678+
set := flag.NewFlagSet("test", 0)
679+
cliCtx := cli.NewContext(app, set, nil)
680+
681+
var flags []components.Flag
682+
if tt.setFlag {
683+
flags = append(flags, setDefaultValue(key, tt.flagKeyValue))
684+
}
685+
flags = append(flags, setDefaultValue(predicate, "test"))
686+
687+
ctx, err := components.ConvertContext(cliCtx, flags...)
688+
assert.NoError(t, err)
689+
690+
// Execute
691+
err = resolveAndNormalizeKey(ctx, key)
692+
assert.NoError(t, err)
693+
694+
// Verify
695+
actualValue := ctx.GetStringFlagValue(key)
696+
697+
// Debug output to help understand the difference in trimming behavior
698+
if tt.setFlag {
699+
t.Logf("Flag input: %q (len=%d)", tt.flagKeyValue, len(tt.flagKeyValue))
700+
} else {
701+
t.Logf("Env input: %q (len=%d)", tt.envKeyValue, len(tt.envKeyValue))
702+
}
703+
t.Logf("Expected: %q (len=%d)", tt.expectedResult, len(tt.expectedResult))
704+
t.Logf("Actual: %q (len=%d)", actualValue, len(actualValue))
705+
706+
assert.Equal(t, tt.expectedResult, actualValue)
707+
})
708+
}
709+
}
710+
617711
func TestValidateSonarQubeRequirements(t *testing.T) {
618712
// Save original environment variables
619713
originalSonarToken := os.Getenv("SONAR_TOKEN")

0 commit comments

Comments
 (0)