/
gnoi_client.go
264 lines (221 loc) · 7.22 KB
/
gnoi_client.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* Copyright 2019 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Contains code from source: https://github.com/google/gnxi/tree/8521faedac371b6e13ac76f752eb41079ca79bd7
package gnoi
import (
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/google/gnxi/utils/credentials"
"github.com/google/gnxi/utils/entity"
"golang.org/x/net/context"
"google.golang.org/grpc"
pbc "ovs-gnxi/shared/gnoi/modeldata/generated/cert"
pbs "ovs-gnxi/shared/gnoi/modeldata/generated/system"
"ovs-gnxi/shared/logging"
)
var (
log = logging.New("ovs-gnxi-client")
)
type Client struct {
targetAddress string
targetName string
encodingName string
caEntity *entity.Entity
}
// NewClient returns an instance of Client struct.
func NewClient(targetAddress, targetName, encodingName string, caEntity *entity.Entity) *Client {
return &Client{
targetAddress: targetAddress,
targetName: targetName,
encodingName: encodingName,
caEntity: caEntity,
}
}
func (c *Client) Reboot(ctx context.Context, rebootMessage string) (*pbs.RebootResponse, error) {
opts := credentials.ClientCredentials(c.targetName)
conn, err := grpc.Dial(c.targetAddress, opts...)
if err != nil {
return nil, err
}
defer conn.Close()
cli := pbs.NewSystemClient(conn)
request := &pbs.RebootRequest{
Message: rebootMessage,
}
log.Debug("== Request:")
log.Debug(proto.MarshalTextString(request))
response, err := cli.Reboot(ctx, request)
if err != nil {
return nil, fmt.Errorf("error in rebooting system: %v", err)
}
return response, nil
}
// RotateCertificates rotates a certificate.
func (c *Client) RotateCertificates(ctx context.Context, certID string) error {
opts := credentials.ClientCredentials(c.targetName)
conn, err := grpc.Dial(c.targetAddress, opts...)
if err != nil {
return err
}
defer conn.Close()
cli := pbc.NewCertificateManagementClient(conn)
requestGen := &pbc.RotateCertificateRequest{
RotateRequest: &pbc.RotateCertificateRequest_GenerateCsr{
GenerateCsr: &pbc.GenerateCSRRequest{
CsrParams: &pbc.CSRParams{
Type: pbc.CertificateType_CT_X509,
MinKeySize: 4096,
KeyType: pbc.KeyType_KT_RSA,
CommonName: "target.gnxi.lan",
Country: "AT",
State: "Vienna",
City: "Vienna",
Organization: "Test",
OrganizationalUnit: "Test",
},
},
},
}
log.Debug("== Request:")
log.Debug(proto.MarshalTextString(requestGen))
certClient, err := cli.Rotate(ctx)
if err != nil {
return fmt.Errorf("failed stream: %v", err)
}
if err = certClient.Send(requestGen); err != nil {
return fmt.Errorf("failed to send GenerateCSRRequest: %v", err)
}
resp, err := certClient.Recv()
if err != nil {
return fmt.Errorf("failed to receive RotateCertificateResponse: %v", err)
}
genCSR := resp.GetGeneratedCsr()
if genCSR == nil || genCSR.Csr == nil {
return fmt.Errorf("expected GenerateCSRRequest, got something else")
}
derCSR, _ := pem.Decode(genCSR.Csr.Csr)
if derCSR == nil {
return fmt.Errorf("failed to decode CSR PEM block")
}
csr, err := x509.ParseCertificateRequest(derCSR.Bytes)
if err != nil {
return fmt.Errorf("failed to parse CSR DER")
}
signedCert, err := c.sign(csr)
if err != nil {
return fmt.Errorf("failed to sign the CSR: %v", err)
}
certPEM := x509toPEM(signedCert)
var caCertificates []*pbc.Certificate
for _, caCert := range []*x509.Certificate{c.caEntity.Certificate.Leaf} {
caCertificates = append(caCertificates, &pbc.Certificate{
Type: pbc.CertificateType_CT_X509,
Certificate: x509toPEM(caCert),
})
}
requestLoad := &pbc.RotateCertificateRequest{
RotateRequest: &pbc.RotateCertificateRequest_LoadCertificate{
LoadCertificate: &pbc.LoadCertificateRequest{
Certificate: &pbc.Certificate{
Type: pbc.CertificateType_CT_X509,
Certificate: certPEM,
},
KeyPair: nil,
CertificateId: certID,
CaCertificates: caCertificates,
},
},
}
log.Debug("== Request:")
log.Debug(proto.MarshalTextString(requestLoad))
if err = certClient.Send(requestLoad); err != nil {
return fmt.Errorf("failed to send LoadCertificateRequest: %v", err)
}
if resp, err = certClient.Recv(); err != nil {
return fmt.Errorf("failed to receive RotateCertificateResponse: %v", err)
}
loadCertificateResponse := resp.GetLoadCertificate()
if loadCertificateResponse == nil {
return fmt.Errorf("expected LoadCertificateResponse, got something else")
}
certs, err := c.GetCertificates(ctx)
if err != nil {
return err
}
if _, ok := certs[certID]; !ok {
return fmt.Errorf("failed to validate rotated certificate: %v", err)
}
if err := certClient.Send(&pbc.RotateCertificateRequest{
RotateRequest: &pbc.RotateCertificateRequest_FinalizeRotation{FinalizeRotation: &pbc.FinalizeRequest{}},
}); err != nil {
return fmt.Errorf("failed to send LoadCertificateRequest: %v", err)
}
return nil
}
// GetCertificates gets a map of certificates in the target, certID to certificate
func (c *Client) GetCertificates(ctx context.Context) (map[string]*x509.Certificate, error) {
opts := credentials.ClientCredentials(c.targetName)
conn, err := grpc.Dial(c.targetAddress, opts...)
if err != nil {
return nil, err
}
defer conn.Close()
cli := pbc.NewCertificateManagementClient(conn)
resp, err := cli.GetCertificates(ctx, &pbc.GetCertificatesRequest{})
if err != nil {
return nil, err
}
certs := map[string]*x509.Certificate{}
for _, certInfo := range resp.CertificateInfo {
if certInfo.Certificate == nil {
continue
}
x509Cert, err := PEMtox509(certInfo.Certificate.Certificate)
if err != nil {
return nil, fmt.Errorf("failed to decode certificate: %v", err)
}
certs[certInfo.CertificateId] = x509Cert
}
return certs, nil
}
// EncodeCert encodes a x509.Certificate into a PEM block.
func x509toPEM(cert *x509.Certificate) []byte {
return pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
})
}
// PEMtox509 decodes a PEM block into a x509.Certificate.
func PEMtox509(bytes []byte) (*x509.Certificate, error) {
certDERBlock, _ := pem.Decode(bytes)
if certDERBlock == nil {
return nil, fmt.Errorf("failed to decode PEM block")
}
certificate, err := x509.ParseCertificate(certDERBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to decode DER bytes")
}
return certificate, nil
}
// sign is called to create a Certificate from a CSR.
func (c *Client) sign(csr *x509.CertificateRequest) (*x509.Certificate, error) {
e, err := entity.FromSigningRequest(csr)
if err != nil {
return nil, fmt.Errorf("failed generating a cert from a CSR: %v", err)
}
if err := e.SignWith(c.caEntity); err != nil {
return nil, fmt.Errorf("failed to sign the certificate: %v", err)
}
return e.Certificate.Leaf, nil
}