Skip to content

Commit

Permalink
Fix: Sanitize user identity before injecting into task pod as K8s lab…
Browse files Browse the repository at this point in the history
…el (#5023)

* Fix: Sanitize user identity before injecting into task pod as K8s label

Signed-off-by: Fabio Graetz <[email protected]>

* Lint

Signed-off-by: Fabio Graetz <[email protected]>

---------

Signed-off-by: Fabio Graetz <[email protected]>
  • Loading branch information
fg91 authored Mar 8, 2024
1 parent 992641c commit f52164d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
pluginsCore "github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/utils"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/utils/secrets"
k8sUtils "github.com/flyteorg/flyte/flytepropeller/pkg/utils"
)

const executionIdentityVariable = "execution-identity"
Expand Down Expand Up @@ -60,7 +61,8 @@ func newTaskExecutionMetadata(tCtx pluginsCore.TaskExecutionMetadata, taskTmpl *

id := tCtx.GetSecurityContext().RunAs.ExecutionIdentity
if len(id) > 0 {
injectLabels[executionIdentityVariable] = id
sanitizedID := k8sUtils.SanitizeLabelValue(id)
injectLabels[executionIdentityVariable] = sanitizedID
}

return TaskExecutionMetadata{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ func Test_newTaskExecutionMetadata(t *testing.T) {
assert.Equal(t, 2, len(actual.GetLabels()))
assert.Equal(t, "test-exec-identity", actual.GetLabels()[executionIdentityVariable])
})
t.Run("Inject exec identity K8s label sanitation", func(t *testing.T) {

existingMetadata := &mocks.TaskExecutionMetadata{}
existingAnnotations := map[string]string{}
existingMetadata.OnGetAnnotations().Return(existingAnnotations)

existingMetadata.OnGetSecurityContext().Return(core.SecurityContext{RunAs: &core.Identity{ExecutionIdentity: "[email protected]"}})

existingLabels := map[string]string{
"existingLabel": "existingLabelValue",
}
existingMetadata.OnGetLabels().Return(existingLabels)

actual, err := newTaskExecutionMetadata(existingMetadata, &core.TaskTemplate{})
assert.NoError(t, err)

assert.Equal(t, 2, len(actual.GetLabels()))
assert.Equal(t, "name-company-com", actual.GetLabels()[executionIdentityVariable])
})
}

func Test_newTaskExecutionContext(t *testing.T) {
Expand Down

0 comments on commit f52164d

Please sign in to comment.