-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsecrets.go
139 lines (104 loc) · 3.56 KB
/
secrets.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
package infisical
import (
"os"
api "github.com/infisical/go-sdk/packages/api/secrets"
"github.com/infisical/go-sdk/packages/models"
"github.com/infisical/go-sdk/packages/util"
)
type ListSecretsOptions = api.ListSecretsV3RawRequest
type RetrieveSecretOptions = api.RetrieveSecretV3RawRequest
type UpdateSecretOptions = api.UpdateSecretV3RawRequest
type CreateSecretOptions = api.CreateSecretV3RawRequest
type DeleteSecretOptions = api.DeleteSecretV3RawRequest
type BatchCreateSecret = api.BatchCreateSecret
type SecretMetadata = models.SecretMetadata
type BatchCreateSecretsOptions = api.BatchCreateSecretsV3RawRequest
type BatchSecretsInterface interface {
Create(options BatchCreateSecretsOptions) ([]models.Secret, error)
}
type SecretsInterface interface {
List(options ListSecretsOptions) ([]models.Secret, error)
Retrieve(options RetrieveSecretOptions) (models.Secret, error)
Update(options UpdateSecretOptions) (models.Secret, error)
Create(options CreateSecretOptions) (models.Secret, error)
Delete(options DeleteSecretOptions) (models.Secret, error)
Batch() BatchSecretsInterface
}
type Secrets struct {
client *InfisicalClient
}
type BatchSecrets struct {
client *InfisicalClient
}
func (s *Secrets) List(options ListSecretsOptions) ([]models.Secret, error) {
res, err := api.CallListSecretsV3(s.client.cache, s.client.httpClient, options)
if err != nil {
return nil, err
}
if options.Recursive {
util.EnsureUniqueSecretsByKey(&res.Secrets)
}
secrets := append([]models.Secret(nil), res.Secrets...) // Clone main secrets slice, we will modify this if imports are enabled
if options.IncludeImports {
// Append secrets from imports
for _, importBlock := range res.Imports {
for _, importSecret := range importBlock.Secrets {
// Only append the secret if it is not already in the list, imports take precedence
if !util.ContainsSecret(secrets, importSecret.SecretKey) {
secrets = append(secrets, importSecret)
}
}
}
}
if options.AttachToProcessEnv {
for _, secret := range secrets {
// Only set the environment variable if it is not already set
if os.Getenv(secret.SecretKey) == "" {
os.Setenv(secret.SecretKey, secret.SecretValue)
}
}
}
return util.SortSecretsByKeys(secrets), nil
}
func (s *Secrets) Retrieve(options RetrieveSecretOptions) (models.Secret, error) {
res, err := api.CallRetrieveSecretV3(s.client.cache, s.client.httpClient, options)
if err != nil {
return models.Secret{}, err
}
return res.Secret, nil
}
func (s *Secrets) Update(options UpdateSecretOptions) (models.Secret, error) {
res, err := api.CallUpdateSecretV3(s.client.httpClient, options)
if err != nil {
return models.Secret{}, err
}
return res.Secret, nil
}
func (s *Secrets) Create(options CreateSecretOptions) (models.Secret, error) {
res, err := api.CallCreateSecretV3(s.client.httpClient, options)
if err != nil {
return models.Secret{}, err
}
return res.Secret, nil
}
func (s *Secrets) Delete(options DeleteSecretOptions) (models.Secret, error) {
res, err := api.CallDeleteSecretV3(s.client.httpClient, options)
if err != nil {
return models.Secret{}, err
}
return res.Secret, nil
}
// Batch operations
func (bs *BatchSecrets) Create(options BatchCreateSecretsOptions) ([]models.Secret, error) {
res, err := api.CallBatchCreateSecretV3(bs.client.httpClient, options)
if err != nil {
return nil, err
}
return res.Secrets, nil
}
func (s *Secrets) Batch() BatchSecretsInterface {
return &BatchSecrets{client: s.client}
}
func NewSecrets(client *InfisicalClient) SecretsInterface {
return &Secrets{client: client}
}