-
Notifications
You must be signed in to change notification settings - Fork 20
/
vcert.go
91 lines (83 loc) · 2.35 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
package pki
import (
"context"
"fmt"
"github.com/Venafi/vcert"
"github.com/Venafi/vcert/pkg/endpoint"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
"io/ioutil"
"log"
)
func (b *backend) ClientVenafi(ctx context.Context, s logical.Storage, data *framework.FieldData, req *logical.Request, roleName string) (
endpoint.Connector, error) {
log.Printf("Using role: %s", roleName)
if roleName == "" {
return nil, fmt.Errorf("Missing role name")
}
role, err := b.getRole(ctx, req.Storage, roleName)
if err != nil {
return nil, err
}
if role == nil {
return nil, fmt.Errorf("Unknown role %v", role)
}
var cfg *vcert.Config
if role.Fakemode {
log.Println("Using fakemode to issue certificate")
cfg = &vcert.Config{
ConnectorType: endpoint.ConnectorTypeFake,
LogVerbose: true,
}
} else if role.TPPURL != "" && role.TPPUser != "" && role.TPPPassword != "" {
log.Printf("Using Platform with url %s to issue certificate\n", role.TPPURL)
if role.TrustBundleFile != "" {
log.Printf("Trying to read trust bundle from file %s\n", role.TrustBundleFile)
trustBundle, err := ioutil.ReadFile(role.TrustBundleFile)
if err != nil {
return nil, err
}
trustBundlePEM := string(trustBundle)
cfg = &vcert.Config{
ConnectorType: endpoint.ConnectorTypeTPP,
BaseUrl: role.TPPURL,
ConnectionTrust: trustBundlePEM,
Credentials: &endpoint.Authentication{
User: role.TPPUser,
Password: role.TPPPassword,
},
Zone: role.Zone,
LogVerbose: true,
}
} else {
cfg = &vcert.Config{
ConnectorType: endpoint.ConnectorTypeTPP,
BaseUrl: role.TPPURL,
Credentials: &endpoint.Authentication{
User: role.TPPUser,
Password: role.TPPPassword,
},
Zone: role.Zone,
LogVerbose: true,
}
}
} else if role.Apikey != "" {
log.Println("Using Cloud to issue certificate")
cfg = &vcert.Config{
ConnectorType: endpoint.ConnectorTypeCloud,
BaseUrl: role.CloudURL,
Credentials: &endpoint.Authentication{
APIKey: role.Apikey,
},
Zone: role.Zone,
LogVerbose: true,
}
} else {
return nil, fmt.Errorf("failed to build config for Venafi issuer")
}
client, err := vcert.NewClient(cfg)
if err != nil {
return nil, fmt.Errorf("failed to get Venafi issuer client: %s", err)
}
return client, nil
}