Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalb4doc committed Nov 27, 2024
2 parents 3d3e653 + 293b438 commit 5a3c2ae
Show file tree
Hide file tree
Showing 23 changed files with 1,012 additions and 155 deletions.
75 changes: 53 additions & 22 deletions artifactory/commands/dotnet/dotnetcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"github.com/jfrog/build-info-go/build"
"github.com/jfrog/build-info-go/build/utils/dotnet"
"github.com/jfrog/gofrog/io"
frogio "github.com/jfrog/gofrog/io"
commonBuild "github.com/jfrog/jfrog-cli-core/v2/common/build"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-client-go/auth"
Expand All @@ -19,6 +19,7 @@ import (
)

const (
// SourceName should match the one in the config file template.
SourceName = "JFrogCli"
configFilePattern = "jfrog.cli.nuget."

Expand All @@ -30,14 +31,16 @@ The initial error is:
)

type DotnetCommand struct {
toolchainType dotnet.ToolchainType
subCommand string
argAndFlags []string
repoName string
solutionPath string
useNugetV2 bool
buildConfiguration *commonBuild.BuildConfiguration
serverDetails *config.ServerDetails
toolchainType dotnet.ToolchainType
subCommand string
argAndFlags []string
repoName string
solutionPath string
useNugetV2 bool
// By default, package sources are required to use HTTPS. This option allows sources to use HTTP.
allowInsecureConnections bool
buildConfiguration *commonBuild.BuildConfiguration
serverDetails *config.ServerDetails
}

func (dc *DotnetCommand) SetServerDetails(serverDetails *config.ServerDetails) *DotnetCommand {
Expand Down Expand Up @@ -70,6 +73,11 @@ func (dc *DotnetCommand) SetUseNugetV2(useNugetV2 bool) *DotnetCommand {
return dc
}

func (dc *DotnetCommand) SetAllowInsecureConnections(allowInsecureConnections bool) *DotnetCommand {
dc.allowInsecureConnections = allowInsecureConnections
return dc
}

func (dc *DotnetCommand) SetArgAndFlags(argAndFlags []string) *DotnetCommand {
dc.argAndFlags = argAndFlags
return dc
Expand Down Expand Up @@ -159,21 +167,44 @@ func changeWorkingDir(newWorkingDir string) (string, error) {
return newWorkingDir, errorutils.CheckError(err)
}

// Runs nuget sources add command
func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, configFileName, sourceUrl, user, password string) error {
// Runs nuget/dotnet source add command
func AddSourceToNugetConfig(cmdType dotnet.ToolchainType, sourceUrl, user, password string) error {
cmd, err := dotnet.CreateDotnetAddSourceCmd(cmdType, sourceUrl)
if err != nil {
return err
}

flagPrefix := cmdType.GetTypeFlagPrefix()
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"configfile", configFileName)
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"name", SourceName)
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"username", user)
cmd.CommandFlags = append(cmd.CommandFlags, flagPrefix+"password", password)
output, err := io.RunCmdOutput(cmd)
log.Debug("'Add sources' command executed. Output:", output)
return err
stdOut, errorOut, _, err := frogio.RunCmdWithOutputParser(cmd, false)
if err != nil {
return fmt.Errorf("failed to add source: %w\n%s", err, strings.TrimSpace(stdOut+errorOut))
}
return nil
}

// Runs nuget/dotnet source remove command
func RemoveSourceFromNugetConfigIfExists(cmdType dotnet.ToolchainType) error {
cmd, err := dotnet.NewToolchainCmd(cmdType)
if err != nil {
return err
}
if cmdType == dotnet.DotnetCore {
cmd.Command = append(cmd.Command, "nuget", "remove", "source", SourceName)
} else {
cmd.Command = append(cmd.Command, "sources", "remove")
cmd.CommandFlags = append(cmd.CommandFlags, "-name", SourceName)
}
stdOut, stdErr, _, err := frogio.RunCmdWithOutputParser(cmd, false)
if err != nil {
if strings.Contains(stdOut+stdErr, "Unable to find") {
return nil
}
return fmt.Errorf("failed to remove source: %w\n%s", err, strings.TrimSpace(stdOut+stdErr))
}
return nil
}

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

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

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

