Skip to content

Commit

Permalink
Merge pull request #128 from Scalingo/fix/db/1754/credentials_leak
Browse files Browse the repository at this point in the history
[Document] Do not add sensitive information in the log
  • Loading branch information
Soulou authored Aug 13, 2020
2 parents 0bca61f + 1a3292c commit 72cf44f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
12 changes: 7 additions & 5 deletions errors/validation_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ func TestValidationErrorsBuilder_MergeWithPrefix(t *testing.T) {
func TestValidationErrors_Error(t *testing.T) {
cases := map[string]struct {
Errors ValidationErrors
ExpectedString string
expectedErrors []string
}{
"should return a string with one error in it": {
Errors: ValidationErrors{
Errors: map[string][]string{
"name": {"invalid name"},
},
},
ExpectedString: "name=invalid name",
expectedErrors: []string{"name=invalid name"},
},
"should return a string with multiple errors in it with the same field name": {
Errors: ValidationErrors{
Errors: map[string][]string{
"name": {"invalid name", "should contains alphanumeric characters"},
},
},
ExpectedString: "name=invalid name, should contains alphanumeric characters",
expectedErrors: []string{"name=invalid name", "should contains alphanumeric characters"},
},
"should return a string with multiple errors in it with multiple field name": {
Errors: ValidationErrors{
Expand All @@ -92,13 +92,15 @@ func TestValidationErrors_Error(t *testing.T) {
"type": {"invalid type", "type not exist"},
},
},
ExpectedString: "name=invalid name, should contains alphanumeric characters type=invalid type, type not exist",
expectedErrors: []string{"name=invalid name, should contains alphanumeric characters", "type=invalid type, type not exist"},
},
}

for title, c := range cases {
t.Run(title, func(t *testing.T) {
require.Equal(t, c.ExpectedString, c.Errors.Error())
for _, expectedError := range c.expectedErrors {
require.Contains(t, c.Errors.Error(), expectedError)
}
})
}
}
4 changes: 3 additions & 1 deletion graceful/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func ensurePidFileProcessKilled(t *testing.T) {
process, err := os.FindProcess(pid)
require.NoError(t, err)
err = process.Kill()
require.NoError(t, err)
if err != nil && !strings.Contains(err.Error(), "already finished") {
require.NoError(t, err)
}
err = os.Remove("./test-fixtures/server.pid")
require.NoError(t, err)
}
Expand Down
20 changes: 16 additions & 4 deletions mongo/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func Create(ctx context.Context, collectionName string, doc document) error {

c := mongo.Session(log).Clone().DB("").C(collectionName)
defer c.Database.Session.Close()
log.WithField(collectionName, doc).Debugf("save '%v'", collectionName)
log.WithFields(logrus.Fields{
"collection": collectionName,
"doc_id": doc.getID(),
}).Debugf("save '%v'", collectionName)
return c.Insert(doc)

}
Expand All @@ -82,7 +85,10 @@ func Save(ctx context.Context, collectionName string, doc document) error {

c := mongo.Session(log).Clone().DB("").C(collectionName)
defer c.Database.Session.Close()
log.WithField(collectionName, doc).Debugf("save '%v'", collectionName)
log.WithFields(logrus.Fields{
"collection": collectionName,
"doc_id": doc.getID(),
}).Debugf("save '%v'", collectionName)
_, err := c.UpsertId(doc.getID(), doc)
return err
}
Expand All @@ -96,7 +102,10 @@ func ReallyDestroy(ctx context.Context, collectionName string, doc document) err
log := logger.Get(ctx)
c := mongo.Session(log).Clone().DB("").C(collectionName)
defer c.Database.Session.Close()
log.WithField(collectionName, doc).Debugf("remove '%v'", collectionName)
log.WithFields(logrus.Fields{
"collection": collectionName,
"doc_id": doc.getID(),
}).Debugf("remove '%v'", collectionName)
return c.RemoveId(doc.getID())
}

Expand Down Expand Up @@ -232,7 +241,10 @@ func Update(ctx context.Context, collectionName string, update bson.M, doc docum
return err
}

log.WithField("query", update).Debugf("update %v", collectionName)
log.WithFields(logrus.Fields{
"collection": collectionName,
"doc_id": doc.getID(),
}).Debugf("update %v", collectionName)
return c.UpdateId(doc.getID(), update)
}

Expand Down

0 comments on commit 72cf44f

Please sign in to comment.