-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
213 lines (187 loc) · 4.32 KB
/
main.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/hashicorp/vault/api"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
type Vault struct {
SourceAddr string
SourceToken string
DestinationAddr string
DestinationToken string
authType string
}
var (
githubRef string
githubSHA string
httpClient = &http.Client{Timeout: 30 * time.Second}
tokenPath = "/.github-token"
)
func getToken(tokenPath string) (string, error) {
token, err := ioutil.ReadFile(tokenPath)
if err != nil {
return "", errors.Wrap(err, "get token")
}
return strings.TrimSpace(string(token)), nil
}
func login(addr, token, authType string) (*api.Logical, error) {
config := &api.Config{
Address: addr,
HttpClient: httpClient,
}
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
switch authType {
case "github":
data, err := client.Logical().Write(
"/auth/github/login",
map[string]interface{}{"token": token},
)
if err != nil {
return nil, err
}
client.SetToken(data.Auth.ClientToken)
case "token":
client.SetToken(token)
}
return client.Logical(), nil
}
func getPaths(client *api.Logical, currentPath string) ([]string, error) {
var tmpValue string
var results []string
secret, err := client.List(currentPath)
if err != nil {
return []string{""}, err
}
if secret == nil {
return []string{currentPath}, nil
}
for _, v := range secret.Data["keys"].([]interface{}) {
tmpValue = v.(string)
tmpValue = fmt.Sprintf("%s%s", currentPath, tmpValue)
innerResults, err := getPaths(client, tmpValue)
if err != nil {
return results, err
}
results = append(results, innerResults...)
}
return results, nil
}
func copy(srcClient, dstClient *api.Logical, paths []string, src, dst string, wg *sync.WaitGroup) {
for _, p := range paths {
if strings.HasPrefix(p, "kv-v2") {
p = strings.ReplaceAll(p, "metadata", "data")
}
secret, err := srcClient.Read(p)
if err != nil {
errors.Wrapf(err, "read path")
}
_, err = dstClient.Write(p, secret.Data)
if err != nil {
errors.Wrapf(err, "copy to destination")
}
fmt.Printf("~> from [%s] to [%s] - writting secrets on path: %s\n", src, dst, p)
}
wg.Done()
}
func parseToken(v Vault) Vault {
home, err := homedir.Dir()
if err != nil {
log.Fatal(err)
}
if v.SourceToken == "" || v.DestinationToken == "" {
token, err := getToken(home + tokenPath)
if err != nil {
log.Fatal(err)
}
v.SourceToken = token
v.DestinationToken = token
return v
}
return v
}
func run(v Vault) error {
v = parseToken(v)
var wg sync.WaitGroup
srcClient, err := login(v.SourceAddr, v.SourceToken, v.authType)
if err != nil {
return err
}
dstClient, err := login(v.DestinationAddr, v.DestinationToken, v.authType)
if err != nil {
return err
}
kvPaths, err := getPaths(srcClient, "kv-v2/metadata/")
if err != nil {
return err
}
secretPaths, err := getPaths(srcClient, "secret/")
if err != nil {
return err
}
wg.Add(1)
go copy(srcClient, dstClient, kvPaths, v.SourceAddr, v.DestinationAddr, &wg)
wg.Add(1)
go copy(srcClient, dstClient, secretPaths, v.SourceAddr, v.DestinationAddr, &wg)
wg.Wait()
return nil
}
func main() {
var v Vault
app := &cli.App{
Name: "vault-sync",
Version: fmt.Sprintf("%s commit: %s", githubRef, githubSHA),
Usage: "synchronize vault data",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "srcaddr",
Usage: "Source Vault Address",
Required: true,
Destination: &v.SourceAddr,
},
&cli.StringFlag{
Name: "srctoken",
Usage: "Source Vault Token",
Required: false,
Destination: &v.SourceToken,
},
&cli.StringFlag{
Name: "dstaddr",
Usage: "Destination Vault Address",
Required: true,
Destination: &v.DestinationAddr,
},
&cli.StringFlag{
Name: "dsttoken",
Usage: "Destination Vault Token",
Required: false,
Destination: &v.DestinationToken,
},
&cli.StringFlag{
Name: "method",
Usage: "Define auth method (github/token)",
Required: false,
Value: "github",
Destination: &v.authType,
},
},
Action: func(c *cli.Context) error {
return run(v)
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}