Fix staticcheck ST1005 errors in generated Go client (#13633)

* Fix staticcheck ST1005 errors in go client

* Samples updated

* sample test fix for openapiv3 petstore go client
This commit is contained in:
Maëlick 2022-10-18 16:46:49 +02:00 committed by GitHub
parent 3f4e3afab2
commit 41255c1f18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 81 additions and 81 deletions

View File

@ -127,7 +127,7 @@ func typeCheckParameter(obj interface{}, expected string, name string) error {
// Check the type is as expected. // Check the type is as expected.
if reflect.TypeOf(obj).String() != expected { if reflect.TypeOf(obj).String() != expected {
return fmt.Errorf("Expected %s to be of type %s but received %s.", name, expected, reflect.TypeOf(obj).String()) return fmt.Errorf("expected %s to be of type %s but received %s", name, expected, reflect.TypeOf(obj).String())
} }
return nil return nil
} }
@ -514,7 +514,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
} }
if bodyBuf.Len() == 0 { if bodyBuf.Len() == 0 {
err = fmt.Errorf("Invalid body type %s\n", contentType) err = fmt.Errorf("invalid body type %s\n", contentType)
return nil, err return nil, err
} }
return bodyBuf, nil return bodyBuf, nil

View File

@ -207,7 +207,7 @@ func (c *Configuration) AddDefaultHeader(key string, value string) {
// URL formats template on a index using given variables // URL formats template on a index using given variables
func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) { func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) {
if index < 0 || len(sc) <= index { if index < 0 || len(sc) <= index {
return "", fmt.Errorf("Index %v out of range %v", index, len(sc)-1) return "", fmt.Errorf("index %v out of range %v", index, len(sc)-1)
} }
server := sc[index] server := sc[index]
url := server.URL url := server.URL
@ -222,7 +222,7 @@ func (sc ServerConfigurations) URL(index int, variables map[string]string) (stri
} }
} }
if !found { if !found {
return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) return "", fmt.Errorf("the variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
} }
url = strings.Replace(url, "{"+name+"}", value, -1) url = strings.Replace(url, "{"+name+"}", value, -1)
} else { } else {

View File

@ -22,7 +22,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
var jsonDict map[string]interface{} var jsonDict map[string]interface{}
err = json.Unmarshal(data, &jsonDict) err = json.Unmarshal(data, &jsonDict)
if err != nil { if err != nil {
return fmt.Errorf("Failed to unmarshal JSON into map for the discriminator lookup.") return fmt.Errorf("failed to unmarshal JSON into map for the discriminator lookup")
} }
{{/-first}} {{/-first}}
@ -59,7 +59,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
} }
{{/anyOf}} {{/anyOf}}
return fmt.Errorf("Data failed to match schemas in anyOf({{classname}})") return fmt.Errorf("data failed to match schemas in anyOf({{classname}})")
} }
// Marshal data from the first non-nil pointers in the struct to JSON // Marshal data from the first non-nil pointers in the struct to JSON

View File

@ -33,7 +33,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
var jsonDict map[string]interface{} var jsonDict map[string]interface{}
err = newStrictDecoder(data).Decode(&jsonDict) err = newStrictDecoder(data).Decode(&jsonDict)
if err != nil { if err != nil {
return fmt.Errorf("Failed to unmarshal JSON into map for the discriminator lookup.") return fmt.Errorf("failed to unmarshal JSON into map for the discriminator lookup")
} }
{{/-first}} {{/-first}}
@ -45,7 +45,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
return nil // data stored in dst.{{{modelName}}}, return on the first match return nil // data stored in dst.{{{modelName}}}, return on the first match
} else { } else {
dst.{{{modelName}}} = nil dst.{{{modelName}}} = nil
return fmt.Errorf("Failed to unmarshal {{classname}} as {{{modelName}}}: %s", err.Error()) return fmt.Errorf("failed to unmarshal {{classname}} as {{{modelName}}}: %s", err.Error())
} }
} }
@ -75,11 +75,11 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
{{/oneOf}} {{/oneOf}}
return fmt.Errorf("Data matches more than one schema in oneOf({{classname}})") return fmt.Errorf("data matches more than one schema in oneOf({{classname}})")
} else if match == 1 { } else if match == 1 {
return nil // exactly one match return nil // exactly one match
} else { // no match } else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf({{classname}})") return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
} }
{{/discriminator}} {{/discriminator}}
{{/useOneOfDiscriminatorLookup}} {{/useOneOfDiscriminatorLookup}}
@ -106,11 +106,11 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
{{/oneOf}} {{/oneOf}}
return fmt.Errorf("Data matches more than one schema in oneOf({{classname}})") return fmt.Errorf("data matches more than one schema in oneOf({{classname}})")
} else if match == 1 { } else if match == 1 {
return nil // exactly one match return nil // exactly one match
} else { // no match } else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf({{classname}})") return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
} }
{{/useOneOfDiscriminatorLookup}} {{/useOneOfDiscriminatorLookup}}
} }

