-
Notifications
You must be signed in to change notification settings - Fork 14
/
macaroon_utils.go
83 lines (73 loc) · 2.05 KB
/
macaroon_utils.go
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
package grpcinterface
import (
"context"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"github.com/tdex-network/tdex-daemon/pkg/macaroons"
"gopkg.in/macaroon-bakery.v2/bakery"
)
// bakeMacaroon creates a new macaroon with newest version and the given
// permissions then returns it binary serialized.
func bakeMacaroon(
ctx context.Context, svc *macaroons.Service, permissions []bakery.Op,
) ([]byte, error) {
mac, err := svc.NewMacaroon(
ctx, macaroons.DefaultRootKeyID, permissions...,
)
if err != nil {
return nil, err
}
return mac.M().MarshalBinary()
}
// genMacaroons generates four macaroon files; one admin-level, one for
// updating the strategy of a market, one for updating its price and one
// read-only. Admin and read-only can also be used to generate more granular
// macaroons.
func genMacaroons(
ctx context.Context, svc *macaroons.Service, datadir string,
) error {
adminMacFile := filepath.Join(datadir, AdminMacaroonFile)
roMacFile := filepath.Join(datadir, ReadOnlyMacaroonFile)
marketMacFile := filepath.Join(datadir, MarketMacaroonFile)
priceMacFile := filepath.Join(datadir, PriceMacaroonFile)
if pathExists(adminMacFile) || pathExists(roMacFile) ||
pathExists(marketMacFile) || pathExists(priceMacFile) {
return nil
}
// Let's create the datadir if it doesn't exist.
if err := makeDirectoryIfNotExists(datadir); err != nil {
return err
}
for macFilename, macPermissions := range Macaroons {
mktMacBytes, err := bakeMacaroon(ctx, svc, macPermissions)
if err != nil {
return err
}
macFile := filepath.Join(datadir, macFilename)
perms := fs.FileMode(0644)
if macFilename == AdminMacaroonFile {
perms = 0600
}
if err := ioutil.WriteFile(macFile, mktMacBytes, perms); err != nil {
os.Remove(macFile)
return err
}
}
return nil
}
func makeDirectoryIfNotExists(path string) error {
if pathExists(path) {
return nil
}
return os.MkdirAll(path, os.ModeDir|0755)
}
func pathExists(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}