Skip to content

Commit 5a3c2ae

Browse files
committed
Merge branch 'dev'
2 parents 3d3e653 + 293b438 commit 5a3c2ae

File tree

23 files changed

+1012
-155
lines changed

23 files changed

+1012
-155
lines changed

artifactory/commands/dotnet/dotnetcommand.go

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"github.com/jfrog/build-info-go/build"
77
"github.com/jfrog/build-info-go/build/utils/dotnet"
8-
"github.com/jfrog/gofrog/io"
8+
frogio "github.com/jfrog/gofrog/io"
99
commonBuild "github.com/jfrog/jfrog-cli-core/v2/common/build"
1010
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
1111
"github.com/jfrog/jfrog-client-go/auth"
@@ -19,6 +19,7 @@ import (
1919
)
2020

2121
const (
22+
// SourceName should match the one in the config file template.
2223
SourceName = "JFrogCli"
2324
configFilePattern = "jfrog.cli.nuget."
2425

@@ -30,14 +31,16 @@ The initial error is:
3031
)
3132

3233
type DotnetCommand struct {
33-
toolchainType dotnet.ToolchainType
34-
subCommand string
35-
argAndFlags []string
36-
repoName string
37-
solutionPath string
38-
useNugetV2 bool
39-
buildConfiguration *commonBuild.BuildConfiguration
40-
serverDetails *config.ServerDetails
34+
toolchainType dotnet.ToolchainType
35+
subCommand string
36+
argAndFlags []string
37+
repoName string
38+
solutionPath string
39+
useNugetV2 bool
40+
// By default, package sources are required to use HTTPS. This option allows sources to use HTTP.
41+
allowInsecureConnections bool
42+
buildConfiguration *commonBuild.BuildConfiguration
43+
serverDetails *config.ServerDetails
4144
}
4245

4346
func (dc *DotnetCommand) SetServerDetails(serverDetails *config.ServerDetails) *DotnetCommand {
@@ -70,6 +73,11 @@ func (dc *DotnetCommand) SetUseNugetV2(useNugetV2 bool) *DotnetCommand {
7073
return dc
7174
}
7275

76+
func (dc *DotnetCommand) SetAllowInsecureConnections(allowInsecureConnections bool) *DotnetCommand {
77+
dc.allowInsecureConnections = allowInsecureConnections
78+
return dc
79+
}
80+
7381
func (dc *DotnetCommand) SetArgAndFlags(argAndFlags []string) *DotnetCommand {
7482
dc.argAndFlags = argAndFlags
7583
return dc
@@ -159,21 +167,44 @@ func changeWorkingDir(newWorkingDir string) (string, error) {
159167
return newWorkingDir, errorutils.CheckError(err)
160168
}
161169

162-
// Runs nuget sources add command
163-
func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, configFileName, sourceUrl, user, password string) error {
170+
// Runs nuget/dotnet source add command
171+
func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, sourceUrl, user, password string) error {
164172
cmd, err := dotnet.CreateDotnetAddSourceCmd(cmdType, sourceUrl)
165173
if err != nil {
166174
return err
167175
}
168176

169177
flagPrefix := cmdType.GetTypeFlagPrefix()
170-
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"configfile", configFileName)
171178
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"name", SourceName)
172179
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"username", user)
173180
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"password", password)
174-
output, err := io.RunCmdOutput(cmd)
175-
log.Debug("'Add sources' command executed. Output:", output)
176-
return err
181+
stdOut, errorOut, _, err := frogio.RunCmdWithOutputParser(cmd, false)
182+
if err != nil {
183+
return fmt.Errorf("failed to add source: %w\n%s", err, strings.TrimSpace(stdOut+errorOut))
184+
}
185+
return nil
186+
}
187+
188+
// Runs nuget/dotnet source remove command
189+
func RemoveSourceFromNugetConfigIfExists(cmdType dotnet.ToolchainType) error {
190+
cmd, err := dotnet.NewToolchainCmd(cmdType)
191+
if err != nil {
192+
return err
193+
}
194+
if cmdType == dotnet.DotnetCore {
195+
cmd.Command = append(cmd.Command, "nuget", "remove", "source", SourceName)
196+
} else {
197+
cmd.Command = append(cmd.Command, "sources", "remove")
198+
cmd.CommandFlags = append(cmd.CommandFlags, "-name", SourceName)
199+
}
200+
stdOut, stdErr, _, err := frogio.RunCmdWithOutputParser(cmd, false)
201+
if err != nil {
202+
if strings.Contains(stdOut+stdErr, "Unable to find") {
203+
return nil
204+
}
205+
return fmt.Errorf("failed to remove source: %w\n%s", err, strings.TrimSpace(stdOut+stdErr))
206+
}
207+
return nil
177208
}
178209

179210
// Checks if the user provided input such as -configfile flag or -Source flag.
@@ -219,7 +250,7 @@ func (dc *DotnetCommand) prepareConfigFileIfNeeded() (cleanup func() error, err
219250
return fileutils.RemoveTempDir(tempDirPath)
220251
}
221252

