This commit makes the `crypto.Error` type a native go (string) type. That allows us to define error values as constants instead of variables. For reference see: - https://twitter.com/_aead_/status/1118170258215514115?s=20 - https://dave.cheney.net/2016/04/07/constant-errors
Unified · Split
| @@ -1723,6 +1723,12 @@ func toAPIError(ctx context.Context, err error) APIError { · 1722 unmodified lines | |||
| 1723 | // their internal error types. This code is only | 1723 | // their internal error types. This code is only |
| 1724 | // useful with gateway implementations. | 1724 | // useful with gateway implementations. |
| 1725 | switch e := err.(type) { | 1725 | switch e := err.(type) { |
| 1726 | case crypto.Error: | ||
| 1727 | apiErr = APIError{ | ||
| 1728 | Code: "XKMSInternalError", | ||
| 1729 | Description: e.Error(), | ||
| 1730 | HTTPStatusCode: http.StatusBadRequest, | ||
| 1731 | } | ||
| 1726 | case minio.ErrorResponse: | 1732 | case minio.ErrorResponse: |
| 1727 | apiErr = APIError{ | 1733 | apiErr = APIError{ |
| 1728 | Code: e.Code, | 1734 | Code: e.Code, |
| @@ -19,9 +19,9 @@ import "errors" · 18 unmodified lines | |||
| 19 | // Error is the generic type for any error happening during decrypting | 19 | // Error is the generic type for any error happening during decrypting |
| 20 | // an object. It indicates that the object itself or its metadata was | 20 | // an object. It indicates that the object itself or its metadata was |
| 21 | // modified accidentally or maliciously. | 21 | // modified accidentally or maliciously. |
| 22 | type Error struct{ msg string } | 22 | type Error string |
| 23 | 23 | ||
| 24 | func (e Error) Error() string { return e.msg } | 24 | func (e Error) Error() string { return string(e) } |
| 25 | 25 | ||
| 26 | var ( | 26 | var ( |
| 27 | // ErrInvalidEncryptionMethod indicates that the specified SSE encryption method | 27 | // ErrInvalidEncryptionMethod indicates that the specified SSE encryption method |
| @@ -56,14 +56,14 @@ var ( · 28 unmodified lines | |||
| 56 | ErrIncompatibleEncryptionMethod = errors.New("Server side encryption specified with both SSE-C and SSE-S3 headers") | 56 | ErrIncompatibleEncryptionMethod = errors.New("Server side encryption specified with both SSE-C and SSE-S3 headers") |
| 57 | ) | 57 | ) |
| 58 | 58 | ||
| 59 | var ( | 59 | const ( |
| 60 | errMissingInternalIV = Error{"The object metadata is missing the internal encryption IV"} | 60 | errMissingInternalIV Error = "The object metadata is missing the internal encryption IV" |
| 61 | errMissingInternalSealAlgorithm = Error{"The object metadata is missing the internal seal algorithm"} | 61 | errMissingInternalSealAlgorithm Error = "The object metadata is missing the internal seal algorithm" |
| 62 | 62 | ||
| 63 | errInvalidInternalIV = Error{"The internal encryption IV is malformed"} | 63 | errInvalidInternalIV Error = "The internal encryption IV is malformed" |
| 64 | errInvalidInternalSealAlgorithm = Error{"The internal seal algorithm is invalid and not supported"} | 64 | errInvalidInternalSealAlgorithm Error = "The internal seal algorithm is invalid and not supported" |
| 65 | 65 | ||
| 66 | errMissingUpdatedKey = Error{"The key update returned no error but also no sealed key"} | 66 | errMissingUpdatedKey Error = "The key update returned no error but also no sealed key" |
| 67 | ) | 67 | ) |
| 68 | 68 | ||
| 69 | var ( | 69 | var ( |
| @@ -108,7 +108,7 @@ func (key *ObjectKey) Unseal(extKey [32]byte, sealedKey SealedKey, domain, bucke · 107 unmodified lines | |||
| 108 | ) | 108 | ) |
| 109 | switch sealedKey.Algorithm { | 109 | switch sealedKey.Algorithm { |
| 110 | default: | 110 | default: |
| 111 | return Error{fmt.Sprintf("The sealing algorithm '%s' is not supported", sealedKey.Algorithm)} | 111 | return Error(fmt.Sprintf("The sealing algorithm '%s' is not supported", sealedKey.Algorithm)) |
| 112 | case SealAlgorithm: | 112 | case SealAlgorithm: |
| 113 | mac := hmac.New(sha256.New, extKey[:]) | 113 | mac := hmac.New(sha256.New, extKey[:]) |
| 114 | mac.Write(sealedKey.IV[:]) | 114 | mac.Write(sealedKey.IV[:]) |
| @@ -172,7 +172,7 @@ func (s3) ParseMetadata(metadata map[string]string) (keyID string, kmsKey []byte · 171 unmodified lines | |||
| 172 | } | 172 | } |
| 173 | b64SealedKey, ok := metadata[S3SealedKey] | 173 | b64SealedKey, ok := metadata[S3SealedKey] |
| 174 | if !ok { | 174 | if !ok { |
| 175 | return keyID, kmsKey, sealedKey, Error{"The object metadata is missing the internal sealed key for SSE-S3"} | 175 | return keyID, kmsKey, sealedKey, Error("The object metadata is missing the internal sealed key for SSE-S3") |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | // There are two possibilites: | 178 | // There are two possibilites: |
| @@ -182,10 +182,10 @@ func (s3) ParseMetadata(metadata map[string]string) (keyID string, kmsKey []byte · 3 unmodified lines | |||
| 182 | keyID, idPresent := metadata[S3KMSKeyID] | 182 | keyID, idPresent := metadata[S3KMSKeyID] |
| 183 | b64KMSSealedKey, kmsKeyPresent := metadata[S3KMSSealedKey] | 183 | b64KMSSealedKey, kmsKeyPresent := metadata[S3KMSSealedKey] |
| 184 | if !idPresent && kmsKeyPresent { | 184 | if !idPresent && kmsKeyPresent { |
| 185 | return keyID, kmsKey, sealedKey, Error{"The object metadata is missing the internal KMS key-ID for SSE-S3"} | 185 | return keyID, kmsKey, sealedKey, Error("The object metadata is missing the internal KMS key-ID for SSE-S3") |
| 186 | } | 186 | } |
| 187 | if idPresent && !kmsKeyPresent { | 187 | if idPresent && !kmsKeyPresent { |
| 188 | return keyID, kmsKey, sealedKey, Error{"The object metadata is missing the internal sealed KMS data key for SSE-S3"} | 188 | return keyID, kmsKey, sealedKey, Error("The object metadata is missing the internal sealed KMS data key for SSE-S3") |
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | // Check whether all extracted values are well-formed | 191 | // Check whether all extracted values are well-formed |
| @@ -198,12 +198,12 @@ func (s3) ParseMetadata(metadata map[string]string) (keyID string, kmsKey []byte · 6 unmodified lines | |||
| 198 | } | 198 | } |
| 199 | encryptedKey, err := base64.StdEncoding.DecodeString(b64SealedKey) | 199 | encryptedKey, err := base64.StdEncoding.DecodeString(b64SealedKey) |
| 200 | if err != nil || len(encryptedKey) != 64 { | 200 | if err != nil || len(encryptedKey) != 64 { |
| 201 | return keyID, kmsKey, sealedKey, Error{"The internal sealed key for SSE-S3 is invalid"} | 201 | return keyID, kmsKey, sealedKey, Error("The internal sealed key for SSE-S3 is invalid") |
| 202 | } | 202 | } |
| 203 | if idPresent && kmsKeyPresent { // We are using a KMS -> parse the sealed KMS data key. | 203 | if idPresent && kmsKeyPresent { // We are using a KMS -> parse the sealed KMS data key. |
| 204 | kmsKey, err = base64.StdEncoding.DecodeString(b64KMSSealedKey) | 204 | kmsKey, err = base64.StdEncoding.DecodeString(b64KMSSealedKey) |
| 205 | if err != nil { | 205 | if err != nil { |
| 206 | return keyID, kmsKey, sealedKey, Error{"The internal sealed KMS data key for SSE-S3 is invalid"} | 206 | return keyID, kmsKey, sealedKey, Error("The internal sealed KMS data key for SSE-S3 is invalid") |
| 207 | } | 207 | } |
| 208 | } | 208 | } |
| 209 | 209 | ||
| @@ -244,7 +244,7 @@ func (ssec) ParseMetadata(metadata map[string]string) (sealedKey SealedKey, err · 34 unmodified lines | |||
| 244 | } | 244 | } |
| 245 | b64SealedKey, ok := metadata[SSECSealedKey] | 245 | b64SealedKey, ok := metadata[SSECSealedKey] |
| 246 | if !ok { | 246 | if !ok { |
| 247 | return sealedKey, Error{"The object metadata is missing the internal sealed key for SSE-C"} | 247 | return sealedKey, Error("The object metadata is missing the internal sealed key for SSE-C") |
| 248 | } | 248 | } |
| 249 | 249 | ||
| 250 | // Check whether all extracted values are well-formed | 250 | // Check whether all extracted values are well-formed |
| @@ -257,7 +257,7 @@ func (ssec) ParseMetadata(metadata map[string]string) (sealedKey SealedKey, err · 6 unmodified lines | |||
| 257 | } | 257 | } |
| 258 | encryptedKey, err := base64.StdEncoding.DecodeString(b64SealedKey) | 258 | encryptedKey, err := base64.StdEncoding.DecodeString(b64SealedKey) |
| 259 | if err != nil || len(encryptedKey) != 64 { | 259 | if err != nil || len(encryptedKey) != 64 { |
| 260 | return sealedKey, Error{"The internal sealed key for SSE-C is invalid"} | 260 | return sealedKey, Error("The internal sealed key for SSE-C is invalid") |
| 261 | } | 261 | } |
| 262 | 262 | ||
| 263 | sealedKey.Algorithm = algorithm | 263 | sealedKey.Algorithm = algorithm |
| @@ -125,15 +125,15 @@ var s3ParseMetadataTests = []struct { · 124 unmodified lines | |||
| 125 | DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, | 125 | DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, |
| 126 | }, // 1 | 126 | }, // 1 |
| 127 | { | 127 | { |
| 128 | ExpectedErr: Error{"The object metadata is missing the internal sealed key for SSE-S3"}, | 128 | ExpectedErr: Error("The object metadata is missing the internal sealed key for SSE-S3"), |
| 129 | Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: ""}, DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, | 129 | Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: ""}, DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, |
| 130 | }, // 2 | 130 | }, // 2 |
| 131 | { | 131 | { |
| 132 | ExpectedErr: Error{"The object metadata is missing the internal KMS key-ID for SSE-S3"}, | 132 | ExpectedErr: Error("The object metadata is missing the internal KMS key-ID for SSE-S3"), |
| 133 | Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: "", S3SealedKey: "", S3KMSSealedKey: "IAAF0b=="}, DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, | 133 | Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: "", S3SealedKey: "", S3KMSSealedKey: "IAAF0b=="}, DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, |
| 134 | }, // 3 | 134 | }, // 3 |
| 135 | { | 135 | { |
| 136 | ExpectedErr: Error{"The object metadata is missing the internal sealed KMS data key for SSE-S3"}, | 136 | ExpectedErr: Error("The object metadata is missing the internal sealed KMS data key for SSE-S3"), |
| 137 | Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: "", S3SealedKey: "", S3KMSKeyID: ""}, | 137 | Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: "", S3SealedKey: "", S3KMSKeyID: ""}, |
| 138 | DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, | 138 | DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, |
| 139 | }, // 4 | 139 | }, // 4 |
| @@ -150,7 +150,7 @@ var s3ParseMetadataTests = []struct { · 10 unmodified lines | |||
| 150 | DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, | 150 | DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, |
| 151 | }, // 6 | 151 | }, // 6 |
| 152 | { | 152 | { |
| 153 | ExpectedErr: Error{"The internal sealed key for SSE-S3 is invalid"}, | 153 | ExpectedErr: Error("The internal sealed key for SSE-S3 is invalid"), |
| 154 | Metadata: map[string]string{ | 154 | Metadata: map[string]string{ |
| 155 | SSEIV: base64.StdEncoding.EncodeToString(make([]byte, 32)), SSESealAlgorithm: SealAlgorithm, S3SealedKey: "", | 155 | SSEIV: base64.StdEncoding.EncodeToString(make([]byte, 32)), SSESealAlgorithm: SealAlgorithm, S3SealedKey: "", |
| 156 | S3KMSKeyID: "", S3KMSSealedKey: "", | 156 | S3KMSKeyID: "", S3KMSSealedKey: "", |
| @@ -158,7 +158,7 @@ var s3ParseMetadataTests = []struct { · 1 unmodified line | |||
| 158 | DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, | 158 | DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{}, |
| 159 | }, // 7 | 159 | }, // 7 |
| 160 | { | 160 | { |
| 161 | ExpectedErr: Error{"The internal sealed KMS data key for SSE-S3 is invalid"}, | 161 | ExpectedErr: Error("The internal sealed KMS data key for SSE-S3 is invalid"), |
| 162 | Metadata: map[string]string{ | 162 | Metadata: map[string]string{ |
| 163 | SSEIV: base64.StdEncoding.EncodeToString(make([]byte, 32)), SSESealAlgorithm: SealAlgorithm, | 163 | SSEIV: base64.StdEncoding.EncodeToString(make([]byte, 32)), SSESealAlgorithm: SealAlgorithm, |
| 164 | S3SealedKey: base64.StdEncoding.EncodeToString(make([]byte, 64)), S3KMSKeyID: "key-1", | 164 | S3SealedKey: base64.StdEncoding.EncodeToString(make([]byte, 64)), S3KMSKeyID: "key-1", |
| @@ -218,7 +218,7 @@ var ssecParseMetadataTests = []struct { · 53 unmodified lines | |||
| 218 | {ExpectedErr: errMissingInternalIV, Metadata: map[string]string{}, SealedKey: SealedKey{}}, // 0 | 218 | {ExpectedErr: errMissingInternalIV, Metadata: map[string]string{}, SealedKey: SealedKey{}}, // 0 |
| 219 | {ExpectedErr: errMissingInternalSealAlgorithm, Metadata: map[string]string{SSEIV: ""}, SealedKey: SealedKey{}}, // 1 | 219 | {ExpectedErr: errMissingInternalSealAlgorithm, Metadata: map[string]string{SSEIV: ""}, SealedKey: SealedKey{}}, // 1 |
| 220 | { | 220 | { |
| 221 | ExpectedErr: Error{"The object metadata is missing the internal sealed key for SSE-C"}, | 221 | ExpectedErr: Error("The object metadata is missing the internal sealed key for SSE-C"), |
| 222 | Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: ""}, SealedKey: SealedKey{}, | 222 | Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: ""}, SealedKey: SealedKey{}, |
| 223 | }, // 2 | 223 | }, // 2 |
| 224 | { | 224 | { |
| @@ -233,7 +233,7 @@ var ssecParseMetadataTests = []struct { · 8 unmodified lines | |||
| 233 | SealedKey: SealedKey{}, | 233 | SealedKey: SealedKey{}, |
| 234 | }, // 4 | 234 | }, // 4 |
| 235 | { | 235 | { |
| 236 | ExpectedErr: Error{"The internal sealed key for SSE-C is invalid"}, | 236 | ExpectedErr: Error("The internal sealed key for SSE-C is invalid"), |
| 237 | Metadata: map[string]string{ | 237 | Metadata: map[string]string{ |
| 238 | SSEIV: base64.StdEncoding.EncodeToString(make([]byte, 32)), SSESealAlgorithm: SealAlgorithm, SSECSealedKey: "", | 238 | SSEIV: base64.StdEncoding.EncodeToString(make([]byte, 32)), SSESealAlgorithm: SealAlgorithm, SSECSealedKey: "", |
| 239 | }, | 239 | }, |