|
| 1 | +package secret |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/imdario/mergo" |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func newBasicAuthTestSecret(extraAnnotations map[string]string) *corev1.Secret { |
| 15 | + annotations := map[string]string{ |
| 16 | + AnnotationSecretType: string(SecretTypeBasicAuth), |
| 17 | + } |
| 18 | + |
| 19 | + if extraAnnotations != nil { |
| 20 | + if err := mergo.Merge(&annotations, extraAnnotations, mergo.WithOverride); err != nil { |
| 21 | + panic(err) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + s := &corev1.Secret{ |
| 26 | + ObjectMeta: metav1.ObjectMeta{ |
| 27 | + Name: getSecretName(), |
| 28 | + Namespace: "default", |
| 29 | + Labels: map[string]string{ |
| 30 | + labelSecretGeneratorTest: "yes", |
| 31 | + }, |
| 32 | + Annotations: annotations, |
| 33 | + }, |
| 34 | + Type: corev1.SecretTypeOpaque, |
| 35 | + Data: map[string][]byte{}, |
| 36 | + } |
| 37 | + |
| 38 | + return s |
| 39 | +} |
| 40 | + |
| 41 | +// verify basic fields of the secret are present |
| 42 | +func verifyBasicAuthSecret(t *testing.T, in, out *corev1.Secret) { |
| 43 | + if out.Annotations[AnnotationSecretType] != string(SecretTypeBasicAuth) { |
| 44 | + t.Errorf("generated secret has wrong type %s on %s annotation", out.Annotations[AnnotationSecretType], AnnotationSecretType) |
| 45 | + } |
| 46 | + |
| 47 | + _, wasGenerated := in.Annotations[AnnotationSecretAutoGeneratedAt] |
| 48 | + |
| 49 | + auth := out.Data[SecretFieldBasicAuthIngress] |
| 50 | + password := out.Data[SecretFieldBasicAuthPassword] |
| 51 | + |
| 52 | + // check if password has been saved in clear text |
| 53 | + // and has correct length (if the secret has actually been generated) |
| 54 | + if !wasGenerated && (len(password) == 0 || len(password) != desiredLength(in)) { |
| 55 | + t.Errorf("generated field has wrong length of %d", len(password)) |
| 56 | + } |
| 57 | + |
| 58 | + // check if auth field has been generated (with separator) |
| 59 | + if len(auth) == 0 || !strings.Contains(string(auth), ":") { |
| 60 | + t.Errorf("auth field has wrong or no values %s", string(auth)) |
| 61 | + } |
| 62 | + |
| 63 | + if _, ok := out.Annotations[AnnotationSecretAutoGeneratedAt]; !ok { |
| 64 | + t.Errorf("secret has no %s annotation", AnnotationSecretAutoGeneratedAt) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestGenerateBasicAuthWithoutUsername(t *testing.T) { |
| 69 | + in := newBasicAuthTestSecret(map[string]string{}) |
| 70 | + require.NoError(t, mgr.GetClient().Create(context.TODO(), in)) |
| 71 | + |
| 72 | + doReconcile(t, in, false) |
| 73 | + |
| 74 | + out := &corev1.Secret{} |
| 75 | + require.NoError(t, mgr.GetClient().Get(context.TODO(), client.ObjectKey{ |
| 76 | + Name: in.Name, |
| 77 | + Namespace: in.Namespace}, out)) |
| 78 | + |
| 79 | + verifyBasicAuthSecret(t, in, out) |
| 80 | + require.Equal(t, "admin", string(out.Data[SecretFieldBasicAuthUsername])) |
| 81 | +} |
| 82 | + |
| 83 | +func TestGenerateBasicAuthWithUsername(t *testing.T) { |
| 84 | + in := newBasicAuthTestSecret(map[string]string{ |
| 85 | + AnnotationBasicAuthUsername: "test123", |
| 86 | + }) |
| 87 | + require.NoError(t, mgr.GetClient().Create(context.TODO(), in)) |
| 88 | + |
| 89 | + doReconcile(t, in, false) |
| 90 | + |
| 91 | + out := &corev1.Secret{} |
| 92 | + require.NoError(t, mgr.GetClient().Get(context.TODO(), client.ObjectKey{ |
| 93 | + Name: in.Name, |
| 94 | + Namespace: in.Namespace}, out)) |
| 95 | + |
| 96 | + verifyBasicAuthSecret(t, in, out) |
| 97 | + require.Equal(t, "test123", string(out.Data[SecretFieldBasicAuthUsername])) |
| 98 | +} |
| 99 | + |
| 100 | +func TestGenerateBasicAuthRegenerate(t *testing.T) { |
| 101 | + in := newBasicAuthTestSecret(map[string]string{ |
| 102 | + AnnotationBasicAuthUsername: "test123", |
| 103 | + }) |
| 104 | + require.NoError(t, mgr.GetClient().Create(context.TODO(), in)) |
| 105 | + |
| 106 | + doReconcile(t, in, false) |
| 107 | + |
| 108 | + out := &corev1.Secret{} |
| 109 | + require.NoError(t, mgr.GetClient().Get(context.TODO(), client.ObjectKey{ |
| 110 | + Name: in.Name, |
| 111 | + Namespace: in.Namespace}, out)) |
| 112 | + |
| 113 | + verifyBasicAuthSecret(t, in, out) |
| 114 | + require.Equal(t, "test123", string(out.Data[SecretFieldBasicAuthUsername])) |
| 115 | + oldPassword := string(out.Data[SecretFieldBasicAuthPassword]) |
| 116 | + oldAuth := string(out.Data[SecretFieldBasicAuthIngress]) |
| 117 | + |
| 118 | + // force regenerate |
| 119 | + out.Annotations[AnnotationSecretRegenerate] = "yes" |
| 120 | + require.NoError(t, mgr.GetClient().Update(context.TODO(), out)) |
| 121 | + |
| 122 | + doReconcile(t, out, false) |
| 123 | + |
| 124 | + outNew := &corev1.Secret{} |
| 125 | + require.NoError(t, mgr.GetClient().Get(context.TODO(), client.ObjectKey{ |
| 126 | + Name: in.Name, |
| 127 | + Namespace: in.Namespace}, outNew)) |
| 128 | + newPassword := string(outNew.Data[SecretFieldBasicAuthPassword]) |
| 129 | + newAuth := string(outNew.Data[SecretFieldBasicAuthIngress]) |
| 130 | + |
| 131 | + if oldPassword == newPassword { |
| 132 | + t.Errorf("secret has not been updated") |
| 133 | + } |
| 134 | + |
| 135 | + if oldAuth == newAuth { |
| 136 | + t.Errorf("secret has not been updated") |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestGenerateBasicAuthNoRegenerate(t *testing.T) { |
| 141 | + in := newBasicAuthTestSecret(map[string]string{ |
| 142 | + AnnotationBasicAuthUsername: "test123", |
| 143 | + }) |
| 144 | + require.NoError(t, mgr.GetClient().Create(context.TODO(), in)) |
| 145 | + |
| 146 | + doReconcile(t, in, false) |
| 147 | + |
| 148 | + out := &corev1.Secret{} |
| 149 | + require.NoError(t, mgr.GetClient().Get(context.TODO(), client.ObjectKey{ |
| 150 | + Name: in.Name, |
| 151 | + Namespace: in.Namespace}, out)) |
| 152 | + |
| 153 | + verifyBasicAuthSecret(t, in, out) |
| 154 | + require.Equal(t, "test123", string(out.Data[SecretFieldBasicAuthUsername])) |
| 155 | + oldPassword := string(out.Data[SecretFieldBasicAuthPassword]) |
| 156 | + oldAuth := string(out.Data[SecretFieldBasicAuthIngress]) |
| 157 | + |
| 158 | + doReconcile(t, in, false) |
| 159 | + |
| 160 | + outNew := &corev1.Secret{} |
| 161 | + require.NoError(t, mgr.GetClient().Get(context.TODO(), client.ObjectKey{ |
| 162 | + Name: in.Name, |
| 163 | + Namespace: in.Namespace}, outNew)) |
| 164 | + newPassword := string(out.Data[SecretFieldBasicAuthPassword]) |
| 165 | + newAuth := string(out.Data[SecretFieldBasicAuthIngress]) |
| 166 | + |
| 167 | + if oldPassword != newPassword { |
| 168 | + t.Errorf("secret has been updated") |
| 169 | + } |
| 170 | + |
| 171 | + if oldAuth != newAuth { |
| 172 | + t.Errorf("secret has been updated") |
| 173 | + } |
| 174 | +} |
0 commit comments