Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(rexctl): clean up and classify object store check errors

Hampus5f00e53bcc53
diff --git a/fluxer_rexctl/internal/objectstore/objectstore.go b/fluxer_rexctl/internal/objectstore/objectstore.go
index da4286571..30e627b9e 100644
--- a/fluxer_rexctl/internal/objectstore/objectstore.go
+++ b/fluxer_rexctl/internal/objectstore/objectstore.go
@@ -8,6 +8,8 @@ import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
+ "crypto/tls"
+ "crypto/x509"
"encoding/hex"
"encoding/xml"
"errors"
@@ -15,6 +17,7 @@ import (
"io"
"net/http"
"net/url"
+ "slices"
"strings"
"time"
)
@@ -31,6 +34,8 @@ type Problem int
const (
Unreachable Problem = iota
+ Untrusted
+ WrongName
Credentials
WrongRegion
MissingBuckets
@@ -50,6 +55,8 @@ func (e *Error) Error() string {
switch {
case e.Err != nil:
return e.Err.Error()
+ case e.Problem == MissingBuckets && e.Code == "":
+ return "missing buckets: " + strings.Join(e.Buckets, ", ")
case e.Message != "":
return e.Code + ": " + e.Message
}
@@ -84,6 +91,11 @@ func (s Store) probe(ctx context.Context, bucket string) error {
if err := s.do(ctx, http.MethodPut, bucket, key, []byte("ok")); err != nil {
return err
}
+ defer func() {
+ ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
+ defer cancel()
+ _ = s.do(ctx, http.MethodDelete, bucket, key, nil)
+ }()
if err := s.do(ctx, http.MethodGet, bucket, key, nil); err != nil {
return err
}
@@ -106,7 +118,7 @@ func (s Store) do(ctx context.Context, method, bucket, key string, body []byte)
}
resp, err := client.Do(req)
if err != nil {
- return &Error{Problem: Unreachable, Err: err}
+ return &Error{Problem: transportProblem(err), Err: err}
}
defer resp.Body.Close()
data, _ := io.ReadAll(io.LimitReader(resp.Body, 64*1024))
@@ -118,21 +130,48 @@ func (s Store) do(ctx context.Context, method, bucket, key string, body []byte)
if eb.Code == "" {
eb.Code = resp.Status
}
- e := &Error{Problem: Other, Buckets: []string{bucket}, Code: eb.Code, Message: eb.Message}
+ return &Error{Problem: classify(resp.StatusCode, eb.Code, eb.Message), Buckets: []string{bucket}, Code: eb.Code, Message: eb.Message}
+}
+
+var credentialCodes = []string{"InvalidAccessKeyId", "SignatureDoesNotMatch", "InvalidToken", "InvalidSecurity", "ExpiredToken", "TokenRefreshRequired", "Unauthorized", "InvalidSignature"}
+
+var credentialMessages = []string{"invalid signature", "no such key", "access key", "signature does not match", "signature we calculated", "unauthorized"}
+
+var regionCodes = []string{"AuthorizationHeaderMalformed", "PermanentRedirect", "IllegalLocationConstraintException", "InvalidRegion"}
+
+func classify(status int, code, message string) Problem {
+ msg := strings.ToLower(message)
+ credentialText := slices.ContainsFunc(credentialMessages, func(m string) bool { return strings.Contains(msg, m) })
+ switch {
+ case code == "NoSuchBucket":
+ return MissingBuckets
+ case slices.Contains(credentialCodes, code), status == http.StatusUnauthorized, code == "InvalidArgument" && credentialText:
+ return Credentials
+ case slices.Contains(regionCodes, code), status == http.StatusMovedPermanently, status == http.StatusTemporaryRedirect:
+ return WrongRegion
+ case code == "AccessDenied" || status == http.StatusForbidden:
+ if credentialText {
+ return Credentials
+ }
+ return Denied
+ case status == http.StatusNotFound && message == "":
+ return MissingBuckets
+ }
+ return Other
+}
+
+func transportProblem(err error) Problem {
+ var unknown x509.UnknownAuthorityError
+ var invalid x509.CertificateInvalidError
+ var host x509.HostnameError
+ var verify *tls.CertificateVerificationError
switch {
- case eb.Code == "NoSuchBucket":
- e.Problem = MissingBuckets
- case eb.Code == "InvalidAccessKeyId", eb.Code == "SignatureDoesNotMatch", eb.Code == "InvalidToken", eb.Code == "InvalidSecurity":
- e.Problem = Credentials
- case eb.Code == "AuthorizationHeaderMalformed", eb.Code == "PermanentRedirect", eb.Code == "IllegalLocationConstraintException",
- resp.StatusCode == http.StatusMovedPermanently, resp.StatusCode == http.StatusTemporaryRedirect:
- e.Problem = WrongRegion
- case eb.Code == "AccessDenied", resp.StatusCode == http.StatusForbidden:
- e.Problem = Denied
- case resp.StatusCode == http.StatusNotFound && eb.Message == "":
- e.Problem = MissingBuckets
+ case errors.As(err, &host):
+ return WrongName
+ case errors.As(err, &unknown), errors.As(err, &invalid), errors.As(err, &verify):
+ return Untrusted
}
- return e
+ return Unreachable
}
func (s Store) request(ctx context.Context, method, bucket, key string, body []byte) (*http.Request, error) {
diff --git a/fluxer_rexctl/internal/objectstore/objectstore_test.go b/fluxer_rexctl/internal/objectstore/objectstore_test.go
new file mode 100644
index 000000000..dcf6e3c66
--- /dev/null
+++ b/fluxer_rexctl/internal/objectstore/objectstore_test.go
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package objectstore
+
+import (
+ "crypto/tls"
+ "crypto/x509"
+ "errors"
+ "net/url"
+ "testing"
+)
+
+func TestClassify(t *testing.T) {
+ cases := []struct {
+ name string
+ status int
+ code string
+ message string
+ want Problem
+ }{
+ {"aws wrong secret", 403, "SignatureDoesNotMatch", "The request signature we calculated does not match the signature you provided. Check your key and signing method.", Credentials},
+ {"aws unknown key", 403, "InvalidAccessKeyId", "The AWS Access Key Id you provided does not exist in our records.", Credentials},
+ {"aws denied", 403, "AccessDenied", "Access Denied", Denied},
+ {"aws wrong region", 400, "AuthorizationHeaderMalformed", "The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'eu-west-1'", WrongRegion},
+ {"aws missing bucket", 404, "NoSuchBucket", "The specified bucket does not exist", MissingBuckets},
+ {"garage wrong secret", 403, "AccessDenied", "Forbidden: Invalid signature", Credentials},
+ {"garage unknown key", 403, "AccessDenied", "Forbidden: No such key: GK0123456789abcdef", Credentials},
+ {"garage denied", 403, "AccessDenied", "Forbidden: Operation is not allowed for this key.", Denied},
+ {"r2 malformed key", 400, "InvalidArgument", "Credential access key has length 3, should be 32", Credentials},
+ {"r2 unauthorized", 401, "Unauthorized", "Unauthorized", Credentials},
+ {"b2 unknown key", 403, "InvalidAccessKeyId", "Malformed Access Key Id", Credentials},
+ {"head missing bucket", 404, "404 Not Found", "", MissingBuckets},
+ }
+ for _, c := range cases {
+ if got := classify(c.status, c.code, c.message); got != c.want {
+ t.Errorf("%s: got %d, want %d", c.name, got, c.want)
+ }
+ }
+}
+
+func TestTransportProblem(t *testing.T) {
+ cases := []struct {
+ name string
+ err error
+ want Problem
+ }{
+ {"unknown authority", &url.Error{Op: "Put", Err: &tls.CertificateVerificationError{Err: x509.UnknownAuthorityError{}}}, Untrusted},
+ {"wrong name", &url.Error{Op: "Put", Err: &tls.CertificateVerificationError{Err: x509.HostnameError{Certificate: &x509.Certificate{}, Host: "s3.example.com"}}}, WrongName},
+ {"expired", &url.Error{Op: "Put", Err: x509.CertificateInvalidError{Reason: x509.Expired}}, Untrusted},
+ {"refused", &url.Error{Op: "Put", Err: errors.New("connection refused")}, Unreachable},
+ }
+ for _, c := range cases {
+ if got := transportProblem(c.err); got != c.want {
+ t.Errorf("%s: got %d, want %d", c.name, got, c.want)
+ }
+ }
+}