View File

@ -126,26 +126,26 @@ func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error {
// are invalid. // are invalid.
func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Context, error) { func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Context, error) {
if h.KeyId == "" { if h.KeyId == "" {
return nil, fmt.Errorf("Key ID must be specified") return nil, fmt.Errorf("key ID must be specified")
} }
if h.PrivateKeyPath == "" && h.privateKey == nil { if h.PrivateKeyPath == "" && h.privateKey == nil {
return nil, fmt.Errorf("Private key path must be specified") return nil, fmt.Errorf("private key path must be specified")
} }
if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok { if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok {
return nil, fmt.Errorf("Invalid signing scheme: '%v'", h.SigningScheme) return nil, fmt.Errorf("invalid signing scheme: '%v'", h.SigningScheme)
} }
m := make(map[string]bool) m := make(map[string]bool)
for _, h := range h.SignedHeaders { for _, h := range h.SignedHeaders {
if strings.EqualFold(h, HttpHeaderAuthorization) { if strings.EqualFold(h, HttpHeaderAuthorization) {
return nil, fmt.Errorf("Signed headers cannot include the 'Authorization' header") return nil, fmt.Errorf("signed headers cannot include the 'Authorization' header")
} }
m[h] = true m[h] = true
} }
if len(m) != len(h.SignedHeaders) { if len(m) != len(h.SignedHeaders) {
return nil, fmt.Errorf("List of signed headers cannot have duplicate names") return nil, fmt.Errorf("list of signed headers cannot have duplicate names")
} }
if h.SignatureMaxValidity < 0 { if h.SignatureMaxValidity < 0 {
return nil, fmt.Errorf("Signature max validity must be a positive value") return nil, fmt.Errorf("signature max validity must be a positive value")
} }
if err := h.loadPrivateKey(); err != nil { if err := h.loadPrivateKey(); err != nil {
return nil, err return nil, err
@ -168,7 +168,7 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) {
default: default:
// Do not change '%T' to anything else such as '%v'! // Do not change '%T' to anything else such as '%v'!
// The value of the private key must not be returned. // The value of the private key must not be returned.
return nil, fmt.Errorf("Unsupported key: %T", h.privateKey) return nil, fmt.Errorf("unsupported key: %T", h.privateKey)
} }
} }
@ -181,7 +181,7 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) {
var file *os.File var file *os.File
file, err = os.Open(h.PrivateKeyPath) file, err = os.Open(h.PrivateKeyPath)
if err != nil { if err != nil {
return fmt.Errorf("Cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err) return fmt.Errorf("cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err)
} }
defer func() { defer func() {
err = file.Close() err = file.Close()
@ -199,7 +199,7 @@ func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error {
pemBlock, _ := pem.Decode(priv) pemBlock, _ := pem.Decode(priv)
if pemBlock == nil { if pemBlock == nil {
// No PEM data has been found. // No PEM data has been found.
return fmt.Errorf("File '%s' does not contain PEM data", h.PrivateKeyPath) return fmt.Errorf("file '%s' does not contain PEM data", h.PrivateKeyPath)
} }
var privKey []byte var privKey []byte
var err error var err error
@ -225,7 +225,7 @@ func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error {
return err return err
} }
default: default:
return fmt.Errorf("Key '%s' is not supported", pemBlock.Type) return fmt.Errorf("key '%s' is not supported", pemBlock.Type)
} }
return nil return nil
} }
@ -248,7 +248,7 @@ func SignRequest(
auth HttpSignatureAuth) error { auth HttpSignatureAuth) error {
if auth.privateKey == nil { if auth.privateKey == nil {
return fmt.Errorf("Private key is not set") return fmt.Errorf("private key is not set")
} }
now := time.Now() now := time.Now()
date := now.UTC().Format(http.TimeFormat) date := now.UTC().Format(http.TimeFormat)
@ -262,7 +262,7 @@ func SignRequest(
var expiresUnix float64 var expiresUnix float64
if auth.SignatureMaxValidity < 0 { if auth.SignatureMaxValidity < 0 {
return fmt.Errorf("Signature validity must be a positive value") return fmt.Errorf("signature validity must be a positive value")
} }
if auth.SignatureMaxValidity > 0 { if auth.SignatureMaxValidity > 0 {
e := now.Add(auth.SignatureMaxValidity) e := now.Add(auth.SignatureMaxValidity)
@ -278,10 +278,10 @@ func SignRequest(
h = crypto.SHA256 h = crypto.SHA256
prefix = "SHA-256=" prefix = "SHA-256="
default: default:
return fmt.Errorf("Unsupported signature scheme: %v", auth.SigningScheme) return fmt.Errorf("unsupported signature scheme: %v", auth.SigningScheme)
} }
if !h.Available() { if !h.Available() {
return fmt.Errorf("Hash '%v' is not available", h) return fmt.Errorf("hash '%v' is not available", h)
} }
// Build the "(request-target)" signature header. // Build the "(request-target)" signature header.
@ -308,7 +308,7 @@ func SignRequest(
m[h] = true m[h] = true
} }
if len(m) != len(signedHeaders) { if len(m) != len(signedHeaders) {
return fmt.Errorf("List of signed headers must not have any duplicates") return fmt.Errorf("list of signed headers must not have any duplicates")
} }
hasCreatedParameter := false hasCreatedParameter := false
hasExpiresParameter := false hasExpiresParameter := false
@ -317,7 +317,7 @@ func SignRequest(
var value string var value string
switch header { switch header {
case strings.ToLower(HttpHeaderAuthorization): case strings.ToLower(HttpHeaderAuthorization):
return fmt.Errorf("Cannot include the 'Authorization' header as a signed header.") return fmt.Errorf("cannot include the 'Authorization' header as a signed header")
case HttpSignatureParameterRequestTarget: case HttpSignatureParameterRequestTarget:
value = requestTarget value = requestTarget
case HttpSignatureParameterCreated: case HttpSignatureParameterCreated:
@ -325,7 +325,7 @@ func SignRequest(
hasCreatedParameter = true hasCreatedParameter = true
case HttpSignatureParameterExpires: case HttpSignatureParameterExpires:
if auth.SignatureMaxValidity.Nanoseconds() == 0 { if auth.SignatureMaxValidity.Nanoseconds() == 0 {
return fmt.Errorf("Cannot set '(expires)' signature parameter. SignatureMaxValidity is not configured.") return fmt.Errorf("cannot set '(expires)' signature parameter. SignatureMaxValidity is not configured")
} }
value = fmt.Sprintf("%.3f", expiresUnix) value = fmt.Sprintf("%.3f", expiresUnix)
hasExpiresParameter = true hasExpiresParameter = true
@ -361,7 +361,7 @@ func SignRequest(
if v, ok = r.Header[canonicalHeader]; !ok { if v, ok = r.Header[canonicalHeader]; !ok {
// If a header specified in the headers parameter cannot be matched with // If a header specified in the headers parameter cannot be matched with
// a provided header in the message, the implementation MUST produce an error. // a provided header in the message, the implementation MUST produce an error.
return fmt.Errorf("Header '%s' does not exist in the request", canonicalHeader) return fmt.Errorf("header '%s' does not exist in the request", canonicalHeader)
} }
// If there are multiple instances of the same header field, all // If there are multiple instances of the same header field, all
// header field values associated with the header field MUST be // header field values associated with the header field MUST be
@ -376,7 +376,7 @@ func SignRequest(
fmt.Fprintf(&sb, "%s: %s", header, value) fmt.Fprintf(&sb, "%s: %s", header, value)
} }
if expiresUnix != 0 && !hasExpiresParameter { if expiresUnix != 0 && !hasExpiresParameter {
return fmt.Errorf("SignatureMaxValidity is specified, but '(expired)' parameter is not present") return fmt.Errorf("signatureMaxValidity is specified, but '(expired)' parameter is not present")
} }
msg := []byte(sb.String()) msg := []byte(sb.String())
msgHash := h.New() msgHash := h.New()
@ -394,14 +394,14 @@ func SignRequest(
case "", HttpSigningAlgorithmRsaPSS: case "", HttpSigningAlgorithmRsaPSS:
signature, err = rsa.SignPSS(rand.Reader, key, h, d, nil) signature, err = rsa.SignPSS(rand.Reader, key, h, d, nil)
default: default:
return fmt.Errorf("Unsupported signing algorithm: '%s'", auth.SigningAlgorithm) return fmt.Errorf("unsupported signing algorithm: '%s'", auth.SigningAlgorithm)
} }
case *ecdsa.PrivateKey: case *ecdsa.PrivateKey:
signature, err = key.Sign(rand.Reader, d, h) signature, err = key.Sign(rand.Reader, d, h)
case ed25519.PrivateKey: // requires go 1.13 case ed25519.PrivateKey: // requires go 1.13
signature, err = key.Sign(rand.Reader, msg, crypto.Hash(0)) signature, err = key.Sign(rand.Reader, msg, crypto.Hash(0))
default: default:
return fmt.Errorf("Unsupported private key") return fmt.Errorf("unsupported private key")
} }
if err != nil { if err != nil {
return err return err

View File

@ -135,7 +135,7 @@ func typeCheckParameter(obj interface{}, expected string, name string) error {
// Check the type is as expected. // Check the type is as expected.
if reflect.TypeOf(obj).String() != expected { if reflect.TypeOf(obj).String() != expected {
return fmt.Errorf("Expected %s to be of type %s but received %s.", name, expected, reflect.TypeOf(obj).String()) return fmt.Errorf("expected %s to be of type %s but received %s", name, expected, reflect.TypeOf(obj).String())
} }
return nil return nil
} }
@ -474,7 +474,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
} }
if bodyBuf.Len() == 0 { if bodyBuf.Len() == 0 {
err = fmt.Errorf("Invalid body type %s\n", contentType) err = fmt.Errorf("invalid body type %s\n", contentType)
return nil, err return nil, err
} }
return bodyBuf, nil return bodyBuf, nil

View File

@ -123,7 +123,7 @@ func (c *Configuration) AddDefaultHeader(key string, value string) {
// URL formats template on a index using given variables // URL formats template on a index using given variables
func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) { func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) {
if index < 0 || len(sc) <= index { if index < 0 || len(sc) <= index {
return "", fmt.Errorf("Index %v out of range %v", index, len(sc)-1) return "", fmt.Errorf("index %v out of range %v", index, len(sc)-1)
} }
server := sc[index] server := sc[index]
url := server.URL url := server.URL
@ -138,7 +138,7 @@ func (sc ServerConfigurations) URL(index int, variables map[string]string) (stri
} }
} }
if !found { if !found {
return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) return "", fmt.Errorf("the variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
} }
url = strings.Replace(url, "{"+name+"}", value, -1) url = strings.Replace(url, "{"+name+"}", value, -1)
} else { } else {

View File

@ -120,7 +120,7 @@ func typeCheckParameter(obj interface{}, expected string, name string) error {
// Check the type is as expected. // Check the type is as expected.
if reflect.TypeOf(obj).String() != expected { if reflect.TypeOf(obj).String() != expected {
return fmt.Errorf("Expected %s to be of type %s but received %s.", name, expected, reflect.TypeOf(obj).String()) return fmt.Errorf("expected %s to be of type %s but received %s", name, expected, reflect.TypeOf(obj).String())
} }
return nil return nil
} }
@ -459,7 +459,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
} }
if bodyBuf.Len() == 0 { if bodyBuf.Len() == 0 {
err = fmt.Errorf("Invalid body type %s\n", contentType) err = fmt.Errorf("invalid body type %s\n", contentType)
return nil, err return nil, err
} }
return bodyBuf, nil return bodyBuf, nil

