-
Notifications
You must be signed in to change notification settings - Fork 635
/
keystore.go
118 lines (106 loc) · 2.32 KB
/
keystore.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
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
package keystore
import (
"encoding/json"
"fmt"
"io"
"os"
"regexp"
"github.com/chubaofs/chubaofs/util/caps"
)
var roleSet = map[string]bool{
"client": true,
"service": true,
}
// KeyInfo defines the key info structure in key store
type KeyInfo struct {
ID string `json:"id"`
AuthKey []byte `json:"auth_key"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Ts int64 `json:"create_ts"`
Role string `json:"role"`
Caps []byte `json:"caps"`
}
// DumpJSONFile dump KeyInfo to file in json format
func (u *KeyInfo) DumpJSONFile(filename string) (err error) {
var (
data string
)
if data, err = u.DumpJSONStr(); err != nil {
return
}
file, err := os.Create(filename)
if err != nil {
return
}
defer file.Close()
_, err = io.WriteString(file, data)
if err != nil {
return
}
return
}
// DumpJSONStr dump KeyInfo to string in json format
func (u *KeyInfo) DumpJSONStr() (r string, err error) {
dumpInfo := struct {
ID string `json:"id"`
AuthKey []byte `json:"auth_key"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Ts int64 `json:"create_ts"`
Role string `json:"role"`
Caps string `json:"caps"`
}{
u.ID,
u.AuthKey,
u.AccessKey,
u.SecretKey,
u.Ts,
u.Role,
string(u.Caps),
}
data, err := json.MarshalIndent(dumpInfo, "", " ")
if err != nil {
return
}
r = string(data)
return
}
// IsValidID check the validity of ID
func (u *KeyInfo) IsValidID() (err error) {
re := regexp.MustCompile("^[A-Za-z]{1,1}[A-Za-z0-9_]{0,20}$")
if !re.MatchString(u.ID) {
err = fmt.Errorf("invalid ID [%s]", u.ID)
return
}
return
}
// IsValidRole check the validity of role
func (u *KeyInfo) IsValidRole() (err error) {
if _, ok := roleSet[u.Role]; !ok {
err = fmt.Errorf("invalid Role [%s]", u.Role)
return
}
return
}
// IsValidCaps check the validity of caps
func (u *KeyInfo) IsValidCaps() (err error) {
cap := new(caps.Caps)
if err = cap.Init(u.Caps); err != nil {
err = fmt.Errorf("Invalid caps [%s] %s", u.Caps, err.Error())
}
return
}
// IsValidKeyInfo is a valid of KeyInfo
func (u *KeyInfo) IsValidKeyInfo() (err error) {
if err = u.IsValidID(); err != nil {
return
}
if err = u.IsValidRole(); err != nil {
return
}
if err = u.IsValidCaps(); err != nil {
return
}
return
}