Skip to content

Commit 18be369

Browse files
authored
NuGet - Allow to set allowInsecureConnections package source attribute (#2758)
1 parent 6fa5830 commit 18be369

File tree

7 files changed

+144
-17
lines changed

7 files changed

+144
-17
lines changed

buildtools/cli.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,18 @@ func NugetCmd(c *cli.Context) error {
566566
return err
567567
}
568568

569+
allowInsecureConnection, err := cliutils.ExtractBoolFlagFromArgs(&filteredNugetArgs, "allow-insecure-connections")
570+
if err != nil {
571+
return err
572+
}
573+
569574
nugetCmd := dotnet.NewNugetCommand()
570-
nugetCmd.SetServerDetails(rtDetails).SetRepoName(targetRepo).SetBuildConfiguration(buildConfiguration).
571-
SetBasicCommand(filteredNugetArgs[0]).SetUseNugetV2(useNugetV2)
575+
nugetCmd.SetServerDetails(rtDetails).
576+
SetRepoName(targetRepo).
577+
SetBuildConfiguration(buildConfiguration).
578+
SetBasicCommand(filteredNugetArgs[0]).
579+
SetUseNugetV2(useNugetV2).
580+
SetAllowInsecureConnections(allowInsecureConnection)
572581
// Since we are using the values of the command's arguments and flags along the buildInfo collection process,
573582
// we want to separate the actual NuGet basic command (restore/build...) from the arguments and flags
574583
if len(filteredNugetArgs) > 1 {
@@ -604,10 +613,15 @@ func DotnetCmd(c *cli.Context) error {
604613
return err
605614
}
606615

616+
allowInsecureConnection, err := cliutils.ExtractBoolFlagFromArgs(&filteredDotnetArgs, "allow-insecure-connections")
617+
if err != nil {
618+
return err
619+
}
620+
607621
// Run command.
608622
dotnetCmd := dotnet.NewDotnetCoreCliCommand()
609623
dotnetCmd.SetServerDetails(rtDetails).SetRepoName(targetRepo).SetBuildConfiguration(buildConfiguration).
610-
SetBasicCommand(filteredDotnetArgs[0]).SetUseNugetV2(useNugetV2)
624+
SetBasicCommand(filteredDotnetArgs[0]).SetUseNugetV2(useNugetV2).SetAllowInsecureConnections(allowInsecureConnection)
611625
// Since we are using the values of the command's arguments and flags along the buildInfo collection process,
612626
// we want to separate the actual .NET basic command (restore/build...) from the arguments and flags
613627
if len(filteredDotnetArgs) > 1 {

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ require (
167167
gopkg.in/yaml.v3 v3.0.1 // indirect
168168
)
169169

170-
// replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20241113152357-24197a744331
170+
replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20241121163158-04daeb8132c3
171171

172-
// replace github.com/jfrog/jfrog-cli-security => github.com/jfrog/jfrog-cli-security v1.12.5-0.20241107141149-42cf964808a1
172+
replace github.com/jfrog/jfrog-cli-security => github.com/EyalDelarea/jfrog-cli-security v0.0.0-20241121103043-02719f295f02
173173

174174
// replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240918081224-1c584cc334c7
175175

176-
// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20240918150101-ad5b10435a12
176+
replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20241121100855-e7a75ceee2bd
177177

178178
// replace github.com/jfrog/gofrog => github.com/jfrog/gofrog dev

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0
1010
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
1111
github.com/CycloneDX/cyclonedx-go v0.9.0 h1:inaif7qD8bivyxp7XLgxUYtOXWtDez7+j72qKTMQTb8=
1212
github.com/CycloneDX/cyclonedx-go v0.9.0/go.mod h1:NE/EWvzELOFlG6+ljX/QeMlVt9VKcTwu8u0ccsACEsw=
13+
github.com/EyalDelarea/jfrog-cli-security v0.0.0-20241121103043-02719f295f02 h1:oyTvu0FWw+qlEcinSd/8/U+JWR00uQSSa9y0fO+ZVAo=
14+
github.com/EyalDelarea/jfrog-cli-security v0.0.0-20241121103043-02719f295f02/go.mod h1:5LBGwth7TXkEH8MO0JJXvpoRktMAV2BK7Q5nQePNrv4=
1315
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
1416
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
1517
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
@@ -161,8 +163,8 @@ github.com/jedib0t/go-pretty/v6 v6.6.1 h1:iJ65Xjb680rHcikRj6DSIbzCex2huitmc7bDtx
161163
github.com/jedib0t/go-pretty/v6 v6.6.1/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
162164
github.com/jfrog/archiver/v3 v3.6.1 h1:LOxnkw9pOn45DzCbZNFV6K0+6dCsQ0L8mR3ZcujO5eI=
163165
github.com/jfrog/archiver/v3 v3.6.1/go.mod h1:VgR+3WZS4N+i9FaDwLZbq+jeU4B4zctXL+gL4EMzfLw=
164-
github.com/jfrog/build-info-go v1.10.5 h1:cW03JlPlKv7RMUU896uLUxyLWXAmCgR5Y5QX0fwgz0Q=
165-
github.com/jfrog/build-info-go v1.10.5/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
166+
github.com/jfrog/build-info-go v1.8.9-0.20241121100855-e7a75ceee2bd h1:PzxnJ1mjHIL4bAC4RPm87WnJ1TZXFBicyOhtIHRQH6g=
167+
github.com/jfrog/build-info-go v1.8.9-0.20241121100855-e7a75ceee2bd/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
166168
github.com/jfrog/froggit-go v1.16.2 h1:F//S83iXH14qsCwYzv0zB2JtjS2pJVEsUoEmYA+37dQ=
167169
github.com/jfrog/froggit-go v1.16.2/go.mod h1:5VpdQfAcbuyFl9x/x8HGm7kVk719kEtW/8YJFvKcHPA=
168170
github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s=
@@ -171,12 +173,10 @@ github.com/jfrog/jfrog-apps-config v1.0.1 h1:mtv6k7g8A8BVhlHGlSveapqf4mJfonwvXYL
171173
github.com/jfrog/jfrog-apps-config v1.0.1/go.mod h1:8AIIr1oY9JuH5dylz2S6f8Ym2MaadPLR6noCBO4C22w=
172174
github.com/jfrog/jfrog-cli-artifactory v0.1.7 h1:/PBDO6nS6cf3PK+GRkd6BJtZnvYasi1PrQhRiayirso=
173175
github.com/jfrog/jfrog-cli-artifactory v0.1.7/go.mod h1:M5pZTHnsYNDmml/FAnoxxt4QiHOIUHPx91th30AtwfM=
174-
github.com/jfrog/jfrog-cli-core/v2 v2.56.8 h1:UexulAwRVN20VmYACijkTFYKqtUq5myE4okEgmUrorw=
175-
github.com/jfrog/jfrog-cli-core/v2 v2.56.8/go.mod h1:RY74eDpw1WBxruSfZ0HO1ax7c1NAj+rbBgA/hVOJNME=
176+
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20241121163158-04daeb8132c3 h1:cJSPTMflqE+ucC/h2/BB6BkVxz3BG8PnivCb00Dxt/Y=
177+
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20241121163158-04daeb8132c3/go.mod h1:zVyWxMkBpZwy/AvTohefIlaZzYKBMFje+gKKKlkunNo=
176178
github.com/jfrog/jfrog-cli-platform-services v1.4.0 h1:g6A30+tOfXd1h6VASeNwH+5mhs5bPQJ0MFzZs/4nlvs=
177179
github.com/jfrog/jfrog-cli-platform-services v1.4.0/go.mod h1:Ky4SDXuMeaiNP/5zMT1YSzIuXG+cNYYOl8BaEA7Awbc=
178-
github.com/jfrog/jfrog-cli-security v1.12.5 h1:2JHPyapXuHQw/qEaElGxBUGrJCZlVFLXDdxkqhf10vE=
179-
github.com/jfrog/jfrog-cli-security v1.12.5/go.mod h1:5LBGwth7TXkEH8MO0JJXvpoRktMAV2BK7Q5nQePNrv4=
180180
github.com/jfrog/jfrog-client-go v1.48.0 h1:hx5B7+Wnobmzq4aFVZtALtbEVDFcjpn0Wb4q2m6H4KU=
181181
github.com/jfrog/jfrog-client-go v1.48.0/go.mod h1:1a7bmQHkRmPEza9wva2+WVrYzrGbosrMymq57kyG5gU=
182182
github.com/jszwec/csvutil v1.10.0 h1:upMDUxhQKqZ5ZDCs/wy+8Kib8rZR8I8lOR34yJkdqhI=

nuget_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func TestNuGetWithGlobalConfig(t *testing.T) {
107107
assert.NoError(t, err)
108108
err = createConfigFileForTest([]string{jfrogHomeDir}, tests.NugetRemoteRepo, "", t, project.Nuget, true)
109109
assert.NoError(t, err)
110+
// allow insecure connection for testings to work with localhost server
110111
testNugetCmd(t, projectPath, tests.NuGetBuildName, "1", []string{"packagesconfig"}, []string{"nuget", "restore"}, []int{6})
111112

112113
cleanTestsHomeEnv()
@@ -117,7 +118,10 @@ func testNugetCmd(t *testing.T, projectPath, buildName, buildNumber string, expe
117118
assert.NoError(t, err, "Failed to get current dir")
118119
chdirCallback := clientTestUtils.ChangeDirWithCallback(t, wd, projectPath)
119120
defer chdirCallback()
121+
122+
allowInsecureConnectionForTests(&args)
120123
args = append(args, "--build-name="+buildName, "--build-number="+buildNumber)
124+
121125
err = runNuGet(t, args...)
122126
if err != nil {
123127
return
@@ -152,6 +156,12 @@ func testNugetCmd(t *testing.T, projectPath, buildName, buildNumber string, expe
152156
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, buildName, artHttpDetails)
153157
}
154158

159+
// Add allow insecure connection for testings to work with localhost server
160+
func allowInsecureConnectionForTests(args *[]string) *[]string {
161+
*args = append(*args, "--allow-insecure-connections")
162+
return args
163+
}
164+
155165
func assertNugetDependencies(t *testing.T, module buildInfo.Module, moduleName string) {
156166
for _, dependency := range module.Dependencies {
157167
switch dependency.Id {
@@ -224,10 +234,11 @@ func runInitNewConfig(t *testing.T, testSuite testInitNewConfigDescriptor, baseR
224234
params := &dotnet.DotnetCommand{}
225235
server := &config.ServerDetails{ArtifactoryUrl: baseRtUrl, User: "user", Password: "password"}
226236
params.SetServerDetails(server).
227-
SetUseNugetV2(testSuite.useNugetV2)
228-
// Prepare the config file with NuGet authentication
237+
SetUseNugetV2(testSuite.useNugetV2).
238+
SetAllowInsecureConnections(true)
229239

230-
configFile, err := dotnet.InitNewConfig(tempDirPath, "", server, testSuite.useNugetV2)
240+
// Prepare the config file with NuGet authentication
241+
configFile, err := dotnet.InitNewConfig(tempDirPath, "", server, testSuite.useNugetV2, true)
231242
if err != nil {
232243
assert.NoError(t, err)
233244
return

utils/cliutils/commandsflags.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ const (
376376
npmDetailedSummary = npmPrefix + detailedSummary
377377

378378
// Unique nuget/dotnet config flags
379-
nugetV2 = "nuget-v2"
379+
nugetV2 = "nuget-v2"
380+
allowInsecureConnections = "allow-insecure-connections"
380381

381382
// Unique go flags
382383
noFallback = "no-fallback"
@@ -1161,6 +1162,10 @@ var flagsMap = map[string]cli.Flag{
11611162
Name: nugetV2,
11621163
Usage: "[Default: false] Set to true if you'd like to use the NuGet V2 protocol when restoring packages from Artifactory.` `",
11631164
},
1165+
allowInsecureConnections: cli.BoolFlag{
1166+
Name: allowInsecureConnections,
1167+
Usage: "[Default: false] Set to true if you wish to configure NuGet sources with unsecured connections. This is recommended for testing purposes only. ` `",
1168+
},
11641169
noFallback: cli.BoolTFlag{
11651170
Name: noFallback,
11661171
Usage: "[Default: false] Set to true to avoid downloading packages from the VCS, if they are missing in Artifactory.` `",
@@ -1882,7 +1887,7 @@ var commandFlags = map[string][]string{
18821887
global, serverIdResolve, repoResolve, nugetV2,
18831888
},
18841889
Nuget: {
1885-
buildName, buildNumber, module, Project,
1890+
buildName, buildNumber, module, Project, allowInsecureConnections,
18861891
},
18871892
DotnetConfig: {
18881893
global, serverIdResolve, repoResolve, nugetV2,

utils/cliutils/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,15 @@ func getDebFlag(c *cli.Context) (deb string, err error) {
809809
}
810810
return deb, nil
811811
}
812+
813+
// ExtractBoolFlagFromArgs Extracts a boolean flag from the args and removes it from the slice.
814+
func ExtractBoolFlagFromArgs(filteredArgs *[]string, flagName string) (value bool, err error) {
815+
var flagIndex int
816+
var boolFlag bool
817+
flagIndex, boolFlag, err = coreutils.FindBooleanFlag("--"+flagName, *filteredArgs)
818+
if err != nil {
819+
return false, err
820+
}
821+
coreutils.RemoveFlagFromCommand(filteredArgs, flagIndex, flagIndex)
822+
return boolFlag, nil
823+
}

utils/cliutils/utils_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,88 @@ func TestShouldCheckLatestCliVersion(t *testing.T) {
140140
assert.NoError(t, err)
141141
assert.True(t, shouldCheck)
142142
}
143+
144+
func TestExtractBoolFlagFromArgs(t *testing.T) {
145+
testCases := []struct {
146+
name string
147+
args []string
148+
flagName string
149+
expectedValue bool
150+
expectedErr bool
151+
expectedArgs []string
152+
}{
153+
{
154+
name: "Flag present as --flagName (implied true)",
155+
args: []string{"somecmd", "--flagName", "otherarg"},
156+
flagName: "flagName",
157+
expectedValue: true,
158+
expectedErr: false,
159+
expectedArgs: []string{"somecmd", "otherarg"},
160+
},
161+
{
162+
name: "Flag present as --flagName=true",
163+
args: []string{"somecmd", "--flagName=true", "otherarg"},
164+
flagName: "flagName",
165+
expectedValue: true,
166+
expectedErr: false,
167+
expectedArgs: []string{"somecmd", "otherarg"},
168+
},
169+
{
170+
name: "Flag present as --flagName=false",
171+
args: []string{"somecmd", "--flagName=false", "otherarg"},
172+
flagName: "flagName",
173+
expectedValue: false,
174+
expectedErr: false,
175+
expectedArgs: []string{"somecmd", "otherarg"},
176+
},
177+
{
178+
name: "Flag not present",
179+
args: []string{"somecmd", "otherarg"},
180+
flagName: "flagName",
181+
expectedValue: false,
182+
expectedErr: false,
183+
expectedArgs: []string{"somecmd", "otherarg"},
184+
},
185+
{
186+
name: "Flag present with invalid value",
187+
args: []string{"somecmd", "--flagName=invalid", "otherarg"},
188+
flagName: "flagName",
189+
expectedValue: false,
190+
expectedErr: true,
191+
expectedArgs: []string{"somecmd", "--flagName=invalid", "otherarg"},
192+
},
193+
{
194+
name: "Flag present as -flagName (should not be found)",
195+
args: []string{"somecmd", "-flagName", "otherarg"},
196+
flagName: "flagName",
197+
expectedValue: false,
198+
expectedErr: false,
199+
expectedArgs: []string{"somecmd", "-flagName", "otherarg"},
200+
},
201+
{
202+
name: "Flag present multiple times",
203+
args: []string{"somecmd", "--flagName", "--flagName=false", "otherarg"},
204+
flagName: "flagName",
205+
expectedValue: true,
206+
expectedErr: false,
207+
expectedArgs: []string{"somecmd", "--flagName=false", "otherarg"},
208+
},
209+
}
210+
211+
for _, tc := range testCases {
212+
t.Run(tc.name, func(t *testing.T) {
213+
// Make a copy to avoid modifying the original
214+
argsCopy := append([]string(nil), tc.args...)
215+
value, err := ExtractBoolFlagFromArgs(&argsCopy, tc.flagName)
216+
217+
if tc.expectedErr {
218+
assert.Error(t, err)
219+
} else {
220+
assert.NoError(t, err)
221+
}
222+
223+
assert.Equal(t, tc.expectedValue, value)
224+
assert.Equal(t, tc.expectedArgs, argsCopy)
225+
})
226+
}
227+
}

0 commit comments

Comments
 (0)