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

Do not marshal SMPT User/Password and SlackWebhookURL in json file #289

Merged
merged 1 commit into from
Jul 6, 2024
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
8 changes: 8 additions & 0 deletions middlewares/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ type TestJob struct {
core.BareJob
}

type TestJobConfig struct {
TestJob
MailConfig
OverlapConfig
SaveConfig
SlackConfig
}

func (j *TestJob) Run(ctx *core.Context) error {
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions middlewares/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"crypto/tls"

"gopkg.in/gomail.v2"

"github.com/mcuadros/ofelia/core"
Expand All @@ -19,8 +20,8 @@ import (
type MailConfig struct {
SMTPHost string `gcfg:"smtp-host" mapstructure:"smtp-host"`
SMTPPort int `gcfg:"smtp-port" mapstructure:"smtp-port"`
SMTPUser string `gcfg:"smtp-user" mapstructure:"smtp-user"`
SMTPPassword string `gcfg:"smtp-password" mapstructure:"smtp-password"`
SMTPUser string `gcfg:"smtp-user" mapstructure:"smtp-user" json:"-"`
SMTPPassword string `gcfg:"smtp-password" mapstructure:"smtp-password" json:"-"`
SMTPTLSSkipVerify bool `gcfg:"smtp-tls-skip-verify" mapstructure:"smtp-tls-skip-verify"`
EmailTo string `gcfg:"email-to" mapstructure:"email-to"`
EmailFrom string `gcfg:"email-from" mapstructure:"email-from"`
Expand Down
65 changes: 65 additions & 0 deletions middlewares/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"

"github.com/mcuadros/ofelia/core"
. "gopkg.in/check.v1"
)

Expand All @@ -15,6 +17,31 @@ type SuiteSave struct {

var _ = Suite(&SuiteSave{})

func (s *SuiteSave) SetUpTest(c *C) {
job := &TestJobConfig{
TestJob: TestJob{
BareJob: core.BareJob{
Name: "test-job-save",
},
},
MailConfig: MailConfig{
SMTPHost: "test-host",
SMTPPassword: "secret-password",
SMTPUser: "secret-user",
},
SlackConfig: SlackConfig{
SlackWebhook: "secret-url",
},
}

s.job = &job.TestJob

sh := core.NewScheduler(&TestLogger{})
e := core.NewExecution()

s.ctx = core.NewContext(sh, job, e)
}

func (s *SuiteSave) TestNewSlackEmpty(c *C) {
c.Assert(NewSave(&SaveConfig{}), IsNil)
}
Expand Down Expand Up @@ -58,3 +85,41 @@ func (s *SuiteSave) TestRunSuccessOnError(c *C) {
_, err = os.Stat(filepath.Join(dir, "00010101_000000_foo.json"))
c.Assert(err, Not(IsNil))
}

func (s *SuiteSave) TestSensitiveData(c *C) {
dir, err := ioutil.TempDir("/tmp", "save")
c.Assert(err, IsNil)

s.ctx.Start()
s.ctx.Stop(nil)

s.job.Name = "job-with-sensitive-data"
s.ctx.Execution.Date = time.Time{}

m := NewSave(&SaveConfig{SaveFolder: dir})
c.Assert(m.Run(s.ctx), IsNil)

expectedFileName := "00010101_000000_job-with-sensitive-data"
_, err = os.Stat(filepath.Join(dir, expectedFileName+".json"))
c.Assert(err, IsNil)

_, err = os.Stat(filepath.Join(dir, expectedFileName+".stdout.log"))
c.Assert(err, IsNil)

_, err = os.Stat(filepath.Join(dir, expectedFileName+".stderr.log"))
c.Assert(err, IsNil)

files, err := os.ReadDir(dir)
c.Assert(err, IsNil)
c.Assert(files, HasLen, 3)

for _, file := range files {
b, err := os.ReadFile(filepath.Join(dir, file.Name()))
c.Assert(err, IsNil)

if strings.Contains(string(b), "secret") {
c.Log(string(b))
c.Errorf("found secret string in %q", file.Name())
}
}
}
2 changes: 1 addition & 1 deletion middlewares/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (

// SlackConfig configuration for the Slack middleware
type SlackConfig struct {
SlackWebhook string `gcfg:"slack-webhook" mapstructure:"slack-webhook"`
SlackWebhook string `gcfg:"slack-webhook" mapstructure:"slack-webhook" json:"-"`
SlackOnlyOnError bool `gcfg:"slack-only-on-error" mapstructure:"slack-only-on-error"`
}

Expand Down
Loading