-
Notifications
You must be signed in to change notification settings - Fork 8
Add input validation to support dynamic changes to gorouter log level #73
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b64ccc0
add input validation to support to support gorouter log level changes…
navinms711 f20ee4a
add uppercase log levels
navinms711 3f84006
add validation and input normalization logic
navinms711 ec80c88
remove old tests and add new ones for checking log-level endpoint
navinms711 229ffef
removed emojis and some excess comments
navinms711 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,68 @@ | ||
| package debugserver | ||
|
|
||
| import ( | ||
| lager "code.cloudfoundry.org/lager/v3" | ||
| "errors" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| // zapLogLevelController is an interface that defines a method to set the minimum log level. | ||
| type zapLogLevelController interface { | ||
| SetMinLevel(level lager.LogLevel) | ||
| } | ||
|
|
||
| // LagerAdapter is an adapter for the ReconfigurableSinkInterface to work with lager.LogLevel. | ||
| type LagerAdapter struct { | ||
| Sink ReconfigurableSinkInterface | ||
| } | ||
|
|
||
| // SetMinLevel sets the minimum log level for the LagerAdapter. | ||
| func (l *LagerAdapter) SetMinLevel(level lager.LogLevel) { | ||
| l.Sink.SetMinLevel(level) | ||
| } | ||
|
|
||
| // normalizeLogLevel returns a single value that represents | ||
| // various forms of the same input level. For example: | ||
| // "0", "d", "debug", all of these represents debug log level. | ||
| func normalizeLogLevel(input string) string { | ||
| switch strings.ToLower(strings.TrimSpace(input)) { | ||
| case "0", "d", "debug": | ||
| return "debug" | ||
| case "1", "i", "info": | ||
| return "info" | ||
| case "2", "w", "warn": | ||
| return "warn" | ||
| case "3", "e", "error": | ||
| return "error" | ||
| case "4", "f", "fatal": | ||
| return "fatal" | ||
| default: | ||
| return "" | ||
| } | ||
| } | ||
|
|
||
| // validateAndNormalize does two things: | ||
| // It validates the incoming request is HTTP type, uses POST method and has non-nil level specified. | ||
| // It also normalizes the various forms of the same log level type. For ex: 0, d, debug are all same. | ||
| func validateAndNormalize(w http.ResponseWriter, r *http.Request, level []byte) (string, error) { | ||
| if r.Method != http.MethodPost { | ||
| return "", errors.New("method not allowed, use POST") | ||
| } | ||
|
|
||
| if r.TLS != nil { | ||
| return "", errors.New("invalid scheme, https is not allowed") | ||
| } | ||
|
|
||
| if len(level) == 0 { | ||
| return "", errors.New("log level cannot be empty") | ||
| } | ||
|
|
||
| input := strings.TrimSpace(string(level)) | ||
| normalized := normalizeLogLevel(input) | ||
| if normalized == "" { | ||
| return "", errors.New("invalid log level: " + string(level)) | ||
| } | ||
|
|
||
| return normalized, nil | ||
| } |
This file contains hidden or 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 hidden or 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,12 @@ | ||
| //go:build test | ||
|
|
||
| package debugserver | ||
|
|
||
| import ( | ||
| "net/http" | ||
| ) | ||
|
|
||
| // Exported only for tests | ||
| func ValidateAndNormalize(w http.ResponseWriter, r *http.Request, level []byte) (string, error) { | ||
| return validateAndNormalize(w, r, level) | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.