Skip to content

Commit

Permalink
TRIVIAL add possibility to disable privilege escalation (#132)
Browse files Browse the repository at this point in the history
TRIVIAL add possibility to disable privilege escalation
  • Loading branch information
deynekas authored Feb 18, 2025
1 parent ae7f1de commit e07191c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
15 changes: 10 additions & 5 deletions pkg/inject/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ type InitContainerResources struct {
Limits InitContainerResourcesList `yaml:"limits"`
}

type InitContainerSecurityContext struct {
AllowPrivilegeEscalation *bool `yaml:"allowPrivilegeEscalation,omitempty"`
}

// WebhookConfig struct representing webhook configuration values.
type WebhookConfig struct {
AnnotationPrefix string `yaml:"annotation-prefix,omitempty"`
Policy InjectionPolicy `yaml:"policy,omitempty"`
ContainerImage string `yaml:"container-image,omitempty"`
Default WebhookConfigDefaults `yaml:"default,omitempty"`
Resources InitContainerResources `yaml:"resources,omitempty"`
AnnotationPrefix string `yaml:"annotation-prefix,omitempty"`
Policy InjectionPolicy `yaml:"policy,omitempty"`
ContainerImage string `yaml:"container-image,omitempty"`
Default WebhookConfigDefaults `yaml:"default,omitempty"`
Resources InitContainerResources `yaml:"resources,omitempty"`
SecurityContext InitContainerSecurityContext `yaml:"securityContext,omitempty"`
}

// Webhook implements a mutating webhook for automatic config injection.
Expand Down
9 changes: 7 additions & 2 deletions pkg/inject/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
)

var (
Expand Down Expand Up @@ -264,6 +265,9 @@ func createWebhook(t testing.TB) (*Webhook, func()) {
Memory: resource.NewScaledQuantity(50, resource.Mega).String(),
},
},
SecurityContext: InitContainerSecurityContext{
AllowPrivilegeEscalation: ptr.To(false),
},
}

configBytes, err := yaml.Marshal(config)
Expand Down Expand Up @@ -363,7 +367,8 @@ func TestRunAndServe(t *testing.T) {
"image":"wanderadock/scccmd",
"args":["get","values","--source","http://config-service.default.svc:8080","--application","c1","--profile","default","--label","master","--destination","config.yaml"],
"resources":{"limits":{"cpu":"50m","memory":"50M"},"requests":{"cpu":"10m","memory":"10M"}},
"volumeMounts":[{"name":"config-volume","mountPath":"/config"}]
"volumeMounts":[{"name":"config-volume","mountPath":"/config"}],
"securityContext":{"allowPrivilegeEscalation":false}
}
},
{
Expand Down Expand Up @@ -464,7 +469,7 @@ func TestRunAndServe(t *testing.T) {
t.Fatal(err.Error())
}
}
testutil.AssertString(t, "got bad patch", wantPatch.String(), wantPatch.String())
testutil.AssertString(t, "got bad patch", wantPatch.String(), gotPatch.String())
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func injectionData(spec *v1.PodSpec, metadata *metav1.ObjectMeta, config *Webhoo
"memory": resource.MustParse(config.Resources.Limits.Memory),
},
},
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: config.SecurityContext.AllowPrivilegeEscalation,
},
},
},
VolumeMounts: []corev1.VolumeMount{volumeMount},
Expand Down

0 comments on commit e07191c

Please sign in to comment.