-
Notifications
You must be signed in to change notification settings - Fork 9
/
vcert.go
104 lines (93 loc) · 2.74 KB
/
vcert.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
package pki
import (
"context"
"encoding/json"
"fmt"
"github.com/Venafi/vcert"
"github.com/Venafi/vcert/pkg/endpoint"
"github.com/hashicorp/vault/logical"
"io/ioutil"
"log"
)
//Set it false to disable Venafi policy check. It can be done only on the code level of the plugin.
const venafiPolciyCheck = true
const venafiPolicyDenyAll = true
func (b *backend) ClientVenafi(ctx context.Context, s logical.Storage, configName string, configType string) (
endpoint.Connector, error) {
if configName == "" {
return nil, fmt.Errorf("missing %s name", configType)
}
log.Printf("Using %s: %s", configType, configName)
if configType == "role" {
role, err := b.getRole(ctx, s, configName)
if err != nil {
return nil, err
}
if role == nil {
return nil, fmt.Errorf("unknown role %v", role)
}
return role.venafiConnectionConfig.getConnection()
} else if configType == "policy" {
policy, err := b.getPolicyConfig(ctx, s, configName)
if err != nil {
return nil, err
}
if policy == nil {
return nil, fmt.Errorf("expected policy but got nil from Vault storage %v", policy)
}
return policy.venafiConnectionConfig.getConnection()
} else {
return nil, fmt.Errorf("couldn't determine config type")
}
}
func pp(a interface{}) string {
b, err := json.MarshalIndent(a, "", " ")
if err != nil {
fmt.Println("error:", err)
}
return fmt.Sprint(string(b))
}
type venafiConnectionConfig struct {
TPPURL string `json:"tpp_url"`
Zone string `json:"zone"`
TPPPassword string `json:"tpp_password"`
TPPUser string `json:"tpp_user"`
TrustBundleFile string `json:"trust_bundle_file"`
Apikey string `json:"apikey"`
CloudURL string `json:"cloud_url"`
}
func (c venafiConnectionConfig) getConnection() (endpoint.Connector, error) {
cfg := vcert.Config{
Zone: c.Zone,
LogVerbose: true,
}
if c.TPPURL != "" && c.TPPUser != "" && c.TPPPassword != "" {
cfg.ConnectorType = endpoint.ConnectorTypeTPP
cfg.BaseUrl = c.TPPURL
cfg.Credentials = &endpoint.Authentication{
User: c.TPPUser,
Password: c.TPPPassword,
}
if c.TrustBundleFile != "" {
trustBundle, err := ioutil.ReadFile(c.TrustBundleFile)
if err != nil {
log.Printf("Can`t read trust bundle from file %s: %v\n", c.TrustBundleFile, err)
return nil, err
}
cfg.ConnectionTrust = string(trustBundle)
}
} else if c.Apikey != "" {
cfg.ConnectorType = endpoint.ConnectorTypeCloud
cfg.BaseUrl = c.CloudURL
cfg.Credentials = &endpoint.Authentication{
APIKey: c.Apikey,
}
} else {
return nil, fmt.Errorf("failed to build config for Venafi conection")
}
client, err := vcert.NewClient(&cfg)
if err != nil {
return nil, fmt.Errorf("failed to get Venafi issuer client: %s", err)
}
return client, nil
}