Skip to content

Commit

Permalink
[feat][enhance] remove sentitive information out of response headers …
Browse files Browse the repository at this point in the history
…as receiving error, add error category
  • Loading branch information
cuongpiger committed Sep 9, 2024
1 parent 6d477a0 commit 7950e32
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 14 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/vngcloud/vngcloud-go-sdk/v2
go 1.22

require (
github.com/cuongpiger/joat v1.0.12
github.com/imroc/req/v3 v3.43.7
github.com/cuongpiger/joat v1.0.13
github.com/imroc/req/v3 v3.44.0
github.com/joho/godotenv v1.5.1
)

Expand All @@ -15,7 +15,7 @@ require (
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/onsi/ginkgo/v2 v2.16.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/quic-go v0.41.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cuongpiger/joat v1.0.12 h1:jWaWJ0yI9iotmU41yvkgrdoC57tg0lL1Hh5c2ErzUso=
github.com/cuongpiger/joat v1.0.12/go.mod h1:Oi1GvzXKHc0nlEnh9pGCFK13h8+EhDnWmfv2TiNH35g=
github.com/cuongpiger/joat v1.0.13 h1:AVLRz2Dsojs8MZOW+/aoXsL5uKEfr0RXYd4wX2ixXrU=
github.com/cuongpiger/joat v1.0.13/go.mod h1:Oi1GvzXKHc0nlEnh9pGCFK13h8+EhDnWmfv2TiNH35g=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -22,12 +22,12 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/imroc/req/v3 v3.43.7 h1:dOcNb9n0X83N5/5/AOkiU+cLhzx8QFXjv5MhikazzQA=
github.com/imroc/req/v3 v3.43.7/go.mod h1:SQIz5iYop16MJxbo8ib+4LnostGCok8NQf8ToyQc2xA=
github.com/imroc/req/v3 v3.44.0 h1:JBrxoN5DykYvkeEZx3dski8/A+WVz9HD147cwEeyFlM=
github.com/imroc/req/v3 v3.44.0/go.mod h1:6cQ91w7qPn+bYjcWnA1S/LmTh6z3cz8Oa9qdwm7gS8w=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/onsi/ginkgo/v2 v2.16.0 h1:7q1w9frJDzninhXxjZd+Y/x54XNjG/UlRLIYPZafsPM=
github.com/onsi/ginkgo/v2 v2.16.0/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
Expand Down
9 changes: 8 additions & 1 deletion vngcloud/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,18 @@ func (s *reauthFuture) set(err lserr.IError) {
}

func defaultErrorResponse(perr error, purl string, preq IRequest, resp *lreq.Response) lserr.IError {
headers := preq.GetMoreHeaders()

// Remove sensitive information
if headers != nil {
delete(headers, "Authorization")
}

return lserr.ErrorHandler(perr).WithKVparameters(
"statusCode", resp.StatusCode,
"url", purl,
"method", preq.GetRequestMethod(),
"requestHeaders", preq.GetMoreHeaders(),
"requestHeaders", headers,
"responseHeaders", resp.Header,
)
}
12 changes: 12 additions & 0 deletions vngcloud/sdk_error/categories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package sdk_error

// quota error category
const (
ErrCatQuota ErrorCategory = "quota"
)

// login error category

const (
ErrCatIam ErrorCategory = "iam"
)
10 changes: 8 additions & 2 deletions vngcloud/sdk_error/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
lfmt "fmt"
)

const (
loginFailedPrefixMsg = "There are some problems with your service account key pair, please re-generate a new one. Error message: %s"
)

func WithErrorAuthenticationFailed(perrResp IErrorRespone) func(IError) {
return func(sdkErr IError) {
if perrResp == nil {
Expand All @@ -17,7 +21,8 @@ func WithErrorAuthenticationFailed(perrResp IErrorRespone) func(IError) {
if perrResp.GetError().Error() == "AUTHENTICATION_FAILED" {
sdkErr.WithErrorCode(EcAuthenticationFailed).
WithErrors(perrResp.GetError()).
WithMessage(perrResp.GetMessage())
WithMessage(lfmt.Sprintf(loginFailedPrefixMsg, perrResp.GetMessage())).
WithErrorCategories(ErrCatIam)
}
}
}
Expand All @@ -43,7 +48,8 @@ func WithErrorTooManyFailedLogin(perrResp IErrorRespone) func(IError) {
if perrResp.GetError().Error() == "TOO_MANY_FAILED_LOGINS" {
sdkErr.WithErrorCode(EcTooManyFailedLogins).
WithErrors(perrResp.GetError()).
WithMessage(perrResp.GetMessage())
WithMessage(lfmt.Sprintf(loginFailedPrefixMsg, perrResp.GetMessage())).
WithErrorCategories(ErrCatIam)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions vngcloud/sdk_error/isdk_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type IError interface {
WithErrorCode(perrCode ErrorCode) IError
WithMessage(pmsg string) IError
WithErrors(perrs ...error) IError
WithErrorCategories(pcategories ...ErrorCategory) IError
WithParameters(pparams map[string]interface{}) IError
WithKVparameters(pparams ...interface{}) IError

Expand Down
15 changes: 15 additions & 0 deletions vngcloud/sdk_error/sdk_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sdk_error
import (
lerrors "errors"
lfmt "fmt"

ljset "github.com/cuongpiger/joat/data-structure/set"
)

var (
Expand All @@ -14,10 +16,13 @@ type (
error error
errorCode ErrorCode
message string
categories ljset.Set[ErrorCategory]
parameters map[string]interface{}
}

ErrorCode string

ErrorCategory string
)

func (s *SdkError) IsError(perrCode ErrorCode) bool {
Expand Down Expand Up @@ -61,6 +66,16 @@ func (s *SdkError) WithErrors(perrs ...error) IError {
return s
}

func (s *SdkError) WithErrorCategories(pcategories ...ErrorCategory) IError {
if s.categories == nil {
s.categories = ljset.NewSet[ErrorCategory](pcategories...)
} else {
s.categories.Append(pcategories...)
}

return s
}

func (s *SdkError) WithParameters(pparams map[string]interface{}) IError {
if s.parameters == nil {
s.parameters = pparams
Expand Down
3 changes: 2 additions & 1 deletion vngcloud/sdk_error/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ func WithErrorServerExceedQuota(perrResp IErrorRespone) func(sdkError IError) {
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerExceedQuota) {
sdkError.WithErrorCode(EcVServerServerExceedQuota).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
WithErrors(perrResp.GetError()).
WithErrorCategories(ErrCatQuota)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion vngcloud/services/identity/v2/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *IdentityServiceV2) GetAccessToken(popts IGetAccessTokenRequest) (*lsent
lserr.WithErrorTooManyFailedLogin(errResp),
lserr.WithErrorAuthenticationFailed(errResp),
lserr.WithErrorUnknownAuthFailure(errResp)). // Always put this handler at the end
WithKVparameters("clientId", popts.GetClientId(), "clientSecret", popts.GetClientSecret())
WithKVparameters("clientId", popts.GetClientId())
}

return resp.ToEntityAccessToken(), nil
Expand Down

0 comments on commit 7950e32

Please sign in to comment.