Repository navigation
Expand file tree
/
Copy pathcmd-bootstrap.go
More file actions
127 lines (115 loc) · 4.03 KB
/
Copy pathcmd-bootstrap.go
File metadata and controls
127 lines (115 loc) · 4.03 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
package fedbox
import (
"bytes"
"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"
"golang.org/x/crypto/bcrypt"
)
type ResetCmd struct{}
func (b ResetCmd) Run(ctl *Base) error {
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)
}
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, err := conf.StorageInitFns(l)
if err != nil {
return http.Annotatef(err, "Unable to initialize storage")
}
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 {
initFn, err := conf.StorageInitFns(l)
if err != nil {
return http.Annotatef(err, "Unable to initialize storage")
}
if err := storage.Clean(initFn...); 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")
}
}
if AppDefaultPw != "" {
// NOTE(marius): the default app pw can be used alongside the one set in the pw file.
ctl.Logger.WithContext(lw.Ctx{"pw": mask.S(AppDefaultPw)}).Infof("Build time password for service")
ctl.ServicePwHash, _ = bcrypt.GenerateFromPassword([]byte(AppDefaultPw), bcrypt.MinCost)
}
// NOTE(marius): try to find if there's a password set in the storage path
var pw []byte
pwFilePath := filepath.Join(ctl.Conf.StoragePath, ctl.Conf.Hostname+".pw")
if fpw, err := os.ReadFile(pwFilePath); err == nil {
if pw = bytes.TrimSpace(fpw); len(pw) > 256 {
pw = pw[:256]
}
ctl.Logger.WithContext(lw.Ctx{"pw": mask.B(pw)}).Infof("Found valid password file")
defer os.RemoveAll(pwFilePath)
}
return pw, pair
}