forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new config var
enable_key_logging added
(TykTechnologies#1301)
If enabled, tyk logs will show direct key hashes in logs. If turned off, all hash keys will be replaced by `<hidden>`
- Loading branch information
Showing
19 changed files
with
255 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -658,6 +658,9 @@ const confSchema = `{ | |
} | ||
} | ||
} | ||
}, | ||
"enable_key_logging": { | ||
"type": "boolean" | ||
} | ||
} | ||
}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/Sirupsen/logrus" | ||
|
||
"github.com/TykTechnologies/tyk/config" | ||
) | ||
|
||
// identifies that field value was hidden before output to the log | ||
const ( | ||
logHiddenValue = "<hidden>" | ||
) | ||
|
||
func getLogEntryForRequest(r *http.Request, key string, data map[string]interface{}) *logrus.Entry { | ||
// populate http request fields | ||
fields := logrus.Fields{ | ||
"path": r.URL.Path, | ||
"origin": requestIP(r), | ||
} | ||
// add key to log if configured to do so | ||
if key != "" { | ||
fields["key"] = key | ||
if !config.Global.EnableKeyLogging { | ||
fields["key"] = logHiddenValue | ||
} | ||
} | ||
// add to log additional fields if any passed | ||
if data != nil && len(data) > 0 { | ||
for key, val := range data { | ||
fields[key] = val | ||
} | ||
} | ||
return log.WithFields(fields) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package main | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/Sirupsen/logrus" | ||
|
||
"github.com/TykTechnologies/tyk/config" | ||
) | ||
|
||
func TestGetLogEntryForRequest(t *testing.T) { | ||
testReq := httptest.NewRequest("GET", "http://tyk.io/test", nil) | ||
testReq.RemoteAddr = "127.0.0.1:80" | ||
testData := []struct { | ||
EnableKeyLogging bool | ||
Key string | ||
Data map[string]interface{} | ||
Result *logrus.Entry | ||
}{ | ||
// enable_key_logging is set, key passed, no additional data fields | ||
{ | ||
EnableKeyLogging: true, | ||
Key: "abc", | ||
Data: nil, | ||
Result: logrus.WithFields(logrus.Fields{ | ||
"path": "/test", | ||
"origin": "127.0.0.1", | ||
"key": "abc", | ||
}), | ||
}, | ||
// enable_key_logging is set, key is not passed, no additional data fields | ||
{ | ||
EnableKeyLogging: true, | ||
Key: "", | ||
Data: nil, | ||
Result: logrus.WithFields(logrus.Fields{ | ||
"path": "/test", | ||
"origin": "127.0.0.1", | ||
}), | ||
}, | ||
// enable_key_logging is set, key passed, additional data fields are passed | ||
{ | ||
EnableKeyLogging: true, | ||
Key: "abc", | ||
Data: map[string]interface{}{"a": 1, "b": "test"}, | ||
Result: logrus.WithFields(logrus.Fields{ | ||
"path": "/test", | ||
"origin": "127.0.0.1", | ||
"key": "abc", | ||
"a": 1, | ||
"b": "test", | ||
}), | ||
}, | ||
// enable_key_logging is set, key is not passed, additional data fields are passed | ||
{ | ||
EnableKeyLogging: true, | ||
Key: "", | ||
Data: map[string]interface{}{"a": 1, "b": "test"}, | ||
Result: logrus.WithFields(logrus.Fields{ | ||
"path": "/test", | ||
"origin": "127.0.0.1", | ||
"a": 1, | ||
"b": "test", | ||
}), | ||
}, | ||
// enable_key_logging is not set, key passed, no additional data field | ||
{ | ||
EnableKeyLogging: false, | ||
Key: "abc", | ||
Data: nil, | ||
Result: logrus.WithFields(logrus.Fields{ | ||
"path": "/test", | ||
"origin": "127.0.0.1", | ||
"key": logHiddenValue, | ||
}), | ||
}, | ||
// enable_key_logging is not set, key is not passed, no additional data field | ||
{ | ||
EnableKeyLogging: false, | ||
Key: "", | ||
Data: nil, | ||
Result: logrus.WithFields(logrus.Fields{ | ||
"path": "/test", | ||
"origin": "127.0.0.1", | ||
}), | ||
}, | ||
// enable_key_logging is not set, key passed, additional data fields are passed | ||
{ | ||
EnableKeyLogging: false, | ||
Key: "abc", | ||
Data: map[string]interface{}{"a": 1, "b": "test"}, | ||
Result: logrus.WithFields(logrus.Fields{ | ||
"path": "/test", | ||
"origin": "127.0.0.1", | ||
"a": 1, | ||
"b": "test", | ||
"key": logHiddenValue, | ||
}), | ||
}, | ||
// enable_key_logging is not set, key is not passed, additional data fields are passed | ||
{ | ||
EnableKeyLogging: false, | ||
Key: "", | ||
Data: map[string]interface{}{"a": 1, "b": "test"}, | ||
Result: logrus.WithFields(logrus.Fields{ | ||
"path": "/test", | ||
"origin": "127.0.0.1", | ||
"a": 1, | ||
"b": "test", | ||
}), | ||
}, | ||
} | ||
for _, test := range testData { | ||
config.Global.EnableKeyLogging = test.EnableKeyLogging | ||
logEntry := getLogEntryForRequest(testReq, test.Key, test.Data) | ||
if logEntry.Data["path"] != test.Result.Data["path"] { | ||
t.Error("Expected 'path':", test.Result.Data["path"], "Got:", logEntry.Data["path"]) | ||
} | ||
if logEntry.Data["origin"] != test.Result.Data["origin"] { | ||
t.Error("Expected 'origin':", test.Result.Data["origin"], "Got:", logEntry.Data["origin"]) | ||
} | ||
if logEntry.Data["key"] != test.Result.Data["key"] { | ||
t.Error("Expected 'key':", test.Result.Data["key"], "Got:", logEntry.Data["key"]) | ||
} | ||
if test.Data != nil { | ||
for key, val := range test.Data { | ||
if logEntry.Data[key] != val { | ||
t.Error("Expected data key:", key, "with value:", val, "Got:", logEntry.Data[key]) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.