-
Notifications
You must be signed in to change notification settings - Fork 36
/
hsmdaemon.go
325 lines (292 loc) · 10.1 KB
/
hsmdaemon.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright contributors to the Hyperledger Fabric Operator project
*
* SPDX-License-Identifier: Apache-2.0
*
* 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:
*
* http://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.
*/
package initializer
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/pkg/errors"
current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1"
caconfig "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca/config"
"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/config"
controller "github.com/IBM-Blockchain/fabric-operator/pkg/k8s/controllerclient"
jobv1 "github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources/job"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
"github.com/IBM-Blockchain/fabric-operator/pkg/util/image"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
// HSMDaemon implements the ability to initialize HSM Daemon based CA
type HSMDaemon struct {
Config *config.HSMConfig
Scheme *runtime.Scheme
Timeouts HSMInitJobTimeouts
Client controller.Client
}
// Create creates the crypto and config materical to initialize an HSM based CA
func (h *HSMDaemon) Create(instance *current.IBPCA, overrides *v1.ServerConfig, ca IBPCA) (*Response, error) {
log.Info(fmt.Sprintf("Creating job to initialize ca '%s'", instance.GetName()))
if err := ca.OverrideServerConfig(overrides); err != nil {
return nil, err
}
if err := createCACryptoSecret(h.Client, h.Scheme, instance, ca); err != nil {
return nil, err
}
if err := createCAConfigMap(h.Client, h.Scheme, instance, h.Config.Library.FilePath, ca); err != nil {
return nil, err
}
dbConfig, err := getDBConfig(instance, ca.GetType())
if err != nil {
return nil, errors.Wrapf(err, "failed get DB config for CA '%s'", instance.GetName())
}
job := h.initHSMCAJob(instance, dbConfig, ca.GetType())
if err := h.Client.Create(context.TODO(), job.Job, controller.CreateOption{
Owner: instance,
Scheme: h.Scheme,
}); err != nil {
return nil, errors.Wrap(err, "failed to create HSM ca initialization job")
}
log.Info(fmt.Sprintf("Job '%s' created", job.GetName()))
if err := job.WaitUntilActive(h.Client); err != nil {
return nil, err
}
log.Info(fmt.Sprintf("Job '%s' active", job.GetName()))
if err := job.WaitUntilContainerFinished(h.Client, CertGen); err != nil {
return nil, err
}
log.Info(fmt.Sprintf("Job '%s' finished", job.GetName()))
status, err := job.ContainerStatus(h.Client, CertGen)
if err != nil {
return nil, err
}
switch status {
case jobv1.FAILED:
return nil, fmt.Errorf("Job '%s' finished unsuccessfully, not cleaning up pods to allow for error evaluation", job.GetName())
case jobv1.COMPLETED:
// For posterity, job is only deleted if successful, not deleting on failure allows logs to be
// examined.
if err := job.Delete(h.Client); err != nil {
return nil, err
}
}
if ca.GetType().Is(caconfig.EnrollmentCA) {
if err := updateCAConfigMap(h.Client, h.Scheme, instance, ca); err != nil {
return nil, errors.Wrapf(err, "failed to update CA configmap for CA %s", instance.GetName())
}
}
return nil, nil
}
const (
// HSMClient is the name of container that contain the HSM client library
HSMClient = "hsm-client"
// CertGen is the name of container that runs the command to generate the certificate for the CA
CertGen = "certgen"
)
func (h *HSMDaemon) initHSMCAJob(instance *current.IBPCA, dbConfig *v1.CAConfigDB, caType caconfig.Type) *jobv1.Job {
var typ string
switch caType {
case caconfig.EnrollmentCA:
typ = "ca"
case caconfig.TLSCA:
typ = "tlsca"
}
cryptoMountPath := fmt.Sprintf("/crypto/%s", typ)
homeDir := fmt.Sprintf("/tmp/data/%s/%s", instance.GetName(), typ)
secretName := fmt.Sprintf("%s-%s-crypto", instance.GetName(), typ)
jobName := fmt.Sprintf("%s-%s-init", instance.GetName(), typ)
hsmLibraryPath := h.Config.Library.FilePath
hsmLibraryName := filepath.Base(hsmLibraryPath)
t := true
user := int64(1000)
root := int64(0)
backoffLimit := int32(0)
mountPath := "/shared"
pvcVolumeName := "fabric-ca"
batchJob := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
Namespace: instance.GetNamespace(),
Labels: map[string]string{
"name": jobName,
"owner": instance.GetName(),
},
},
Spec: batchv1.JobSpec{
BackoffLimit: &backoffLimit,
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
ServiceAccountName: instance.GetName(),
ImagePullSecrets: util.AppendImagePullSecretIfMissing(instance.GetPullSecrets(), h.Config.BuildPullSecret()),
RestartPolicy: corev1.RestartPolicyNever,
InitContainers: []corev1.Container{
{
Name: HSMClient,
Image: h.Config.Library.Image,
ImagePullPolicy: corev1.PullAlways,
Command: []string{
"sh",
"-c",
fmt.Sprintf("mkdir -p %s/hsm && dst=\"%s/hsm/%s\" && echo \"Copying %s to ${dst}\" && mkdir -p $(dirname $dst) && cp -r %s $dst", mountPath, mountPath, hsmLibraryName, hsmLibraryPath, hsmLibraryPath),
},
SecurityContext: &corev1.SecurityContext{
RunAsUser: &user,
RunAsNonRoot: &t,
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "shared",
MountPath: mountPath,
},
},
Resources: instance.GetResource("init"),
},
},
Containers: []corev1.Container{
{
Name: CertGen,
Image: image.Format(
instance.Spec.Images.EnrollerImage,
instance.Spec.Images.EnrollerTag,
),
ImagePullPolicy: corev1.PullAlways,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &root,
Privileged: &t,
AllowPrivilegeEscalation: &t,
},
Command: []string{
"sh",
"-c",
},
Args: []string{
fmt.Sprintf(config.DAEMON_CHECK_CMD+" && /usr/local/bin/enroller ca %s %s %s %s %s %s", instance.GetName(), instance.GetNamespace(), homeDir, cryptoMountPath, secretName, caType),
},
Env: h.Config.GetEnvs(),
Resources: instance.GetResource(current.ENROLLER),
VolumeMounts: []corev1.VolumeMount{
{
Name: "shared",
MountPath: "/hsm/lib",
SubPath: "hsm",
},
{
Name: "shared",
MountPath: "/shared",
},
{
Name: "caconfig",
MountPath: fmt.Sprintf("/tmp/data/%s/%s/fabric-ca-server-config.yaml", instance.GetName(), typ),
SubPath: "fabric-ca-server-config.yaml",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "shared",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: corev1.StorageMediumMemory,
},
},
},
{
Name: "caconfig",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: fmt.Sprintf("%s-%s-config", instance.GetName(), typ),
},
},
},
},
{
Name: pvcVolumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: instance.PVCName(),
},
},
},
},
},
},
},
}
job := jobv1.New(batchJob, &jobv1.Timeouts{
WaitUntilActive: h.Timeouts.JobStart.Get(),
WaitUntilFinished: h.Timeouts.JobCompletion.Get(),
})
job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes, h.Config.GetVolumes()...)
job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts, h.Config.GetVolumeMounts()...)
if dbConfig != nil {
// If using postgres with TLS enabled need to mount trusted root TLS certificate for database server
if strings.ToLower(dbConfig.Type) == "postgres" {
if dbConfig.TLS.IsEnabled() {
job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts,
corev1.VolumeMount{
Name: "cacrypto",
MountPath: fmt.Sprintf("/crypto/%s/db-certfile0.pem", typ),
SubPath: "db-certfile0.pem",
})
job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes,
corev1.Volume{
Name: "cacrypto",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: fmt.Sprintf("%s-%s-crypto", instance.GetName(), typ),
Items: []corev1.KeyToPath{
corev1.KeyToPath{
Key: "db-certfile0.pem",
Path: "db-certfile0.pem",
},
},
},
},
},
)
}
}
}
// If daemon settings are configured in HSM config, create a sidecar that is running the daemon image
if h.Config.Daemon != nil {
// Certain token information requires to be stored in persistent store, the administrator
// responsible for configuring HSM sets the HSM config to point to the path where the PVC
// needs to be mounted.
var pvcMount *corev1.VolumeMount
for _, vm := range h.Config.MountPaths {
if vm.UsePVC {
pvcMount = &corev1.VolumeMount{
Name: pvcVolumeName,
MountPath: vm.MountPath,
}
}
}
// Add daemon container to the deployment
config.AddDaemonContainer(h.Config, job, instance.GetResource(current.HSMDAEMON), pvcMount)
// If a pvc mount has been configured in HSM config, set the volume mount on the CertGen container
if pvcMount != nil {
job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts, *pvcMount)
}
}
return job
}