Skip to content

Commit

Permalink
Improve token and build info service errors (#1005)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 authored Aug 18, 2024
1 parent e68b65d commit 512e430
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
7 changes: 4 additions & 3 deletions artifactory/services/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"encoding/json"
"fmt"
buildinfo "github.com/jfrog/build-info-go/entities"
"net/http"

Expand Down Expand Up @@ -61,14 +62,14 @@ func (bis *BuildInfoService) PublishBuildInfo(build *buildinfo.BuildInfo, projec
if bis.IsDryRun() {
log.Info("[Dry run] Logging Build info preview...")
log.Output(clientutils.IndentJson(content))
return summary, err
return summary, nil
}
httpClientsDetails := bis.GetArtifactoryDetails().CreateHttpClientDetails()
utils.SetContentType("application/vnd.org.jfrog.artifactory+json", &httpClientsDetails.Headers)
log.Info("Deploying build info...")
log.Info(fmt.Sprintf("Publishing build info for <%s>/<%s>...", build.Name, build.Number))
resp, body, err := bis.client.SendPut(bis.GetArtifactoryDetails().GetUrl()+"api/build"+utils.GetProjectQueryParam(projectKey), content, &httpClientsDetails)
if err != nil {
return summary, err
return summary, fmt.Errorf("error occurred while publishing build info: %s", err.Error())
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK, http.StatusCreated, http.StatusNoContent); err != nil {
return summary, err
Expand Down
42 changes: 24 additions & 18 deletions artifactory/services/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
Expand All @@ -14,8 +15,13 @@ import (
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

const tokenPath = "api/security/token"
const APIKeyPath = "api/security/apiKey"
const (
tokenPath = "api/security/token"
APIKeyPath = "api/security/apiKey"
errorMsgPrefix = "error occurred while attempting to"
unexpectedServerResponsePrefix = "got unexpected server response while attempting to"
couldntParseServerResponsePrefix = "couldn't parse server response while attempting to"
)

type SecurityService struct {
client *jfroghttpclient.JfrogHttpClient
Expand Down Expand Up @@ -94,7 +100,7 @@ func (ss *SecurityService) GetAPIKey() (string, error) {
func getApiKeyFromBody(body []byte) (string, error) {
var data = make(map[string]interface{})
if err := json.Unmarshal(body, &data); err != nil {
return "", errorutils.CheckErrorf("unable to decode json. Error: %w Upstream response: %s", err, string(body))
return "", errorutils.CheckErrorf("unable to decode json. Error: %s Upstream response: %s", err.Error(), string(body))
}

if len(data) == 0 {
Expand All @@ -114,15 +120,15 @@ func (ss *SecurityService) CreateToken(params CreateTokenParams) (auth.CreateTok
resp, body, err := ss.client.SendPostForm(artifactoryUrl+tokenPath, data, &httpClientsDetails)
tokenInfo := auth.CreateTokenResponseData{}
if err != nil {
return tokenInfo, err
return tokenInfo, fmt.Errorf("%s create token: %w", errorMsgPrefix, err)
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
return tokenInfo, err
return tokenInfo, fmt.Errorf("%s create token: %w", unexpectedServerResponsePrefix, err)
}
if err = json.Unmarshal(body, &tokenInfo); err != nil {
return tokenInfo, errorutils.CheckError(err)
return tokenInfo, errorutils.CheckErrorf("%s create token: %s", couldntParseServerResponsePrefix, err.Error())
}
return tokenInfo, err
return tokenInfo, nil
}

func (ss *SecurityService) GetTokens() (GetTokensResponseData, error) {
Expand All @@ -131,15 +137,15 @@ func (ss *SecurityService) GetTokens() (GetTokensResponseData, error) {
resp, body, _, err := ss.client.SendGet(artifactoryUrl+tokenPath, true, &httpClientsDetails)
tokens := GetTokensResponseData{}
if err != nil {
return tokens, err
return tokens, fmt.Errorf("%s get tokens: %w", errorMsgPrefix, err)
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
return tokens, err
return tokens, fmt.Errorf("%s get tokens: %w", unexpectedServerResponsePrefix, err)
}
if err = json.Unmarshal(body, &tokens); err != nil {
return tokens, errorutils.CheckError(err)
return tokens, errorutils.CheckErrorf("%s get tokens: %s", couldntParseServerResponsePrefix, err.Error())
}
return tokens, err
return tokens, nil
}

func (ss *SecurityService) GetUserTokens(username string) ([]string, error) {
Expand All @@ -163,15 +169,15 @@ func (ss *SecurityService) RefreshToken(params ArtifactoryRefreshTokenParams) (a
resp, body, err := ss.client.SendPostForm(artifactoryUrl+tokenPath, data, &httpClientsDetails)
tokenInfo := auth.CreateTokenResponseData{}
if err != nil {
return tokenInfo, err
return tokenInfo, fmt.Errorf("%s refresh token: %w", errorMsgPrefix, err)
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
return tokenInfo, err
return tokenInfo, fmt.Errorf("%s refresh token: %w", unexpectedServerResponsePrefix, err)
}
if err = json.Unmarshal(body, &tokenInfo); err != nil {
return tokenInfo, errorutils.CheckError(err)
return tokenInfo, errorutils.CheckErrorf("%s refresh token: %s", couldntParseServerResponsePrefix, err.Error())
}
return tokenInfo, err
return tokenInfo, nil
}

func (ss *SecurityService) RevokeToken(params RevokeTokenParams) (string, error) {
Expand All @@ -181,12 +187,12 @@ func (ss *SecurityService) RevokeToken(params RevokeTokenParams) (string, error)
data := buildRevokeTokenUrlValues(params)
resp, body, err := ss.client.SendPostForm(requestFullUrl, data, &httpClientsDetails)
if err != nil {
return "", err
return "", fmt.Errorf("%s revoke token: %w", errorMsgPrefix, err)
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
return "", err
return "", fmt.Errorf("%s revoke token: %w", unexpectedServerResponsePrefix, err)
}
return string(body), err
return string(body), nil
}

func buildCreateTokenUrlValues(params CreateTokenParams) url.Values {
Expand Down
2 changes: 1 addition & 1 deletion artifactory/services/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (ss *SystemService) sendGet(endpoint string) ([]byte, error) {
httpDetails := (*ss.artDetails).CreateHttpClientDetails()
resp, body, _, err := ss.client.SendGet((*ss.artDetails).GetUrl()+apiSystem+endpoint, true, &httpDetails)
if err != nil {
return nil, err
return nil, fmt.Errorf("error occurred while attempting to get JFrog Artifactory %s: %s", endpoint, err.Error())
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK, http.StatusCreated); err != nil {
return nil, fmt.Errorf("got unexpected server response while attempting to get JFrog Artifactory %s:\n%s", endpoint, err.Error())
Expand Down
7 changes: 2 additions & 5 deletions tests/artifactoryctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tests

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -40,8 +39,6 @@ func testCtxTimeout(t *testing.T) {
assert.NoError(t, err)
time.Sleep(time.Millisecond * 300)
_, err = sm.GetVersion()
assert.Error(t, err)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fail()
}
// Expect timeout error
assert.ErrorContains(t, err, context.DeadlineExceeded.Error())
}

0 comments on commit 512e430

Please sign in to comment.