你可以使用Go语言如何在Artifactory中进行身份验证?

huangapple go评论77阅读模式
英文:

How can I authenticate with Artifactory in Go?

问题

我无法真正理解这份非常简洁的文档——我的使用情况是,我有一个在Artifactory中已知URL的文件,我想要下载它。为此,我需要使用Artifactory进行身份验证。

我有以下代码:

func authenticateToArtifactory() string {
    rtDetails := auth.NewArtifactoryDetails()
    rtDetails.SetUrl(artifactoryURL)
    fmt.Printf("Artifactory login name....\n")
    var userName string
    fmt.Scanln(&userName)
    fmt.Printf("Artifactory password....\n")
    var passWord string
    fmt.Scanln(&passWord)
    rtDetails.SetUser(userName)
    rtDetails.SetPassword(passWord)
    apiKey := rtDetails.GetAPIKey()
    fmt.Printf("API key is %s\n", apiKey)
    return apiKey
}

但它不起作用——老实说,这并不令人惊讶,因为甚至没有定义getAPIKey()方法——但我真的不知道该怎么办。

应该如何正确处理这个问题?

编辑:从阅读周围的内容,我认为我可能必须使用一个HTTP客户端来完成这个任务——也就是说,我不能直接与Artifactory进行身份验证:这样正确吗?

英文:

I cannot really make any sense of the extremely terse documentation - my use case is I have a file at a known URL in Artifactory and I want to download it. For this I need to authenticate with Artifactory.

I have this code:

func authenticateToArtifactory() string {

rtDetails := auth.NewArtifactoryDetails()
rtDetails.SetUrl(artifactoryURL)
fmt.Printf("Artifactory login name....\n")
var userName string
fmt.Scanln(&userName)
fmt.Printf("Artifactory password....\n")
var passWord string
fmt.Scanln(&passWord)
rtDetails.SetUser(userName)
rtDetails.SetPassword(passWord)
apiKey := rtDetails.GetAPIKey()
fmt.Printf("API key is %s\n", apiKey)
return apiKey
}

But it doesn't work - to be honest this isn't a surprise as there isn't even a getAPIKey() call defined - but I really don't know what to do here.

What's the right way to do this?

Edit: From reading around I think I might have to use a http client for this - ie I cannot authenticate with Artifactory directly: is that correct?

答案1

得分: 0

经过很多的挫折和试错,我认为我已经解决了这个问题 - 文档质量令人震惊地差,所以我希望这能帮助到其他人。

以下是一个基本的设置...(在这里,将所有这些尾随的'/'都加上是必不可少的,据我所见,没有这方面的文档):

var artifactoryURL = "https://your.remote.server/artifactory/"

rtDetails := auth.NewArtifactoryDetails()
rtDetails.SetUrl(artifactoryURL)
fmt.Printf("Artifactory登录名....\n")
var userName string
userName = [通过命令行等方式获取用户名]
fmt.Printf("Artifactory密码....\n")
var passWord string
passWord = [通过命令行等方式获取Artifactory密码]
rtDetails.SetUser(userName)
rtDetails.SetPassword(passWord)

artifactoryHTTP := &http.Client{Transport: &http.Transport{}}
serviceConfig, err := config.NewConfigBuilder().SetServiceDetails(rtDetails).SetDryRun(false).SetHttpClient(artifactoryHTTP).Build()
if err != nil {
	panic(err)
}
rtManager, errSC := artifactory.New(serviceConfig)
if errSC != nil {
	panic(errSC)
}
params := services.NewDownloadParams()
params.Pattern = "[repo_name]/path/to/your/file/your_file"
_, _, err = rtManager.DownloadFiles(params)
if err != nil {
	fmt.Printf("%s\n", err.Error())
	panic(err)
}

repo_name是文件URL中artifactory后的第一部分,例如

http://my.server.address/artifactory/repo_name/blah/blah/blah/file_I_want.tar.gz

英文:

After a lot of frustration and trial and error, I think I have this working - the documentation is shockingly poor, so I do hope this helps someone else.

Here's a basic Set up... (getting all these trailing '/' in is essential and as far as I can see none of this is documented):

var artifactoryURL = "https://your.remote.server/artifactory/"

rtDetails := auth.NewArtifactoryDetails()
rtDetails.SetUrl(artifactoryURL)
fmt.Printf("Artifactory login name....\n")
var userName string
userName = [some way of getting user name eg via command line]
fmt.Printf("Artifactory password....\n")
var passWord string
passWord = [some way of getting artifactory password eg via command line]
rtDetails.SetUser(userName)
rtDetails.SetPassword(passWord)

artifactoryHTTP := &http.Client{Transport: &http.Transport{}}
serviceConfig, err := config.NewConfigBuilder().SetServiceDetails(rtDetails).SetDryRun(false).SetHttpClient(artifactoryHTTP).Build()
if err != nil {
	panic(err)
}
rtManager, errSC := artifactory.New(serviceConfig)
if errSC != nil {
	panic(errSC)
}
params := services.NewDownloadParams()
params.Pattern = "[repo_name]/path/to/your/file/your_file"
_, _, err = rtManager.DownloadFiles(params)
if err != nil {
	fmt.Printf("%s\n", err.Error())
	panic(err)
}

The repo name is the first bit of the file's URL after artifactory: eg

http://my.server.address/artifactory/repo_name/blah/blah/blah/file_I_want.tar.gz

huangapple
  • 本文由 发表于 2022年10月17日 21:16:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/74097931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定