222-
configFile, err := InitNewConfig(tempDirPath, dc.repoName, dc.serverDetails, dc.useNugetV2)
253+
configFile, err := InitNewConfig(tempDirPath, dc.repoName, dc.serverDetails, dc.useNugetV2, dc.allowInsecureConnections)
223254
if err == nil {
224255
dc.argAndFlags = append(dc.argAndFlags, dc.GetToolchain().GetTypeFlagPrefix()+"configfile", configFile.Name())
225256
}
@@ -246,7 +277,7 @@ func getFlagValueIfExists(cmdFlag string, argAndFlags []string) (string, error)
246277
}
247278

248279
// InitNewConfig is used when neither of the flags were provided, and we need to init our own config.
249-
func InitNewConfig(configDirPath, repoName string, server *config.ServerDetails, useNugetV2 bool) (configFile *os.File, err error) {
280+
func InitNewConfig(configDirPath, repoName string, server *config.ServerDetails, useNugetV2, allowInsecureConnections bool) (configFile *os.File, err error) {
250281
// Initializing a new NuGet config file that NuGet will use into a temp file
251282
configFile, err = os.CreateTemp(configDirPath, configFilePattern)
252283
if errorutils.CheckError(err) != nil {
@@ -260,13 +291,13 @@ func InitNewConfig(configDirPath, repoName string, server *config.ServerDetails,
260291
// We would prefer to write the NuGet configuration using the `nuget add source` command,
261292
// but the NuGet configuration utility doesn't currently allow setting protocolVersion.
262293
// Until that is supported, the templated method must be used.
263-
err = addSourceToNugetTemplate(configFile, server, useNugetV2, repoName)
294+
err = addSourceToNugetTemplate(configFile, server, useNugetV2, repoName, allowInsecureConnections)
264295
return
265296
}
266297

267298
// Adds a source to the nuget config template
268-
func addSourceToNugetTemplate(configFile *os.File, server *config.ServerDetails, useNugetV2 bool, repoName string) error {
269-
sourceUrl, user, password, err := getSourceDetails(server, repoName, useNugetV2)
299+
func addSourceToNugetTemplate(configFile *os.File, server *config.ServerDetails, useNugetV2 bool, repoName string, allowInsecureConnections bool) error {
300+
sourceUrl, user, password, err := GetSourceDetails(server, repoName, useNugetV2)
270301
if err != nil {
271302
return err
272303
}
@@ -278,11 +309,11 @@ func addSourceToNugetTemplate(configFile *os.File, server *config.ServerDetails,
278309
}
279310

280311
// Format the templates
281-
_, err = fmt.Fprintf(configFile, dotnet.ConfigFileFormat, sourceUrl, protoVer, user, password)
312+
_, err = fmt.Fprintf(configFile, dotnet.ConfigFileFormat, sourceUrl, protoVer, allowInsecureConnections, user, password)
282313
return err
283314
}
284315

285-
func getSourceDetails(details *config.ServerDetails, repoName string, useNugetV2 bool) (sourceURL, user, password string, err error) {
316+
func GetSourceDetails(details *config.ServerDetails, repoName string, useNugetV2 bool) (sourceURL, user, password string, err error) {
286317
var u *url.URL
287318
u, err = url.Parse(details.ArtifactoryUrl)
288319
if errorutils.CheckError(err) != nil {

artifactory/commands/dotnet/dotnetcommand_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestInitNewConfig(t *testing.T) {
7474
User: "user",
7575
Password: "pass",
7676
}
77-
configFile, err := InitNewConfig(tmpDir, repoName, server, false)
77+
configFile, err := InitNewConfig(tmpDir, repoName, server, false, true)
7878
assert.NoError(t, err)
7979
f, err := os.Open(configFile.Name())
8080
assert.NoError(t, err)
@@ -87,7 +87,7 @@ func TestInitNewConfig(t *testing.T) {
8787
assert.Equal(t, `<?xml version="1.0" encoding="utf-8"?>
8888
<configuration>
8989
<packageSources>
90-
<add key="JFrogCli" value="https://server.com/artifactory/api/nuget/v3/test-repo" protocolVersion="3" />
90+
<add key="JFrogCli" value="https://server.com/artifactory/api/nuget/v3/test-repo" protocolVersion="3" allowInsecureConnections="true"/>
9191
</packageSources>
9292
<packageSourceCredentials>
9393
<JFrogCli>
@@ -98,7 +98,7 @@ func TestInitNewConfig(t *testing.T) {
9898
</configuration>`, string(buf[:n]))
9999
server.Password = ""
100100
server.AccessToken = "abc123"
101-
configFile, err = InitNewConfig(tmpDir, repoName, server, true)
101+
configFile, err = InitNewConfig(tmpDir, repoName, server, true, true)
102102
assert.NoError(t, err)
103103
updatedConfigFile, err := os.Open(configFile.Name())
104104
assert.NoError(t, err)
@@ -111,7 +111,7 @@ func TestInitNewConfig(t *testing.T) {
111111
assert.Equal(t, `<?xml version="1.0" encoding="utf-8"?>
112112
<configuration>
113113
<packageSources>
114-
<add key="JFrogCli" value="https://server.com/artifactory/api/nuget/test-repo" protocolVersion="2" />
114+
<add key="JFrogCli" value="https://server.com/artifactory/api/nuget/test-repo" protocolVersion="2" allowInsecureConnections="true"/>
115115
</packageSources>
116116
<packageSourceCredentials>
117117
<JFrogCli>
@@ -129,14 +129,14 @@ func TestGetSourceDetails(t *testing.T) {
129129
Password: "pass",
130130
}
131131
repoName := "repo-name"
132-
url, user, pass, err := getSourceDetails(server, repoName, false)
132+
url, user, pass, err := GetSourceDetails(server, repoName, false)
133133
assert.NoError(t, err)
134134
assert.Equal(t, "user", user)
135135
assert.Equal(t, "pass", pass)
136136
assert.Equal(t, "https://server.com/artifactory/api/nuget/v3/repo-name", url)
137137
server.Password = ""
138138
server.AccessToken = "abc123"
139-
url, user, pass, err = getSourceDetails(server, repoName, true)
139+
url, user, pass, err = GetSourceDetails(server, repoName, true)
140140
assert.Equal(t, "user", user)
141141
assert.Equal(t, "abc123", pass)
142142
assert.NoError(t, err)
@@ -164,11 +164,12 @@ func testPrepareDotnetBuildInfoModule(t *testing.T, subCommand string, flags []s
164164
}()
165165
module := createNewDotnetModule(t, tmpDir)
166166
cmd := DotnetCommand{
167-
toolchainType: dotnet.DotnetCore,
168-
subCommand: subCommand,
169-
argAndFlags: flags,
170-
buildConfiguration: buildUtils.NewBuildConfiguration("", "", "mod", ""),
171-
serverDetails: &config.ServerDetails{ArtifactoryUrl: "https://my-instance.jfrog.io"},
167+
toolchainType: dotnet.DotnetCore,
168+
subCommand: subCommand,
169+
argAndFlags: flags,
170+
buildConfiguration: buildUtils.NewBuildConfiguration("", "", "mod", ""),
171+
serverDetails: &config.ServerDetails{ArtifactoryUrl: "https://my-instance.jfrog.io"},
172+
allowInsecureConnections: true,
172173
}
173174
callbackFunc, err := cmd.prepareDotnetBuildInfoModule(module)
174175
if !assert.NoError(t, err) {

artifactory/commands/golang/go.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (gc *GoCommand) run() (err error) {
154154
return
155155
}
156156
// If noFallback=false, missing packages will be fetched directly from VCS
157-
repoUrl, err := getArtifactoryRemoteRepoUrl(resolverDetails, gc.resolverParams.TargetRepo(), GoProxyUrlParams{Direct: !gc.noFallback})
157+
repoUrl, err := GetArtifactoryRemoteRepoUrl(resolverDetails, gc.resolverParams.TargetRepo(), GoProxyUrlParams{Direct: !gc.noFallback})
158158
if err != nil {
159159
return
160160
}
@@ -336,7 +336,7 @@ func SetArtifactoryAsResolutionServer(serverDetails *config.ServerDetails, depsR
336336
}
337337

338338
func setGoProxy(server *config.ServerDetails, remoteGoRepo string, goProxyParams GoProxyUrlParams) error {
339-
repoUrl, err := getArtifactoryRemoteRepoUrl(server, remoteGoRepo, goProxyParams)
339+
repoUrl, err := GetArtifactoryRemoteRepoUrl(server, remoteGoRepo, goProxyParams)
340340
if err != nil {
341341
return err
342342
}
@@ -380,7 +380,7 @@ func (gdu *GoProxyUrlParams) addDirect(url string) string {
380380
return url
381381
}
382382

383-
func getArtifactoryRemoteRepoUrl(serverDetails *config.ServerDetails, repo string, goProxyParams GoProxyUrlParams) (string, error) {
383+
func GetArtifactoryRemoteRepoUrl(serverDetails *config.ServerDetails, repo string, goProxyParams GoProxyUrlParams) (string, error) {
384384
authServerDetails, err := serverDetails.CreateArtAuthConfig()
385385
if err != nil {
386386
return "", err

artifactory/commands/golang/go_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestGetArtifactoryRemoteRepoUrl(t *testing.T) {
8686
AccessToken: "eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA",
8787
}
8888
repoName := "test-repo"
89-
repoUrl, err := getArtifactoryRemoteRepoUrl(server, repoName, GoProxyUrlParams{})
89+
repoUrl, err := GetArtifactoryRemoteRepoUrl(server, repoName, GoProxyUrlParams{})
9090
assert.NoError(t, err)
9191
assert.Equal(t, "https://test:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@server.com/artifactory/api/go/test-repo", repoUrl)
9292
}

0 commit comments

Comments
 (0)