Skip to content

Commit ac743de

Browse files
committed
[SECURESIGN-1393] Migrate trillian
1 parent 874dec9 commit ac743de

File tree

10 files changed

+343
-324
lines changed

10 files changed

+343
-324
lines changed

internal/controller/common/utils/kubernetes/ensure/deployment.go

+57-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@ import (
88
corev1 "k8s.io/api/core/v1"
99
)
1010

11+
const (
12+
CaTrustVolumeName = "ca-trust"
13+
TLSVolumeName = "tls-cert"
14+
15+
TLSVolumeMount = "/var/run/secrets/tas"
16+
17+
TLSKeyPath = TLSVolumeMount + "/tls.key"
18+
TLSCertPath = TLSVolumeMount + "/tls.crt"
19+
)
20+
1121
func Proxy() func(*v1.Deployment) error {
1222
return func(dp *v1.Deployment) error {
1323
utils.SetProxyEnvs(dp)
1424
return nil
1525
}
1626
}
1727

18-
const CaTrustVolumeName = "ca-trust"
19-
2028
// TrustedCA mount config map with trusted CA bundle to all deployment's containers.
2129
func TrustedCA(lor *v1alpha1.LocalObjectReference) func(dp *v1.Deployment) error {
2230
return func(dp *v1.Deployment) error {
@@ -52,3 +60,50 @@ func TrustedCA(lor *v1alpha1.LocalObjectReference) func(dp *v1.Deployment) error
5260
return nil
5361
}
5462
}
63+
64+
// TLS mount secret with tls cert to all deployment's containers.
65+
func TLS(tls v1alpha1.TLS) func(dp *v1.Deployment) error {
66+
return func(dp *v1.Deployment) error {
67+
template := &dp.Spec.Template
68+
69+
for i := range template.Spec.Containers {
70+
volumeMount := kubernetes.FindVolumeMountByNameOrCreate(&template.Spec.Containers[i], TLSVolumeName)
71+
volumeMount.MountPath = TLSVolumeMount
72+
volumeMount.ReadOnly = true
73+
}
74+
75+
volume := kubernetes.FindVolumeByNameOrCreate(&template.Spec, TLSVolumeName)
76+
if volume.Projected == nil {
77+
volume.Projected = &corev1.ProjectedVolumeSource{}
78+
}
79+
volume.Projected.Sources = []corev1.VolumeProjection{
80+
{
81+
Secret: &corev1.SecretProjection{
82+
LocalObjectReference: corev1.LocalObjectReference{
83+
Name: tls.CertRef.Name,
84+
},
85+
Items: []corev1.KeyToPath{
86+
{
87+
Key: tls.CertRef.Key,
88+
Path: "tls.crt",
89+
},
90+
},
91+
},
92+
},
93+
{
94+
Secret: &corev1.SecretProjection{
95+
LocalObjectReference: corev1.LocalObjectReference{
96+
Name: tls.PrivateKeyRef.Name,
97+
},
98+
Items: []corev1.KeyToPath{
99+
{
100+
Key: tls.PrivateKeyRef.Key,
101+
Path: "tls.key",
102+
},
103+
},
104+
},
105+
},
106+
}
107+
return nil
108+
}
109+
}

internal/controller/common/utils/kubernetes/ensure/deployment_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/onsi/gomega"
8+
"github.com/securesign/operator/api/v1alpha1"
89
"github.com/securesign/operator/internal/controller/annotations"
910
"github.com/securesign/operator/internal/controller/common/utils"
1011
"github.com/securesign/operator/internal/controller/common/utils/kubernetes"
@@ -63,3 +64,76 @@ func TestEnsureTrustedCAFromAnnotations(t *testing.T) {
6364

6465
})
6566
}
67+
68+
func TestEnsureTLS(t *testing.T) {
69+
gomega.RegisterTestingT(t)
70+
t.Run("update existing object", func(t *testing.T) {
71+
72+
ctx := context.TODO()
73+
c := testAction.FakeClientBuilder().
74+
WithObjects(&v1.Deployment{
75+
ObjectMeta: v2.ObjectMeta{Name: name, Namespace: "default"},
76+
Spec: v1.DeploymentSpec{
77+
Template: v3.PodTemplateSpec{
78+
Spec: v3.PodSpec{
79+
Containers: []v3.Container{
80+
{Name: name, Image: "test"},
81+
},
82+
},
83+
},
84+
},
85+
}).
86+
Build()
87+
88+
result, err := kubernetes.CreateOrUpdate(ctx, c,
89+
&v1.Deployment{ObjectMeta: v2.ObjectMeta{Name: name, Namespace: "default"}},
90+
TLS(v1alpha1.TLS{
91+
PrivateKeyRef: &v1alpha1.SecretKeySelector{
92+
LocalObjectReference: v1alpha1.LocalObjectReference{
93+
Name: "testSecret",
94+
},
95+
Key: "key",
96+
},
97+
CertRef: &v1alpha1.SecretKeySelector{
98+
LocalObjectReference: v1alpha1.LocalObjectReference{
99+
Name: "testSecret",
100+
},
101+
Key: "cert",
102+
},
103+
}),
104+
)
105+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
106+
107+
gomega.Expect(result).To(gomega.Equal(controllerutil.OperationResultUpdated))
108+
109+
existing := &v1.Deployment{}
110+
gomega.Expect(c.Get(ctx, client.ObjectKey{Namespace: "default", Name: name}, existing)).To(gomega.Succeed())
111+
112+
gomega.Expect(existing.Spec.Template.Spec.Containers[0].VolumeMounts).To(gomega.HaveLen(1))
113+
gomega.Expect(existing.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name).To(gomega.Equal(TLSVolumeName))
114+
gomega.Expect(existing.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath).To(gomega.Equal("/var/run/secrets/tas"))
115+
116+
gomega.Expect(existing.Spec.Template.Spec.Volumes).To(gomega.HaveLen(1))
117+
gomega.Expect(existing.Spec.Template.Spec.Volumes[0].Name).To(gomega.Equal(TLSVolumeName))
118+
gomega.Expect(existing.Spec.Template.Spec.Volumes[0].Projected.Sources).To(gomega.HaveLen(2))
119+
gomega.Expect(existing.Spec.Template.Spec.Volumes[0].Projected.Sources).To(gomega.ContainElements(
120+
gomega.And(
121+
gomega.WithTransform(func(s v3.VolumeProjection) string {
122+
return s.Secret.Name
123+
}, gomega.Equal("testSecret")),
124+
gomega.WithTransform(func(s v3.VolumeProjection) string {
125+
return s.Secret.Items[0].Key
126+
}, gomega.Equal("key")),
127+
),
128+
gomega.And(
129+
gomega.WithTransform(func(s v3.VolumeProjection) string {
130+
return s.Secret.Name
131+
}, gomega.Equal("testSecret")),
132+
gomega.WithTransform(func(s v3.VolumeProjection) string {
133+
return s.Secret.Items[0].Key
134+
}, gomega.Equal("cert")),
135+
),
136+
))
137+
138+
})
139+
}

internal/controller/common/utils/set_tls.go

-64
This file was deleted.

internal/controller/trillian/actions/constants.go

+7
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ const (
2222
ServerPortName = "grpc"
2323
MetricsPort = 8090
2424
MetricsPortName = "metrics"
25+
26+
SecretRootPassword = "mysql-root-password"
27+
SecretPassword = "mysql-password"
28+
SecretDatabaseName = "mysql-database"
29+
SecretUser = "mysql-user"
30+
SecretPort = "mysql-port"
31+
SecretHost = "mysql-host"
2532
)

0 commit comments

Comments
 (0)