feat(storage): add private file layer and isolate SeaweedFS on a storage network
This commit is contained in:
1 parent
f5b07e8f4d
commit
8378ad45b0
22 files changed
+1668
-40
No files matched your search
@@ -6,13 +6,16 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
tcexec "github.com/testcontainers/testcontainers-go/exec"
|
||||
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"github.com/twmb/franz-go/pkg/kadm"
|
||||
@@ -53,6 +56,11 @@ const (
|
||||
kafkaStartupTimeout = time.Minute
|
||||
kafkaReadyTimeout = 30 * time.Second
|
||||
|
||||
seaweedfsS3Port = "8333/tcp"
|
||||
seaweedfsFilerPort = "8888/tcp"
|
||||
seaweedfsMasterPort = "9333/tcp"
|
||||
seaweedfsVolumePort = "9340/tcp"
|
||||
|
||||
kafkaClientPort = "9092/tcp"
|
||||
// kafkaListenersFile is written by a post-start hook once the host port is
|
||||
// mapped; the container entrypoint waits for it before launching the broker.
|
||||
@@ -71,6 +79,18 @@ const (
|
||||
kafkaService
|
||||
)
|
||||
|
||||
// seaweedfsEntrypoint mirrors deploy/docker/docker-compose.yml. Each start
|
||||
// gets random signing keys, so the internal filer and volume HTTP APIs reject
|
||||
// requests without a JWT minted inside the container.
|
||||
var seaweedfsEntrypoint = []string{
|
||||
"/bin/sh", "-ec",
|
||||
`for name in WEED_JWT_SIGNING_KEY WEED_JWT_SIGNING_READ_KEY WEED_JWT_FILER_SIGNING_KEY WEED_JWT_FILER_SIGNING_READ_KEY; do
|
||||
export "$name=$(head -c 32 /dev/urandom | base64)"
|
||||
done
|
||||
exec /entrypoint.sh "$@"`,
|
||||
"seaweedfs",
|
||||
}
|
||||
|
||||
// defaultServices form the full readiness environment. Kafka is not part of
|
||||
// readiness, so it starts only for tests that select kafkaService.
|
||||
var defaultServices = []service{postgresService, redisService, seaweedfsService}
|
||||
@@ -252,21 +272,24 @@ func (env *testEnvironment) startSeaweedFS(ctx context.Context, t *testing.T) {
|
||||
t.Helper()
|
||||
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
||||
ContainerRequest: testcontainers.ContainerRequest{
|
||||
Image: seaweedfsImage,
|
||||
ExposedPorts: []string{"8333/tcp"},
|
||||
Image: seaweedfsImage,
|
||||
// Only the S3 port is used by the adapter. The internal ports are
|
||||
// published so tests can prove they refuse unauthenticated access.
|
||||
ExposedPorts: []string{seaweedfsS3Port, seaweedfsFilerPort, seaweedfsMasterPort, seaweedfsVolumePort},
|
||||
Entrypoint: seaweedfsEntrypoint,
|
||||
Env: map[string]string{
|
||||
"AWS_ACCESS_KEY_ID": env.Config.SeaweedFSAccessKey,
|
||||
"AWS_SECRET_ACCESS_KEY": env.Config.SeaweedFSSecretKey,
|
||||
"S3_BUCKET": env.Config.SeaweedFSBucket,
|
||||
},
|
||||
Cmd: []string{
|
||||
"mini", "-dir=/data", "-ip=127.0.0.1", "-ip.bind=0.0.0.0",
|
||||
"mini", "-dir=/data", "-ip=127.0.0.1", "-ip.bind=0.0.0.0", "-whiteList=127.0.0.1,@",
|
||||
"-webdav=false", "-admin.ui=false", "-s3.iam=false",
|
||||
"-s3.port.iceberg=0", "-s3.port.lance=0", "-s3.autoCreateBucket=false",
|
||||
"-s3.allowDeleteBucketNotEmpty=false", "-master.telemetry=false",
|
||||
},
|
||||
Mounts: testcontainers.ContainerMounts{testcontainers.VolumeMount(env.seaweedfsVolume, "/data")},
|
||||
WaitingFor: wait.ForHTTP("/healthz").WithPort("8333/tcp").WithStartupTimeout(seaweedfsStartupTimeout),
|
||||
WaitingFor: wait.ForHTTP("/healthz").WithPort(seaweedfsS3Port).WithStartupTimeout(seaweedfsStartupTimeout),
|
||||
},
|
||||
Started: true,
|
||||
})
|
||||
@@ -275,7 +298,7 @@ func (env *testEnvironment) startSeaweedFS(ctx context.Context, t *testing.T) {
|
||||
t.Fatalf("start SeaweedFS container: %v", err)
|
||||
}
|
||||
env.seaweedfsContainer = container
|
||||
endpoint, err := container.PortEndpoint(ctx, "8333/tcp", "http")
|
||||
endpoint, err := container.PortEndpoint(ctx, seaweedfsS3Port, "http")
|
||||
if err != nil {
|
||||
t.Fatalf("get SeaweedFS endpoint: %v", err)
|
||||
}
|
||||
@@ -284,6 +307,44 @@ func (env *testEnvironment) startSeaweedFS(ctx context.Context, t *testing.T) {
|
||||
waitForStorage(ctx, t, env.SeaweedFS)
|
||||
}
|
||||
|
||||
// seaweedfsInternalEndpoint returns a host URL for an internal SeaweedFS port.
|
||||
func (env *testEnvironment) seaweedfsInternalEndpoint(ctx context.Context, t *testing.T, port string) string {
|
||||
t.Helper()
|
||||
if env.seaweedfsContainer == nil {
|
||||
t.Fatal("SeaweedFS was not selected in bootstrap")
|
||||
}
|
||||
endpoint, err := env.seaweedfsContainer.PortEndpoint(ctx, port, "http")
|
||||
if err != nil {
|
||||
t.Fatalf("get SeaweedFS internal endpoint: %v", err)
|
||||
}
|
||||
return endpoint
|
||||
}
|
||||
|
||||
// assignSeaweedFSVolume asks the master for a file ID from inside the
|
||||
// container, where the whitelist allows it, and returns the fid with the
|
||||
// signed volume write token the master hands out.
|
||||
func (env *testEnvironment) assignSeaweedFSVolume(ctx context.Context, t *testing.T) (string, string) {
|
||||
t.Helper()
|
||||
code, output, err := env.seaweedfsContainer.Exec(ctx, []string{
|
||||
"sh", "-ec", `curl -sf -D - -o /tmp/assign http://127.0.0.1:9333/dir/assign | sed -n "s/^[Aa]uthorization: //p" | tr -d "\r"; cat /tmp/assign`,
|
||||
}, tcexec.Multiplexed())
|
||||
if err != nil || code != 0 {
|
||||
t.Fatalf("assign SeaweedFS volume: exit %d, %v", code, err)
|
||||
}
|
||||
data, err := io.ReadAll(output)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, body, _ := strings.Cut(string(data), "\n")
|
||||
var assignment struct {
|
||||
FID string `json:"fid"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(body), &assignment); err != nil || assignment.FID == "" || token == "" {
|
||||
t.Fatalf("unexpected volume assignment: %v", err)
|
||||
}
|
||||
return assignment.FID, token
|
||||
}
|
||||
|
||||
func (env *testEnvironment) newStorage(t *testing.T, settings config.Config) *seaweedfs.Storage {
|
||||
t.Helper()
|
||||
storage, cleanup, err := seaweedfs.NewStorage(settings)
|
||||
|
||||
Reference in new issue
Block a user