-
Notifications
You must be signed in to change notification settings - Fork 1
/
venafi_secret.go
200 lines (166 loc) · 5.32 KB
/
venafi_secret.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
package venafi
import (
"fmt"
"github.com/Venafi/vcert/v4/pkg/endpoint"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/opencredo/venafi-vault-wizard/app/config/errors"
"github.com/opencredo/venafi-vault-wizard/app/config/generate"
"github.com/opencredo/venafi-vault-wizard/app/plugins/venafi/venafi_wrapper"
"github.com/opencredo/venafi-vault-wizard/app/reporter"
"github.com/opencredo/venafi-vault-wizard/app/vault/api"
)
type PluginType int
const (
SecretsEngine = iota
MonitorEngine
)
func ConfigureVenafiSecret(
reportSection reporter.Section,
vaultClient api.VaultAPIClient,
venafiClient venafi_wrapper.VenafiWrapper,
secretPath string,
secretValue VenafiConnectionConfig,
pluginType PluginType,
zone *string,
) error {
check := reportSection.AddCheck("Adding Venafi secret...")
secretParameters, err := secretValue.GetAsMap(pluginType, venafiClient)
if err != nil {
check.Errorf("Error getting values for the Venafi secret: %s", err)
return err
}
if zone != nil {
secretParameters["zone"] = *zone
}
_, err = vaultClient.WriteValue(secretPath, secretParameters)
if err != nil {
check.Errorf("Error creating Venafi secret: %s", err)
return err
}
check.Success("Venafi secret configured at " + secretPath)
return nil
}
func VerifyVenafiSecret(reportSection reporter.Section, vaultClient api.VaultAPIClient, secretPath string, secretValue VenafiConnectionConfig) error {
check := reportSection.AddCheck("Checking Venafi secret...")
_, err := vaultClient.ReadValue(secretPath)
if err != nil {
check.Errorf("Error retrieving Venafi secret: %s", err)
return err
}
check.Success("Venafi secret correctly configured at " + secretPath)
return nil
}
// VenafiSecret Used to create either VaaS or TPP style secrets. This is not used directly, but extended by consuming plugins
type VenafiSecret struct {
VaaS *VenafiVaaSConnection `hcl:"venafi_vaas,block"`
TPP *VenafiTPPConnection `hcl:"venafi_tpp,block"`
}
type VenafiConnectionConfig interface {
GetAsMap(pluginType PluginType, venafiClient venafi_wrapper.VenafiWrapper) (map[string]interface{}, error)
}
func (v *VenafiSecret) Validate() error {
vaasConnectionProvided := v.VaaS != nil
tppConnectionProvided := v.TPP != nil
// Ensure only one of VaaS or TPP is defined
if (vaasConnectionProvided && tppConnectionProvided) || (!vaasConnectionProvided && !tppConnectionProvided) {
return fmt.Errorf("error, must provide exactly one of VaaS or TPP connection details: %w", errors.ErrConflictingBlocks)
}
if vaasConnectionProvided {
return v.VaaS.Validate()
}
if tppConnectionProvided {
return v.TPP.Validate()
}
return nil
}
func (v VenafiSecret) GetAsMap(pluginType PluginType, venafiClient venafi_wrapper.VenafiWrapper) (map[string]interface{}, error) {
if v.VaaS != nil {
m := map[string]interface{}{
"apikey": v.VaaS.APIKey,
}
return m, nil
}
if v.TPP != nil {
m, err := v.TPP.getAccessToken(pluginType, venafiClient)
if err != nil {
return nil, err
}
return m, nil
}
return nil, nil
}
func (v *VenafiSecret) WriteHCL(hclBody *hclwrite.Body) {
if v.TPP != nil {
v.TPP.WriteHCL(hclBody)
return
}
if v.VaaS != nil {
v.VaaS.WriteHCL(hclBody)
return
}
}
type VenafiVaaSConnection struct {
APIKey string `hcl:"apikey"`
}
type VenafiTPPConnection struct {
URL string `hcl:"url"`
Username string `hcl:"username"`
Password string `hcl:"password"`
}
func (c *VenafiVaaSConnection) Validate() error {
if c.APIKey == "" {
return fmt.Errorf("error with Venafi API key: %w", errors.ErrBlankParam)
}
return nil
}
func (c *VenafiVaaSConnection) WriteHCL(hclBody *hclwrite.Body) {
vaasBlock := hclBody.AppendNewBlock("venafi_vaas", nil)
vaasBody := vaasBlock.Body()
generate.WriteStringAttributeToHCL("apikey", c.APIKey, vaasBody)
}
func (c *VenafiTPPConnection) Validate() error {
if c.URL == "" {
return fmt.Errorf("error with TPP URL: %w", errors.ErrBlankParam)
}
if c.Username == "" {
return fmt.Errorf("error with TPP Username: %w", errors.ErrBlankParam)
}
if c.Password == "" {
return fmt.Errorf("error with TPP Password: %w", errors.ErrBlankParam)
}
return nil
}
func (c *VenafiTPPConnection) WriteHCL(hclBody *hclwrite.Body) {
tppBlock := hclBody.AppendNewBlock("venafi_tpp", nil)
tppBody := tppBlock.Body()
generate.WriteStringAttributeToHCL("url", c.URL, tppBody)
generate.WriteStringAttributeToHCL("username", c.Username, tppBody)
generate.WriteStringAttributeToHCL("password", c.Password, tppBody)
}
func (c *VenafiTPPConnection) getAccessToken(pluginType PluginType, venafiClient venafi_wrapper.VenafiWrapper) (map[string]interface{}, error) {
var scope string
var clientID string
if pluginType == MonitorEngine {
scope = "certificate:manage,discover"
clientID = "hashicorp-vault-monitor-by-venafi"
} else if pluginType == SecretsEngine {
scope = "certificate:manage,revoke"
clientID = "hashicorp-vault-by-venafi"
} else {
return nil, fmt.Errorf("unrecognised plugin type")
}
tokens, err := venafiClient.GetRefreshToken(&endpoint.Authentication{
User: c.Username,
Password: c.Password,
Scope: scope,
ClientId: clientID,
})
if err != nil {
return nil, fmt.Errorf("error trying to request an access and refresh token for TPP: %w", err)
}
return map[string]interface{}{
"url": c.URL,
"access_token": tokens.Access_token,
"refresh_token": tokens.Refresh_token,
}, nil
}