Skip to content

Commit 2a53b12

Browse files
committed
Merge remote-tracking branch 'upstream/dev'
2 parents 4d330e6 + df50c7a commit 2a53b12

33 files changed

+891
-1623
lines changed

artifactory/commands/transferfiles/transfer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ func TestSignalStopError(t *testing.T) {
8787
}
8888

8989
const (
90-
//#nosec G101 - Dummy token for tests.
90+
//#nosec G101 jfrog-ignore - Dummy token for tests.
9191
firstUuidTokenForTest = "347cd3e9-86b6-4bec-9be9-e053a485f327"
92-
//#nosec G101 - Dummy token for tests.
92+
//#nosec G101 jfrog-ignore - Dummy token for tests.
9393
secondUuidTokenForTest = "af14706e-e0c1-4b7d-8791-6a18bd1fd339"
94-
//#nosec G101 - Dummy token for tests.
94+
//#nosec G101 jfrog-ignore - Dummy token for tests.
9595
nodeIdForTest = "nodea0gwihu76sk5g-artifactory-primary-0"
9696
)
9797

artifactory/utils/container/image.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package container
33
import (
44
"net/http"
55
"path"
6+
"strconv"
67
"strings"
78

89
"github.com/jfrog/jfrog-client-go/artifactory"
@@ -152,6 +153,9 @@ func (image *Image) GetRemoteRepo(serviceManager artifactory.ArtifactoryServices
152153
if err != nil {
153154
return "", err
154155
}
156+
if resp.StatusCode == http.StatusForbidden {
157+
return "", errorutils.CheckErrorf(getStatusForbiddenErrorMessage())
158+
}
155159
if resp.StatusCode != http.StatusOK {
156160
return "", errorutils.CheckErrorf("error while getting docker repository name. Artifactory response: " + resp.Status)
157161
}
@@ -169,3 +173,10 @@ func buildRequestUrl(longImageName, imageTag, containerRegistryUrl string, https
169173
}
170174
return "http://" + endpoint
171175
}
176+
177+
func getStatusForbiddenErrorMessage() string {
178+
errorMessage := "error while getting docker repository name. Artifactory response: " + strconv.Itoa(http.StatusForbidden) +
179+
", Possible causes include: \n- Xray scan in progress \n- Xray policy violations \n- insufficient permissions\n- invalid authentication method\n- disabled anonymous access\n- missing Docker manifests." +
180+
"\nPlease verify the above factors to resolve the issue."
181+
return errorMessage
182+
}

common/cliutils/spec.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
speccore "github.com/jfrog/jfrog-cli-core/v2/common/spec"
55
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
66
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
7+
"strconv"
78
"strings"
89
)
910

