mirror of
https://github.com/fluxerapp/fluxer.git
synced 2026-12-05 16:42:44 -08:00
feat(rexctl): let install use your own object store
This commit is contained in:
5 files changed
+629
No files matched your search
+28
@@ -348,6 +348,34 @@ rexctl install [flags]
|
||||
|
||||
The username for the SMTP server.
|
||||
|
||||
- `--storage-access-key string`
|
||||
|
||||
The object store access key.
|
||||
|
||||
- `--storage-bucket-prefix string`
|
||||
|
||||
Text to put before each bucket name, like chat- for chat-fluxer.
|
||||
|
||||
- `--storage-endpoint string`
|
||||
|
||||
Your own S3-compatible object store, like https://s3.example.com, instead of the bundled one.
|
||||
|
||||
- `--storage-path-style`
|
||||
|
||||
Put the bucket in the path of object store addresses instead of the host name. The default is `true`.
|
||||
|
||||
- `--storage-public-endpoint string`
|
||||
|
||||
The object store address browsers use, when it differs from --storage-endpoint.
|
||||
|
||||
- `--storage-region string`
|
||||
|
||||
The object store region (found from --storage-endpoint when it can be).
|
||||
|
||||
- `--storage-secret-key-file string`
|
||||
|
||||
File with the object store secret key, or - for stdin.
|
||||
|
||||
- `--take-over`
|
||||
|
||||
Take over Fluxer data found on this machine.
|
||||
|
||||
@@ -56,10 +56,12 @@ type installFlags struct {
|
||||
VoiceTCPPort int
|
||||
VoiceUDPPort int
|
||||
Setup setupFlags
|
||||
Storage storageFlags
|
||||
}
|
||||
|
||||
type installer struct {
|
||||
answers *setup.Answers
|
||||
storage *storageChoice
|
||||
app *App
|
||||
c *cobra.Command
|
||||
f *installFlags
|
||||
@@ -105,6 +107,7 @@ func newInstallCmd(app *App) *cobra.Command {
|
||||
}
|
||||
confirmFlag(c, &f.Confirm)
|
||||
f.Setup.bind(c)
|
||||
f.Storage.bind(c)
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -112,6 +115,9 @@ func runInstall(ctx context.Context, app *App, c *cobra.Command, f *installFlags
|
||||
if err := validateInstallFlags(f); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f.Storage.validate(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if names := f.Setup.passedNames(c); f.SetupWhere == "browser" && len(names) > 0 {
|
||||
return &ui.ExitError{Code: ui.ExitUsage, Msg: fmt.Sprintf("--setup browser doesn't take %s.", ui.JoinAnd(names)), Fix: "Pass --setup terminal instead, or leave out the setup options and enter them in the browser."}
|
||||
}
|
||||
@@ -177,6 +183,12 @@ func runInstall(ctx context.Context, app *App, c *cobra.Command, f *installFlags
|
||||
if err := in.openFirewall(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := in.askStorage(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := in.checkStorage(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := in.askSetupWhere(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -213,6 +225,7 @@ func (in *installer) missingFlags() error {
|
||||
if f.Domain == "" {
|
||||
missing = append(missing, "--domain")
|
||||
}
|
||||
missing = append(missing, f.Storage.missing(in.c)...)
|
||||
terminal := f.SetupWhere == "terminal" || (f.SetupWhere == "" && f.Setup.passed(in.c))
|
||||
if terminal && !f.NoStart {
|
||||
s := f.Setup
|
||||
@@ -857,6 +870,9 @@ func (in *installer) write(ctx context.Context, inst *instance.Instance, deleteO
|
||||
if f.MemoryProfile != "" {
|
||||
st.Settings[settings.MemoryProfile] = f.MemoryProfile
|
||||
}
|
||||
if in.storage != nil {
|
||||
maps.Copy(st.Settings, in.storage.values())
|
||||
}
|
||||
if _, err := secrets.Mint(st.Secrets, false); err != nil {
|
||||
return ui.Wrap(err, "Couldn't create your instance's secrets.", "Run rexctl install again.")
|
||||
}
|
||||
@@ -911,6 +927,9 @@ func (in *installer) resume(ctx context.Context, inst *instance.Instance) error
|
||||
in.app.UI.Info("rexctl would start Fluxer at %s and finish setup.", inst.Origin())
|
||||
return nil
|
||||
}
|
||||
if err := in.resumeStorage(ctx, inst); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := in.askSetupWhere(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"
|
||||
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"
|
||||
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
|
||||
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/objectstore"
|
||||
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"
|
||||
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"
|
||||
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
|
||||
)
|
||||
|
||||
type storageFlags struct {
|
||||
Endpoint string
|
||||
Public string
|
||||
Region string
|
||||
PathStyle bool
|
||||
AccessKey string
|
||||
SecretFile string
|
||||
Prefix string
|
||||
}
|
||||
|
||||
type storageChoice struct {
|
||||
objectstore.Store
|
||||
Public string
|
||||
Prefix string
|
||||
Buckets map[string]string
|
||||
}
|
||||
|
||||
var storageFlagNames = []string{
|
||||
"storage-endpoint", "storage-public-endpoint", "storage-region", "storage-path-style", "storage-access-key", "storage-secret-key-file", "storage-bucket-prefix",
|
||||
}
|
||||
|
||||
var storageBucketVars = []string{"FLUXER_S3_BUCKET_CDN", "FLUXER_S3_BUCKET_UPLOADS", "FLUXER_S3_BUCKET_REPORTS", "FLUXER_S3_BUCKET_HARVESTS"}
|
||||
|
||||
var storageKeys = append([]string{
|
||||
settings.StorageEndpoint, settings.StoragePublic, settings.StorageRegion, settings.StoragePath, settings.StorageAccess, settings.StorageSecret,
|
||||
}, storageBucketVars...)
|
||||
|
||||
var regionSuffixes = []string{".amazonaws.com", ".backblazeb2.com", ".wasabisys.com", ".digitaloceanspaces.com", ".scw.cloud", ".your-objectstorage.com", ".linodeobjects.com"}
|
||||
|
||||
var bucketPrefixPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`)
|
||||
|
||||
func (f *storageFlags) bind(c *cobra.Command) {
|
||||
fl := c.Flags()
|
||||
fl.StringVar(&f.Endpoint, "storage-endpoint", "", "your own S3-compatible object store, like https://s3.example.com, instead of the bundled one")
|
||||
fl.StringVar(&f.Public, "storage-public-endpoint", "", "the object store address browsers use, when it differs from --storage-endpoint")
|
||||
fl.StringVar(&f.Region, "storage-region", "", "the object store region (found from --storage-endpoint when it can be)")
|
||||
fl.BoolVar(&f.PathStyle, "storage-path-style", true, "put the bucket in the path of object store addresses instead of the host name")
|
||||
fl.StringVar(&f.AccessKey, "storage-access-key", "", "the object store access key")
|
||||
fl.StringVar(&f.SecretFile, "storage-secret-key-file", "", "file with the object store secret key, or - for stdin")
|
||||
fl.StringVar(&f.Prefix, "storage-bucket-prefix", "", "text to put before each bucket name, like chat- for chat-fluxer")
|
||||
}
|
||||
|
||||
func (f *storageFlags) passed(c *cobra.Command) bool {
|
||||
for _, name := range storageFlagNames {
|
||||
if fl := c.Flags().Lookup(name); fl != nil && fl.Changed {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *storageFlags) missing(c *cobra.Command) []string {
|
||||
if !f.passed(c) {
|
||||
return nil
|
||||
}
|
||||
var out []string
|
||||
for _, r := range []struct{ flag, value string }{
|
||||
{"storage-endpoint", f.Endpoint},
|
||||
{"storage-access-key", f.AccessKey},
|
||||
{"storage-secret-key-file", f.SecretFile},
|
||||
} {
|
||||
if r.value == "" {
|
||||
out = append(out, "--"+r.flag)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (f *storageFlags) validate(c *cobra.Command) error {
|
||||
for _, r := range []struct {
|
||||
flag string
|
||||
check func(string) error
|
||||
}{
|
||||
{"storage-endpoint", validStorageEndpoint},
|
||||
{"storage-public-endpoint", storageValidator(settings.StoragePublic)},
|
||||
{"storage-region", storageValidator(settings.StorageRegion)},
|
||||
{"storage-access-key", storageValidator(settings.StorageAccess)},
|
||||
{"storage-bucket-prefix", validBucketPrefix},
|
||||
} {
|
||||
fl := c.Flags().Lookup(r.flag)
|
||||
if fl == nil || !fl.Changed {
|
||||
continue
|
||||
}
|
||||
if err := r.check(strings.TrimSpace(fl.Value.String())); err != nil {
|
||||
return ui.Usage(fmt.Sprintf("--%s can't be %q. %s", r.flag, fl.Value.String(), err.Error()))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func storageValidator(name string) func(string) error {
|
||||
k, err := settings.Lookup(stack.Embedded(), nil, name, nil)
|
||||
if err != nil || k.Validate == nil {
|
||||
return func(string) error { return nil }
|
||||
}
|
||||
return k.Validate
|
||||
}
|
||||
|
||||
func validStorageEndpoint(v string) error {
|
||||
if err := storageValidator(settings.StorageEndpoint)(v); err != nil {
|
||||
return err
|
||||
}
|
||||
u, err := url.Parse(strings.TrimSpace(v))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
host := strings.ToLower(u.Hostname())
|
||||
local := host == "localhost" || strings.HasSuffix(host, ".localhost")
|
||||
if ip, err := netip.ParseAddr(host); err == nil {
|
||||
ip = ip.Unmap()
|
||||
local = ip.IsLoopback() || ip.IsLinkLocalUnicast()
|
||||
}
|
||||
if local {
|
||||
return setupInputError(fmt.Sprintf("Fluxer's containers can't reach %s, because it's this machine's own loopback or link-local address. Use the object store's domain name or this machine's network IP address.", host))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func bucketDefaults() map[string]string {
|
||||
out := map[string]string{}
|
||||
for _, v := range storageBucketVars {
|
||||
if b := instance.ReleaseDefault(stack.Embedded(), v); b != "" {
|
||||
out[v] = b
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func validBucketPrefix(v string) error {
|
||||
if v == "" {
|
||||
return nil
|
||||
}
|
||||
longest := 0
|
||||
for _, b := range bucketDefaults() {
|
||||
longest = max(longest, len(b))
|
||||
}
|
||||
most := 63 - longest
|
||||
if !bucketPrefixPattern.MatchString(v) || len(v) > most || strings.HasPrefix(v, "xn--") || strings.HasPrefix(v, "sthree-") {
|
||||
return setupInputError(fmt.Sprintf("Use up to %d lowercase letters, numbers or hyphens, starting with a letter or number.", most))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *storageChoice) bucketNames() []string {
|
||||
var out []string
|
||||
for _, v := range storageBucketVars {
|
||||
if b, ok := s.Buckets[v]; ok {
|
||||
out = append(out, b)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func guessRegion(endpoint string) string {
|
||||
u, err := url.Parse(endpoint)
|
||||
if err == nil {
|
||||
host := strings.ToLower(u.Hostname())
|
||||
if strings.HasSuffix(host, ".r2.cloudflarestorage.com") {
|
||||
return "auto"
|
||||
}
|
||||
for _, suffix := range regionSuffixes {
|
||||
rest, ok := strings.CutSuffix(host, suffix)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
label := strings.TrimPrefix(rest[strings.LastIndex(rest, ".")+1:], "s3-")
|
||||
if label != "" && label != "s3" {
|
||||
return label
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance.ReleaseDefault(stack.Embedded(), "FLUXER_S3_REGION")
|
||||
}
|
||||
|
||||
func (in *installer) askStorage() error {
|
||||
u := in.app.UI
|
||||
f := &in.f.Storage
|
||||
if !f.passed(in.c) {
|
||||
if !u.Interactive() {
|
||||
return nil
|
||||
}
|
||||
choice := "bundled"
|
||||
if err := u.Ask(nil, ui.Field{
|
||||
Title: "Where do you want to keep uploaded files?",
|
||||
Value: &choice,
|
||||
Options: []ui.Option{
|
||||
{Label: "In Fluxer's bundled object store", Value: "bundled"},
|
||||
{Label: "In my own S3-compatible object store", Value: "own"},
|
||||
},
|
||||
}); err != nil || choice == "bundled" {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := u.Ask(in.c,
|
||||
ui.Field{Flag: "storage-endpoint", Title: "Object store address", Value: &f.Endpoint, Validate: validStorageEndpoint},
|
||||
ui.Field{Flag: "storage-access-key", Title: "Access key", Value: &f.AccessKey, Validate: storageValidator(settings.StorageAccess)},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
secret, err := askSecret(in.app, in.c, "storage-secret-key-file", "Secret key", f.SecretFile, storageValidator(settings.StorageSecret))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pathStyle := strconv.FormatBool(f.PathStyle)
|
||||
if err := u.Ask(in.c,
|
||||
ui.Field{Flag: "storage-region", Title: "Region", Value: &f.Region, Default: guessRegion(strings.TrimSpace(f.Endpoint)), Validate: storageValidator(settings.StorageRegion)},
|
||||
ui.Field{Flag: "storage-bucket-prefix", Title: "Bucket name prefix, if your provider shares bucket names with other accounts", Value: &f.Prefix, Optional: true, Validate: validBucketPrefix},
|
||||
ui.Field{
|
||||
Flag: "storage-path-style",
|
||||
Title: "Where does the bucket go in the address?",
|
||||
Value: &pathStyle,
|
||||
Validate: storageValidator(settings.StoragePath),
|
||||
Options: []ui.Option{
|
||||
{Label: "In the path, like s3.example.com/bucket", Value: "true"},
|
||||
{Label: "In the host name, like bucket.s3.example.com", Value: "false"},
|
||||
},
|
||||
},
|
||||
ui.Field{Flag: "storage-public-endpoint", Title: "Address browsers use, if it's different", Value: &f.Public, Optional: true, Validate: storageValidator(settings.StoragePublic)},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
prefix := strings.TrimSpace(f.Prefix)
|
||||
buckets := bucketDefaults()
|
||||
for v, b := range buckets {
|
||||
buckets[v] = prefix + b
|
||||
}
|
||||
in.storage = &storageChoice{
|
||||
Store: objectstore.Store{
|
||||
Endpoint: strings.TrimSpace(f.Endpoint),
|
||||
Region: strings.TrimSpace(f.Region),
|
||||
AccessKey: strings.TrimSpace(f.AccessKey),
|
||||
SecretKey: secret,
|
||||
PathStyle: pathStyle == "true",
|
||||
},
|
||||
Public: strings.TrimSpace(f.Public),
|
||||
Prefix: prefix,
|
||||
Buckets: buckets,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (in *installer) checkStorage(ctx context.Context) error {
|
||||
s := in.storage
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
if in.f.DryRun {
|
||||
in.plan = append(in.plan, fmt.Sprintf("Check that Fluxer can use the buckets %s in your object store.", ui.JoinAnd(buckets)))
|
||||
return nil
|
||||
}
|
||||
err := in.app.UI.Step(ctx, "Checking your object store", func(ctx context.Context, st *ui.Step) error {
|
||||
if err := s.Check(ctx, buckets); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (in *installer) storageFailure(err error) error {
|
||||
var se *objectstore.Error
|
||||
if !errors.As(err, &se) {
|
||||
return err
|
||||
}
|
||||
s := in.storage
|
||||
again := "then run rexctl install again."
|
||||
switch se.Problem {
|
||||
case objectstore.Unreachable:
|
||||
return ui.Wrap(err, fmt.Sprintf("Couldn't reach your object store at %s.", s.Endpoint), "Check the address and that this machine can connect to it, "+again)
|
||||
case objectstore.Credentials:
|
||||
return ui.Wrap(err, "Your object store didn't accept the access key and secret key.", "Check both keys, "+again)
|
||||
case objectstore.WrongRegion:
|
||||
return ui.Wrap(err, "Your object store expects a different region.", "Check the region your provider shows for the bucket, "+again)
|
||||
case objectstore.MissingBuckets:
|
||||
if len(se.Buckets) == 1 {
|
||||
msg = fmt.Sprintf("Your object store has no bucket called %s.", se.Buckets[0])
|
||||
}
|
||||
case objectstore.Denied:
|
||||
return ui.Wrap(err, fmt.Sprintf("The access key can't read and write files in the bucket %s.", ui.JoinAnd(se.Buckets)),
|
||||
fmt.Sprintf("Give the key read, write and delete access to the buckets %s, %s", ui.JoinAnd(s.bucketNames()), again)+in.prefixHint())
|
||||
}
|
||||
return ui.Wrap(err, fmt.Sprintf("Your object store answered with an error: %s.", strings.TrimSuffix(se.Error(), ".")), "Check your object store settings, "+again)
|
||||
}
|
||||
|
||||
func (in *installer) prefixHint() string {
|
||||
if in.storage.Prefix != "" {
|
||||
return ""
|
||||
}
|
||||
how := "pass the prefix with --storage-bucket-prefix"
|
||||
if in.app.UI.Interactive() {
|
||||
how = "enter the prefix when rexctl asks for a bucket name prefix"
|
||||
}
|
||||
return fmt.Sprintf(" If another account already has those names on your provider, create the buckets with a prefix, like chat-%s, and %s.", in.storage.bucketNames()[0], how)
|
||||
}
|
||||
|
||||
func (s *storageChoice) values() map[string]string {
|
||||
out := map[string]string{
|
||||
settings.StorageEndpoint: s.Endpoint,
|
||||
settings.StorageRegion: s.Region,
|
||||
settings.StorageAccess: s.AccessKey,
|
||||
settings.StorageSecret: s.SecretKey,
|
||||
}
|
||||
if !s.PathStyle {
|
||||
out[settings.StoragePath] = "false"
|
||||
}
|
||||
if s.Public != "" {
|
||||
out[settings.StoragePublic] = s.Public
|
||||
}
|
||||
if s.Prefix != "" {
|
||||
maps.Copy(out, s.Buckets)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (in *installer) resumeStorage(ctx context.Context, inst *instance.Instance) error {
|
||||
if !in.f.Storage.passed(in.c) {
|
||||
return nil
|
||||
}
|
||||
if err := in.askStorage(); err != nil {
|
||||
return err
|
||||
}
|
||||
current := map[string]string{}
|
||||
for _, k := range storageKeys {
|
||||
if v, ok := inst.State.Settings[k]; ok {
|
||||
current[k] = v
|
||||
}
|
||||
}
|
||||
if maps.Equal(current, want) {
|
||||
return nil
|
||||
}
|
||||
if endpoint := current[settings.StorageEndpoint]; endpoint != "" {
|
||||
return ui.Refused(fmt.Sprintf("This unfinished install already uses different object store settings, with the store at %s.", endpoint),
|
||||
"Run rexctl install again without the storage options to keep them, or change them after the install with rexctl config set.")
|
||||
}
|
||||
if err := in.checkStorage(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
changes := map[string]*string{}
|
||||
for _, k := range slices.Sorted(maps.Keys(want)) {
|
||||
v := want[k]
|
||||
changes[k] = &v
|
||||
}
|
||||
return in.app.ChangeSettings(ctx, inst, func(e *engine.Engine) error {
|
||||
var logPath string
|
||||
change, err := deploy.Set(ctx, inst, e, changes, deploy.ApplyOptions{
|
||||
Wait: true,
|
||||
Failed: func(p *engine.Project, err error) {
|
||||
logPath = writeFailureLog(ctx, inst, p, "start.log", err, nil)
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return settingsFailure(change, err, logPath)
|
||||
}
|
||||
in.app.UI.OK("Saved your object store settings.")
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -355,3 +355,7 @@ func removeIfExists(path string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReleaseDefault(set *stack.Set, name string) string {
|
||||
return composeDefault(set, nil, nil, name)
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
Endpoint string
|
||||
Region string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
PathStyle bool
|
||||
}
|
||||
|
||||
type Problem int
|
||||
|
||||
const (
|
||||
Unreachable Problem = iota
|
||||
Credentials
|
||||
WrongRegion
|
||||
MissingBuckets
|
||||
Denied
|
||||
Other
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Problem Problem
|
||||
Buckets []string
|
||||
Code string
|
||||
Message string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
switch {
|
||||
case e.Err != nil:
|
||||
return e.Err.Error()
|
||||
case e.Message != "":
|
||||
return e.Code + ": " + e.Message
|
||||
}
|
||||
return e.Code
|
||||
}
|
||||
|
||||
func (e *Error) Unwrap() error { return e.Err }
|
||||
|
||||
func (s Store) Check(ctx context.Context, buckets []string) error {
|
||||
var missing []string
|
||||
for _, b := range buckets {
|
||||
err := s.probe(ctx, b)
|
||||
var se *Error
|
||||
if errors.As(err, &se) && se.Problem == MissingBuckets {
|
||||
missing = append(missing, b)
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return &Error{Problem: MissingBuckets, Buckets: missing}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Store) probe(ctx context.Context, bucket string) error {
|
||||
var id [8]byte
|
||||
_, _ = rand.Read(id[:])
|
||||
key := "rexctl-check-" + hex.EncodeToString(id[:])
|
||||
if err := s.do(ctx, http.MethodPut, bucket, key, []byte("ok")); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.do(ctx, http.MethodGet, bucket, key, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.do(ctx, http.MethodDelete, bucket, key, nil)
|
||||
}
|
||||
|
||||
type errorBody struct {
|
||||
Code string `xml:"Code"`
|
||||
Message string `xml:"Message"`
|
||||
}
|
||||
|
||||
func (s Store) do(ctx context.Context, method, bucket, key string, body []byte) error {
|
||||
req, err := s.request(ctx, method, bucket, key, body)
|
||||
if err != nil {
|
||||
return &Error{Problem: Unreachable, Err: err}
|
||||
}
|
||||
client := &http.Client{
|
||||
Timeout: 20 * time.Second,
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return &Error{Problem: Unreachable, Err: err}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, _ := io.ReadAll(io.LimitReader(resp.Body, 64*1024))
|
||||
if resp.StatusCode < 300 {
|
||||
return nil
|
||||
}
|
||||
var eb errorBody
|
||||
_ = xml.Unmarshal(data, &eb)
|
||||
if eb.Code == "" {
|
||||
eb.Code = resp.Status
|
||||
}
|
||||
e := &Error{Problem: Other, Buckets: []string{bucket}, Code: eb.Code, Message: eb.Message}
|
||||
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
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (s Store) request(ctx context.Context, method, bucket, key string, body []byte) (*http.Request, error) {
|
||||
u, err := url.Parse(s.Endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base := strings.TrimSuffix(u.Path, "/")
|
||||
if s.PathStyle {
|
||||
u.Path = base + "/" + bucket + "/" + key
|
||||
} else {
|
||||
u.Host = bucket + "." + u.Host
|
||||
u.Path = base + "/" + key
|
||||
}
|
||||
u.RawPath, u.RawQuery, u.Fragment = "", "", ""
|
||||
req, err := http.NewRequestWithContext(ctx, method, u.String(), bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.sign(req, body, time.Now().UTC())
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (s Store) sign(req *http.Request, body []byte, now time.Time) {
|
||||
payload := hashHex(body)
|
||||
stamp := now.Format("20060102T150405Z")
|
||||
day := now.Format("20060102")
|
||||
req.Header.Set("X-Amz-Date", stamp)
|
||||
req.Header.Set("X-Amz-Content-Sha256", payload)
|
||||
signed := "host;x-amz-content-sha256;x-amz-date"
|
||||
canonical := strings.Join([]string{
|
||||
req.Method,
|
||||
req.URL.EscapedPath(),
|
||||
"",
|
||||
"host:" + req.URL.Host,
|
||||
"x-amz-content-sha256:" + payload,
|
||||
"x-amz-date:" + stamp,
|
||||
"",
|
||||
signed,
|
||||
payload,
|
||||
}, "\n")
|
||||
scope := day + "/" + s.Region + "/s3/aws4_request"
|
||||
toSign := "AWS4-HMAC-SHA256\n" + stamp + "\n" + scope + "\n" + hashHex([]byte(canonical))
|
||||
k := hmacSHA256([]byte("AWS4"+s.SecretKey), day)
|
||||
k = hmacSHA256(k, s.Region)
|
||||
k = hmacSHA256(k, "s3")
|
||||
k = hmacSHA256(k, "aws4_request")
|
||||
sig := hex.EncodeToString(hmacSHA256(k, toSign))
|
||||
req.Header.Set("Authorization", fmt.Sprintf("AWS4-HMAC-SHA256 Credential=%s/%s, SignedHeaders=%s, Signature=%s", s.AccessKey, scope, signed, sig))
|
||||
}
|
||||
|
||||
func hashHex(b []byte) string {
|
||||
h := sha256.Sum256(b)
|
||||
return hex.EncodeToString(h[:])
|
||||
}
|
||||
|
||||
func hmacSHA256(key []byte, data string) []byte {
|
||||
m := hmac.New(sha256.New, key)
|
||||
m.Write([]byte(data))
|
||||
return m.Sum(nil)
|
||||
}
|
||||
Reference in new issue
Block a user