|
| 1 | +package actions |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + "github.com/securesign/operator/api/v1alpha1" |
| 7 | + "github.com/securesign/operator/controllers/common/action" |
| 8 | + testAction "github.com/securesign/operator/controllers/common/test/action" |
| 9 | + "github.com/securesign/operator/controllers/common/utils/kubernetes" |
| 10 | + "github.com/securesign/operator/controllers/constants" |
| 11 | + "github.com/securesign/operator/controllers/fulcio/actions" |
| 12 | + v1 "k8s.io/api/core/v1" |
| 13 | + "k8s.io/apimachinery/pkg/api/meta" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/types" |
| 16 | + "testing" |
| 17 | +) |
| 18 | + |
| 19 | +func Test_HandleFulcioCert_Autodiscover(t *testing.T) { |
| 20 | + g := NewWithT(t) |
| 21 | + |
| 22 | + instance := &v1alpha1.CTlog{ |
| 23 | + ObjectMeta: metav1.ObjectMeta{ |
| 24 | + Name: "auto", |
| 25 | + Namespace: "default", |
| 26 | + }, |
| 27 | + Spec: v1alpha1.CTlogSpec{}, |
| 28 | + Status: v1alpha1.CTlogStatus{ |
| 29 | + Conditions: []metav1.Condition{ |
| 30 | + { |
| 31 | + Type: constants.Ready, |
| 32 | + Reason: constants.Creating, |
| 33 | + Status: metav1.ConditionFalse, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + c := testAction.FakeClientBuilder().WithObjects( |
| 40 | + kubernetes.CreateSecret("secret", "default", |
| 41 | + map[string][]byte{"key": nil}, map[string]string{actions.FulcioCALabel: "key"}), |
| 42 | + instance, |
| 43 | + ).Build() |
| 44 | + |
| 45 | + i := &v1alpha1.CTlog{} |
| 46 | + if err := c.Get(context.TODO(), types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()}, i); err != nil { |
| 47 | + t.Error(err) |
| 48 | + } |
| 49 | + |
| 50 | + a := testAction.PrepareAction(c, NewHandleFulcioCertAction()) |
| 51 | + g.Expect(a.CanHandle(context.TODO(), i)).To(BeTrue()) |
| 52 | + |
| 53 | + _ = a.Handle(context.TODO(), i) |
| 54 | + |
| 55 | + g.Expect(len(i.Status.RootCertificates)).Should(Equal(1)) |
| 56 | + g.Expect(i.Status.RootCertificates[0].Key).Should(Equal("key")) |
| 57 | + g.Expect(i.Status.RootCertificates[0].Name).Should(Equal("secret")) |
| 58 | + |
| 59 | + g.Expect(meta.IsStatusConditionTrue(i.Status.Conditions, CertCondition)).To(BeTrue()) |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +func Test_HandleFulcioCert_Empty(t *testing.T) { |
| 64 | + g := NewWithT(t) |
| 65 | + |
| 66 | + instance := &v1alpha1.CTlog{ |
| 67 | + ObjectMeta: metav1.ObjectMeta{ |
| 68 | + Name: "empty", |
| 69 | + Namespace: "default", |
| 70 | + }, |
| 71 | + Spec: v1alpha1.CTlogSpec{}, |
| 72 | + Status: v1alpha1.CTlogStatus{ |
| 73 | + Conditions: []metav1.Condition{ |
| 74 | + { |
| 75 | + Type: constants.Ready, |
| 76 | + Reason: constants.Creating, |
| 77 | + Status: metav1.ConditionFalse, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + c := testAction.FakeClientBuilder().WithObjects( |
| 84 | + instance, |
| 85 | + ).Build() |
| 86 | + |
| 87 | + i := &v1alpha1.CTlog{} |
| 88 | + if err := c.Get(context.TODO(), types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()}, i); err != nil { |
| 89 | + t.Error(err) |
| 90 | + } |
| 91 | + |
| 92 | + a := testAction.PrepareAction(c, NewHandleFulcioCertAction()) |
| 93 | + g.Expect(a.CanHandle(context.TODO(), i)).To(BeTrue()) |
| 94 | + |
| 95 | + result := a.Handle(context.TODO(), i) |
| 96 | + var dummyAction = action.BaseAction{} |
| 97 | + g.Expect(result).To(Equal(dummyAction.Requeue())) |
| 98 | +} |
| 99 | + |
| 100 | +func Test_HandleFulcioCert_Configured(t *testing.T) { |
| 101 | + g := NewWithT(t) |
| 102 | + |
| 103 | + instance := &v1alpha1.CTlog{ |
| 104 | + ObjectMeta: metav1.ObjectMeta{ |
| 105 | + Name: "configured", |
| 106 | + Namespace: "default", |
| 107 | + }, |
| 108 | + Spec: v1alpha1.CTlogSpec{ |
| 109 | + RootCertificates: []v1alpha1.SecretKeySelector{ |
| 110 | + { |
| 111 | + Key: "key", |
| 112 | + LocalObjectReference: v1alpha1.LocalObjectReference{Name: "secret"}, |
| 113 | + }, |
| 114 | + { |
| 115 | + Key: "key", |
| 116 | + LocalObjectReference: v1alpha1.LocalObjectReference{Name: "secret-2"}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + Status: v1alpha1.CTlogStatus{ |
| 121 | + Conditions: []metav1.Condition{ |
| 122 | + { |
| 123 | + Type: constants.Ready, |
| 124 | + Reason: constants.Creating, |
| 125 | + Status: metav1.ConditionFalse, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + c := testAction.FakeClientBuilder().WithObjects( |
| 132 | + kubernetes.CreateSecret("secret", "default", |
| 133 | + map[string][]byte{"key": nil}, map[string]string{}), |
| 134 | + instance, |
| 135 | + ).Build() |
| 136 | + |
| 137 | + i := &v1alpha1.CTlog{} |
| 138 | + if err := c.Get(context.TODO(), types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()}, i); err != nil { |
| 139 | + t.Error(err) |
| 140 | + } |
| 141 | + |
| 142 | + a := testAction.PrepareAction(c, NewHandleFulcioCertAction()) |
| 143 | + g.Expect(a.CanHandle(context.TODO(), i)).To(BeTrue()) |
| 144 | + |
| 145 | + _ = a.Handle(context.TODO(), i) |
| 146 | + g.Expect(len(i.Status.RootCertificates)).Should(Equal(2)) |
| 147 | + g.Expect(i.Status.RootCertificates[0].Key).Should(Equal("key")) |
| 148 | + g.Expect(i.Status.RootCertificates[0].Name).Should(Equal("secret")) |
| 149 | + g.Expect(i.Status.RootCertificates[1].Key).Should(Equal("key")) |
| 150 | + g.Expect(i.Status.RootCertificates[1].Name).Should(Equal("secret-2")) |
| 151 | + |
| 152 | + g.Expect(meta.IsStatusConditionTrue(i.Status.Conditions, CertCondition)).To(BeTrue()) |
| 153 | +} |
| 154 | + |
| 155 | +func Test_HandleFulcioCert_Configured_Priority(t *testing.T) { |
| 156 | + g := NewWithT(t) |
| 157 | + |
| 158 | + instance := &v1alpha1.CTlog{ |
| 159 | + ObjectMeta: metav1.ObjectMeta{ |
| 160 | + Name: "configured-priority", |
| 161 | + Namespace: "default", |
| 162 | + }, |
| 163 | + Spec: v1alpha1.CTlogSpec{ |
| 164 | + RootCertificates: []v1alpha1.SecretKeySelector{ |
| 165 | + { |
| 166 | + Key: "key", |
| 167 | + LocalObjectReference: v1alpha1.LocalObjectReference{Name: "my-secret"}, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + Status: v1alpha1.CTlogStatus{ |
| 172 | + Conditions: []metav1.Condition{ |
| 173 | + { |
| 174 | + Type: constants.Ready, |
| 175 | + Reason: constants.Creating, |
| 176 | + Status: metav1.ConditionFalse, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + c := testAction.FakeClientBuilder().WithObjects( |
| 183 | + kubernetes.CreateSecret("my-secret", "default", |
| 184 | + map[string][]byte{"key": nil}, map[string]string{}), |
| 185 | + kubernetes.CreateSecret("incorrect-secret", "default", |
| 186 | + map[string][]byte{"key": nil}, map[string]string{actions.FulcioCALabel: "key"}), |
| 187 | + instance, |
| 188 | + ).Build() |
| 189 | + |
| 190 | + i := &v1alpha1.CTlog{} |
| 191 | + if err := c.Get(context.TODO(), types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()}, i); err != nil { |
| 192 | + t.Error(err) |
| 193 | + } |
| 194 | + |
| 195 | + a := testAction.PrepareAction(c, NewHandleFulcioCertAction()) |
| 196 | + g.Expect(a.CanHandle(context.TODO(), i)).To(BeTrue()) |
| 197 | + |
| 198 | + _ = a.Handle(context.TODO(), i) |
| 199 | + g.Expect(len(i.Status.RootCertificates)).Should(Equal(1)) |
| 200 | + g.Expect(i.Status.RootCertificates[0].Key).Should(Equal("key")) |
| 201 | + g.Expect(i.Status.RootCertificates[0].Name).Should(Equal("my-secret")) |
| 202 | + |
| 203 | + g.Expect(meta.IsStatusConditionTrue(i.Status.Conditions, CertCondition)).To(BeTrue()) |
| 204 | +} |
| 205 | + |
| 206 | +func Test_HandleFulcioCert_Delete_ServerConfig(t *testing.T) { |
| 207 | + g := NewWithT(t) |
| 208 | + |
| 209 | + instance := &v1alpha1.CTlog{ |
| 210 | + ObjectMeta: metav1.ObjectMeta{ |
| 211 | + Name: "delete-config", |
| 212 | + Namespace: "default", |
| 213 | + }, |
| 214 | + Spec: v1alpha1.CTlogSpec{ |
| 215 | + RootCertificates: []v1alpha1.SecretKeySelector{ |
| 216 | + { |
| 217 | + Key: "key", |
| 218 | + LocalObjectReference: v1alpha1.LocalObjectReference{Name: "secret"}, |
| 219 | + }, |
| 220 | + }, |
| 221 | + }, |
| 222 | + Status: v1alpha1.CTlogStatus{ |
| 223 | + ServerConfigRef: &v1alpha1.LocalObjectReference{Name: "ctlog-config"}, |
| 224 | + Conditions: []metav1.Condition{ |
| 225 | + { |
| 226 | + Type: constants.Ready, |
| 227 | + Reason: constants.Creating, |
| 228 | + Status: metav1.ConditionFalse, |
| 229 | + }, |
| 230 | + }, |
| 231 | + }, |
| 232 | + } |
| 233 | + |
| 234 | + c := testAction.FakeClientBuilder().WithObjects( |
| 235 | + kubernetes.CreateImmutableSecret("ctlog-config", instance.Namespace, map[string][]byte{}, map[string]string{}), |
| 236 | + instance, |
| 237 | + ).Build() |
| 238 | + |
| 239 | + i := &v1alpha1.CTlog{} |
| 240 | + if err := c.Get(context.TODO(), types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()}, i); err != nil { |
| 241 | + t.Error(err) |
| 242 | + } |
| 243 | + |
| 244 | + a := testAction.PrepareAction(c, NewHandleFulcioCertAction()) |
| 245 | + g.Expect(a.CanHandle(context.TODO(), i)).To(BeTrue()) |
| 246 | + |
| 247 | + _ = a.Handle(context.TODO(), i) |
| 248 | + g.Expect(meta.IsStatusConditionTrue(i.Status.Conditions, CertCondition)).To(BeTrue()) |
| 249 | + |
| 250 | + g.Expect(i.Status.ServerConfigRef).To(BeNil()) |
| 251 | + g.Expect(c.Get(context.TODO(), types.NamespacedName{Name: "ctlog-config", Namespace: instance.GetNamespace()}, &v1.Secret{})).To(HaveOccurred()) |
| 252 | +} |
| 253 | + |
0 commit comments