-
Notifications
You must be signed in to change notification settings - Fork 9
/
registrar_client.go
221 lines (187 loc) · 5.34 KB
/
registrar_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
// Copyright 2023 The Keylime Authors
// SPDX-License-Identifier: Apache-2.0
package registrar
import (
"context"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"net/http"
"net/url"
khttp "github.com/keylime/attestation-operator/pkg/client/http"
)
const (
apiVersion = "v2.1"
)
type Client interface {
ListAgents(ctx context.Context) ([]string, error)
GetAgent(ctx context.Context, uuid string) (*Agent, error)
DeleteAgent(ctx context.Context, uuid string) error
}
type registrarClient struct {
http *http.Client
url *url.URL
internalCtx context.Context
internalCtxCancel context.CancelFunc
}
var _ Client = ®istrarClient{}
func New(ctx context.Context, httpClient *http.Client, registrarURL string) (Client, error) {
parsedURL, err := url.Parse(registrarURL)
if err != nil {
return nil, khttp.InvalidURL(err)
}
internalCtx, internalCtxCancel := context.WithCancel(ctx)
return ®istrarClient{
http: httpClient,
url: parsedURL,
internalCtx: internalCtx,
internalCtxCancel: internalCtxCancel,
}, nil
}
// ListAgents implements https://keylime.readthedocs.io/en/latest/rest_apis.html#get--v2.1-agents-
func (c *registrarClient) ListAgents(ctx context.Context) ([]string, error) {
type listResults struct {
UUIDs []string `json:"uuids"`
}
type list struct {
Code int `json:"code"`
Status string `json:"status"`
Results listResults `json:"results"`
}
u := khttp.CloneURL(c.url)
reqPath, err := url.JoinPath(u.Path, apiVersion, "agents/")
if err != nil {
return nil, err
}
u.Path = reqPath
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
httpReq.Header.Set("Accept", "application/json")
httpReq.Header.Set("Content-Type", "application/json")
httpResp, err := c.http.Do(httpReq)
if err != nil {
return nil, err
}
defer httpResp.Body.Close()
// parse response
// if it was an error, return as such
if httpResp.StatusCode != http.StatusOK {
return nil, khttp.NewHTTPErrorFromBody(httpResp)
}
// otherwise we parse it as an IPAM response
var resp list
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
return nil, err
}
return resp.Results.UUIDs, nil
}
type Agent struct {
UUID string
AIK []byte
EK []byte
EKCert *x509.Certificate
MTLSCert *x509.Certificate
IP string
Port uint16
RegCount uint
}
type getResults struct {
AIK []byte `json:"aik_tpm"`
EK []byte `json:"ek_tpm"`
EKCert []byte `json:"ekcert"`
MTLSCert string `json:"mtls_cert"`
IP string `json:"ip"`
Port uint16 `json:"port"`
RegCount uint `json:"regcount"`
}
type get struct {
Code int `json:"code"`
Status string `json:"status"`
Results getResults `json:"results"`
}
// GetAgent implements https://keylime.readthedocs.io/en/latest/rest_apis.html#get--v2.1-agents-agent_id-UUID
func (c *registrarClient) GetAgent(ctx context.Context, uuid string) (*Agent, error) {
u := khttp.CloneURL(c.url)
reqPath, err := url.JoinPath(u.Path, apiVersion, "agents", uuid)
if err != nil {
return nil, err
}
u.Path = reqPath
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
httpReq.Header.Set("Accept", "application/json")
httpReq.Header.Set("Content-Type", "application/json")
httpResp, err := c.http.Do(httpReq)
if err != nil {
return nil, err
}
defer httpResp.Body.Close()
// parse response
// if it was an error, return as such
if httpResp.StatusCode != http.StatusOK {
return nil, khttp.NewHTTPErrorFromBody(httpResp)
}
// otherwise we parse it as an IPAM response
var resp get
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
return nil, err
}
return parseAgent(uuid, resp)
}
func parseAgent(uuid string, resp get) (*Agent, error) {
ekCert, err := x509.ParseCertificate(resp.Results.EKCert)
if err != nil {
return nil, fmt.Errorf("EK cert parsing: %w", err)
}
mtlsCertPEM, _ := pem.Decode([]byte(resp.Results.MTLSCert))
if mtlsCertPEM == nil {
return nil, fmt.Errorf("MTLS cert is not PEM encoded")
}
if mtlsCertPEM.Type != "CERTIFICATE" {
return nil, fmt.Errorf("MTLS cert PEM type is not a certificate, but %s", mtlsCertPEM.Type)
}
mtlsCert, err := x509.ParseCertificate(mtlsCertPEM.Bytes)
if err != nil {
return nil, fmt.Errorf("MTLS cert parsing: %w", err)
}
return &Agent{
UUID: uuid,
AIK: resp.Results.AIK,
EK: resp.Results.EK,
EKCert: ekCert,
MTLSCert: mtlsCert,
IP: resp.Results.IP,
Port: resp.Results.Port,
RegCount: resp.Results.RegCount,
}, nil
}
func (c *registrarClient) DeleteAgent(ctx context.Context, uuid string) error {
u := khttp.CloneURL(c.url)
reqPath, err := url.JoinPath(u.Path, apiVersion, "agents", uuid)
if err != nil {
return err
}
u.Path = reqPath
httpReq, err := http.NewRequestWithContext(ctx, http.MethodDelete, u.String(), nil)
if err != nil {
return err
}
httpReq.Header.Set("Accept", "application/json")
httpReq.Header.Set("Content-Type", "application/json")
httpResp, err := c.http.Do(httpReq)
if err != nil {
return err
}
defer httpResp.Body.Close()
// parse response
// if it was an error, return as such
if httpResp.StatusCode != http.StatusOK {
return khttp.NewHTTPErrorFromBody(httpResp)
}
return nil
}