Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty secret refs in service bindings #887

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions pkg/buildpod/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func (g *Generator) fetchServiceBindings(ctx context.Context, build BuildPodable
return nil, err
}

var forbiddenSecrets = map[string]bool{}
var forbiddenSecrets = map[string]struct{}{}
for _, serviceAccount := range serviceAccounts {
for _, secret := range serviceAccount.Secrets {
forbiddenSecrets[secret.Name] = true
forbiddenSecrets[secret.Name] = struct{}{}
}
}

Expand All @@ -111,7 +111,7 @@ func (g *Generator) fetchServiceBindings(ctx context.Context, build BuildPodable
}
}

if bindingUsesForbiddenSecret(forbiddenSecrets, sb.SecretRef.Name) {
if bindingUsesForbiddenSecret(forbiddenSecrets, sb.SecretRef) {
return nil, errors.Errorf("build rejected: service %q uses forbidden secret %q", sb.Name, sb.SecretRef.Name)
}

Expand All @@ -122,7 +122,7 @@ func (g *Generator) fetchServiceBindings(ctx context.Context, build BuildPodable

if cnbBindings := build.CnbBindings(); len(cnbBindings) != 0 {
for _, b := range cnbBindings {
if bindingUsesForbiddenSecret(forbiddenSecrets, b.SecretRef.Name) {
if bindingUsesForbiddenSecret(forbiddenSecrets, b.SecretRef) {
return nil, fmt.Errorf("build rejected: binding %q uses forbidden secret %q", b.Name, b.SecretRef.Name)
}
bindings = append(bindings, &corev1alpha1.CNBServiceBinding{
Expand Down Expand Up @@ -150,8 +150,12 @@ func (g *Generator) readProvisionedServiceDuckType(ctx context.Context, build Bu
return ps, nil
}

func bindingUsesForbiddenSecret(forbiddenSecrets map[string]bool, secretName string) bool {
_, ok := forbiddenSecrets[secretName]
func bindingUsesForbiddenSecret(forbiddenSecrets map[string]struct{}, secretRef *corev1.LocalObjectReference) bool {
if secretRef == nil {
return false
}

_, ok := forbiddenSecrets[secretRef.Name]
return ok
}

Expand Down
32 changes: 31 additions & 1 deletion pkg/buildpod/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) {
serviceAccount.ImagePullSecrets = append(serviceAccount.ImagePullSecrets, corev1.LocalObjectReference{Name: "docker-secret-1"})
serviceAccount.ImagePullSecrets = append(serviceAccount.ImagePullSecrets, corev1.LocalObjectReference{Name: "image-pull-duplicate-1"})
serviceAccount.ImagePullSecrets = append(serviceAccount.ImagePullSecrets, corev1.LocalObjectReference{Name: "image-pull-duplicate-1"})
fakeK8sClient.CoreV1().ServiceAccounts(namespace).Update(context.TODO(), serviceAccount, metav1.UpdateOptions{})
_, err := fakeK8sClient.CoreV1().ServiceAccounts(namespace).Update(context.TODO(), serviceAccount, metav1.UpdateOptions{})
require.NoError(t, err)

pod, err := generator.Generate(context.TODO(), build)
require.NoError(t, err)
Expand Down Expand Up @@ -360,6 +361,35 @@ func testGenerator(t *testing.T, when spec.G, it spec.S) {
assert.Equal(t, expectedBindings, build.buildPodCalls[0].BuildContext.Bindings)
})

it("supports empty secret refs", func() {
var build = &testBuildPodable{
namespace: namespace,
cnbBindings: corev1alpha1.CNBBindings{
{
Name: "test",
MetadataRef: &corev1.LocalObjectReference{Name: "binding-configmap"},
},
},
serviceAccount: serviceAccountName,
buildBuilderSpec: corev1alpha1.BuildBuilderSpec{
Image: linuxBuilderImage,
ImagePullSecrets: builderPullSecrets,
},
}
_, err := generator.Generate(context.TODO(), build)
require.NoError(t, err)

expectedBindings := []buildapi.ServiceBinding{
&corev1alpha1.CNBServiceBinding{
Name: "test",
MetadataRef: &corev1.LocalObjectReference{Name: "binding-configmap"},
},
}

assert.Len(t, build.buildPodCalls[0].BuildContext.Bindings, 1)
assert.Equal(t, expectedBindings, build.buildPodCalls[0].BuildContext.Bindings)
})

it("rejects a build with a cnb binding secret that is attached to a service account", func() {
var build = &testBuildPodable{
namespace: namespace,
Expand Down