Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ overrides it.
| `FLEET_PUBLIC_URL` | `--public-url` | — | the fleet's public HTTPS origin (https://fleet.example.org), for the agents' one-liner and links |
| `FLEET_LISTEN` | `--listen` | `127.0.0.1:8070` | the HTTP listener's address |
| `FLEET_DB` | `--db` | — | the SQLite database file (default: fleet.db in FLEET_DATA) |
| `FLEET_DB_JOURNAL` | `--db-journal` | `wal` | the SQLite journal mode: `wal`, `delete`, `truncate` or `persist`; use `delete` or `truncate` on network filesystems where WAL locking is unsafe; those modes use `synchronous=FULL` |
| `FLEET_DATA` | `--data` | `fleet-data` | the folder of the fleet's files: the library's blobs, the releases' zips, fleet.key, bin/ |
| `FLEET_SECRET_KEY` | `--secret-key` | — | the key that encrypts secrets at rest (made if missing; default: fleet.key in FLEET_DATA) |
| `FLEET_TRUSTED_PROXIES` | `--trusted-proxies` | — | reverse proxies (addresses or CIDRs, commas) whose X-Forwarded-* are believed |
Expand Down
10 changes: 5 additions & 5 deletions cmd/opence-fleet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func openFleet(args []string, name string) (*config.ServeConfig, *store.Store, *
if err := os.MkdirAll(c.Data, 0o700); err != nil {
return nil, nil, nil, err
}
s, err := store.Open(c.DB)
s, err := store.OpenWithJournal(c.DB, c.DBJournal)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func serve(args []string) error {
if err := update.BackupDatabase(c.DB, version, log); err != nil {
return err
}
s, err := store.Open(c.DB)
s, err := store.OpenWithJournal(c.DB, c.DBJournal)
if err != nil {
return fmt.Errorf("the database %s: %w", c.DB, err)
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func joinToken(args []string) error {
if err != nil {
return err
}
s, err := store.Open(c.DB)
s, err := store.OpenWithJournal(c.DB, c.DBJournal)
if err != nil {
return err
}
Expand Down Expand Up @@ -522,7 +522,7 @@ func releaseZip(args []string) error {
if err != nil {
return err
}
s, err := store.Open(c.DB)
s, err := store.OpenWithJournal(c.DB, c.DBJournal)
if err != nil {
return err
}
Expand Down Expand Up @@ -583,7 +583,7 @@ func resetPassword(args []string) error {
if err != nil {
return err
}
s, err := store.Open(c.DB)
s, err := store.OpenWithJournal(c.DB, c.DBJournal)
if err != nil {
return err
}
Expand Down
34 changes: 21 additions & 13 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var Serve = []Option{
{Env: "FLEET_PUBLIC_URL", Flag: "public-url", Help: "the fleet's public HTTPS origin (https://fleet.example.org), for the agents' one-liner and links"},
{Env: "FLEET_LISTEN", Flag: "listen", Help: "the HTTP listener's address", Default: "127.0.0.1:8070"},
{Env: "FLEET_DB", Flag: "db", Help: "the SQLite database file (default: fleet.db in FLEET_DATA)"},
{Env: "FLEET_DB_JOURNAL", Flag: "db-journal", Help: "the SQLite journal mode: wal, delete, truncate or persist (use delete or truncate on network filesystems where WAL locking is unsafe)", Default: "wal"},
{Env: "FLEET_DATA", Flag: "data", Help: "the folder of the fleet's files: the library's blobs, the releases' zips, fleet.key, bin/", Default: "fleet-data"},
{Env: "FLEET_SECRET_KEY", Flag: "secret-key", Help: "the key that encrypts secrets at rest (made if missing; default: fleet.key in FLEET_DATA)"},
{Env: "FLEET_TRUSTED_PROXIES", Flag: "trusted-proxies", Help: "reverse proxies (addresses or CIDRs, commas) whose X-Forwarded-* are believed"},
Expand Down Expand Up @@ -139,25 +140,25 @@ func Bool(text string) (bool, error) {

// ServeConfig is the control plane's options, read.
type ServeConfig struct {
Name, PublicURL, Listen, DB, Data, SecretKey string
TrustedProxies []netip.Prefix
TLSCert, TLSKey, MastersFile string
Image, ReleaseRepository string
ReleaseCheckInterval time.Duration
EventsDays, SamplesDays int
LogAddresses, PublicStatus, Metrics bool
InsecureHTTP bool
LogLevel, Updates, UpdateURL, UpdateKey string
Timezone *time.Location
CAFile string
SessionHours, SessionIdleMinutes int
Name, PublicURL, Listen, DB, DBJournal, Data, SecretKey string
TrustedProxies []netip.Prefix
TLSCert, TLSKey, MastersFile string
Image, ReleaseRepository string
ReleaseCheckInterval time.Duration
EventsDays, SamplesDays int
LogAddresses, PublicStatus, Metrics bool
InsecureHTTP bool
LogLevel, Updates, UpdateURL, UpdateKey string
Timezone *time.Location
CAFile string
SessionHours, SessionIdleMinutes int
}

// LoadServe reads the control plane's options.
func LoadServe(v Values) (*ServeConfig, error) {
c := &ServeConfig{
Name: v.Get("FLEET_NAME"), PublicURL: strings.TrimRight(v.Get("FLEET_PUBLIC_URL"), "/"),
Listen: v.Get("FLEET_LISTEN"), DB: v.Get("FLEET_DB"), Data: v.Get("FLEET_DATA"),
Listen: v.Get("FLEET_LISTEN"), DB: v.Get("FLEET_DB"), DBJournal: strings.ToLower(v.Get("FLEET_DB_JOURNAL")), Data: v.Get("FLEET_DATA"),
SecretKey: v.Get("FLEET_SECRET_KEY"), TLSCert: v.Get("FLEET_TLS_CERT"), TLSKey: v.Get("FLEET_TLS_KEY"),
MastersFile: v.Get("FLEET_MASTERS_FILE"), Image: strings.TrimRight(v.Get("FLEET_IMAGE"), "/"),
ReleaseRepository: v.Get("FLEET_RELEASE_REPOSITORY"), LogLevel: strings.ToLower(v.Get("FLEET_LOG_LEVEL")),
Expand All @@ -177,6 +178,13 @@ func LoadServe(v Values) (*ServeConfig, error) {
if c.DB == "" {
c.DB = filepath.Join(c.Data, "fleet.db")
}
switch c.DBJournal {
case "":
c.DBJournal = "wal"
case "wal", "delete", "truncate", "persist":
default:
errs = append(errs, errors.New("FLEET_DB_JOURNAL: wal, delete, truncate or persist"))
}
if c.SecretKey == "" {
c.SecretKey = filepath.Join(c.Data, "fleet.key")
}
Expand Down
27 changes: 27 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import (
"flag"
"testing"
)

func TestLoadServeJournal(t *testing.T) {
load := func(value string) (*ServeConfig, error) {
set := flag.NewFlagSet("serve", flag.ContinueOnError)
values := Flags(set, Serve)
if err := set.Parse([]string{"--db-journal", value}); err != nil {
t.Fatal(err)
}
return LoadServe(values)
}
c, err := load("delete")
if err != nil {
t.Fatal(err)
}
if c.DBJournal != "delete" {
t.Fatalf("journal = %q", c.DBJournal)
}
if _, err := load("not-a-journal"); err == nil {
t.Fatal("invalid journal mode accepted")
}
}
27 changes: 24 additions & 3 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,26 @@ type Store struct {
Path string
}

// Open opens (making it, mode 0600) the database and brings its schema up
// to date.
// Open opens (making it, mode 0600) the database with the default WAL
// journal and brings its schema up to date.
func Open(path string) (*Store, error) {
return OpenWithJournal(path, "wal")
}

// OpenWithJournal opens (making it, mode 0600) the database with a journal
// mode of wal, delete, truncate or persist and brings its schema up to date.
// delete and truncate are the safe choices on network filesystems, where WAL
// locking is not reliable.
func OpenWithJournal(path, journal string) (*Store, error) {
journal = strings.ToLower(strings.TrimSpace(journal))
if journal == "" {
journal = "wal"
}
switch journal {
case "wal", "delete", "truncate", "persist":
default:
return nil, fmt.Errorf("journal mode %q: wal, delete, truncate or persist", journal)
}
memory := path == ":memory:" || strings.HasPrefix(path, "file::memory:")
if !memory {
if dir := filepath.Dir(path); dir != "" {
Expand All @@ -55,7 +72,11 @@ func Open(path string) (*Store, error) {
// (one connection: each would be its own empty database)
dsn = "file::memory:?_pragma=foreign_keys(1)&_txlock=immediate"
} else {
dsn += "&_pragma=journal_mode(WAL)&_pragma=synchronous(NORMAL)"
synchronous := "NORMAL"
if journal != "wal" {
synchronous = "FULL"
}
dsn += "&_pragma=journal_mode(" + strings.ToUpper(journal) + ")&_pragma=synchronous(" + synchronous + ")"
}
conn, err := sql.Open("sqlite", dsn)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions internal/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -146,3 +147,34 @@ func TestUpdatePolicyNewFleet(t *testing.T) {
}
}
}

func TestOpenWithJournal(t *testing.T) {
path := filepath.Join(t.TempDir(), "fleet.db")
s, err := OpenWithJournal(path, "delete")
if err != nil {
t.Fatal(err)
}
var mode string
if err := s.DB.QueryRow(`PRAGMA journal_mode`).Scan(&mode); err != nil {
t.Fatal(err)
}
if strings.ToLower(mode) != "delete" {
t.Fatalf("journal mode = %q", mode)
}
var synchronous int
if err := s.DB.QueryRow(`PRAGMA synchronous`).Scan(&synchronous); err != nil {
t.Fatal(err)
}
if synchronous != 2 {
t.Fatalf("synchronous = %d, want FULL (2)", synchronous)
}
s.Close()
for _, suffix := range []string{"-wal", "-shm"} {
if _, err := os.Stat(path + suffix); !os.IsNotExist(err) {
t.Fatalf("rollback journal left %s: %v", suffix, err)
}
}
if _, err := OpenWithJournal(path, "not-a-journal"); err == nil {
t.Fatal("invalid journal mode accepted")
}
}