Skip to content
File

Blob: cmd/config/config.go

go124 lines
1package config
2 
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "io"
9 "os"
10 "path/filepath"
11 
12 "go.miragespace.co/specter/acme"
13 "go.miragespace.co/specter/config"
14 "go.miragespace.co/specter/util"
15 
16 "github.com/urfave/cli/v3"
17)
18 
19func Generate() *cli.Command {
20 return &cli.Command{
21 Name: "config",
22 Usage: "manage shared server configuration",
23 Commands: []*cli.Command{{
24 Name: "init",
25 Usage: "initialize shared configuration on the first server",
26 ArgsUsage: " ",
27 Description: `Create the first server's shared configuration from a JSON array of
28{name, schema, value} documents. Run once as the server's service user.
29Other servers obtain the documents from peers. The server may be running;
30existing shared configuration is never replaced.
31See docs/shared-configuration.md for the input format and rollout sequence.`,
32 Flags: []cli.Flag{
33 &cli.StringFlag{
34 Name: "data-dir",
35 Aliases: []string{"data"},
36 Usage: "the first server's existing data directory",
37 Required: true,
38 },
39 &cli.StringFlag{
40 Name: "file",
41 Usage: "JSON file containing the initial named documents",
42 Required: true,
43 },
44 },
45 Action: cmdInit,
46 }},
47 }
48}
49 
50type initialDocument struct {
51 Name string `json:"name"`
52 Schema uint64 `json:"schema"`
53 Value json.RawMessage `json:"value"`
54}
55 
56func cmdInit(_ context.Context, cmd *cli.Command) error {
57 if cmd.Args().Len() != 0 {
58 return fmt.Errorf("config init takes no positional arguments")
59 }
60 if cmd.String("data-dir") == "" {
61 return fmt.Errorf("data-dir must not be empty")
62 }
63 file, err := os.Open(cmd.String("file"))
64 if err != nil {
65 return fmt.Errorf("open initial configuration: %w", err)
66 }
67 defer file.Close()
68 
69 records, err := readInitialDocuments(file)
70 if err != nil {
71 return err
72 }
73 dataDir, err := filepath.Abs(cmd.String("data-dir"))
74 if err != nil {
75 return fmt.Errorf("resolve data directory: %w", err)
76 }
77 dir := filepath.Join(dataDir, "config")
78 if err := config.Init(dir, records); err != nil {
79 return fmt.Errorf("initialize shared configuration: %w", err)
80 }
81 _, err = fmt.Fprintf(cmd.Root().Writer, "Initialized %d shared configuration document(s) in %s\n", len(records), filepath.Join(dir, config.FileName))
82 return err
83}
84 
85func readInitialDocuments(input io.Reader) ([]config.Record, error) {
86 data, err := io.ReadAll(io.LimitReader(input, config.MaxSnapshotBytes+1))
87 if err != nil {
88 return nil, fmt.Errorf("read initial configuration: %w", err)
89 }
90 if len(data) > config.MaxSnapshotBytes {
91 return nil, fmt.Errorf("initial configuration exceeds %d bytes", config.MaxSnapshotBytes)
92 }
93 var documents []initialDocument
94 if err := util.DecodeJSON(bytes.NewReader(data), &documents); err != nil {
95 return nil, fmt.Errorf("decode initial configuration: %w", err)
96 }
97 if len(documents) == 0 || len(documents) > config.MaxDocuments {
98 return nil, fmt.Errorf("initial configuration must contain between 1 and %d documents", config.MaxDocuments)
99 }
100 
101 records := make([]config.Record, 0, len(documents))
102 names := make(map[string]bool, len(documents))
103 for i, document := range documents {
104 if !json.Valid(document.Value) {
105 return nil, fmt.Errorf("document %d must include a JSON value", i+1)
106 }
107 if names[document.Name] {
108 return nil, fmt.Errorf("document %d repeats an earlier document name", i+1)
109 }
110 names[document.Name] = true
111 record, err := config.NewRecord(document.Name, document.Schema, 1, document.Value)
112 if err != nil {
113 return nil, fmt.Errorf("document %d: %w", i+1, err)
114 }
115 if document.Name == acme.DNSConfigName {
116 if _, err := acme.DecodeDNSConfig(record, ""); err != nil {
117 return nil, fmt.Errorf("document %d: %w", i+1, err)
118 }
119 }
120 records = append(records, record)
121 }
122 return records, nil
123}