// Adds a source to the nuget config template
func addSourceToNugetTemplate(configFile *os.File, server *config.ServerDetails, useNugetV2 bool, repoName string) error {
sourceUrl, user, password, err := getSourceDetails(server, repoName, useNugetV2)
func addSourceToNugetTemplate(configFile *os.File, server *config.ServerDetails, useNugetV2 bool, repoName string, allowInsecureConnections bool) error {
sourceUrl, user, password, err := GetSourceDetails(server, repoName, useNugetV2)
if err != nil {
return err
}
Expand All @@ -278,11 +309,11 @@ func addSourceToNugetTemplate(configFile *os.File, server *config.ServerDetails,
}

// Format the templates
_, err = fmt.Fprintf(configFile, dotnet.ConfigFileFormat, sourceUrl, protoVer, user, password)
_, err = fmt.Fprintf(configFile, dotnet.ConfigFileFormat, sourceUrl, protoVer, allowInsecureConnections, user, password)
return err
}

func getSourceDetails(details *config.ServerDetails, repoName string, useNugetV2 bool) (sourceURL, user, password string, err error) {
func GetSourceDetails(details *config.ServerDetails, repoName string, useNugetV2 bool) (sourceURL, user, password string, err error) {
var u *url.URL
u, err = url.Parse(details.ArtifactoryUrl)
if errorutils.CheckError(err) != nil {
Expand Down
23 changes: 12 additions & 11 deletions artifactory/commands/dotnet/dotnetcommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestInitNewConfig(t *testing.T) {
User: "user",
Password: "pass",
}
configFile, err := InitNewConfig(tmpDir, repoName, server, false)
configFile, err := InitNewConfig(tmpDir, repoName, server, false, true)
assert.NoError(t, err)
f, err := os.Open(configFile.Name())
assert.NoError(t, err)
Expand All @@ -87,7 +87,7 @@ func TestInitNewConfig(t *testing.T) {
assert.Equal(t, `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="JFrogCli" value="https://server.com/artifactory/api/nuget/v3/test-repo" protocolVersion="3" />
<add key="JFrogCli" value="https://server.com/artifactory/api/nuget/v3/test-repo" protocolVersion="3" allowInsecureConnections="true"/>
</packageSources>
<packageSourceCredentials>
<JFrogCli>
Expand All @@ -98,7 +98,7 @@ func TestInitNewConfig(t *testing.T) {
</configuration>`, string(buf[:n]))
server.Password = ""
server.AccessToken = "abc123"
configFile, err = InitNewConfig(tmpDir, repoName, server, true)
configFile, err = InitNewConfig(tmpDir, repoName, server, true, true)
assert.NoError(t, err)
updatedConfigFile, err := os.Open(configFile.Name())
assert.NoError(t, err)
Expand All @@ -111,7 +111,7 @@ func TestInitNewConfig(t *testing.T) {
assert.Equal(t, `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="JFrogCli" value="https://server.com/artifactory/api/nuget/test-repo" protocolVersion="2" />
<add key="JFrogCli" value="https://server.com/artifactory/api/nuget/test-repo" protocolVersion="2" allowInsecureConnections="true"/>
</packageSources>
<packageSourceCredentials>
<JFrogCli>
Expand All @@ -129,14 +129,14 @@ func TestGetSourceDetails(t *testing.T) {
Password: "pass",
}
repoName := "repo-name"
url, user, pass, err := getSourceDetails(server, repoName, false)
url, user, pass, err := GetSourceDetails(server, repoName, false)
assert.NoError(t, err)
assert.Equal(t, "user", user)
assert.Equal(t, "pass", pass)
assert.Equal(t, "https://server.com/artifactory/api/nuget/v3/repo-name", url)
server.Password = ""
server.AccessToken = "abc123"
url, user, pass, err = getSourceDetails(server, repoName, true)
url, user, pass, err = GetSourceDetails(server, repoName, true)
assert.Equal(t, "user", user)
assert.Equal(t, "abc123", pass)
assert.NoError(t, err)
Expand Down Expand Up @@ -164,11 +164,12 @@ func testPrepareDotnetBuildInfoModule(t *testing.T, subCommand string, flags []s
}()
module := createNewDotnetModule(t, tmpDir)
cmd := DotnetCommand{
toolchainType: dotnet.DotnetCore,
subCommand: subCommand,
argAndFlags: flags,
buildConfiguration: buildUtils.NewBuildConfiguration("", "", "mod", ""),
serverDetails: &config.ServerDetails{ArtifactoryUrl: "https://my-instance.jfrog.io"},
toolchainType: dotnet.DotnetCore,
subCommand: subCommand,
argAndFlags: flags,
buildConfiguration: buildUtils.NewBuildConfiguration("", "", "mod", ""),
serverDetails: &config.ServerDetails{ArtifactoryUrl: "https://my-instance.jfrog.io"},
allowInsecureConnections: true,
}
callbackFunc, err := cmd.prepareDotnetBuildInfoModule(module)
if !assert.NoError(t, err) {
Expand Down
6 changes: 3 additions & 3 deletions artifactory/commands/golang/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (gc *GoCommand) run() (err error) {
return
}
// If noFallback=false, missing packages will be fetched directly from VCS
repoUrl, err := getArtifactoryRemoteRepoUrl(resolverDetails, gc.resolverParams.TargetRepo(), GoProxyUrlParams{Direct: !gc.noFallback})
repoUrl, err := GetArtifactoryRemoteRepoUrl(resolverDetails, gc.resolverParams.TargetRepo(), GoProxyUrlParams{Direct: !gc.noFallback})
if err != nil {
return
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func SetArtifactoryAsResolutionServer(serverDetails *config.ServerDetails, depsR
}

func setGoProxy(server *config.ServerDetails, remoteGoRepo string, goProxyParams GoProxyUrlParams) error {
repoUrl, err := getArtifactoryRemoteRepoUrl(server, remoteGoRepo, goProxyParams)
repoUrl, err := GetArtifactoryRemoteRepoUrl(server, remoteGoRepo, goProxyParams)
if err != nil {
return err
}
Expand Down Expand Up @@ -380,7 +380,7 @@ func (gdu *GoProxyUrlParams) addDirect(url string) string {
return url
}

func getArtifactoryRemoteRepoUrl(serverDetails *config.ServerDetails, repo string, goProxyParams GoProxyUrlParams) (string, error) {
func GetArtifactoryRemoteRepoUrl(serverDetails *config.ServerDetails, repo string, goProxyParams GoProxyUrlParams) (string, error) {
authServerDetails, err := serverDetails.CreateArtAuthConfig()
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion artifactory/commands/golang/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestGetArtifactoryRemoteRepoUrl(t *testing.T) {
AccessToken: "eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA",
}
repoName := "test-repo"
repoUrl, err := getArtifactoryRemoteRepoUrl(server, repoName, GoProxyUrlParams{})
repoUrl, err := GetArtifactoryRemoteRepoUrl(server, repoName, GoProxyUrlParams{})
assert.NoError(t, err)
assert.Equal(t, "https://test:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@server.com/artifactory/api/go/test-repo", repoUrl)
}
Expand Down
Loading

0 comments on commit 5a3c2ae

Please sign in to comment.