-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #530 from fabriziosestito/feat/validate-imagepulls…
…ecret-policyserver feat: validate PolicyServer and imagePullSecret
- Loading branch information
Showing
8 changed files
with
229 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package policyserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// ValidateImagePullSecret validates that the specified PolicyServer imagePullSecret exists and is of type kubernetes.io/dockerconfigjson | ||
func ValidateImagePullSecret(ctx context.Context, k8sClient client.Client, imagePullSecret string, deploymentsNamespace string) error { | ||
secret := &corev1.Secret{} | ||
err := k8sClient.Get(ctx, client.ObjectKey{ | ||
Namespace: deploymentsNamespace, | ||
Name: imagePullSecret, | ||
}, secret) | ||
if err != nil { | ||
return fmt.Errorf("cannot get spec.ImagePullSecret: %w", err) | ||
} | ||
|
||
if secret.Type != "kubernetes.io/dockerconfigjson" { | ||
return fmt.Errorf("spec.ImagePullSecret secret \"%s\" is not of type kubernetes.io/dockerconfigjson", secret.Name) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package policyserver | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
func TestValidateImagePullSecret(t *testing.T) { | ||
ctx := context.Background() | ||
deploymentsNamespace := "test" | ||
|
||
tests := []struct { | ||
name string | ||
imagePullSecret string | ||
secret *v1.Secret | ||
valid bool | ||
}{ | ||
{ | ||
"non existing secret", | ||
"test", | ||
nil, | ||
false, | ||
}, | ||
{ | ||
"secret of wrong type", | ||
"test", | ||
&v1.Secret{ | ||
Type: "Opaque", | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"valid secret", | ||
"test", | ||
&v1.Secret{ | ||
Type: "kubernetes.io/dockerconfigjson", | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
}, | ||
false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
k8sClient := fake.NewClientBuilder().Build() | ||
|
||
if test.secret != nil { | ||
err := k8sClient.Create(ctx, test.secret) | ||
if err != nil { | ||
t.Errorf("failed to create secret: %s", err.Error()) | ||
} | ||
} | ||
|
||
err := ValidateImagePullSecret(ctx, k8sClient, test.imagePullSecret, deploymentsNamespace) | ||
if err != nil && test.valid { | ||
t.Errorf("unexpected error: %s", err.Error()) | ||
} else if err == nil && !test.valid { | ||
t.Errorf("expected error, got nil") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.