View File

@ -156,7 +156,7 @@ func (c *Configuration) AddDefaultHeader(key string, value string) {
// URL formats template on a index using given variables // URL formats template on a index using given variables
func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) { func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) {
if index < 0 || len(sc) <= index { if index < 0 || len(sc) <= index {
return "", fmt.Errorf("Index %v out of range %v", index, len(sc)-1) return "", fmt.Errorf("index %v out of range %v", index, len(sc)-1)
} }
server := sc[index] server := sc[index]
url := server.URL url := server.URL
@ -171,7 +171,7 @@ func (sc ServerConfigurations) URL(index int, variables map[string]string) (stri
} }
} }
if !found { if !found {
return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) return "", fmt.Errorf("the variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
} }
url = strings.Replace(url, "{"+name+"}", value, -1) url = strings.Replace(url, "{"+name+"}", value, -1)
} else { } else {

View File

@ -138,7 +138,7 @@ func typeCheckParameter(obj interface{}, expected string, name string) error {
// Check the type is as expected. // Check the type is as expected.
if reflect.TypeOf(obj).String() != expected { if reflect.TypeOf(obj).String() != expected {
return fmt.Errorf("Expected %s to be of type %s but received %s.", name, expected, reflect.TypeOf(obj).String()) return fmt.Errorf("expected %s to be of type %s but received %s", name, expected, reflect.TypeOf(obj).String())
} }
return nil return nil
} }
@ -487,7 +487,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
} }
if bodyBuf.Len() == 0 { if bodyBuf.Len() == 0 {
err = fmt.Errorf("Invalid body type %s\n", contentType) err = fmt.Errorf("invalid body type %s\n", contentType)
return nil, err return nil, err
} }
return bodyBuf, nil return bodyBuf, nil

