diff --git a/README.md b/README.md index 1571e17b8..171d1d4e7 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ The library can be used as a go-module, which should be added to your project's rtDetails.SetApiKey("apikey") rtDetails.SetUser("user") rtDetails.SetPassword("password") + rtDetails.SetAccessToken("accesstoken") ``` ### Creating a Service Manager @@ -449,6 +450,7 @@ Optional flags: | `-rt.apikey` | [Optional] Artifactory API key. | | `-rt.sshKeyPath` | [Optional] Ssh key file path. Should be used only if the Artifactory URL format is ssh://[domain]:port | | `-rt.sshPassphrase` | [Optional] Ssh key passphrase. | +| `-rt.accessToken` | [Optional] Artifactory access token. | | `-log-level` | [Default: INFO] Sets the log level. | diff --git a/artifactory/auth/rtdetails.go b/artifactory/auth/rtdetails.go index b14f0bbf8..7aa97de4b 100644 --- a/artifactory/auth/rtdetails.go +++ b/artifactory/auth/rtdetails.go @@ -21,12 +21,14 @@ type ArtifactoryDetails interface { GetUser() string GetPassword() string GetApiKey() string + GetAccessToken() string GetSshAuthHeaders() map[string]string GetVersion() (string, error) SetUrl(url string) SetUser(user string) SetPassword(password string) SetApiKey(apiKey string) + SetAccessToken(accessToken string) SetSshAuthHeaders(sshAuthHeaders map[string]string) AuthenticateSsh(sshKey, sshPassphrase string) error @@ -39,6 +41,7 @@ type artifactoryDetails struct { User string `json:"-"` Password string `json:"-"` ApiKey string `json:"-"` + AccessToken string `json:"-"` version string `json:"-"` SshAuthHeaders map[string]string `json:"-"` } @@ -59,6 +62,10 @@ func (rt *artifactoryDetails) GetApiKey() string { return rt.ApiKey } +func (rt *artifactoryDetails) GetAccessToken() string { + return rt.AccessToken +} + func (rt *artifactoryDetails) GetSshAuthHeaders() map[string]string { return rt.SshAuthHeaders } @@ -79,6 +86,10 @@ func (rt *artifactoryDetails) SetApiKey(apiKey string) { rt.ApiKey = apiKey } +func (rt *artifactoryDetails) SetAccessToken(accessToken string) { + rt.AccessToken = accessToken +} + func (rt *artifactoryDetails) SetSshAuthHeaders(sshAuthHeaders map[string]string) { rt.SshAuthHeaders = sshAuthHeaders } @@ -95,10 +106,11 @@ func (rt *artifactoryDetails) AuthenticateSsh(sshKeyPath, sshPassphrase string) func (rt *artifactoryDetails) CreateHttpClientDetails() httputils.HttpClientDetails { return httputils.HttpClientDetails{ - User: rt.User, - Password: rt.Password, - ApiKey: rt.ApiKey, - Headers: utils.CopyMap(rt.SshAuthHeaders)} + User: rt.User, + Password: rt.Password, + ApiKey: rt.ApiKey, + AccessToken: rt.AccessToken, + Headers: utils.CopyMap(rt.SshAuthHeaders)} } func (rt *artifactoryDetails) GetVersion() (string, error) { diff --git a/artifactory/services/ping.go b/artifactory/services/ping.go index d42064bee..57239d32e 100644 --- a/artifactory/services/ping.go +++ b/artifactory/services/ping.go @@ -5,7 +5,6 @@ import ( "github.com/jfrog/jfrog-client-go/artifactory/auth" "github.com/jfrog/jfrog-client-go/artifactory/services/utils" "github.com/jfrog/jfrog-client-go/httpclient" - clientutils "github.com/jfrog/jfrog-client-go/utils" "github.com/jfrog/jfrog-client-go/utils/errorutils" "github.com/jfrog/jfrog-client-go/utils/log" "net/http" @@ -46,7 +45,7 @@ func (ps *PingService) Ping() ([]byte, error) { return nil, err } if resp.StatusCode != http.StatusOK { - return nil, errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(respBody))) + return respBody, errorutils.CheckError(errors.New("Artifactory response: " + resp.Status)) } log.Debug("Artifactory response: ", resp.Status) return respBody, nil diff --git a/httpclient/client.go b/httpclient/client.go index 1d073f7c4..5558bd57c 100644 --- a/httpclient/client.go +++ b/httpclient/client.go @@ -555,7 +555,17 @@ func setAuthentication(req *http.Request, httpClientsDetails httputils.HttpClien } else { req.Header.Set("X-JFrog-Art-Api", httpClientsDetails.ApiKey) } - } else if httpClientsDetails.Password != "" { + return + } + if httpClientsDetails.AccessToken != "" { + if httpClientsDetails.User != "" { + req.SetBasicAuth(httpClientsDetails.User, httpClientsDetails.AccessToken) + } else { + req.Header.Set("Authorization", "Bearer "+httpClientsDetails.AccessToken) + } + return + } + if httpClientsDetails.Password != "" { req.SetBasicAuth(httpClientsDetails.User, httpClientsDetails.Password) } } diff --git a/tests/utils.go b/tests/utils.go index 490bdac9d..5005c9245 100644 --- a/tests/utils.go +++ b/tests/utils.go @@ -24,6 +24,7 @@ var RtPassword *string var RtApiKey *string var RtSshKeyPath *string var RtSshPassphrase *string +var RtAccessToken *string var LogLevel *string var testsUploadService *services.UploadService var testsSearchService *services.SearchService @@ -43,6 +44,7 @@ func init() { RtApiKey = flag.String("rt.apikey", "", "Artifactory user API key") RtSshKeyPath = flag.String("rt.sshKeyPath", "", "Ssh key file path") RtSshPassphrase = flag.String("rt.sshPassphrase", "", "Ssh key passphrase") + RtAccessToken = flag.String("rt.accessToken", "", "Artifactory access token") LogLevel = flag.String("log-level", "INFO", "Sets the log level") } @@ -74,6 +76,8 @@ func getArtDetails() auth.ArtifactoryDetails { if !fileutils.IsSshUrl(rtDetails.GetUrl()) { if *RtApiKey != "" { rtDetails.SetApiKey(*RtApiKey) + } else if *RtAccessToken != "" { + rtDetails.SetAccessToken(*RtAccessToken) } else { rtDetails.SetUser(*RtUser) rtDetails.SetPassword(*RtPassword) diff --git a/utils/io/httputils/httpclient.go b/utils/io/httputils/httpclient.go index cf6287f58..c2470c7c1 100644 --- a/utils/io/httputils/httpclient.go +++ b/utils/io/httputils/httpclient.go @@ -6,19 +6,21 @@ import ( ) type HttpClientDetails struct { - User string - Password string - ApiKey string - Headers map[string]string - Transport *http.Transport + User string + Password string + ApiKey string + AccessToken string + Headers map[string]string + Transport *http.Transport } func (httpClientDetails HttpClientDetails) Clone() *HttpClientDetails { headers := make(map[string]string) utils.MergeMaps(httpClientDetails.Headers, headers) return &HttpClientDetails{ - User: httpClientDetails.User, - Password: httpClientDetails.Password, - ApiKey: httpClientDetails.ApiKey, - Headers: headers} + User: httpClientDetails.User, + Password: httpClientDetails.Password, + ApiKey: httpClientDetails.ApiKey, + AccessToken: httpClientDetails.AccessToken, + Headers: headers} }