@@ -62,7 +63,12 @@ func OverrideFieldsIfSet(spec *speccore.File, c *components.Context) {
6263
// If `fieldName` exist in the cli args, read it to `field` as a string.
6364
func overrideStringIfSet(field *string, c *components.Context, fieldName string) {
6465
if c.IsFlagSet(fieldName) {
65-
*field = c.GetStringFlagValue(fieldName)
66+
stringFlag := c.GetStringFlagValue(fieldName)
67+
if stringFlag != "" {
68+
*field = stringFlag
69+
return
70+
}
71+
*field = strconv.FormatBool(c.GetBoolFlagValue(fieldName))
6672
}
6773
}
6874

common/cliutils/utils.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package cliutils
33
import (
44
"errors"
55
"fmt"
6+
"github.com/jfrog/jfrog-cli-core/v2/common/project"
67
"io"
78
"os"
9+
"path/filepath"
810
"strconv"
911
"strings"
1012

@@ -18,6 +20,13 @@ import (
1820
"github.com/jfrog/jfrog-client-go/utils/log"
1921
)
2022

23+
const (
24+
JfConfigDirName = ".jfrog"
25+
JfConfigFileName = "config.yml"
26+
ApplicationRootYML = "application"
27+
Key = "key"
28+
)
29+
2130
func FixWinPathBySource(path string, fromSpec bool) string {
2231
if strings.Count(path, "/") > 0 {
2332
// Assuming forward slashes - not doubling backslash to allow regexp escaping
@@ -251,3 +260,39 @@ func FixWinPathsForFileSystemSourcedCmds(uploadSpec *spec.SpecFiles, specFlag, e
251260
}
252261
}
253262
}
263+
264+
// Retrieves the application key from the .jfrog/config file or the environment variable.
265+
// If the application key is not found in either, returns an empty string.
266+
func ReadJFrogApplicationKeyFromConfigOrEnv() (applicationKeyValue string) {
267+
applicationKeyValue = getApplicationKeyFromConfig()
268+
if applicationKeyValue != "" {
269+
log.Debug("Found application key in config file:", applicationKeyValue)
270+
return
271+
}
272+
applicationKeyValue = os.Getenv(coreutils.ApplicationKey)
273+
if applicationKeyValue != "" {
274+
log.Debug("Found application key in environment variable:", applicationKeyValue)
275+
return
276+
}
277+
log.Debug("Application key is not found in the config file or environment variable.")
278+
return ""
279+
}
280+
281+
func getApplicationKeyFromConfig() string {
282+
configFilePath := filepath.Join(JfConfigDirName, JfConfigFileName)
283+
vConfig, err := project.ReadConfigFile(configFilePath, project.YAML)
284+
if err != nil {
285+
log.Debug("error reading config file: %v", err)
286+
return ""
287+
}
288+
289+
application := vConfig.GetStringMapString(ApplicationRootYML)
290+
applicationKey, ok := application[Key]
291+
if !ok {
292+
log.Debug("Application key is not found in the config file.")
293+
return ""
294+
}
295+
296+
log.Debug("Found application key:", applicationKey)
297+
return applicationKey
298+
}

common/cliutils/utils_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cliutils
2+
3+
import (
4+
testUtils "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestReadJFrogApplicationKeyFromConfigOrEnv(t *testing.T) {
14+
configFilePath := filepath.Join(JfConfigDirName, JfConfigFileName)
15+
16+
// Test cases
17+
tests := []struct {
18+
name string
19+
configContent string
20+
envValue string
21+
expectedResult string
22+
}{
23+
{
24+
name: "Application key in config file",
25+
configContent: "application:\n key: configKey",
26+
envValue: "",
27+
expectedResult: "configKey",
28+
},
29+
{
30+
name: "Application key in environment variable",
31+
configContent: "",
32+
envValue: "envKey",
33+
expectedResult: "envKey",
34+
},
35+
{
36+
name: "Application key in both config file and environment variable",
37+
configContent: "application:\n key: configKey",
38+
envValue: "envKey",
39+
expectedResult: "configKey",
40+
},
41+
{
42+
name: "No application key in config file or environment variable",
43+
configContent: "",
44+
envValue: "",
45+
expectedResult: "",
46+
},
47+
}
48+
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
// Setup temp dir for each test
52+
testDirPath, err := filepath.Abs(filepath.Join("../", "tests", "applicationConfigTestDir"))
53+
assert.NoError(t, err)
54+
_, cleanUp := testUtils.CreateTestWorkspace(t, testDirPath)
55+
56+
// Write config content to file
57+
if tt.configContent != "" {
58+
err = os.WriteFile(configFilePath, []byte(tt.configContent), 0644)
59+
assert.NoError(t, err)
60+
}
61+
62+
// Set environment variable
63+
if tt.envValue != "" {
64+
assert.NoError(t, os.Setenv(coreutils.ApplicationKey, tt.envValue))
65+
} else {
66+
assert.NoError(t, os.Unsetenv(coreutils.ApplicationKey))
67+
}
68+
69+
// Call the function
70+
result := ReadJFrogApplicationKeyFromConfigOrEnv()
71+
72+
// Assert the result
73+
assert.Equal(t, tt.expectedResult, result)
74+
// delete temp folder
75+
cleanUp()
76+
})
77+
}
78+
}

common/commands/command.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ func Exec(command Command) error {
3939
return err
4040
}
4141

42+
// ExecAndThenReportUsage runs the command and then triggers a usage report
43+
// Is used for commands which don't have the full server details before execution
44+
// For example: oidc exchange command, which will get access token only after execution.
45+
func ExecAndThenReportUsage(cc Command) (err error) {
46+
if err = cc.Run(); err != nil {
47+
return
48+
}
49+
reportUsage(cc, nil)
50+
return
51+
}
52+
4253
func reportUsage(command Command, channel chan<- bool) {
4354
// When the usage reporting is done, signal to the channel.
4455
defer signalReportUsageFinished(channel)

0 commit comments

Comments
 (0)