diff --git a/docker/s3vectors/README.md b/docker/s3vectors/README.md index d0cb14473d..39d0ba5672 100644 --- a/docker/s3vectors/README.md +++ b/docker/s3vectors/README.md @@ -34,6 +34,7 @@ A native development setup can use `weed mini -vector -vectorBucket=embeddings` with `weed-vector` beside `weed` or on PATH. `-vector.binary` selects an explicit binary, and `-vector.metricsPort=9329` enables HTTP readiness and metrics on a compatible engine. The child inherits the cluster's configuration directory and -stops with mini. Run `weed-worker` separately for ANN maintenance. `weed shell` +stops with mini. Mini also starts the `weed-worker` maintenance worker, found the +same way, unless `-vector.worker=false`. `weed shell` provides `s3vectors -operation -file request.json` for the same API operations using trusted cluster credentials. diff --git a/weed/command/mini.go b/weed/command/mini.go index 2cfdc10f2f..731dc6bce6 100644 --- a/weed/command/mini.go +++ b/weed/command/mini.go @@ -422,6 +422,8 @@ var ( miniVectorBinary = cmdMini.Flag.String("vector.binary", "weed-vector", "path to the vector engine binary") miniVectorPort = cmdMini.Flag.Int("vector.port", 8090, "vector engine gRPC port") miniVectorMetricsPort = cmdMini.Flag.Int("vector.metricsPort", 0, "optional vector HTTP health and metrics port") + miniVectorWorker = cmdMini.Flag.Bool("vector.worker", true, "with -vector, also start weed-worker, which builds the vector and metadata indexes, when it is installed") + miniVectorWorkerBinary = cmdMini.Flag.String("vector.worker.binary", "weed-worker", "path to the weed-worker binary") miniVectorBucket = cmdMini.Flag.String("vectorBucket", "", "comma-separated vector buckets to create; falls back to S3_VECTOR_BUCKET") ) @@ -1490,6 +1492,7 @@ func runMini(cmd *Command, args []string) bool { // plaintext-KEK warning and the IAM "no signing key" error don't fire. // Does nothing for keys the operator has already configured. ensureMiniDevSSES3Keys(*miniDataFolders) + prepareMiniVectorWorker() *miniOptions.cpuprofile = util.ResolvePath(*miniOptions.cpuprofile) *miniOptions.memprofile = util.ResolvePath(*miniOptions.memprofile) @@ -1745,6 +1748,11 @@ func runMini(cmd *Command, args []string) bool { triggerMiniClientsShutdown(5 * time.Second) return false } + if err := startMiniVectorWorker(); err != nil { + glog.Errorf("start weed-worker: %v", err) + triggerMiniClientsShutdown(5 * time.Second) + return false + } } // Print welcome message after all services are running diff --git a/weed/command/mini_vector.go b/weed/command/mini_vector.go index 93d9c4bc81..6589aecced 100644 --- a/weed/command/mini_vector.go +++ b/weed/command/mini_vector.go @@ -2,6 +2,8 @@ package command import ( "context" + "crypto/rand" + "encoding/hex" "fmt" "net" "os" @@ -16,18 +18,97 @@ import ( "github.com/seaweedfs/seaweedfs/weed/util" ) -func startMiniVector() error { - binary := *miniVectorBinary - if binary == "weed-vector" { +// miniVectorWorkerPath is weed-worker's resolved path, empty when mini does +// not start it. +var miniVectorWorkerPath string + +// lookMiniBinary finds a companion binary named by its default name next to +// weed first, then on PATH; any other value is used as given. +func lookMiniBinary(binary, defaultName string) (string, error) { + if binary == defaultName { if executable, err := os.Executable(); err == nil { - candidate := filepath.Join(filepath.Dir(executable), binary) - if resolved, err := exec.LookPath(candidate); err == nil { - binary = resolved + if resolved, err := exec.LookPath(filepath.Join(filepath.Dir(executable), binary)); err == nil { + return resolved, nil } } } - if _, err := exec.LookPath(binary); err != nil { - return fmt.Errorf("%s is unavailable: install the vector component or set -vector.binary: %w", binary, err) + return exec.LookPath(binary) +} + +// miniSecurityToml is the security.toml in -config_dir for a child process, +// unless its own environment variable already names one. +func miniSecurityToml(env string) string { + directory := util.ConfigurationFileDirectory.String() + if directory == "" { + return "" + } + path := filepath.Join(util.ResolvePath(directory), "security.toml") + if _, explicit := os.LookupEnv(env); explicit || !util.FileExists(path) { + return "" + } + return path +} + +// prepareMiniVectorWorker finds weed-worker before the S3 gateway starts. The +// Lance Namespace trusts the worker only by a token signed with a key derived +// from jwt.filer_signing.key, so when the cluster has none, the namespace and +// the worker share a key made for this run. +func prepareMiniVectorWorker() { + if !*miniVector || !*miniVectorWorker { + return + } + binary, err := lookMiniBinary(*miniVectorWorkerBinary, "weed-worker") + if err != nil { + glog.Warningf("%s is unavailable, so vector and metadata indexes will not be built: install weed-worker or set -vector.worker.binary: %v", *miniVectorWorkerBinary, err) + return + } + miniVectorWorkerPath = binary + if util.GetViper().GetString("jwt.filer_signing.key") != "" { + return + } + key := make([]byte, 32) + if _, err := rand.Read(key); err != nil { + glog.Warningf("weed-worker will not be started: generate its signing key: %v", err) + miniVectorWorkerPath = "" + return + } + miniS3Options.lanceWorkerKey = security.SigningKey(hex.EncodeToString(key)) +} + +// startMiniVectorWorker starts weed-worker against mini's admin, which names +// the filers and the Lance Namespace with every job. +func startMiniVectorWorker() error { + if miniVectorWorkerPath == "" { + return nil + } + admin := pb.NewServerAddress(miniAdminWorkerDialIP(miniAdminOptions.workerGrpcBindIp), *miniAdminOptions.port, *miniAdminOptions.grpcPort) + args := []string{"--admin=" + string(admin)} + if path := miniSecurityToml("WEED_WORKER_SECURITY_TOML"); path != "" { + args = append(args, "--security-toml="+path) + } + command := exec.CommandContext(miniClientsCtx(), miniVectorWorkerPath, args...) + command.Stdout, command.Stderr = os.Stdout, os.Stderr + if key := miniS3Options.lanceWorkerKey; len(key) > 0 { + command.Env = append(os.Environ(), "WEED_FILER_SIGNING_KEY="+string(key)) + } + if err := command.Start(); err != nil { + return err + } + done := trackMiniClient() + go func() { + defer done() + if err := command.Wait(); err != nil && miniClientsCtx().Err() == nil { + glog.Errorf("weed-worker exited: %v", err) + } + }() + glog.V(0).Infof("weed-worker started against admin %s", admin) + return nil +} + +func startMiniVector() error { + binary, err := lookMiniBinary(*miniVectorBinary, "weed-vector") + if err != nil { + return fmt.Errorf("%s is unavailable: install the vector component or set -vector.binary: %w", *miniVectorBinary, err) } address := util.JoinHostPort(*miniIp, *miniVectorPort) bindIP := *miniBindIp @@ -54,11 +135,8 @@ func startMiniVector() error { if *miniVectorMetricsPort > 0 { args = append(args, fmt.Sprintf("--metrics-port=%d", *miniVectorMetricsPort), "--metrics-ip="+bindIP) } - if directory := util.ConfigurationFileDirectory.String(); directory != "" { - path := filepath.Join(util.ResolvePath(directory), "security.toml") - if _, explicit := os.LookupEnv("WEED_VECTOR_SECURITY_TOML"); !explicit && util.FileExists(path) { - args = append(args, "--security-toml="+path) - } + if path := miniSecurityToml("WEED_VECTOR_SECURITY_TOML"); path != "" { + args = append(args, "--security-toml="+path) } command := exec.CommandContext(miniClientsCtx(), binary, args...) command.Stdout, command.Stderr = os.Stdout, os.Stderr diff --git a/weed/command/mini_vector_test.go b/weed/command/mini_vector_test.go index 4027e49a81..5b7d360acc 100644 --- a/weed/command/mini_vector_test.go +++ b/weed/command/mini_vector_test.go @@ -1,10 +1,52 @@ package command import ( + "os" + "path/filepath" "strings" "testing" + + "github.com/seaweedfs/seaweedfs/weed/util" ) +func TestPrepareMiniVectorWorker(t *testing.T) { + oldVector, oldWorker, oldBinary, oldPath, oldKey := *miniVector, *miniVectorWorker, *miniVectorWorkerBinary, miniVectorWorkerPath, miniS3Options.lanceWorkerKey + defer func() { + *miniVector, *miniVectorWorker, *miniVectorWorkerBinary, miniVectorWorkerPath, miniS3Options.lanceWorkerKey = oldVector, oldWorker, oldBinary, oldPath, oldKey + }() + dir := t.TempDir() + worker, missing := filepath.Join(dir, "weed-worker"), filepath.Join(dir, "missing") + if err := os.WriteFile(worker, []byte("#!/bin/sh\n"), 0755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", dir) + t.Setenv("WEED_JWT_FILER_SIGNING_KEY", "") + defer util.GetViper().Set("jwt.filer_signing.key", "") + + for _, tc := range []struct { + name string + vector, start bool + binary string + clusterKey string + wantWorker string + wantKey bool + }{ + {"installed", true, true, "weed-worker", "", worker, true}, + {"cluster key configured", true, true, "weed-worker", "cluster-key", worker, false}, + {"not installed", true, true, missing, "", "", false}, + {"-vector.worker=false", true, false, "weed-worker", "", "", false}, + {"without -vector", false, true, "weed-worker", "", "", false}, + } { + *miniVector, *miniVectorWorker, *miniVectorWorkerBinary = tc.vector, tc.start, tc.binary + miniVectorWorkerPath, miniS3Options.lanceWorkerKey = "", nil + util.GetViper().Set("jwt.filer_signing.key", tc.clusterKey) + prepareMiniVectorWorker() + if miniVectorWorkerPath != tc.wantWorker || (len(miniS3Options.lanceWorkerKey) == 64) != tc.wantKey { + t.Errorf("%s: worker %q, key length %d", tc.name, miniVectorWorkerPath, len(miniS3Options.lanceWorkerKey)) + } + } +} + func TestMiniVectorRejectsExplicitListenerConflict(t *testing.T) { oldVector, oldPort, oldMetrics, oldFlags := *miniVector, *miniVectorPort, *miniVectorMetricsPort, explicitFlags defer func() { diff --git a/weed/command/s3.go b/weed/command/s3.go index 046b7f9438..06d4c67cea 100644 --- a/weed/command/s3.go +++ b/weed/command/s3.go @@ -114,6 +114,9 @@ type S3Options struct { icebergBound atomic.Bool lanceBound atomic.Bool lanceServer *lance.Server + // lanceWorkerKey is set by mini for the weed-worker it starts when the + // cluster has no jwt.filer_signing.key; not a flag. + lanceWorkerKey security.SigningKey } func init() { @@ -748,7 +751,16 @@ func (s3opt *S3Options) startIcebergServer(s3ApiServer *s3api.S3ApiServer, liste func (s3opt *S3Options) newLanceServer(s3ApiServer *s3api.S3ApiServer) *lance.Server { lanceServer := lance.NewServer(s3ApiServer, s3ApiServer) lanceServer.SetCredentialValidator(s3ApiServer) - lanceServer.SetSigningKey(s3ApiServer.FilerSigningKey) + signingKey := s3ApiServer.FilerSigningKey + if workerKey := s3opt.lanceWorkerKey; len(workerKey) > 0 { + signingKey = func() security.SigningKey { + if key := s3ApiServer.FilerSigningKey(); len(key) > 0 { + return key + } + return workerKey + } + } + lanceServer.SetSigningKey(signingKey) if s3opt.vendsCredentials() { lanceServer.SetCredentialVendor(lanceCredentialVendor{s3ApiServer}) }