/
x509pop.go
177 lines (146 loc) · 4.57 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
package x509pop
import (
"context"
"crypto"
"crypto/tls"
"encoding/json"
"strings"
"sync"
"github.com/hashicorp/hcl"
nodeattestorv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/plugin/agent/nodeattestor/v1"
configv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/service/common/config/v1"
"github.com/spiffe/spire/pkg/common/catalog"
"github.com/spiffe/spire/pkg/common/plugin/x509pop"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
pluginName = "x509pop"
)
func BuiltIn() catalog.BuiltIn {
return builtin(New())
}
func builtin(p *Plugin) catalog.BuiltIn {
return catalog.MakeBuiltIn(pluginName,
nodeattestorv1.NodeAttestorPluginServer(p),
configv1.ConfigServiceServer(p))
}
type configData struct {
privateKey crypto.PrivateKey
attestationPayload []byte
}
type Config struct {
PrivateKeyPath string `hcl:"private_key_path"`
CertificatePath string `hcl:"certificate_path"`
IntermediatesPath string `hcl:"intermediates_path"`
}
type Plugin struct {
nodeattestorv1.UnsafeNodeAttestorServer
configv1.UnsafeConfigServer
m sync.Mutex
c *Config
}
func New() *Plugin {
return &Plugin{}
}
func (p *Plugin) AidAttestation(stream nodeattestorv1.NodeAttestor_AidAttestationServer) (err error) {
data, err := p.loadConfigData()
if err != nil {
return err
}
// send the attestation data back to the agent
if err := stream.Send(&nodeattestorv1.PayloadOrChallengeResponse{
Data: &nodeattestorv1.PayloadOrChallengeResponse_Payload{
Payload: data.attestationPayload,
},
}); 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 status.Errorf(codes.Internal, "unable to unmarshal challenge: %v", err)
}
// calculate and send the challenge response
response, err := x509pop.CalculateResponse(data.privateKey, challenge)
if err != nil {
return status.Errorf(codes.Internal, "failed to calculate challenge response: %v", err)
}
responseBytes, err := json.Marshal(response)
if err != nil {
return status.Errorf(codes.Internal, "unable to marshal challenge response: %v", err)
}
return stream.Send(&nodeattestorv1.PayloadOrChallengeResponse{
Data: &nodeattestorv1.PayloadOrChallengeResponse_ChallengeResponse{
ChallengeResponse: responseBytes,
},
})
}
func (p *Plugin) Configure(_ context.Context, req *configv1.ConfigureRequest) (*configv1.ConfigureResponse, error) {
// Parse HCL config payload into config struct
config := new(Config)
if err := hcl.Decode(config, req.HclConfiguration); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "unable to decode configuration: %v", err)
}
if config.PrivateKeyPath == "" {
return nil, status.Error(codes.InvalidArgument, "private_key_path is required")
}
if config.CertificatePath == "" {
return nil, status.Error(codes.InvalidArgument, "certificate_path is required")
}
// make sure the configuration produces valid data
if _, err := loadConfigData(config); err != nil {
return nil, err
}
p.setConfig(config)
return &configv1.ConfigureResponse{}, 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, status.Error(codes.FailedPrecondition, "not configured")
}
return loadConfigData(config)
}
func loadConfigData(config *Config) (*configData, error) {
certificate, err := tls.LoadX509KeyPair(config.CertificatePath, config.PrivateKeyPath)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "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, status.Errorf(codes.InvalidArgument, "unable to load intermediate certificates: %v", err)
}
for _, cert := range intermediates {
certificates = append(certificates, cert.Raw)
}
}
attestationPayload, err := json.Marshal(x509pop.AttestationData{
Certificates: certificates,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "unable to marshal attestation data: %v", err)
}
return &configData{
privateKey: certificate.PrivateKey,
attestationPayload: attestationPayload,
}, nil
}