View File

@ -180,7 +180,7 @@ func (c *Configuration) AddDefaultHeader(key string, value string) {
// URL formats template on a index using given variables // URL formats template on a index using given variables
func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) { func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) {
if index < 0 || len(sc) <= index { if index < 0 || len(sc) <= index {
return "", fmt.Errorf("Index %v out of range %v", index, len(sc)-1) return "", fmt.Errorf("index %v out of range %v", index, len(sc)-1)
} }
server := sc[index] server := sc[index]
url := server.URL url := server.URL
@ -195,7 +195,7 @@ func (sc ServerConfigurations) URL(index int, variables map[string]string) (stri
} }
} }
if !found { if !found {
return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) return "", fmt.Errorf("the variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
} }
url = strings.Replace(url, "{"+name+"}", value, -1) url = strings.Replace(url, "{"+name+"}", value, -1)
} else { } else {

View File

@ -71,11 +71,11 @@ func (dst *Fruit) UnmarshalJSON(data []byte) error {
dst.Apple = nil dst.Apple = nil
dst.Banana = nil dst.Banana = nil
return fmt.Errorf("Data matches more than one schema in oneOf(Fruit)") return fmt.Errorf("data matches more than one schema in oneOf(Fruit)")
} else if match == 1 { } else if match == 1 {
return nil // exactly one match return nil // exactly one match
} else { // no match } else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf(Fruit)") return fmt.Errorf("data failed to match schemas in oneOf(Fruit)")
} }
} }

