History

make the crypto error type a native go type (#8267)

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

Andreas Auernhammer committed 7 years ago

Commit ffded5a1 parent 11272935 files · +29 −23Browse files

Unified · Split

5 files
  1. cmd/api-errors.go +6 −0
  2. cmd/crypto/error.go +8 −8
  3. cmd/crypto/key.go +1 −1
  4. cmd/crypto/metadata.go +7 −7
  5. cmd/crypto/metadata_test.go +7 −7
cmd/api-errors.go+6−0
@@ -1723,6 +1723,12 @@ func toAPIError(ctx context.Context, err error) APIError { · 1722 unmodified lines
17231723 // their internal error types. This code is only
17241724 // useful with gateway implementations.
17251725 switch e := err.(type) {
1726+ case crypto.Error:
1727+ apiErr = APIError{
1728+ Code: "XKMSInternalError",
1729+ Description: e.Error(),
1730+ HTTPStatusCode: http.StatusBadRequest,
1731+ }
17261732 case minio.ErrorResponse:
17271733 apiErr = APIError{
17281734 Code: e.Code,
cmd/crypto/error.go+8−8
@@ -19,9 +19,9 @@ import "errors" · 18 unmodified lines
1919 // Error is the generic type for any error happening during decrypting
2020 // an object. It indicates that the object itself or its metadata was
2121 // modified accidentally or maliciously.
22−type Error struct{ msg string }
22+type Error string
2323
24−func (e Error) Error() string { return e.msg }
24+func (e Error) Error() string { return string(e) }
2525
2626 var (
2727 // ErrInvalidEncryptionMethod indicates that the specified SSE encryption method
@@ -56,14 +56,14 @@ var ( · 28 unmodified lines
5656 ErrIncompatibleEncryptionMethod = errors.New("Server side encryption specified with both SSE-C and SSE-S3 headers")
5757 )
5858
59−var (
60− errMissingInternalIV = Error{"The object metadata is missing the internal encryption IV"}
61− errMissingInternalSealAlgorithm = Error{"The object metadata is missing the internal seal algorithm"}
59+const (
60+ errMissingInternalIV Error = "The object metadata is missing the internal encryption IV"
61+ errMissingInternalSealAlgorithm Error = "The object metadata is missing the internal seal algorithm"
6262
63− errInvalidInternalIV = Error{"The internal encryption IV is malformed"}
64− errInvalidInternalSealAlgorithm = Error{"The internal seal algorithm is invalid and not supported"}
63+ errInvalidInternalIV Error = "The internal encryption IV is malformed"
64+ errInvalidInternalSealAlgorithm Error = "The internal seal algorithm is invalid and not supported"
6565
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"
6767 )
6868
6969 var (
cmd/crypto/key.go+1−1
@@ -108,7 +108,7 @@ func (key *ObjectKey) Unseal(extKey [32]byte, sealedKey SealedKey, domain, bucke · 107 unmodified lines
108108 )
109109 switch sealedKey.Algorithm {
110110 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))
112112 case SealAlgorithm:
113113 mac := hmac.New(sha256.New, extKey[:])
114114 mac.Write(sealedKey.IV[:])
cmd/crypto/metadata.go+7−7
@@ -172,7 +172,7 @@ func (s3) ParseMetadata(metadata map[string]string) (keyID string, kmsKey []byte · 171 unmodified lines
172172 }
173173 b64SealedKey, ok := metadata[S3SealedKey]
174174 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")
176176 }
177177
178178 // There are two possibilites:
@@ -182,10 +182,10 @@ func (s3) ParseMetadata(metadata map[string]string) (keyID string, kmsKey []byte · 3 unmodified lines
182182 keyID, idPresent := metadata[S3KMSKeyID]
183183 b64KMSSealedKey, kmsKeyPresent := metadata[S3KMSSealedKey]
184184 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")
186186 }
187187 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")
189189 }
190190
191191 // 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
198198 }
199199 encryptedKey, err := base64.StdEncoding.DecodeString(b64SealedKey)
200200 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")
202202 }
203203 if idPresent && kmsKeyPresent { // We are using a KMS -> parse the sealed KMS data key.
204204 kmsKey, err = base64.StdEncoding.DecodeString(b64KMSSealedKey)
205205 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")
207207 }
208208 }
209209
@@ -244,7 +244,7 @@ func (ssec) ParseMetadata(metadata map[string]string) (sealedKey SealedKey, err · 34 unmodified lines
244244 }
245245 b64SealedKey, ok := metadata[SSECSealedKey]
246246 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")
248248 }
249249
250250 // 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
257257 }
258258 encryptedKey, err := base64.StdEncoding.DecodeString(b64SealedKey)
259259 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")
261261 }
262262
263263 sealedKey.Algorithm = algorithm
cmd/crypto/metadata_test.go+7−7
@@ -125,15 +125,15 @@ var s3ParseMetadataTests = []struct { · 124 unmodified lines
125125 DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{},
126126 }, // 1
127127 {
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"),
129129 Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: ""}, DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{},
130130 }, // 2
131131 {
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"),
133133 Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: "", S3SealedKey: "", S3KMSSealedKey: "IAAF0b=="}, DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{},
134134 }, // 3
135135 {
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"),
137137 Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: "", S3SealedKey: "", S3KMSKeyID: ""},
138138 DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{},
139139 }, // 4
@@ -150,7 +150,7 @@ var s3ParseMetadataTests = []struct { · 10 unmodified lines
150150 DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{},
151151 }, // 6
152152 {
153− ExpectedErr: Error{"The internal sealed key for SSE-S3 is invalid"},
153+ ExpectedErr: Error("The internal sealed key for SSE-S3 is invalid"),
154154 Metadata: map[string]string{
155155 SSEIV: base64.StdEncoding.EncodeToString(make([]byte, 32)), SSESealAlgorithm: SealAlgorithm, S3SealedKey: "",
156156 S3KMSKeyID: "", S3KMSSealedKey: "",
@@ -158,7 +158,7 @@ var s3ParseMetadataTests = []struct { · 1 unmodified line
158158 DataKey: []byte{}, KeyID: "", SealedKey: SealedKey{},
159159 }, // 7
160160 {
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"),
162162 Metadata: map[string]string{
163163 SSEIV: base64.StdEncoding.EncodeToString(make([]byte, 32)), SSESealAlgorithm: SealAlgorithm,
164164 S3SealedKey: base64.StdEncoding.EncodeToString(make([]byte, 64)), S3KMSKeyID: "key-1",
@@ -218,7 +218,7 @@ var ssecParseMetadataTests = []struct { · 53 unmodified lines
218218 {ExpectedErr: errMissingInternalIV, Metadata: map[string]string{}, SealedKey: SealedKey{}}, // 0
219219 {ExpectedErr: errMissingInternalSealAlgorithm, Metadata: map[string]string{SSEIV: ""}, SealedKey: SealedKey{}}, // 1
220220 {
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"),
222222 Metadata: map[string]string{SSEIV: "", SSESealAlgorithm: ""}, SealedKey: SealedKey{},
223223 }, // 2
224224 {
@@ -233,7 +233,7 @@ var ssecParseMetadataTests = []struct { · 8 unmodified lines
233233 SealedKey: SealedKey{},
234234 }, // 4
235235 {
236− ExpectedErr: Error{"The internal sealed key for SSE-C is invalid"},
236+ ExpectedErr: Error("The internal sealed key for SSE-C is invalid"),
237237 Metadata: map[string]string{
238238 SSEIV: base64.StdEncoding.EncodeToString(make([]byte, 32)), SSESealAlgorithm: SealAlgorithm, SSECSealedKey: "",
239239 },

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t