-
Notifications
You must be signed in to change notification settings - Fork 6
/
secrets.go
109 lines (83 loc) · 2.84 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
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 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)
}
type Secrets struct {
client *InfisicalClient
}
func (s *Secrets) List(options ListSecretsOptions) ([]models.Secret, error) {
res, err := api.CallListSecretsV3(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.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
}
func NewSecrets(client *InfisicalClient) SecretsInterface {
return &Secrets{client: client}
}