View File

@ -71,11 +71,11 @@ func (dst *FruitReq) UnmarshalJSON(data []byte) error {
dst.AppleReq = nil dst.AppleReq = nil
dst.BananaReq = nil dst.BananaReq = nil
return fmt.Errorf("Data matches more than one schema in oneOf(FruitReq)") return fmt.Errorf("data matches more than one schema in oneOf(FruitReq)")
} else if match == 1 { } else if match == 1 {
return nil // exactly one match return nil // exactly one match
} else { // no match } else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf(FruitReq)") return fmt.Errorf("data failed to match schemas in oneOf(FruitReq)")
} }
} }

View File

@ -50,7 +50,7 @@ func (dst *GmFruit) UnmarshalJSON(data []byte) error {
dst.Banana = nil dst.Banana = nil
} }
return fmt.Errorf("Data failed to match schemas in anyOf(GmFruit)") return fmt.Errorf("data failed to match schemas in anyOf(GmFruit)")
} }
// Marshal data from the first non-nil pointers in the struct to JSON // Marshal data from the first non-nil pointers in the struct to JSON

View File

@ -71,11 +71,11 @@ func (dst *Mammal) UnmarshalJSON(data []byte) error {
dst.Whale = nil dst.Whale = nil
dst.Zebra = nil dst.Zebra = nil
return fmt.Errorf("Data matches more than one schema in oneOf(Mammal)") return fmt.Errorf("data matches more than one schema in oneOf(Mammal)")
} else if match == 1 { } else if match == 1 {
return nil // exactly one match return nil // exactly one match
} else { // no match } else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf(Mammal)") return fmt.Errorf("data failed to match schemas in oneOf(Mammal)")
} }
} }

View File

@ -93,11 +93,11 @@ func (dst *OneOfPrimitiveType) UnmarshalJSON(data []byte) error {
dst.ArrayOfString = nil dst.ArrayOfString = nil
dst.Int32 = nil dst.Int32 = nil
return fmt.Errorf("Data matches more than one schema in oneOf(OneOfPrimitiveType)") return fmt.Errorf("data matches more than one schema in oneOf(OneOfPrimitiveType)")
} else if match == 1 { } else if match == 1 {
return nil // exactly one match return nil // exactly one match
} else { // no match } else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf(OneOfPrimitiveType)") return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveType)")
} }
} }

