-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (73 loc) · 1.66 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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/infisical/go-sdk/packages/errors"
"github.com/joho/godotenv"
)
func CopyRecursive(path string, vac VaultConnection, ifc InfisicalConnection) error {
fmtpath := strings.Replace(path, ".", "_", -1)
list, err := vac.ListSecrets(path)
if err != nil {
panic(err)
}
for _, item := range list {
if strings.HasSuffix(item, "/") {
err := CopyRecursive(filepath.Join(path, item), vac, ifc)
if err != nil {
return err
}
continue
}
secret, err := vac.GetSecret(path, item)
if err != nil {
fmt.Printf("Error finding secret %s in path %s: %s", item, path, err.Error())
continue
}
err = ifc.IngestSecret("/"+fmtpath, item, secret)
if err != nil {
if ex, ok := err.(*errors.APIError); ok {
if strings.Contains(*ex.ErrorMessage, "Rate limit") {
// time.Sleep(60 * time.Second)
err = ifc.IngestSecret("/"+fmtpath, item, secret)
if err != nil {
return err
}
continue
}
}
return err
}
// Insert a sleep here, because by default Infisical has a rate limit of 3ps/60pm
// time.Sleep(1 * time.Second)
}
return nil
}
func main() {
godotenv.Load()
var config Config
content, err := os.ReadFile("./config.json")
if err == nil {
err := json.Unmarshal(content, &config)
if err != nil {
panic(err)
}
}
vac := VaultConnection{Config: config.Vault}
err = vac.Connect()
if err != nil {
panic(err)
}
ifc := InfisicalConnection{Config: config.Infisical, Settings: config.Settings}
err = ifc.Connect()
if err != nil {
panic(err)
}
err = CopyRecursive("", vac, ifc)
if err != nil {
panic(err)
}
}