-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbackup.go
More file actions
170 lines (142 loc) · 4.42 KB
/
Copy pathbackup.go
File metadata and controls
170 lines (142 loc) · 4.42 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package backup
import (
"encoding/json"
"errors"
"github.com/hashicorp/consul/api"
"github.com/myENA/consul-backinator/common"
)
// backupKeys fetches key/value pairs from consul and writes them to a backup file
func (c *Command) backupKeys() (int, error) {
var kvps api.KVPairs // list of requested kv pairs
var opts *api.QueryOptions // client query options
var count int // key count
var data []byte // read keys
var err error // general error holder
// build query options
opts = &api.QueryOptions{
AllowStale: false,
RequireConsistent: true,
}
// get all keys
if kvps, _, err = c.consulClient.KV().List(c.config.consulPrefix, opts); err != nil {
return 0, err
}
// transform paths
c.pathTransformer.Transform(kvps)
// set count
count = len(kvps)
// check count
if count == 0 {
return 0, errors.New("No keys found")
}
// encode and return
if data, err = json.MarshalIndent(kvps, "", " "); err != nil {
return 0, err
}
// write data to destination
if err = common.WriteData(c.config.fileName, c.config.cryptKey, data); err != nil {
return 0, err
}
// return key count - no error
return count, nil
}
// backupACLs fetches acl tokens consul and writes them to a backup file
func (c *Command) backupACLs() (int, error) {
var aclData *common.BackupACLData // storage for acl data
var opts *api.QueryOptions // client query options
var roleCount int // role count
var policyCount int // policy count
var tokenCount int // token count
var data []byte
var err error // general error holder
// build backup acl data type
aclData = &common.BackupACLData{
Roles: make([]*api.ACLRole, 0),
Policies: make([]*api.ACLPolicy, 0),
Tokens: make([]*api.ACLToken, 0),
}
// build query options
opts = &api.QueryOptions{
AllowStale: false,
RequireConsistent: true,
}
// get all acl roles
if aclData.Roles, _, err = c.consulClient.ACL().RoleList(opts); err != nil {
return 0, err
}
// get all acl policies
if policies, _, err := c.consulClient.ACL().PolicyList(opts); err != nil {
return 0, err
} else {
for _, policy := range policies {
if aclPolicy, _, err := c.consulClient.ACL().PolicyRead(policy.ID, opts); err != nil {
return 0, err
} else {
aclData.Policies = append(aclData.Policies, aclPolicy)
}
}
}
// get all acl tokens
if tokens, _, err := c.consulClient.ACL().TokenList(opts); err != nil {
return 0, err
} else {
for _, token := range tokens {
if aclToken, _, err := c.consulClient.ACL().TokenRead(token.AccessorID, opts); err != nil {
return 0, err
} else {
aclData.Tokens = append(aclData.Tokens, aclToken)
}
}
}
// set counts
roleCount = len(aclData.Roles)
policyCount = len(aclData.Policies)
tokenCount = len(aclData.Tokens)
// check tokenCount
if tokenCount == 0 && roleCount == 0 && policyCount == 0 {
return 0, errors.New("no acl tokens, roles, or policies found")
}
// encode and return
if data, err = json.MarshalIndent(aclData, "", " "); err != nil {
return 0, err
}
// write acls to destination
if err = common.WriteData(c.config.aclFileName, c.config.cryptKey, data); err != nil {
return 0, err
}
// return token tokenCount - no error
return roleCount + policyCount + tokenCount, nil
}
// backupQueries fetches prepared query definitions from consul and writes them to a backup file
func (c *Command) backupQueries() (int, error) {
var queries []*api.PreparedQueryDefinition // list of query definitions
var opts *api.QueryOptions // client query options
var count int // query count
var data []byte // read definitions
var err error // general error holder
// build query options
opts = &api.QueryOptions{
AllowStale: false,
RequireConsistent: true,
}
// get all query definitions
if queries, _, err = c.consulClient.PreparedQuery().List(opts); err != nil {
return 0, err
}
// set count
count = len(queries)
// check count
if count == 0 {
return 0, errors.New("No query definitions found")
}
// encode and return
if data, err = json.MarshalIndent(queries, "", " "); err != nil {
return 0, err
}
// write data to destination
if err = common.WriteData(c.config.queryFileName, c.config.cryptKey, data); err != nil {
return 0, err
}
// return query count - no error
return count, nil
}