Skip to content

Enable protected secrets by default #14083

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

Merged
merged 3 commits into from
Oct 24, 2022
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
51 changes: 30 additions & 21 deletions components/ws-manager/pkg/manager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ func (m *Manager) createDefiniteWorkspacePod(startContext *startWorkspaceContext
pod.Finalizers = append(pod.Finalizers, "gitpod.io/debugfinalizer")
}

setProtectedSecrets(&pod, req)

ffidx := make(map[api.WorkspaceFeatureFlag]struct{})
for _, feature := range startContext.Request.Spec.FeatureFlags {
if _, seen := ffidx[feature]; seen {
Expand Down Expand Up @@ -590,27 +592,6 @@ func (m *Manager) createDefiniteWorkspacePod(startContext *startWorkspaceContext
gitpodGUID := int64(133332)
pod.Spec.SecurityContext.FSGroup = &gitpodGUID

case api.WorkspaceFeatureFlag_PROTECTED_SECRETS:
for _, c := range pod.Spec.Containers {
if c.Name != "workspace" {
continue
}

for i, env := range c.Env {
if !isProtectedEnvVar(env.Name, req.Spec.SysEnvvars) {
continue
}

env.Value = ""
env.ValueFrom = &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: pod.Name},
Key: fmt.Sprintf("%x", sha256.Sum256([]byte(env.Name))),
},
}
c.Env[i] = env
}
}
case api.WorkspaceFeatureFlag_WORKSPACE_CLASS_LIMITING:
limits := startContext.Class.Container.Limits
if limits != nil && limits.CPU != nil {
Expand Down Expand Up @@ -651,6 +632,34 @@ func (m *Manager) createDefiniteWorkspacePod(startContext *startWorkspaceContext
return &pod, nil
}

func setProtectedSecrets(pod *corev1.Pod, req *api.StartWorkspaceRequest) {
for _, c := range pod.Spec.Containers {
if c.Name != "workspace" {
continue
}

for i, env := range c.Env {
if !isProtectedEnvVar(env.Name, req.Spec.SysEnvvars) {
continue
}

// already sourced from somewhere else
if env.ValueFrom != nil {
continue
}

env.Value = ""
env.ValueFrom = &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: pod.Name},
Key: fmt.Sprintf("%x", sha256.Sum256([]byte(env.Name))),
},
}
c.Env[i] = env
}
}
}

func removeVolume(pod *corev1.Pod, name string) {
var vols []corev1.Volume
for _, v := range pod.Spec.Volumes {
Expand Down
43 changes: 17 additions & 26 deletions components/ws-manager/pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,34 +311,25 @@ func (m *Manager) StartWorkspace(ctx context.Context, req *api.StartWorkspaceReq
}
}

var createSecret bool
for _, feature := range startContext.Request.Spec.FeatureFlags {
if feature == api.WorkspaceFeatureFlag_PROTECTED_SECRETS {
createSecret = true
break
}
}
if createSecret {
secrets, _ := buildWorkspaceSecrets(startContext.Request.Spec)
secrets, _ := buildWorkspaceSecrets(startContext.Request.Spec)

// This call actually modifies the initializer and removes the secrets.
// Prior to the `InitWorkspace` call, we inject the secrets back into the initializer.
// We do this so that no Git token is stored as annotation on the pod, but solely
// remains within the Kubernetes secret.
_ = csapi.ExtractAndReplaceSecretsFromInitializer(startContext.Request.Spec.Initializer)
// This call actually modifies the initializer and removes the secrets.
// Prior to the `InitWorkspace` call, we inject the secrets back into the initializer.
// We do this so that no Git token is stored as annotation on the pod, but solely
// remains within the Kubernetes secret.
_ = csapi.ExtractAndReplaceSecretsFromInitializer(startContext.Request.Spec.Initializer)

secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: podName(startContext.Request),
Namespace: m.Config.Namespace,
Labels: startContext.Labels,
},
StringData: secrets,
}
err = m.Clientset.Create(ctx, secret)
if err != nil && !k8serr.IsAlreadyExists(err) {
return nil, xerrors.Errorf("cannot create secret for workspace pod: %w", err)
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: podName(startContext.Request),
Namespace: m.Config.Namespace,
Labels: startContext.Labels,
},
StringData: secrets,
}
err = m.Clientset.Create(ctx, secret)
if err != nil && !k8serr.IsAlreadyExists(err) {
return nil, xerrors.Errorf("cannot create secret for workspace pod: %w", err)
}

err = m.Clientset.Create(ctx, pod)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "ws-test",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -270,4 +275,4 @@
},
"status": {}
}
}
}
9 changes: 7 additions & 2 deletions components/ws-manager/pkg/manager/testdata/cdwp_class.golden
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "ws-test",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -290,4 +295,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "ws-test",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -263,4 +268,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@
},
{
"name": "something_without_gitpod",
"value": "will make it"
"valueFrom": {
"secretKeyRef": {
"name": "ws-test",
"key": "c1cf1305c2eb1eb4b0ce3f327b383751e785615720db2bcafc5227788067761c"
}
}
},
{
"name": "one_from_a_secret",
Expand Down Expand Up @@ -303,4 +308,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "ws-test",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -255,4 +260,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "imagebuild-foobar",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -288,4 +293,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "imagebuild-foobar",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -288,4 +293,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "ws-test",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -260,4 +265,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "prebuild-foobar",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -270,4 +275,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "prebuild-foobar",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -270,4 +275,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "prebuild-foobar",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -270,4 +275,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "ws-test",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -266,4 +271,4 @@
},
"status": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@
},
{
"name": "foo",
"value": "bar"
"valueFrom": {
"secretKeyRef": {
"name": "ws-test",
"key": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
}
}
},
{
"name": "GITPOD_INTERVAL",
Expand Down Expand Up @@ -267,4 +272,4 @@
},
"status": {}
}
}
}
Loading