Skip to content

Commit

Permalink
add custom marshaller for env vars to hide them in the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
thielepaul committed Nov 18, 2024
1 parent 299f98b commit d3d8cfc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pkg/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type JobSpec struct {

Name string `json:"name"`
Retries int `yaml:"retries,omitempty" json:"retries,omitempty"`
Env map[string]string `yaml:"env,omitempty"`
Env map[string]secret `yaml:"env,omitempty"`
WorkingDirectory string `yaml:"working_directory,omitempty" json:"working_directory,omitempty"`
globalSchedule *Schedule
Runs []JobRun `json:"runs" yaml:"-"`
Expand All @@ -50,6 +50,13 @@ type JobSpec struct {
cfg Config
}

type secret string

// custom marshaller to hide secrets
func (secret) MarshalText() ([]byte, error) {
return []byte("***"), nil
}

// JobRun holds information about a job execution.
type JobRun struct {
LogEntryId int `json:"id,omitempty" db:"id"`
Expand Down
19 changes: 18 additions & 1 deletion pkg/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cheek

import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -317,7 +318,7 @@ func TestJobWithBashEval(t *testing.T) {
"bash", "-c", "MY_VAR=$(date +%Y-%m); echo $MY_VAR $FOO",
},
// add random Env to check if it passes through
Env: map[string]string{
Env: map[string]secret{
"FOO": "BAR",
},
cfg: NewConfig(),
Expand Down Expand Up @@ -407,3 +408,19 @@ func TestExecCommandExitError(t *testing.T) {
assert.NotNil(t, result.Duration, "Expected duration to be set")
assert.GreaterOrEqual(t, result.Duration.Milliseconds(), int64(0), "Expected positive duration")
}

func TestMarshalSecret(t *testing.T) {
secrets := map[string]secret{"foo": "bar"}

yamlResult, err := yaml.Marshal(secrets)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(yamlResult), "foo: '***'\n")

jsonResult, err := json.Marshal(secrets)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(jsonResult), `{"foo":"***"}`)
}

0 comments on commit d3d8cfc

Please sign in to comment.