This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
/
identity.go
230 lines (194 loc) · 6.25 KB
/
identity.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
package metadataserver
import (
"crypto/x509"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/Netflix/titus-executor/metadataserver/identity"
"github.com/Netflix/titus-executor/metadataserver/metrics"
"github.com/pkg/errors"
"github.com/Netflix/titus-executor/api/netflix/titus"
"github.com/golang/protobuf/proto" // nolint: staticcheck
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
)
func (ms *MetadataServer) generateTaskIdentity() *titus.TaskIdentity {
now := uint64(time.Now().Unix())
taskStatus := titus.TaskInfo_RUNNING
ti := &titus.TaskIdentity{
Container: ms.containerInfo,
Task: &titus.TaskInfo{
ContainerId: ms.containerInfo.RunState.TaskId,
TaskId: ms.containerInfo.RunState.TaskId,
HostName: ms.containerInfo.RunState.HostName,
Status: &taskStatus,
},
UnixTimestampSec: &now,
}
if ms.ipv4Address != nil {
ipv4Address := ms.ipv4Address.String()
ti.Ipv4Address = &ipv4Address
}
if ms.ipv6Address != nil {
ipv6Address := ms.ipv6Address.String()
ti.Ipv6Address = &ipv6Address
}
return ti
}
func (ms *MetadataServer) generateTaskPodIdentity() (*titus.TaskPodIdentity, error) {
taskStatus := titus.TaskInfo_RUNNING
if ms.pod == nil {
return nil, fmt.Errorf("No pod object available to marshal as a task identity")
}
podData, err := json.Marshal(ms.pod)
if err != nil {
return nil, err
}
ti := &titus.TaskPodIdentity{
Pod: podData,
TaskInfo: &titus.TaskInfo{
ContainerId: ms.containerInfo.RunState.TaskId,
TaskId: ms.containerInfo.RunState.TaskId,
HostName: ms.containerInfo.RunState.HostName,
Status: &taskStatus,
},
RequestTimestamp: timestamppb.Now(),
}
if ms.ipv4Address != nil {
ipv4Address := ms.ipv4Address.String()
ti.Ipv4Address = &ipv4Address
}
if ms.ipv6Address != nil {
ipv6Address := ms.ipv6Address.String()
ti.Ipv6Address = &ipv6Address
}
return ti, nil
}
// SetSigner updates the identity server's signer
func (ms *MetadataServer) SetSigner(newSigner *identity.Signer) error {
newCert, err := x509.ParseCertificate(newSigner.Certificate.Certificate[0])
if err != nil {
return errors.Wrap(err, "error parsing new certificate")
}
ms.signLock.Lock()
defer ms.signLock.Unlock()
oldSigner := ms.signer
oldCert, err := x509.ParseCertificate(oldSigner.Certificate.Certificate[0])
if err != nil {
return errors.Wrap(err, "error parsing old certificate")
}
log.WithFields(log.Fields{
"newSubject": newCert.Subject,
"newIssuer": newCert.Issuer,
"newNotBefore": newCert.NotBefore,
"newNotAfter": newCert.NotAfter,
"newSerialNumber": newCert.SerialNumber,
"oldSubject": oldCert.Subject,
"oldIssuer": oldCert.Issuer,
"oldNotBefore": oldCert.NotBefore,
"oldNotAfter": oldCert.NotAfter,
"oldSerialNumber": oldCert.SerialNumber,
}).Info("signer cert updated")
ms.signer = newSigner
return nil
}
func (ms *MetadataServer) taskIdentity(w http.ResponseWriter, r *http.Request) {
metrics.PublishIncrementCounter("handler.task-identity.count")
w.Header().Set("Content-Type", "application/octet-stream")
taskIdent := ms.generateTaskIdentity()
identData, err := proto.Marshal(taskIdent)
if err != nil {
log.WithError(err).Errorf("Error marshaling task identity protobuf: %+v, task identity: %+v", err, taskIdent)
w.WriteHeader(http.StatusInternalServerError)
return
}
ms.signLock.RLock()
sig, err := ms.signer.Sign(identData)
ms.signLock.RUnlock()
if err != nil {
log.WithError(err).Error("Error signing data")
w.WriteHeader(http.StatusInternalServerError)
return
}
taskIdentDoc := &titus.TaskIdentityDocument{
Identity: identData,
Signature: sig,
}
ret, err := proto.Marshal(taskIdentDoc)
if err != nil {
log.WithError(err).Error("Unable to marshal task identity doc")
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err = w.Write(ret); err != nil {
log.WithError(err).Error("Error writing response")
w.WriteHeader(http.StatusInternalServerError)
}
}
func (ms *MetadataServer) taskIdentityJSON(w http.ResponseWriter, r *http.Request) {
metrics.PublishIncrementCounter("handler.task-identity-json.count")
w.Header().Set("Content-Type", "application/json")
taskIdent := ms.generateTaskIdentity()
identData, err := proto.Marshal(taskIdent)
if err != nil {
log.WithError(err).Errorf("Error marshaling task identity protobuf: %+v, task identity: %+v", err, taskIdent)
w.WriteHeader(http.StatusInternalServerError)
return
}
ms.signLock.RLock()
sig, err := ms.signer.SignString(identData)
ms.signLock.RUnlock()
if err != nil {
log.WithError(err).Error("Error signing data")
w.WriteHeader(http.StatusInternalServerError)
return
}
taskIdentDoc := &titus.TaskIdentityStringDocument{
Identity: taskIdent,
Signature: sig,
}
jEncoder := json.NewEncoder(w)
if err := jEncoder.Encode(taskIdentDoc); err != nil {
log.WithError(err).Error("Error encoding json")
w.WriteHeader(http.StatusInternalServerError)
}
}
func (ms *MetadataServer) taskPodIdentity(w http.ResponseWriter, r *http.Request) {
metrics.PublishIncrementCounter("handler.task-pod-identity.count")
w.Header().Set("Content-Type", "application/octet-stream")
podIdent, err := ms.generateTaskPodIdentity()
if err != nil {
log.WithError(err).Errorf("Error generating a pod identity document: %+v, pod identity: %+v", err, podIdent)
w.WriteHeader(http.StatusInternalServerError)
return
}
podIdentData, err := proto.Marshal(podIdent)
if err != nil {
log.WithError(err).Errorf("Error marshaling task pod identity protobuf: %+v, pod identity: %+v", err, podIdent)
w.WriteHeader(http.StatusInternalServerError)
return
}
ms.signLock.RLock()
sig, err := ms.signer.Sign(podIdentData)
ms.signLock.RUnlock()
if err != nil {
log.WithError(err).Error("Error signing pod identity data")
w.WriteHeader(http.StatusInternalServerError)
return
}
taskIdentDoc := &titus.TaskPodIdentityDocument{
PodIdentity: podIdentData,
Signature: sig,
}
ret, err := proto.Marshal(taskIdentDoc)
if err != nil {
log.WithError(err).Error("Unable to marshal task pod identity doc")
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err = w.Write(ret); err != nil {
log.WithError(err).Error("Error writing response")
w.WriteHeader(http.StatusInternalServerError)
}
}