goartifactoryjfrog-cli

How can I authenticate with Artifactory in Go?


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?


Solution

  • 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

    Adding - A contributor suggests highlighting: auth.NewArtifactoryDetails() this method is present in this "github.com/jfrog/jfrog-client-go/artifactory/auth" package not in this package "github.com/jfrog/jfrog-client-go/auth"