Skip to content

Commit 359aaa4

Browse files
authored
Fix access tests (#2944)
1 parent a50d0cd commit 359aaa4

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

access_test.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -282,32 +282,20 @@ func TestOidcExchangeToken(t *testing.T) {
282282
name string
283283
args []string
284284
expectedOutput string
285-
expectError bool
286285
}{
287286
{
288287
name: "Successful exchange",
289-
args: []string{"eot", "--url=https://ecosysjfrog.jfrog.io", "--oidc-provider-name=setup-jfrog-cli-test"},
290-
expectedOutput: `\{ AccessToken: \*\*\*\* Username: \*\*\*\* \}\n`,
291-
expectError: false,
292-
},
293-
{
294-
name: "Missing OIDC provider name",
295-
args: []string{"eot", "--url=https://ecosysjfrog.jfrog.io"},
296-
expectedOutput: "Error: --oidc-provider-name is required",
297-
expectError: true,
288+
args: []string{"eot", "setup-jfrog-cli-test", "--url=https://ecosysjfrog.jfrog.io"},
289+
expectedOutput: `\{ AccessToken: [^\s]+ Username: [^\s]+ \}`,
298290
},
299291
}
300292

301293
for _, testCase := range testCases {
302294
t.Run(testCase.name, func(t *testing.T) {
303295
output := accessCli.RunCliCmdWithOutput(t, testCase.args...)
304-
if testCase.expectError {
305-
assert.Contains(t, output, testCase.expectedOutput)
306-
} else {
307-
matched, err := regexp.MatchString(testCase.expectedOutput, output)
308-
assert.NoError(t, err)
309-
assert.True(t, matched, "Output did not match expected pattern")
310-
}
296+
matched, err := regexp.MatchString(testCase.expectedOutput, output)
297+
assert.NoError(t, err)
298+
assert.True(t, matched, "Output did not match expected pattern")
311299
})
312300
}
313301
}

docs/general/oidc/help.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package token
22

3-
var Usage = []string{"eot <platformUrl> <oidc-provider-name> <oidc-token-id> [--oidc-audience <audience>] [--oidc-provider-type <type>] [--application-key <key>] [--Project <project>] [--repository <repository>]"}
3+
var Usage = []string{"eot <oidc-provider-name> <oidc-token-id> [--platformUrl <platformUrl>] [--oidc-audience <audience>] [--oidc-provider-type <type>] [--application-key <key>] [--Project <project>] [--repository <repository>]"}
44

55
func GetDescription() string {
66
return `Exchanges a token ID from an OIDC provider with a JFrog server to a valid access token and returns the access token and the username.`
77
}
88

99
func GetArguments() string {
10-
return ` --platformUrl (mandatory) The JFrog platform base URL to which the OIDC token will be exchanged for an access token (e.g., https://mycompany.jfrog.io)."
10+
return `
1111
1212
--oidc-provider-name (mandatory)
1313
The provider name.

general/token/cli.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func AccessTokenCreateCmd(c *cli.Context) error {
7070
}
7171

7272
func ExchangeOidcTokenCmd(c *cli.Context) error {
73-
if c.NArg() > 3 {
73+
if c.NArg() < 1 {
7474
return cliutils.WrongNumberOfArgumentsHandler(c)
7575
}
7676

@@ -99,11 +99,12 @@ func CreateOidcTokenExchangeCommand(c *cli.Context, serverDetails *coreConfig.Se
9999
if err := oidcAccessTokenCreateCmd.SetProviderTypeAsString(cliutils.GetFlagOrEnvValue(c, cliutils.OidcProviderType, coreutils.OidcProviderType)); err != nil {
100100
return nil, err
101101
}
102+
102103
oidcAccessTokenCreateCmd.
103104
SetServerDetails(serverDetails).
104105
// Mandatory flags
105-
SetProviderName(c.String(cliutils.OidcProviderName)).
106-
SetOidcTokenID(cliutils.GetFlagOrEnvValue(c, cliutils.OidcTokenID, coreutils.OidcExchangeTokenId)).
106+
SetProviderName(c.Args().Get(0)).
107+
SetOidcTokenID(getOidcTokenIdInput(c)).
107108
SetAudience(c.String(cliutils.OidcAudience)).
108109
// Optional values exported by CI servers
109110
SetJobId(os.Getenv(coreutils.CIJobID)).
@@ -159,3 +160,14 @@ func assertAccessTokenAvailable(serverDetails *coreConfig.ServerDetails) error {
159160
}
160161
return nil
161162
}
163+
164+
// The OIDC token ID can be provided as a command line argument or as a flag or environment variable.
165+
// Depends on the origin of the command.
166+
// For example when used in CI/CD, the token ID is provided as a environment variable.
167+
func getOidcTokenIdInput(c *cli.Context) string {
168+
oidcTokenId := c.Args().Get(1)
169+
if oidcTokenId == "" {
170+
oidcTokenId = cliutils.GetFlagOrEnvValue(c, cliutils.OidcTokenID, coreutils.OidcExchangeTokenId)
171+
}
172+
return oidcTokenId
173+
}

0 commit comments

Comments
 (0)