mirror of
https://github.com/ferdzo/fs.git
synced 2026-04-05 20:06:25 +00:00
Compare commits
7 Commits
f57c7b8390
...
v0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| b592d6a2f0 | |||
| ef12326975 | |||
| a23577d531 | |||
| e8256d66e3 | |||
| ad53a6d8ac | |||
| cfb9b591ac | |||
| b27f1186cf |
24
.github/workflows/ci.yml
vendored
Normal file
24
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: ["main"]
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version-file: go.mod
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: |
|
||||||
|
export GOCACHE=/tmp/go-build-cache
|
||||||
|
go test ./...
|
||||||
|
|
||||||
66
.github/workflows/release-image.yml
vendored
Normal file
66
.github/workflows/release-image.yml
vendored
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
name: Release Image
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "v*.*.*"
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
docker:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set build date
|
||||||
|
id: date
|
||||||
|
run: echo "value=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v3
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Log in to GHCR
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Docker metadata
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: ghcr.io/${{ github.repository }}
|
||||||
|
tags: |
|
||||||
|
type=semver,pattern={{version}}
|
||||||
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
|
type=semver,pattern={{major}}
|
||||||
|
type=sha
|
||||||
|
labels: |
|
||||||
|
org.opencontainers.image.title=fs
|
||||||
|
org.opencontainers.image.description=Lightweight S3-compatible object storage
|
||||||
|
org.opencontainers.image.source=https://github.com/${{ github.repository }}
|
||||||
|
org.opencontainers.image.revision=${{ github.sha }}
|
||||||
|
org.opencontainers.image.created=${{ steps.date.outputs.value }}
|
||||||
|
|
||||||
|
- name: Build and push image
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: true
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
build-args: |
|
||||||
|
VERSION=${{ github.ref_name }}
|
||||||
|
COMMIT=${{ github.sha }}
|
||||||
|
DATE=${{ steps.date.outputs.value }}
|
||||||
|
|
||||||
@@ -6,7 +6,13 @@ COPY go.mod go.sum ./
|
|||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/fs .
|
ARG VERSION=dev
|
||||||
|
ARG COMMIT=none
|
||||||
|
ARG DATE=unknown
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||||
|
-trimpath \
|
||||||
|
-ldflags "-s -w -X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=${DATE}" \
|
||||||
|
-o /app/fs .
|
||||||
|
|
||||||
FROM alpine:3.23 AS runner
|
FROM alpine:3.23 AS runner
|
||||||
|
|
||||||
|
|||||||
77
README.md
77
README.md
@@ -9,6 +9,19 @@ Single binary, two modes:
|
|||||||
- `fs server` starts the server explicitly
|
- `fs server` starts the server explicitly
|
||||||
- `fs admin ...` runs admin CLI commands
|
- `fs admin ...` runs admin CLI commands
|
||||||
|
|
||||||
|
## Versioning and Releases
|
||||||
|
|
||||||
|
- Versioning follows SemVer tags: `vMAJOR.MINOR.PATCH` (example: `v0.4.2`).
|
||||||
|
- `fs version` shows build metadata (`version`, `commit`, `date`).
|
||||||
|
- Pushing a tag like `v0.4.2` triggers Docker image build/push via GitHub Actions.
|
||||||
|
- Published images: `ghcr.io/<owner>/<repo>:v0.4.2` and related semver tags.
|
||||||
|
|
||||||
|
Tag release example:
|
||||||
|
```bash
|
||||||
|
git tag v0.1.0
|
||||||
|
git push origin v0.1.0
|
||||||
|
```
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
Bucket operations:
|
Bucket operations:
|
||||||
@@ -49,6 +62,61 @@ Admin API (JSON):
|
|||||||
- `PUT /_admin/v1/users/{accessKeyId}/status`
|
- `PUT /_admin/v1/users/{accessKeyId}/status`
|
||||||
- `DELETE /_admin/v1/users/{accessKeyId}`
|
- `DELETE /_admin/v1/users/{accessKeyId}`
|
||||||
|
|
||||||
|
Admin API policy examples (SigV4):
|
||||||
|
```bash
|
||||||
|
ENDPOINT="http://localhost:2600"
|
||||||
|
REGION="us-east-1"
|
||||||
|
ADMIN_ACCESS_KEY="${FS_ROOT_USER}"
|
||||||
|
ADMIN_SECRET_KEY="${FS_ROOT_PASSWORD}"
|
||||||
|
SIGV4="aws:amz:${REGION}:s3"
|
||||||
|
```
|
||||||
|
|
||||||
|
Replace user policy with one scoped statement:
|
||||||
|
```bash
|
||||||
|
curl --aws-sigv4 "$SIGV4" \
|
||||||
|
--user "${ADMIN_ACCESS_KEY}:${ADMIN_SECRET_KEY}" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-X PUT "${ENDPOINT}/_admin/v1/users/test-user/policy" \
|
||||||
|
-d '{
|
||||||
|
"policy": {
|
||||||
|
"statements": [
|
||||||
|
{
|
||||||
|
"effect": "allow",
|
||||||
|
"actions": ["s3:ListBucket", "s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
||||||
|
"bucket": "backup",
|
||||||
|
"prefix": "restic/*"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
Set multiple statements (for multiple buckets):
|
||||||
|
```bash
|
||||||
|
curl --aws-sigv4 "$SIGV4" \
|
||||||
|
--user "${ADMIN_ACCESS_KEY}:${ADMIN_SECRET_KEY}" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-X PUT "${ENDPOINT}/_admin/v1/users/test-user/policy" \
|
||||||
|
-d '{
|
||||||
|
"policy": {
|
||||||
|
"statements": [
|
||||||
|
{
|
||||||
|
"effect": "allow",
|
||||||
|
"actions": ["s3:ListBucket", "s3:GetObject"],
|
||||||
|
"bucket": "test-bucket",
|
||||||
|
"prefix": "*"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"effect": "allow",
|
||||||
|
"actions": ["s3:ListBucket", "s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
||||||
|
"bucket": "test-bucket-2",
|
||||||
|
"prefix": "*"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
Admin CLI:
|
Admin CLI:
|
||||||
- `fs admin user create --access-key backup-user --role readwrite`
|
- `fs admin user create --access-key backup-user --role readwrite`
|
||||||
- `fs admin user list`
|
- `fs admin user list`
|
||||||
@@ -56,8 +124,12 @@ Admin CLI:
|
|||||||
- `fs admin user set-status backup-user --status disabled`
|
- `fs admin user set-status backup-user --status disabled`
|
||||||
- `fs admin user set-role backup-user --role readonly --bucket backup-bucket --prefix restic/`
|
- `fs admin user set-role backup-user --role readonly --bucket backup-bucket --prefix restic/`
|
||||||
- `fs admin user set-role backup-user --role readwrite --bucket backups-2` (appends another statement)
|
- `fs admin user set-role backup-user --role readwrite --bucket backups-2` (appends another statement)
|
||||||
|
- `fs admin user remove-role backup-user --role readonly --bucket backup-bucket --prefix restic/`
|
||||||
- `fs admin user set-role backup-user --role admin --replace` (replaces all statements)
|
- `fs admin user set-role backup-user --role admin --replace` (replaces all statements)
|
||||||
- `fs admin user delete backup-user`
|
- `fs admin user delete backup-user`
|
||||||
|
- `fs admin snapshot create --data-path /var/lib/fs --out /backup/fs-20260311.tar.gz`
|
||||||
|
- `fs admin snapshot inspect --file /backup/fs-20260311.tar.gz`
|
||||||
|
- `fs admin snapshot restore --file /backup/fs-20260311.tar.gz --data-path /var/lib/fs --force`
|
||||||
- `fs admin diag health`
|
- `fs admin diag health`
|
||||||
- `fs admin diag version`
|
- `fs admin diag version`
|
||||||
|
|
||||||
@@ -80,9 +152,12 @@ CLI credential/env resolution for `fs admin`:
|
|||||||
- `FS_ROOT_USER` / `FS_ROOT_PASSWORD` (same defaults as server bootstrap)
|
- `FS_ROOT_USER` / `FS_ROOT_PASSWORD` (same defaults as server bootstrap)
|
||||||
- `FSCLI_ACCESS_KEY` / `FSCLI_SECRET_KEY`
|
- `FSCLI_ACCESS_KEY` / `FSCLI_SECRET_KEY`
|
||||||
- `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY`
|
- `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY`
|
||||||
- `FSCLI_ENDPOINT` (fallback to `ADDRESS` + `PORT`, then `http://localhost:3000`)
|
- `FSCLI_ENDPOINT` (fallback to `ADDRESS` + `PORT`, then `http://localhost:2600`)
|
||||||
- `FSCLI_REGION` (fallback `FS_AUTH_REGION`, default `us-east-1`)
|
- `FSCLI_REGION` (fallback `FS_AUTH_REGION`, default `us-east-1`)
|
||||||
|
|
||||||
|
Note:
|
||||||
|
- `fs admin snapshot ...` commands operate locally on filesystem paths and do not require endpoint or auth credentials.
|
||||||
|
|
||||||
Health:
|
Health:
|
||||||
- `GET /healthz`
|
- `GET /healthz`
|
||||||
- `HEAD /healthz`
|
- `HEAD /healthz`
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"fs/api"
|
"fs/api"
|
||||||
"fs/auth"
|
"fs/auth"
|
||||||
"fs/logging"
|
"fs/logging"
|
||||||
@@ -85,7 +84,7 @@ func RunServer(ctx context.Context) error {
|
|||||||
|
|
||||||
if err := handler.Start(ctx, addr); err != nil {
|
if err := handler.Start(ctx, addr); err != nil {
|
||||||
logger.Error("server_stopped_with_error", "error", err)
|
logger.Error("server_stopped_with_error", "error", err)
|
||||||
return fmt.Errorf("server start failed: %w", err)
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,14 +137,6 @@ Each audit entry includes method, path, remote IP, and request ID (if present).
|
|||||||
|
|
||||||
## Current Scope / Limitations
|
## Current Scope / Limitations
|
||||||
- No STS/session-token auth yet.
|
- No STS/session-token auth yet.
|
||||||
- No admin API for managing multiple users yet.
|
|
||||||
- Policy language is intentionally minimal, not full IAM.
|
- Policy language is intentionally minimal, not full IAM.
|
||||||
- No automatic key rotation workflows.
|
- No automatic key rotation workflows.
|
||||||
|
- No key rotation endpoint for existing users yet.
|
||||||
## Practical Next Step
|
|
||||||
To support multiple users cleanly, add admin operations in auth service + API:
|
|
||||||
- create user
|
|
||||||
- rotate secret
|
|
||||||
- set policy
|
|
||||||
- disable/enable
|
|
||||||
- delete user
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultAdminEndpoint = "http://localhost:3000"
|
defaultAdminEndpoint = "http://localhost:2600"
|
||||||
defaultAdminRegion = "us-east-1"
|
defaultAdminRegion = "us-east-1"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -48,6 +48,7 @@ func newAdminCommand(build BuildInfo) *cobra.Command {
|
|||||||
|
|
||||||
cmd.AddCommand(newAdminUserCommand(opts))
|
cmd.AddCommand(newAdminUserCommand(opts))
|
||||||
cmd.AddCommand(newAdminDiagCommand(opts, build))
|
cmd.AddCommand(newAdminDiagCommand(opts, build))
|
||||||
|
cmd.AddCommand(newAdminSnapshotCommand(opts))
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +108,7 @@ func endpointFromServerConfig(address string, port int) string {
|
|||||||
host = "localhost"
|
host = "localhost"
|
||||||
}
|
}
|
||||||
if port <= 0 || port > 65535 {
|
if port <= 0 || port > 65535 {
|
||||||
port = 3000
|
port = 2600
|
||||||
}
|
}
|
||||||
return "http://" + net.JoinHostPort(host, strconv.Itoa(port))
|
return "http://" + net.JoinHostPort(host, strconv.Itoa(port))
|
||||||
}
|
}
|
||||||
|
|||||||
728
cmd/admin_snapshot.go
Normal file
728
cmd/admin_snapshot.go
Normal file
@@ -0,0 +1,728 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"compress/gzip"
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
bolt "go.etcd.io/bbolt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
snapshotManifestPath = ".fs-snapshot/manifest.json"
|
||||||
|
snapshotFormat = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
type snapshotFileEntry struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
SHA256 string `json:"sha256"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type snapshotManifest struct {
|
||||||
|
FormatVersion int `json:"formatVersion"`
|
||||||
|
CreatedAt string `json:"createdAt"`
|
||||||
|
SourcePath string `json:"sourcePath"`
|
||||||
|
Files []snapshotFileEntry `json:"files"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type snapshotSummary struct {
|
||||||
|
SnapshotFile string `json:"snapshotFile"`
|
||||||
|
CreatedAt string `json:"createdAt"`
|
||||||
|
SourcePath string `json:"sourcePath"`
|
||||||
|
FileCount int `json:"fileCount"`
|
||||||
|
TotalBytes int64 `json:"totalBytes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAdminSnapshotCommand(opts *adminOptions) *cobra.Command {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "snapshot",
|
||||||
|
Short: "Offline snapshot and restore utilities",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
return cmd.Help()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.AddCommand(newAdminSnapshotCreateCommand(opts))
|
||||||
|
cmd.AddCommand(newAdminSnapshotInspectCommand(opts))
|
||||||
|
cmd.AddCommand(newAdminSnapshotRestoreCommand(opts))
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAdminSnapshotCreateCommand(opts *adminOptions) *cobra.Command {
|
||||||
|
var dataPath string
|
||||||
|
var outFile string
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "create",
|
||||||
|
Short: "Create offline snapshot tarball (.tar.gz)",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
dataPath = strings.TrimSpace(dataPath)
|
||||||
|
outFile = strings.TrimSpace(outFile)
|
||||||
|
if dataPath == "" {
|
||||||
|
return usageError("fs admin snapshot create --data-path <path> --out <snapshot.tar.gz>", "--data-path is required")
|
||||||
|
}
|
||||||
|
if outFile == "" {
|
||||||
|
return usageError("fs admin snapshot create --data-path <path> --out <snapshot.tar.gz>", "--out is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := createSnapshotArchive(context.Background(), dataPath, outFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if opts.JSON {
|
||||||
|
return writeJSON(cmd.OutOrStdout(), result)
|
||||||
|
}
|
||||||
|
_, err = fmt.Fprintf(cmd.OutOrStdout(), "snapshot created: %s (files=%d bytes=%d)\n", result.SnapshotFile, result.FileCount, result.TotalBytes)
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.Flags().StringVar(&dataPath, "data-path", "", "Source data path (must contain metadata.db)")
|
||||||
|
cmd.Flags().StringVar(&outFile, "out", "", "Output snapshot file path (.tar.gz)")
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAdminSnapshotInspectCommand(opts *adminOptions) *cobra.Command {
|
||||||
|
var filePath string
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "inspect",
|
||||||
|
Short: "Inspect and verify snapshot archive integrity",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
filePath = strings.TrimSpace(filePath)
|
||||||
|
if filePath == "" {
|
||||||
|
return usageError("fs admin snapshot inspect --file <snapshot.tar.gz>", "--file is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
manifest, summary, err := inspectSnapshotArchive(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if opts.JSON {
|
||||||
|
return writeJSON(cmd.OutOrStdout(), map[string]any{
|
||||||
|
"summary": summary,
|
||||||
|
"manifest": manifest,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_, err = fmt.Fprintf(
|
||||||
|
cmd.OutOrStdout(),
|
||||||
|
"snapshot ok: %s\ncreated_at=%s source=%s files=%d bytes=%d\n",
|
||||||
|
summary.SnapshotFile,
|
||||||
|
summary.CreatedAt,
|
||||||
|
summary.SourcePath,
|
||||||
|
summary.FileCount,
|
||||||
|
summary.TotalBytes,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.Flags().StringVar(&filePath, "file", "", "Snapshot file path (.tar.gz)")
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAdminSnapshotRestoreCommand(opts *adminOptions) *cobra.Command {
|
||||||
|
var filePath string
|
||||||
|
var dataPath string
|
||||||
|
var force bool
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "restore",
|
||||||
|
Short: "Restore snapshot into a data path (offline only)",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
filePath = strings.TrimSpace(filePath)
|
||||||
|
dataPath = strings.TrimSpace(dataPath)
|
||||||
|
if filePath == "" {
|
||||||
|
return usageError("fs admin snapshot restore --file <snapshot.tar.gz> --data-path <path> [--force]", "--file is required")
|
||||||
|
}
|
||||||
|
if dataPath == "" {
|
||||||
|
return usageError("fs admin snapshot restore --file <snapshot.tar.gz> --data-path <path> [--force]", "--data-path is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := restoreSnapshotArchive(context.Background(), filePath, dataPath, force)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if opts.JSON {
|
||||||
|
return writeJSON(cmd.OutOrStdout(), result)
|
||||||
|
}
|
||||||
|
_, err = fmt.Fprintf(
|
||||||
|
cmd.OutOrStdout(),
|
||||||
|
"snapshot restored to %s (files=%d bytes=%d)\n",
|
||||||
|
result.SourcePath,
|
||||||
|
result.FileCount,
|
||||||
|
result.TotalBytes,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.Flags().StringVar(&filePath, "file", "", "Snapshot file path (.tar.gz)")
|
||||||
|
cmd.Flags().StringVar(&dataPath, "data-path", "", "Destination data path")
|
||||||
|
cmd.Flags().BoolVar(&force, "force", false, "Overwrite destination data path if it exists")
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func createSnapshotArchive(ctx context.Context, dataPath, outFile string) (*snapshotSummary, error) {
|
||||||
|
_ = ctx
|
||||||
|
sourceAbs, err := filepath.Abs(filepath.Clean(dataPath))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
outAbs, err := filepath.Abs(filepath.Clean(outFile))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if isPathWithin(sourceAbs, outAbs) {
|
||||||
|
return nil, errors.New("output file cannot be inside --data-path")
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := os.Stat(sourceAbs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !info.IsDir() {
|
||||||
|
return nil, fmt.Errorf("data path %q is not a directory", sourceAbs)
|
||||||
|
}
|
||||||
|
if err := ensureMetadataExists(sourceAbs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := ensureDataPathOffline(sourceAbs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
manifest, totalBytes, err := buildSnapshotManifest(sourceAbs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(filepath.Dir(outAbs), 0o755); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tmpPath := outAbs + ".tmp-" + strconvNowNano()
|
||||||
|
file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = file.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
gzw := gzip.NewWriter(file)
|
||||||
|
tw := tar.NewWriter(gzw)
|
||||||
|
if err := writeManifestToTar(tw, manifest); err != nil {
|
||||||
|
_ = tw.Close()
|
||||||
|
_ = gzw.Close()
|
||||||
|
_ = os.Remove(tmpPath)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range manifest.Files {
|
||||||
|
absPath := filepath.Join(sourceAbs, filepath.FromSlash(entry.Path))
|
||||||
|
if err := writeFileToTar(tw, absPath, entry.Path); err != nil {
|
||||||
|
_ = tw.Close()
|
||||||
|
_ = gzw.Close()
|
||||||
|
_ = os.Remove(tmpPath)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tw.Close(); err != nil {
|
||||||
|
_ = gzw.Close()
|
||||||
|
_ = os.Remove(tmpPath)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := gzw.Close(); err != nil {
|
||||||
|
_ = os.Remove(tmpPath)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := file.Sync(); err != nil {
|
||||||
|
_ = os.Remove(tmpPath)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := file.Close(); err != nil {
|
||||||
|
_ = os.Remove(tmpPath)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := os.Rename(tmpPath, outAbs); err != nil {
|
||||||
|
_ = os.Remove(tmpPath)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := syncDir(filepath.Dir(outAbs)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &snapshotSummary{
|
||||||
|
SnapshotFile: outAbs,
|
||||||
|
CreatedAt: manifest.CreatedAt,
|
||||||
|
SourcePath: sourceAbs,
|
||||||
|
FileCount: len(manifest.Files),
|
||||||
|
TotalBytes: totalBytes,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func inspectSnapshotArchive(filePath string) (*snapshotManifest, *snapshotSummary, error) {
|
||||||
|
fileAbs, err := filepath.Abs(filepath.Clean(filePath))
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
manifest, actual, err := readSnapshotArchive(fileAbs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
expected := map[string]snapshotFileEntry{}
|
||||||
|
var totalBytes int64
|
||||||
|
for _, entry := range manifest.Files {
|
||||||
|
expected[entry.Path] = entry
|
||||||
|
totalBytes += entry.Size
|
||||||
|
}
|
||||||
|
if len(expected) != len(actual) {
|
||||||
|
return nil, nil, fmt.Errorf("snapshot validation failed: expected %d files, got %d", len(expected), len(actual))
|
||||||
|
}
|
||||||
|
for path, exp := range expected {
|
||||||
|
got, ok := actual[path]
|
||||||
|
if !ok {
|
||||||
|
return nil, nil, fmt.Errorf("snapshot validation failed: missing file %s", path)
|
||||||
|
}
|
||||||
|
if got.Size != exp.Size || got.SHA256 != exp.SHA256 {
|
||||||
|
return nil, nil, fmt.Errorf("snapshot validation failed: checksum mismatch for %s", path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return manifest, &snapshotSummary{
|
||||||
|
SnapshotFile: fileAbs,
|
||||||
|
CreatedAt: manifest.CreatedAt,
|
||||||
|
SourcePath: manifest.SourcePath,
|
||||||
|
FileCount: len(manifest.Files),
|
||||||
|
TotalBytes: totalBytes,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func restoreSnapshotArchive(ctx context.Context, filePath, destinationPath string, force bool) (*snapshotSummary, error) {
|
||||||
|
_ = ctx
|
||||||
|
manifest, summary, err := inspectSnapshotArchive(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
destAbs, err := filepath.Abs(filepath.Clean(destinationPath))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi, statErr := os.Stat(destAbs); statErr == nil && fi.IsDir() {
|
||||||
|
if err := ensureDataPathOffline(destAbs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
entries, err := os.ReadDir(destAbs)
|
||||||
|
if err == nil && len(entries) > 0 && !force {
|
||||||
|
return nil, errors.New("destination data path is not empty; use --force to overwrite")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parent := filepath.Dir(destAbs)
|
||||||
|
if err := os.MkdirAll(parent, 0o755); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
stage := filepath.Join(parent, "."+filepath.Base(destAbs)+".restore-"+strconvNowNano())
|
||||||
|
if err := os.MkdirAll(stage, 0o755); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cleanupStage := true
|
||||||
|
defer func() {
|
||||||
|
if cleanupStage {
|
||||||
|
_ = os.RemoveAll(stage)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := extractSnapshotArchive(filePath, stage, manifest); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := syncDir(stage); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(destAbs); err == nil {
|
||||||
|
if !force {
|
||||||
|
return nil, errors.New("destination data path exists; use --force to overwrite")
|
||||||
|
}
|
||||||
|
if err := os.RemoveAll(destAbs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Rename(stage, destAbs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := syncDir(parent); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cleanupStage = false
|
||||||
|
|
||||||
|
summary.SourcePath = destAbs
|
||||||
|
return summary, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureMetadataExists(dataPath string) error {
|
||||||
|
dbPath := filepath.Join(dataPath, "metadata.db")
|
||||||
|
info, err := os.Stat(dbPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("metadata.db not found in %s", dataPath)
|
||||||
|
}
|
||||||
|
if !info.Mode().IsRegular() {
|
||||||
|
return fmt.Errorf("metadata.db in %s is not a regular file", dataPath)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureDataPathOffline(dataPath string) error {
|
||||||
|
dbPath := filepath.Join(dataPath, "metadata.db")
|
||||||
|
if _, err := os.Stat(dbPath); err != nil {
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
db, err := bolt.Open(dbPath, 0o600, &bolt.Options{
|
||||||
|
Timeout: 100 * time.Millisecond,
|
||||||
|
ReadOnly: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("data path appears in use (metadata.db locked): %w", err)
|
||||||
|
}
|
||||||
|
return db.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildSnapshotManifest(dataPath string) (*snapshotManifest, int64, error) {
|
||||||
|
entries := make([]snapshotFileEntry, 0, 128)
|
||||||
|
var totalBytes int64
|
||||||
|
err := filepath.WalkDir(dataPath, func(path string, d fs.DirEntry, walkErr error) error {
|
||||||
|
if walkErr != nil {
|
||||||
|
return walkErr
|
||||||
|
}
|
||||||
|
if d.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
info, err := d.Info()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !info.Mode().IsRegular() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rel, err := filepath.Rel(dataPath, path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rel = filepath.ToSlash(filepath.Clean(rel))
|
||||||
|
if rel == "." || rel == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
sum, err := sha256File(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
totalBytes += info.Size()
|
||||||
|
entries = append(entries, snapshotFileEntry{
|
||||||
|
Path: rel,
|
||||||
|
Size: info.Size(),
|
||||||
|
SHA256: sum,
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
if len(entries) == 0 {
|
||||||
|
return nil, 0, errors.New("data path contains no regular files to snapshot")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &snapshotManifest{
|
||||||
|
FormatVersion: snapshotFormat,
|
||||||
|
CreatedAt: time.Now().UTC().Format(time.RFC3339Nano),
|
||||||
|
SourcePath: dataPath,
|
||||||
|
Files: entries,
|
||||||
|
}, totalBytes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeManifestToTar(tw *tar.Writer, manifest *snapshotManifest) error {
|
||||||
|
if tw == nil || manifest == nil {
|
||||||
|
return errors.New("invalid manifest writer input")
|
||||||
|
}
|
||||||
|
payload, err := json.Marshal(manifest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
header := &tar.Header{
|
||||||
|
Name: snapshotManifestPath,
|
||||||
|
Mode: 0o600,
|
||||||
|
Size: int64(len(payload)),
|
||||||
|
ModTime: time.Now(),
|
||||||
|
}
|
||||||
|
if err := tw.WriteHeader(header); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tw.Write(payload)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeFileToTar(tw *tar.Writer, absPath, relPath string) error {
|
||||||
|
file, err := os.Open(absPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
info, err := file.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
header := &tar.Header{
|
||||||
|
Name: relPath,
|
||||||
|
Mode: int64(info.Mode().Perm()),
|
||||||
|
Size: info.Size(),
|
||||||
|
ModTime: info.ModTime(),
|
||||||
|
}
|
||||||
|
if err := tw.WriteHeader(header); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = io.Copy(tw, file)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func readSnapshotArchive(filePath string) (*snapshotManifest, map[string]snapshotFileEntry, error) {
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
gzr, err := gzip.NewReader(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer gzr.Close()
|
||||||
|
|
||||||
|
tr := tar.NewReader(gzr)
|
||||||
|
actual := make(map[string]snapshotFileEntry)
|
||||||
|
var manifest *snapshotManifest
|
||||||
|
|
||||||
|
for {
|
||||||
|
header, err := tr.Next()
|
||||||
|
if errors.Is(err, io.EOF) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
name, err := cleanArchivePath(header.Name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if header.Typeflag == tar.TypeDir {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if header.Typeflag != tar.TypeReg && header.Typeflag != tar.TypeRegA {
|
||||||
|
return nil, nil, fmt.Errorf("unsupported tar entry type for %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == snapshotManifestPath {
|
||||||
|
raw, err := io.ReadAll(tr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
current := &snapshotManifest{}
|
||||||
|
if err := json.Unmarshal(raw, current); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
manifest = current
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
size, hashHex, err := digestReader(tr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
actual[name] = snapshotFileEntry{
|
||||||
|
Path: name,
|
||||||
|
Size: size,
|
||||||
|
SHA256: hashHex,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if manifest == nil {
|
||||||
|
return nil, nil, errors.New("snapshot manifest.json not found")
|
||||||
|
}
|
||||||
|
if manifest.FormatVersion != snapshotFormat {
|
||||||
|
return nil, nil, fmt.Errorf("unsupported snapshot format version %d", manifest.FormatVersion)
|
||||||
|
}
|
||||||
|
return manifest, actual, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractSnapshotArchive(filePath, destination string, manifest *snapshotManifest) error {
|
||||||
|
expected := make(map[string]snapshotFileEntry, len(manifest.Files))
|
||||||
|
for _, entry := range manifest.Files {
|
||||||
|
expected[entry.Path] = entry
|
||||||
|
}
|
||||||
|
seen := make(map[string]struct{}, len(expected))
|
||||||
|
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
gzr, err := gzip.NewReader(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer gzr.Close()
|
||||||
|
|
||||||
|
tr := tar.NewReader(gzr)
|
||||||
|
for {
|
||||||
|
header, err := tr.Next()
|
||||||
|
if errors.Is(err, io.EOF) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
name, err := cleanArchivePath(header.Name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if name == snapshotManifestPath {
|
||||||
|
if _, err := io.Copy(io.Discard, tr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if header.Typeflag == tar.TypeDir {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if header.Typeflag != tar.TypeReg && header.Typeflag != tar.TypeRegA {
|
||||||
|
return fmt.Errorf("unsupported tar entry type for %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
exp, ok := expected[name]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("snapshot contains unexpected file %s", name)
|
||||||
|
}
|
||||||
|
targetPath := filepath.Join(destination, filepath.FromSlash(name))
|
||||||
|
if !isPathWithin(destination, targetPath) {
|
||||||
|
return fmt.Errorf("invalid archive path %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(filepath.Dir(targetPath), 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
out, err := os.OpenFile(targetPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(header.Mode)&0o777)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hasher := sha256.New()
|
||||||
|
written, copyErr := io.Copy(io.MultiWriter(out, hasher), tr)
|
||||||
|
syncErr := out.Sync()
|
||||||
|
closeErr := out.Close()
|
||||||
|
if copyErr != nil {
|
||||||
|
return copyErr
|
||||||
|
}
|
||||||
|
if syncErr != nil {
|
||||||
|
return syncErr
|
||||||
|
}
|
||||||
|
if closeErr != nil {
|
||||||
|
return closeErr
|
||||||
|
}
|
||||||
|
if err := syncDir(filepath.Dir(targetPath)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sum := hex.EncodeToString(hasher.Sum(nil))
|
||||||
|
if written != exp.Size || sum != exp.SHA256 {
|
||||||
|
return fmt.Errorf("checksum mismatch while extracting %s", name)
|
||||||
|
}
|
||||||
|
seen[name] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(seen) != len(expected) {
|
||||||
|
return fmt.Errorf("restore validation failed: extracted %d files, expected %d", len(seen), len(expected))
|
||||||
|
}
|
||||||
|
for path := range expected {
|
||||||
|
if _, ok := seen[path]; !ok {
|
||||||
|
return fmt.Errorf("restore validation failed: missing file %s", path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanArchivePath(name string) (string, error) {
|
||||||
|
name = strings.TrimSpace(name)
|
||||||
|
if name == "" {
|
||||||
|
return "", errors.New("empty archive path")
|
||||||
|
}
|
||||||
|
name = filepath.ToSlash(filepath.Clean(name))
|
||||||
|
if strings.HasPrefix(name, "/") || strings.HasPrefix(name, "../") || strings.Contains(name, "/../") || name == ".." {
|
||||||
|
return "", fmt.Errorf("unsafe archive path %q", name)
|
||||||
|
}
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func digestReader(r io.Reader) (int64, string, error) {
|
||||||
|
hasher := sha256.New()
|
||||||
|
n, err := io.Copy(hasher, r)
|
||||||
|
if err != nil {
|
||||||
|
return 0, "", err
|
||||||
|
}
|
||||||
|
return n, hex.EncodeToString(hasher.Sum(nil)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sha256File(path string) (string, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
return sha256FromReader(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sha256FromReader(reader io.Reader) (string, error) {
|
||||||
|
hasher := sha256.New()
|
||||||
|
if _, err := io.Copy(hasher, reader); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(hasher.Sum(nil)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPathWithin(base, candidate string) bool {
|
||||||
|
base = filepath.Clean(base)
|
||||||
|
candidate = filepath.Clean(candidate)
|
||||||
|
rel, err := filepath.Rel(base, candidate)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return rel == "." || (rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func syncDir(path string) error {
|
||||||
|
dir, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer dir.Close()
|
||||||
|
return dir.Sync()
|
||||||
|
}
|
||||||
|
|
||||||
|
func strconvNowNano() string {
|
||||||
|
return fmt.Sprintf("%d", time.Now().UnixNano())
|
||||||
|
}
|
||||||
240
cmd/admin_snapshot_test.go
Normal file
240
cmd/admin_snapshot_test.go
Normal file
@@ -0,0 +1,240 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
bolt "go.etcd.io/bbolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type snapshotArchiveEntry struct {
|
||||||
|
Path string
|
||||||
|
Data []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInspectSnapshotArchiveRejectsUnsafePath(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
archive := filepath.Join(t.TempDir(), "bad.tar.gz")
|
||||||
|
manifest := manifestForEntries([]snapshotArchiveEntry{
|
||||||
|
{Path: "metadata.db", Data: []byte("db")},
|
||||||
|
})
|
||||||
|
err := writeSnapshotArchiveForTest(archive, manifest, []snapshotArchiveEntry{
|
||||||
|
{Path: "../escape", Data: []byte("oops")},
|
||||||
|
}, true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("write test archive: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = inspectSnapshotArchive(archive)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "unsafe archive path") {
|
||||||
|
t.Fatalf("expected unsafe archive path error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInspectSnapshotArchiveChecksumMismatch(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
archive := filepath.Join(t.TempDir(), "mismatch.tar.gz")
|
||||||
|
manifest := manifestForEntries([]snapshotArchiveEntry{
|
||||||
|
{Path: "chunks/c1", Data: []byte("good")},
|
||||||
|
})
|
||||||
|
err := writeSnapshotArchiveForTest(archive, manifest, []snapshotArchiveEntry{
|
||||||
|
{Path: "chunks/c1", Data: []byte("bad")},
|
||||||
|
}, true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("write test archive: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = inspectSnapshotArchive(archive)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "checksum mismatch") {
|
||||||
|
t.Fatalf("expected checksum mismatch error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInspectSnapshotArchiveMissingManifest(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
archive := filepath.Join(t.TempDir(), "no-manifest.tar.gz")
|
||||||
|
err := writeSnapshotArchiveForTest(archive, nil, []snapshotArchiveEntry{
|
||||||
|
{Path: "chunks/c1", Data: []byte("x")},
|
||||||
|
}, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("write test archive: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = inspectSnapshotArchive(archive)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "manifest.json not found") {
|
||||||
|
t.Fatalf("expected missing manifest error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInspectSnapshotArchiveUnsupportedFormat(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
archive := filepath.Join(t.TempDir(), "unsupported-format.tar.gz")
|
||||||
|
manifest := manifestForEntries([]snapshotArchiveEntry{
|
||||||
|
{Path: "chunks/c1", Data: []byte("x")},
|
||||||
|
})
|
||||||
|
manifest.FormatVersion = 99
|
||||||
|
err := writeSnapshotArchiveForTest(archive, manifest, []snapshotArchiveEntry{
|
||||||
|
{Path: "chunks/c1", Data: []byte("x")},
|
||||||
|
}, true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("write test archive: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = inspectSnapshotArchive(archive)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "unsupported snapshot format version") {
|
||||||
|
t.Fatalf("expected unsupported format error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRestoreSnapshotArchiveDestinationBehavior(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
root := t.TempDir()
|
||||||
|
archive := filepath.Join(root, "ok.tar.gz")
|
||||||
|
destination := filepath.Join(root, "dst")
|
||||||
|
|
||||||
|
entries := []snapshotArchiveEntry{
|
||||||
|
{Path: "metadata.db", Data: []byte("db-bytes")},
|
||||||
|
{Path: "chunks/c1", Data: []byte("chunk-1")},
|
||||||
|
}
|
||||||
|
manifest := manifestForEntries(entries)
|
||||||
|
if err := writeSnapshotArchiveForTest(archive, manifest, entries, true); err != nil {
|
||||||
|
t.Fatalf("write test archive: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(destination, 0o755); err != nil {
|
||||||
|
t.Fatalf("mkdir destination: %v", err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(destination, "old.txt"), []byte("old"), 0o600); err != nil {
|
||||||
|
t.Fatalf("seed destination: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := restoreSnapshotArchive(context.Background(), archive, destination, false); err == nil || !strings.Contains(err.Error(), "not empty") {
|
||||||
|
t.Fatalf("expected non-empty destination error, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := restoreSnapshotArchive(context.Background(), archive, destination, true); err != nil {
|
||||||
|
t.Fatalf("restore with force: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(filepath.Join(destination, "old.txt")); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("expected old file to be removed, stat err=%v", err)
|
||||||
|
}
|
||||||
|
got, err := os.ReadFile(filepath.Join(destination, "chunks/c1"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read restored chunk: %v", err)
|
||||||
|
}
|
||||||
|
if string(got) != "chunk-1" {
|
||||||
|
t.Fatalf("restored chunk mismatch: got %q", string(got))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateSnapshotArchiveRejectsOutputInsideDataPath(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
root := t.TempDir()
|
||||||
|
if err := os.MkdirAll(filepath.Join(root, "chunks"), 0o755); err != nil {
|
||||||
|
t.Fatalf("mkdir chunks: %v", err)
|
||||||
|
}
|
||||||
|
if err := createBoltDBForTest(filepath.Join(root, "metadata.db")); err != nil {
|
||||||
|
t.Fatalf("create metadata db: %v", err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(root, "chunks/c1"), []byte("x"), 0o600); err != nil {
|
||||||
|
t.Fatalf("write chunk: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
out := filepath.Join(root, "inside.tar.gz")
|
||||||
|
if _, err := createSnapshotArchive(context.Background(), root, out); err == nil || !strings.Contains(err.Error(), "cannot be inside") {
|
||||||
|
t.Fatalf("expected output-inside-data-path error, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeSnapshotArchiveForTest(path string, manifest *snapshotManifest, entries []snapshotArchiveEntry, includeManifest bool) error {
|
||||||
|
file, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
gzw := gzip.NewWriter(file)
|
||||||
|
defer gzw.Close()
|
||||||
|
tw := tar.NewWriter(gzw)
|
||||||
|
defer tw.Close()
|
||||||
|
|
||||||
|
if includeManifest {
|
||||||
|
raw, err := json.Marshal(manifest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := writeTarEntry(tw, snapshotManifestPath, raw); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, entry := range entries {
|
||||||
|
if err := writeTarEntry(tw, entry.Path, entry.Data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeTarEntry(tw *tar.Writer, name string, data []byte) error {
|
||||||
|
header := &tar.Header{
|
||||||
|
Name: name,
|
||||||
|
Mode: 0o600,
|
||||||
|
Size: int64(len(data)),
|
||||||
|
}
|
||||||
|
if err := tw.WriteHeader(header); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := ioCopyBytes(tw, data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func manifestForEntries(entries []snapshotArchiveEntry) *snapshotManifest {
|
||||||
|
files := make([]snapshotFileEntry, 0, len(entries))
|
||||||
|
for _, entry := range entries {
|
||||||
|
sum := sha256.Sum256(entry.Data)
|
||||||
|
files = append(files, snapshotFileEntry{
|
||||||
|
Path: filepath.ToSlash(filepath.Clean(entry.Path)),
|
||||||
|
Size: int64(len(entry.Data)),
|
||||||
|
SHA256: hex.EncodeToString(sum[:]),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return &snapshotManifest{
|
||||||
|
FormatVersion: snapshotFormat,
|
||||||
|
CreatedAt: "2026-03-11T00:00:00Z",
|
||||||
|
SourcePath: "/tmp/source",
|
||||||
|
Files: files,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createBoltDBForTest(path string) error {
|
||||||
|
db, err := bolt.Open(path, 0o600, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
return db.Update(func(tx *bolt.Tx) error {
|
||||||
|
_, err := tx.CreateBucketIfNotExists([]byte("x"))
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ioCopyBytes(w *tar.Writer, data []byte) (int64, error) {
|
||||||
|
n, err := bytes.NewReader(data).WriteTo(w)
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ func newAdminUserCommand(opts *adminOptions) *cobra.Command {
|
|||||||
cmd.AddCommand(newAdminUserDeleteCommand(opts))
|
cmd.AddCommand(newAdminUserDeleteCommand(opts))
|
||||||
cmd.AddCommand(newAdminUserSetStatusCommand(opts))
|
cmd.AddCommand(newAdminUserSetStatusCommand(opts))
|
||||||
cmd.AddCommand(newAdminUserSetRoleCommand(opts))
|
cmd.AddCommand(newAdminUserSetRoleCommand(opts))
|
||||||
|
cmd.AddCommand(newAdminUserRemoveRoleCommand(opts))
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,6 +254,68 @@ func newAdminUserSetRoleCommand(opts *adminOptions) *cobra.Command {
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newAdminUserRemoveRoleCommand(opts *adminOptions) *cobra.Command {
|
||||||
|
var (
|
||||||
|
role string
|
||||||
|
bucket string
|
||||||
|
prefix string
|
||||||
|
)
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "remove-role <access-key-id>",
|
||||||
|
Short: "Remove one role policy statement from user",
|
||||||
|
Args: requireAccessKeyArg("fs admin user remove-role <access-key-id> --role admin|readwrite|readonly [--bucket <name>] [--prefix <path>]"),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
policy, err := buildPolicyFromRole(rolePolicyOptions{
|
||||||
|
Role: role,
|
||||||
|
Bucket: bucket,
|
||||||
|
Prefix: prefix,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return usageError("fs admin user remove-role <access-key-id> --role admin|readwrite|readonly [--bucket <name>] [--prefix <path>]", err.Error())
|
||||||
|
}
|
||||||
|
if len(policy.Statements) == 0 {
|
||||||
|
return usageError("fs admin user remove-role <access-key-id> --role admin|readwrite|readonly [--bucket <name>] [--prefix <path>]", "no statement to remove")
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := newAdminAPIClient(opts, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
existing, err := client.GetUser(context.Background(), args[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if existing.Policy == nil || len(existing.Policy.Statements) == 0 {
|
||||||
|
return fmt.Errorf("user %q has no policy statements", args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
target := policy.Statements[0]
|
||||||
|
nextPolicy, removed := removePolicyStatements(existing.Policy, target)
|
||||||
|
if removed == 0 {
|
||||||
|
return fmt.Errorf("no matching statement found for role=%s bucket=%s prefix=%s", role, bucket, prefix)
|
||||||
|
}
|
||||||
|
if len(nextPolicy.Statements) == 0 {
|
||||||
|
return fmt.Errorf("cannot remove the last policy statement; add another role first or use set-role --replace")
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := client.SetUserPolicy(context.Background(), args[0], nextPolicy)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if opts.JSON {
|
||||||
|
return writeJSON(cmd.OutOrStdout(), out)
|
||||||
|
}
|
||||||
|
return writeUserTable(cmd.OutOrStdout(), out, false)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Flags().StringVar(&role, "role", "readwrite", "Role: admin|readwrite|readonly")
|
||||||
|
cmd.Flags().StringVar(&bucket, "bucket", "*", "Bucket scope, defaults to *")
|
||||||
|
cmd.Flags().StringVar(&prefix, "prefix", "*", "Prefix scope, defaults to *")
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
func mergePolicyStatements(existing *adminPolicy, addition adminPolicy) adminPolicy {
|
func mergePolicyStatements(existing *adminPolicy, addition adminPolicy) adminPolicy {
|
||||||
merged := adminPolicy{}
|
merged := adminPolicy{}
|
||||||
if existing != nil {
|
if existing != nil {
|
||||||
@@ -289,6 +352,25 @@ func policyStatementsEqual(a, b adminPolicyStatement) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removePolicyStatements(existing *adminPolicy, target adminPolicyStatement) (adminPolicy, int) {
|
||||||
|
out := adminPolicy{}
|
||||||
|
if existing == nil {
|
||||||
|
return out, 0
|
||||||
|
}
|
||||||
|
out.Principal = existing.Principal
|
||||||
|
out.Statements = make([]adminPolicyStatement, 0, len(existing.Statements))
|
||||||
|
|
||||||
|
removed := 0
|
||||||
|
for _, stmt := range existing.Statements {
|
||||||
|
if policyStatementsEqual(stmt, target) {
|
||||||
|
removed++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out.Statements = append(out.Statements, stmt)
|
||||||
|
}
|
||||||
|
return out, removed
|
||||||
|
}
|
||||||
|
|
||||||
func requireAccessKeyArg(usage string) cobra.PositionalArgs {
|
func requireAccessKeyArg(usage string) cobra.PositionalArgs {
|
||||||
return func(cmd *cobra.Command, args []string) error {
|
return func(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func NewConfig() *Config {
|
|||||||
config := &Config{
|
config := &Config{
|
||||||
DataPath: sanitizeDataPath(os.Getenv("DATA_PATH")),
|
DataPath: sanitizeDataPath(os.Getenv("DATA_PATH")),
|
||||||
Address: firstNonEmpty(strings.TrimSpace(os.Getenv("ADDRESS")), "0.0.0.0"),
|
Address: firstNonEmpty(strings.TrimSpace(os.Getenv("ADDRESS")), "0.0.0.0"),
|
||||||
Port: envIntRange("PORT", 3000, 1, 65535),
|
Port: envIntRange("PORT", 2600, 1, 65535),
|
||||||
ChunkSize: envIntRange("CHUNK_SIZE", 8192000, 1, 64*1024*1024),
|
ChunkSize: envIntRange("CHUNK_SIZE", 8192000, 1, 64*1024*1024),
|
||||||
LogLevel: strings.ToLower(firstNonEmpty(strings.TrimSpace(os.Getenv("LOG_LEVEL")), "info")),
|
LogLevel: strings.ToLower(firstNonEmpty(strings.TrimSpace(os.Getenv("LOG_LEVEL")), "info")),
|
||||||
LogFormat: strings.ToLower(firstNonEmpty(strings.TrimSpace(os.Getenv("LOG_FORMAT")), strings.TrimSpace(os.Getenv("LOG_TYPE")), "text")),
|
LogFormat: strings.ToLower(firstNonEmpty(strings.TrimSpace(os.Getenv("LOG_FORMAT")), strings.TrimSpace(os.Getenv("LOG_TYPE")), "text")),
|
||||||
|
|||||||
Reference in New Issue
Block a user