View File

@ -135,26 +135,26 @@ func (h *HttpSignatureAuth) SetPrivateKey(privateKey string) error {
// are invalid. // are invalid.
func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Context, error) { func (h *HttpSignatureAuth) ContextWithValue(ctx context.Context) (context.Context, error) {
if h.KeyId == "" { if h.KeyId == "" {
return nil, fmt.Errorf("Key ID must be specified") return nil, fmt.Errorf("key ID must be specified")
} }
if h.PrivateKeyPath == "" && h.privateKey == nil { if h.PrivateKeyPath == "" && h.privateKey == nil {
return nil, fmt.Errorf("Private key path must be specified") return nil, fmt.Errorf("private key path must be specified")
} }
if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok { if _, ok := supportedSigningSchemes[h.SigningScheme]; !ok {
return nil, fmt.Errorf("Invalid signing scheme: '%v'", h.SigningScheme) return nil, fmt.Errorf("invalid signing scheme: '%v'", h.SigningScheme)
} }
m := make(map[string]bool) m := make(map[string]bool)
for _, h := range h.SignedHeaders { for _, h := range h.SignedHeaders {
if strings.EqualFold(h, HttpHeaderAuthorization) { if strings.EqualFold(h, HttpHeaderAuthorization) {
return nil, fmt.Errorf("Signed headers cannot include the 'Authorization' header") return nil, fmt.Errorf("signed headers cannot include the 'Authorization' header")
} }
m[h] = true m[h] = true
} }
if len(m) != len(h.SignedHeaders) { if len(m) != len(h.SignedHeaders) {
return nil, fmt.Errorf("List of signed headers cannot have duplicate names") return nil, fmt.Errorf("list of signed headers cannot have duplicate names")
} }
if h.SignatureMaxValidity < 0 { if h.SignatureMaxValidity < 0 {
return nil, fmt.Errorf("Signature max validity must be a positive value") return nil, fmt.Errorf("signature max validity must be a positive value")
} }
if err := h.loadPrivateKey(); err != nil { if err := h.loadPrivateKey(); err != nil {
return nil, err return nil, err
@ -177,7 +177,7 @@ func (h *HttpSignatureAuth) GetPublicKey() (crypto.PublicKey, error) {
default: default:
// Do not change '%T' to anything else such as '%v'! // Do not change '%T' to anything else such as '%v'!
// The value of the private key must not be returned. // The value of the private key must not be returned.
return nil, fmt.Errorf("Unsupported key: %T", h.privateKey) return nil, fmt.Errorf("unsupported key: %T", h.privateKey)
} }
} }
@ -190,7 +190,7 @@ func (h *HttpSignatureAuth) loadPrivateKey() (err error) {
var file *os.File var file *os.File
file, err = os.Open(h.PrivateKeyPath) file, err = os.Open(h.PrivateKeyPath)
if err != nil { if err != nil {
return fmt.Errorf("Cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err) return fmt.Errorf("cannot load private key '%s'. Error: %v", h.PrivateKeyPath, err)
} }
defer func() { defer func() {
err = file.Close() err = file.Close()
@ -208,7 +208,7 @@ func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error {
pemBlock, _ := pem.Decode(priv) pemBlock, _ := pem.Decode(priv)
if pemBlock == nil { if pemBlock == nil {
// No PEM data has been found. // No PEM data has been found.
return fmt.Errorf("File '%s' does not contain PEM data", h.PrivateKeyPath) return fmt.Errorf("file '%s' does not contain PEM data", h.PrivateKeyPath)
} }
var privKey []byte var privKey []byte
var err error var err error
@ -234,7 +234,7 @@ func (h *HttpSignatureAuth) parsePrivateKey(priv []byte) error {
return err return err
} }
default: default:
return fmt.Errorf("Key '%s' is not supported", pemBlock.Type) return fmt.Errorf("key '%s' is not supported", pemBlock.Type)
} }
return nil return nil
} }
@ -257,7 +257,7 @@ func SignRequest(
auth HttpSignatureAuth) error { auth HttpSignatureAuth) error {
if auth.privateKey == nil { if auth.privateKey == nil {
return fmt.Errorf("Private key is not set") return fmt.Errorf("private key is not set")
} }
now := time.Now() now := time.Now()
date := now.UTC().Format(http.TimeFormat) date := now.UTC().Format(http.TimeFormat)
@ -271,7 +271,7 @@ func SignRequest(
var expiresUnix float64 var expiresUnix float64
if auth.SignatureMaxValidity < 0 { if auth.SignatureMaxValidity < 0 {
return fmt.Errorf("Signature validity must be a positive value") return fmt.Errorf("signature validity must be a positive value")
} }
if auth.SignatureMaxValidity > 0 { if auth.SignatureMaxValidity > 0 {
e := now.Add(auth.SignatureMaxValidity) e := now.Add(auth.SignatureMaxValidity)
@ -287,10 +287,10 @@ func SignRequest(
h = crypto.SHA256 h = crypto.SHA256
prefix = "SHA-256=" prefix = "SHA-256="
default: default:
return fmt.Errorf("Unsupported signature scheme: %v", auth.SigningScheme) return fmt.Errorf("unsupported signature scheme: %v", auth.SigningScheme)
} }
if !h.Available() { if !h.Available() {
return fmt.Errorf("Hash '%v' is not available", h) return fmt.Errorf("hash '%v' is not available", h)
} }
// Build the "(request-target)" signature header. // Build the "(request-target)" signature header.
@ -317,7 +317,7 @@ func SignRequest(
m[h] = true m[h] = true
} }
if len(m) != len(signedHeaders) { if len(m) != len(signedHeaders) {
return fmt.Errorf("List of signed headers must not have any duplicates") return fmt.Errorf("list of signed headers must not have any duplicates")
} }
hasCreatedParameter := false hasCreatedParameter := false
hasExpiresParameter := false hasExpiresParameter := false
@ -326,7 +326,7 @@ func SignRequest(
var value string var value string
switch header { switch header {
case strings.ToLower(HttpHeaderAuthorization): case strings.ToLower(HttpHeaderAuthorization):
return fmt.Errorf("Cannot include the 'Authorization' header as a signed header.") return fmt.Errorf("cannot include the 'Authorization' header as a signed header")
case HttpSignatureParameterRequestTarget: case HttpSignatureParameterRequestTarget:
value = requestTarget value = requestTarget
case HttpSignatureParameterCreated: case HttpSignatureParameterCreated:
@ -334,7 +334,7 @@ func SignRequest(
hasCreatedParameter = true hasCreatedParameter = true
case HttpSignatureParameterExpires: case HttpSignatureParameterExpires:
if auth.SignatureMaxValidity.Nanoseconds() == 0 { if auth.SignatureMaxValidity.Nanoseconds() == 0 {
return fmt.Errorf("Cannot set '(expires)' signature parameter. SignatureMaxValidity is not configured.") return fmt.Errorf("cannot set '(expires)' signature parameter. SignatureMaxValidity is not configured")
} }
value = fmt.Sprintf("%.3f", expiresUnix) value = fmt.Sprintf("%.3f", expiresUnix)
hasExpiresParameter = true hasExpiresParameter = true
@ -370,7 +370,7 @@ func SignRequest(
if v, ok = r.Header[canonicalHeader]; !ok { if v, ok = r.Header[canonicalHeader]; !ok {
// If a header specified in the headers parameter cannot be matched with // If a header specified in the headers parameter cannot be matched with
// a provided header in the message, the implementation MUST produce an error. // a provided header in the message, the implementation MUST produce an error.
return fmt.Errorf("Header '%s' does not exist in the request", canonicalHeader) return fmt.Errorf("header '%s' does not exist in the request", canonicalHeader)
} }
// If there are multiple instances of the same header field, all // If there are multiple instances of the same header field, all
// header field values associated with the header field MUST be // header field values associated with the header field MUST be
@ -385,7 +385,7 @@ func SignRequest(
fmt.Fprintf(&sb, "%s: %s", header, value) fmt.Fprintf(&sb, "%s: %s", header, value)
} }
if expiresUnix != 0 && !hasExpiresParameter { if expiresUnix != 0 && !hasExpiresParameter {
return fmt.Errorf("SignatureMaxValidity is specified, but '(expired)' parameter is not present") return fmt.Errorf("signatureMaxValidity is specified, but '(expired)' parameter is not present")
} }
msg := []byte(sb.String()) msg := []byte(sb.String())
msgHash := h.New() msgHash := h.New()
@ -403,14 +403,14 @@ func SignRequest(
case "", HttpSigningAlgorithmRsaPSS: case "", HttpSigningAlgorithmRsaPSS:
signature, err = rsa.SignPSS(rand.Reader, key, h, d, nil) signature, err = rsa.SignPSS(rand.Reader, key, h, d, nil)
default: default:
return fmt.Errorf("Unsupported signing algorithm: '%s'", auth.SigningAlgorithm) return fmt.Errorf("unsupported signing algorithm: '%s'", auth.SigningAlgorithm)
} }
case *ecdsa.PrivateKey: case *ecdsa.PrivateKey:
signature, err = key.Sign(rand.Reader, d, h) signature, err = key.Sign(rand.Reader, d, h)
case ed25519.PrivateKey: // requires go 1.13 case ed25519.PrivateKey: // requires go 1.13
signature, err = key.Sign(rand.Reader, msg, crypto.Hash(0)) signature, err = key.Sign(rand.Reader, msg, crypto.Hash(0))
default: default:
return fmt.Errorf("Unsupported private key") return fmt.Errorf("unsupported private key")
} }
if err != nil { if err != nil {
return err return err

View File

@ -650,7 +650,7 @@ func TestInvalidHttpSignatureConfiguration(t *testing.T) {
authConfig = sw.HttpSignatureAuth{} authConfig = sw.HttpSignatureAuth{}
_, err = authConfig.ContextWithValue(context.Background()) _, err = authConfig.ContextWithValue(context.Background())
if err == nil || !strings.Contains(err.Error(), "Key ID must be specified") { if err == nil || !strings.Contains(err.Error(), "key ID must be specified") {
t.Fatalf("Invalid configuration: %v", err) t.Fatalf("Invalid configuration: %v", err)
} }
@ -658,7 +658,7 @@ func TestInvalidHttpSignatureConfiguration(t *testing.T) {
KeyId: "my-key-id", KeyId: "my-key-id",
} }
_, err = authConfig.ContextWithValue(context.Background()) _, err = authConfig.ContextWithValue(context.Background())
if err == nil || !strings.Contains(err.Error(), "Private key path must be specified") { if err == nil || !strings.Contains(err.Error(), "private key path must be specified") {
t.Fatalf("Invalid configuration: %v", err) t.Fatalf("Invalid configuration: %v", err)
} }
@ -667,7 +667,7 @@ func TestInvalidHttpSignatureConfiguration(t *testing.T) {
PrivateKeyPath: "test.pem", PrivateKeyPath: "test.pem",
} }
_, err = authConfig.ContextWithValue(context.Background()) _, err = authConfig.ContextWithValue(context.Background())
if err == nil || !strings.Contains(err.Error(), "Invalid signing scheme") { if err == nil || !strings.Contains(err.Error(), "invalid signing scheme") {
t.Fatalf("Invalid configuration: %v", err) t.Fatalf("Invalid configuration: %v", err)
} }
@ -677,7 +677,7 @@ func TestInvalidHttpSignatureConfiguration(t *testing.T) {
SigningScheme: "garbage", SigningScheme: "garbage",
} }
_, err = authConfig.ContextWithValue(context.Background()) _, err = authConfig.ContextWithValue(context.Background())
if err == nil || !strings.Contains(err.Error(), "Invalid signing scheme") { if err == nil || !strings.Contains(err.Error(), "invalid signing scheme") {
t.Fatalf("Invalid configuration: %v", err) t.Fatalf("Invalid configuration: %v", err)
} }
@ -699,7 +699,7 @@ func TestInvalidHttpSignatureConfiguration(t *testing.T) {
SignedHeaders: []string{"foo", "bar", "Authorization"}, SignedHeaders: []string{"foo", "bar", "Authorization"},
} }
_, err = authConfig.ContextWithValue(context.Background()) _, err = authConfig.ContextWithValue(context.Background())
if err == nil || !strings.Contains(err.Error(), "Signed headers cannot include the 'Authorization' header") { if err == nil || !strings.Contains(err.Error(), "signed headers cannot include the 'Authorization' header") {
t.Fatalf("Invalid configuration: %v", err) t.Fatalf("Invalid configuration: %v", err)
} }
@ -711,7 +711,7 @@ func TestInvalidHttpSignatureConfiguration(t *testing.T) {
SignatureMaxValidity: -7 * time.Minute, SignatureMaxValidity: -7 * time.Minute,
} }
_, err = authConfig.ContextWithValue(context.Background()) _, err = authConfig.ContextWithValue(context.Background())
if err == nil || !strings.Contains(err.Error(), "Signature max validity must be a positive value") { if err == nil || !strings.Contains(err.Error(), "signature max validity must be a positive value") {
t.Fatalf("Invalid configuration: %v", err) t.Fatalf("Invalid configuration: %v", err)
} }
} }