-
Notifications
You must be signed in to change notification settings - Fork 0
/
infisical.go
144 lines (117 loc) · 3.14 KB
/
infisical.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
package main
import (
"fmt"
"strings"
"time"
infisical "github.com/infisical/go-sdk"
)
type InfisicalConnection struct {
Config InfisicalConfig
Settings TransferSettings
client infisical.InfisicalClientInterface
}
func (ic *InfisicalConnection) Connect() error {
ic.client = infisical.NewInfisicalClient(infisical.Config{
SiteUrl: ic.Config.Url,
})
_, err := ic.client.Auth().UniversalAuthLogin(ic.Config.ClientId, ic.Config.ClientSecret)
if err != nil {
return err
}
return nil
}
func (ic *InfisicalConnection) CreateFolder(path, name string) error {
_, err := ic.client.Folders().Create(
infisical.CreateFolderOptions{
Environment: ic.Config.Environment,
ProjectID: ic.Config.ProjectId,
Path: path,
Name: name,
},
)
return err
}
func (ic *InfisicalConnection) EnsureFolderExists(path string) error {
pathParts := strings.Split(path, "/")
checkPathParts := []string{}
for i := 0; i < len(pathParts); i++ {
if pathParts[i] == "" {
continue
}
checkPath := "/" + strings.Join(checkPathParts, "/")
fold, err := ic.client.Folders().List(infisical.ListFoldersOptions{
Environment: ic.Config.Environment,
ProjectID: ic.Config.ProjectId,
Path: checkPath,
})
if err != nil {
return err
}
var exists bool = false
for _, f := range fold {
if f.Name == pathParts[i] {
exists = true
checkPathParts = append(checkPathParts, pathParts[i])
break
}
}
if exists {
continue
}
err = ic.CreateFolder(checkPath, pathParts[i])
if err != nil {
return err
}
checkPathParts = append(checkPathParts, pathParts[i])
}
return nil
}
func (ic *InfisicalConnection) IngestSecret(path, item string, data []byte) error {
ex, errX := ic.client.Secrets().Retrieve(infisical.RetrieveSecretOptions{
SecretKey: item,
ProjectID: ic.Config.ProjectId,
Environment: ic.Config.Environment,
SecretPath: path,
})
if errX != nil {
if strings.Contains(errX.Error(), "Rate limit") {
time.Sleep(60 * time.Second)
ex, errX = ic.client.Secrets().Retrieve(infisical.RetrieveSecretOptions{
SecretKey: item,
ProjectID: ic.Config.ProjectId,
Environment: ic.Config.Environment,
SecretPath: path,
})
}
}
fmt.Println(ex.SecretKey)
if errX == nil {
if !ic.Settings.Overwrite {
fmt.Sprintf("secret %s at path %s already exists", item, path)
return nil
}
_, errY := ic.client.Secrets().Update(infisical.UpdateSecretOptions{
SecretKey: item,
ProjectID: ic.Config.ProjectId,
Environment: ic.Config.Environment,
SecretPath: "/" + path,
NewSecretValue: string(data),
NewSkipMultilineEncoding: false,
})
return errY
}
err := ic.EnsureFolderExists(path)
if err != nil {
return err
}
_, err = ic.client.Secrets().Create(infisical.CreateSecretOptions{
SecretKey: item,
ProjectID: ic.Config.ProjectId,
Environment: ic.Config.Environment,
SecretPath: path + "/",
SecretComment: "Imported from HCP Vault",
SkipMultiLineEncoding: false,
SecretValue: string(data),
})
return err
}