Skip to content

Commit a2162c7

Browse files
authored
Do not marshal SMPT User/Password and SlackWebhookURL in json file (#289)
1 parent 433beb2 commit a2162c7

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

middlewares/common_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ type TestJob struct {
5050
core.BareJob
5151
}
5252

53+
type TestJobConfig struct {
54+
TestJob
55+
MailConfig
56+
OverlapConfig
57+
SaveConfig
58+
SlackConfig
59+
}
60+
5361
func (j *TestJob) Run(ctx *core.Context) error {
5462
return nil
5563
}

middlewares/mail.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"crypto/tls"
13+
1314
"gopkg.in/gomail.v2"
1415

1516
"github.com/mcuadros/ofelia/core"
@@ -19,8 +20,8 @@ import (
1920
type MailConfig struct {
2021
SMTPHost string `gcfg:"smtp-host" mapstructure:"smtp-host"`
2122
SMTPPort int `gcfg:"smtp-port" mapstructure:"smtp-port"`
22-
SMTPUser string `gcfg:"smtp-user" mapstructure:"smtp-user"`
23-
SMTPPassword string `gcfg:"smtp-password" mapstructure:"smtp-password"`
23+
SMTPUser string `gcfg:"smtp-user" mapstructure:"smtp-user" json:"-"`
24+
SMTPPassword string `gcfg:"smtp-password" mapstructure:"smtp-password" json:"-"`
2425
SMTPTLSSkipVerify bool `gcfg:"smtp-tls-skip-verify" mapstructure:"smtp-tls-skip-verify"`
2526
EmailTo string `gcfg:"email-to" mapstructure:"email-to"`
2627
EmailFrom string `gcfg:"email-from" mapstructure:"email-from"`

middlewares/save_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"io/ioutil"
55
"os"
66
"path/filepath"
7+
"strings"
78
"time"
89

10+
"github.com/mcuadros/ofelia/core"
911
. "gopkg.in/check.v1"
1012
)
1113

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

1618
var _ = Suite(&SuiteSave{})
1719

20+
func (s *SuiteSave) SetUpTest(c *C) {
21+
job := &TestJobConfig{
22+
TestJob: TestJob{
23+
BareJob: core.BareJob{
24+
Name: "test-job-save",
25+
},
26+
},
27+
MailConfig: MailConfig{
28+
SMTPHost: "test-host",
29+
SMTPPassword: "secret-password",
30+
SMTPUser: "secret-user",
31+
},
32+
SlackConfig: SlackConfig{
33+
SlackWebhook: "secret-url",
34+
},
35+
}
36+
37+
s.job = &job.TestJob
38+
39+
sh := core.NewScheduler(&TestLogger{})
40+
e := core.NewExecution()
41+
42+
s.ctx = core.NewContext(sh, job, e)
43+
}
44+
1845
func (s *SuiteSave) TestNewSlackEmpty(c *C) {
1946
c.Assert(NewSave(&SaveConfig{}), IsNil)
2047
}
@@ -58,3 +85,41 @@ func (s *SuiteSave) TestRunSuccessOnError(c *C) {
5885
_, err = os.Stat(filepath.Join(dir, "00010101_000000_foo.json"))
5986
c.Assert(err, Not(IsNil))
6087
}
88+
89+
func (s *SuiteSave) TestSensitiveData(c *C) {
90+
dir, err := ioutil.TempDir("/tmp", "save")
91+
c.Assert(err, IsNil)
92+
93+
s.ctx.Start()
94+
s.ctx.Stop(nil)
95+
96+
s.job.Name = "job-with-sensitive-data"
97+
s.ctx.Execution.Date = time.Time{}
98+
99+
m := NewSave(&SaveConfig{SaveFolder: dir})
100+
c.Assert(m.Run(s.ctx), IsNil)
101+
102+
expectedFileName := "00010101_000000_job-with-sensitive-data"
103+
_, err = os.Stat(filepath.Join(dir, expectedFileName+".json"))
104+
c.Assert(err, IsNil)
105+
106+
_, err = os.Stat(filepath.Join(dir, expectedFileName+".stdout.log"))
107+
c.Assert(err, IsNil)
108+
109+
_, err = os.Stat(filepath.Join(dir, expectedFileName+".stderr.log"))
110+
c.Assert(err, IsNil)
111+
112+
files, err := os.ReadDir(dir)
113+
c.Assert(err, IsNil)
114+
c.Assert(files, HasLen, 3)
115+
116+
for _, file := range files {
117+
b, err := os.ReadFile(filepath.Join(dir, file.Name()))
118+
c.Assert(err, IsNil)
119+
120+
if strings.Contains(string(b), "secret") {
121+
c.Log(string(b))
122+
c.Errorf("found secret string in %q", file.Name())
123+
}
124+
}
125+
}

middlewares/slack.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717

1818
// SlackConfig configuration for the Slack middleware
1919
type SlackConfig struct {
20-
SlackWebhook string `gcfg:"slack-webhook" mapstructure:"slack-webhook"`
20+
SlackWebhook string `gcfg:"slack-webhook" mapstructure:"slack-webhook" json:"-"`
2121
SlackOnlyOnError bool `gcfg:"slack-only-on-error" mapstructure:"slack-only-on-error"`
2222
}
2323

0 commit comments

Comments
 (0)