forked from remilapeyre/vault-acme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
128 lines (110 loc) · 3.53 KB
/
account.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
package acme
import (
"context"
"crypto"
"crypto/x509"
"encoding/pem"
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration"
"github.com/hashicorp/vault/sdk/logical"
)
type account struct {
Email string
Registration *registration.Resource
Key crypto.PrivateKey
KeyType string
ServerURL string
Provider string
ProviderConfiguration map[string]string
EnableHTTP01 bool
EnableTLSALPN01 bool
TermsOfServiceAgreed bool
DNSResolvers []string
IgnoreDNSPropagation bool
}
// GetEmail returns the Email of the user
func (a *account) GetEmail() string {
return a.Email
}
// GetRegistration returns the Email of the user
func (a *account) GetRegistration() *registration.Resource {
return a.Registration
}
// GetPrivateKey returns the private key of the user
func (a *account) GetPrivateKey() crypto.PrivateKey {
return a.Key
}
func (a *account) getClient() (*lego.Client, error) {
config := lego.NewConfig(a)
config.CADirURL = a.ServerURL
return lego.NewClient(config)
}
func getAccount(ctx context.Context, storage logical.Storage, path string) (*account, error) {
storageEntry, err := storage.Get(ctx, path)
if err != nil {
return nil, err
}
if storageEntry == nil {
return nil, nil
}
var d map[string]interface{}
if err = storageEntry.DecodeJSON(&d); err != nil {
return nil, err
}
block, _ := pem.Decode([]byte(d["private_key"].(string)))
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
providerConfiguration := map[string]string{}
for k, v := range d["provider_configuration"].(map[string]interface{}) {
providerConfiguration[k] = v.(string)
}
a := &account{
Email: d["contact"].(string),
Key: privateKey,
KeyType: d["key_type"].(string),
Registration: ®istration.Resource{
URI: d["registration_uri"].(string),
},
ServerURL: d["server_url"].(string),
Provider: d["provider"].(string),
ProviderConfiguration: providerConfiguration,
TermsOfServiceAgreed: d["terms_of_service_agreed"].(bool),
EnableHTTP01: d["enable_http_01"].(bool),
EnableTLSALPN01: d["enable_tls_alpn_01"].(bool),
}
if ignoreDNSPropagation, ok := d["ignore_dns_propagation"]; ok {
a.IgnoreDNSPropagation = ignoreDNSPropagation.(bool)
}
a.DNSResolvers = make([]string, len(d["dns_resolvers"].([]interface{})))
for i, resolver := range d["dns_resolvers"].([]interface{}) {
a.DNSResolvers[i] = resolver.(string)
}
return a, nil
}
func (a *account) save(ctx context.Context, storage logical.Storage, path string, serverURL string) error {
x509Encoded, err := x509.MarshalPKCS8PrivateKey(a.Key)
if err != nil {
return err
}
pemEncoded := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509Encoded})
storageEntry, err := logical.StorageEntryJSON(path, map[string]interface{}{
"server_url": serverURL,
"registration_uri": a.Registration.URI,
"contact": a.GetEmail(),
"terms_of_service_agreed": a.TermsOfServiceAgreed,
"private_key": string(pemEncoded),
"key_type": a.KeyType,
"provider": a.Provider,
"provider_configuration": a.ProviderConfiguration,
"enable_http_01": a.EnableHTTP01,
"enable_tls_alpn_01": a.EnableTLSALPN01,
"dns_resolvers": a.DNSResolvers,
"ignore_dns_propagation": a.IgnoreDNSPropagation,
})
if err != nil {
return err
}
return storage.Put(ctx, storageEntry)
}