-
Notifications
You must be signed in to change notification settings - Fork 462
/
x509pop.go
189 lines (156 loc) · 4.66 KB
/
x509pop.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
package x509pop
import (
"context"
"crypto"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"github.com/hashicorp/hcl"
"github.com/spiffe/spire/pkg/agent/plugin/nodeattestor"
"github.com/spiffe/spire/pkg/common/catalog"
"github.com/spiffe/spire/pkg/common/plugin/x509pop"
"github.com/spiffe/spire/pkg/common/util"
"github.com/spiffe/spire/proto/spire/common"
"github.com/spiffe/spire/proto/spire/common/plugin"
)
const (
pluginName = "x509pop"
)
func BuiltIn() catalog.Plugin {
return builtin(New())
}
func builtin(p *Plugin) catalog.Plugin {
return catalog.MakePlugin(pluginName, nodeattestor.PluginServer(p))
}
type configData struct {
privateKey crypto.PrivateKey
attestationData *common.AttestationData
}
type Config struct {
trustDomain string
PrivateKeyPath string `hcl:"private_key_path"`
CertificatePath string `hcl:"certificate_path"`
IntermediatesPath string `hcl:"intermediates_path"`
}
type Plugin struct {
m sync.Mutex
c *Config
}
func New() *Plugin {
return &Plugin{}
}
func (p *Plugin) FetchAttestationData(stream nodeattestor.NodeAttestor_FetchAttestationDataServer) (err error) {
data, err := p.loadConfigData()
if err != nil {
return err
}
// send the attestation data back to the agent
if err := stream.Send(&nodeattestor.FetchAttestationDataResponse{
AttestationData: data.attestationData,
}); err != nil {
return err
}
// receive challenge
resp, err := stream.Recv()
if err != nil {
return err
}
challenge := new(x509pop.Challenge)
if err := json.Unmarshal(resp.Challenge, challenge); err != nil {
return fmt.Errorf("x509pop: unable to unmarshal challenge: %v", err)
}
// calculate and send the challenge response
response, err := x509pop.CalculateResponse(data.privateKey, challenge)
if err != nil {
return fmt.Errorf("x509pop: failed to calculate challenge response: %v", err)
}
responseBytes, err := json.Marshal(response)
if err != nil {
return fmt.Errorf("x509pop: unable to marshal challenge response: %v", err)
}
if err := stream.Send(&nodeattestor.FetchAttestationDataResponse{
Response: responseBytes,
}); err != nil {
return err
}
return nil
}
func (p *Plugin) Configure(ctx context.Context, req *plugin.ConfigureRequest) (*plugin.ConfigureResponse, error) {
// Parse HCL config payload into config struct
config := new(Config)
if err := hcl.Decode(config, req.Configuration); err != nil {
return nil, fmt.Errorf("x509pop: unable to decode configuration: %v", err)
}
if req.GlobalConfig == nil {
return nil, errors.New("x509pop: global configuration is required")
}
if req.GlobalConfig.TrustDomain == "" {
return nil, errors.New("x509pop: trust_domain is required")
}
config.trustDomain = req.GlobalConfig.TrustDomain
if config.PrivateKeyPath == "" {
return nil, errors.New("x509pop: private_key_path is required")
}
if config.CertificatePath == "" {
return nil, errors.New("x509pop: certificate_path is required")
}
// make sure the configuration produces valid data
if _, err := loadConfigData(config); err != nil {
return nil, err
}
p.setConfig(config)
return &plugin.ConfigureResponse{}, nil
}
func (p *Plugin) GetPluginInfo(ctx context.Context, req *plugin.GetPluginInfoRequest) (*plugin.GetPluginInfoResponse, error) {
return &plugin.GetPluginInfoResponse{}, nil
}
func (p *Plugin) getConfig() *Config {
p.m.Lock()
defer p.m.Unlock()
return p.c
}
func (p *Plugin) setConfig(c *Config) {
p.m.Lock()
defer p.m.Unlock()
p.c = c
}
func (p *Plugin) loadConfigData() (*configData, error) {
config := p.getConfig()
if config == nil {
return nil, errors.New("x509pop: not configured")
}
return loadConfigData(config)
}
func loadConfigData(config *Config) (*configData, error) {
certificate, err := tls.LoadX509KeyPair(config.CertificatePath, config.PrivateKeyPath)
if err != nil {
return nil, fmt.Errorf("x509pop: unable to load keypair: %v", err)
}
certificates := certificate.Certificate
// Append intermediate certificates if IntermediatesPath is set.
if strings.TrimSpace(config.IntermediatesPath) != "" {
intermediates, err := util.LoadCertificates(config.IntermediatesPath)
if err != nil {
return nil, fmt.Errorf("x509pop: unable to load intermediate certificates: %v", err)
}
for _, cert := range intermediates {
certificates = append(certificates, cert.Raw)
}
}
attestationDataBytes, err := json.Marshal(x509pop.AttestationData{
Certificates: certificates,
})
if err != nil {
return nil, fmt.Errorf("x509pop: unable to marshal attestation data: %v", err)
}
return &configData{
privateKey: certificate.PrivateKey,
attestationData: &common.AttestationData{
Type: pluginName,
Data: attestationDataBytes,
},
}, nil
}