-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcmd-bootstrap.go
More file actions
156 lines (138 loc) · 4.62 KB
/
cmd-bootstrap.go
File metadata and controls
156 lines (138 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package fedbox
import (
"bytes"
"crypto/rand"
"errors"
"os"
"path/filepath"
"git.sr.ht/~mariusor/lw"
"git.sr.ht/~mariusor/mask"
"git.sr.ht/~mariusor/storage-all"
vocab "github.com/go-ap/activitypub"
http "github.com/go-ap/errors"
ap "github.com/go-ap/fedbox/activitypub"
"github.com/go-ap/fedbox/internal/config"
)
type ResetCmd struct{}
func (b ResetCmd) Run(ctl *Base) error {
if err := ctl.Storage.Open(); err != nil {
return http.Annotatef(err, "Unable to open FedBOX storage for path %s", ctl.Conf.StoragePath)
}
defer ctl.Storage.Close()
err := reset(ctl.Conf, ctl.Logger)
if err != nil {
return err
}
keyType := ap.KeyTypeED25519
if ctl.Conf.MastodonCompatible {
keyType = ap.KeyTypeRSA
}
pair, err := ap.GenerateKeyPair(keyType)
if err != nil {
return err
}
return bootstrap(ctl, ctl.Service, ctl.Logger, pair, nil)
}
type BootstrapCmd struct {
KeyType string `help:"Type of keys to generate: ${keyTypes}" enum:"${keyTypes}" default:"${defaultKeyType}"`
Password string `hidden:""`
}
func (b BootstrapCmd) Run(ctl *Base) error {
keyType := ap.KeyType(b.KeyType)
if CTLRun.Storage.Type != "" {
ctl.Conf.Storage = CTLRun.Storage.Type
}
pw, pair := getPwAndKey(ctl, keyType)
if b.Password != "" {
pw = []byte(b.Password)
}
if err := ctl.Bootstrap(pw, pair); err != nil {
ctl.Logger.WithContext(lw.Ctx{"err": err}).Warnf("Unable to bootstrap service actor")
}
return nil
}
func BootstrapStorage(conf config.Options, service vocab.Item, l lw.Logger, pair *ap.KeyPair) error {
ctl := Base{
in: os.Stdin,
out: os.Stdout,
err: os.Stderr,
}
if err := setup(&ctl, conf); err != nil {
return err
}
return bootstrap(&ctl, service, l, pair, nil)
}
func bootstrap(ctl *Base, service vocab.Item, l lw.Logger, pair *ap.KeyPair, pw []byte) error {
conf := ctl.Conf
path, err := conf.BaseStoragePath()
if err != nil {
return err
}
initFns := conf.StorageInitFns(l)
if err := storage.Bootstrap(initFns...); err != nil {
return http.Annotatef(err, "Unable to create %s path for storage %s", path, conf.Storage)
}
l.Infof("Successfully created %s db for storage %s", path, conf.Storage)
if ctl.Storage == nil {
ctl.Storage, err = storage.New(initFns...)
if err != nil {
return http.Annotatef(err, "Unable to initialize %s path for storage %s", path, conf.Storage)
}
}
if err = ctl.Storage.Open(); err != nil {
return http.Annotatef(err, "Unable to open %s path for storage %s", path, conf.Storage)
}
defer ctl.Storage.Close()
if err = CreateService(ctl, service, pair, pw); err != nil {
return http.Annotatef(err, "Unable to create FedBOX service %s for storage %s", service.GetID(), conf.Storage)
}
l.WithContext(lw.Ctx{"storage": conf.Storage, "ID": service.GetID()}).Infof("Successfully created FedBOX service")
return nil
}
func ResetStorage(conf config.Options, l lw.Logger) error {
return reset(conf, l)
}
func reset(conf config.Options, l lw.Logger) error {
if err := storage.Clean(conf.StorageInitFns(l)...); err != nil && !errors.Is(err, os.ErrNotExist) {
return http.Annotatef(err, "Unable to reset %s db for storage %s", conf.StoragePath, conf.Storage)
}
l.Infof("Successfully reset %s db for storage %s", conf.StoragePath, conf.Storage)
return nil
}
func getPwAndKey(ctl *Base, keyType ap.KeyType) ([]byte, *ap.KeyPair) {
var pair *ap.KeyPair
// NOTE(marius): try to find if there's a private key in the storage path
keyFilePath := filepath.Join(ctl.Conf.StoragePath, ctl.Conf.Hostname+".key")
if prvKey, err := os.ReadFile(keyFilePath); err == nil {
pair, err = ap.KeyPairFromPrivateBytes(prvKey)
if err != nil {
ctl.Logger.Warnf("Unable to decode found private key")
} else {
ctl.Logger.WithContext(lw.Ctx{"typ": pair.Type, "valid": pair.Private != nil}).Tracef("Found private key")
defer os.RemoveAll(keyFilePath)
}
}
if pair == nil {
var err error
// NOTE(marius): we generate the key
pair, err = ap.GenerateKeyPair(keyType)
if err != nil {
ctl.Logger.WithContext(lw.Ctx{"typ": keyType}).Warnf("Unable to generate key pair for service")
} else {
ctl.Logger.WithContext(lw.Ctx{"typ": keyType}).Tracef("Generated key pair for service")
}
}
// NOTE(marius): try to find if there's a password set in the storage path
pwFilePath := filepath.Join(ctl.Conf.StoragePath, ctl.Conf.Hostname+".pw")
pw, err := os.ReadFile(pwFilePath)
if err == nil {
pw = bytes.TrimSpace(pw)
ctl.Logger.WithContext(lw.Ctx{"pw": mask.B(pw)}).Tracef("Found valid password file")
defer os.RemoveAll(pwFilePath)
}
if pw == nil {
pw = []byte(rand.Text())
ctl.Logger.WithContext(lw.Ctx{"pw": mask.B(pw)}).Tracef("Generated random password for service")
}
return pw, pair
}