-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
VAULT-9427: Add read support to sys/loggers
endpoints
#17979
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f7377bb
add logger->log-level str func
ccapurso a63bcb8
ensure SetLogLevelByName accounts for duplicates
ccapurso 89aed10
add read handlers for sys/loggers endpoints
ccapurso 0936449
add changelog entry
ccapurso 0a2ee78
update docs
ccapurso 0050e8b
ignore base logger
ccapurso 2b40076
fix docs formatting issue
ccapurso f7b46b1
add ReadOperation support to TestSystemBackend_Loggers
ccapurso 92cc879
add more robust checks to TestSystemBackend_Loggers
ccapurso e7562af
add more robust checks to TestSystemBackend_LoggersByName
ccapurso 30a5508
check for empty name in delete handler
ccapurso 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 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,3 @@ | ||
```release-note:improvement | ||
core: Add read support to `sys/loggers` and `sys/loggers/:name` endpoints | ||
``` |
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 |
---|---|---|
|
@@ -20,9 +20,6 @@ import ( | |
"time" | ||
"unicode" | ||
|
||
"github.com/hashicorp/vault/helper/versions" | ||
"golang.org/x/crypto/sha3" | ||
|
||
"github.com/hashicorp/errwrap" | ||
log "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/go-memdb" | ||
|
@@ -32,10 +29,12 @@ import ( | |
semver "github.com/hashicorp/go-version" | ||
"github.com/hashicorp/vault/helper/hostutil" | ||
"github.com/hashicorp/vault/helper/identity" | ||
"github.com/hashicorp/vault/helper/logging" | ||
"github.com/hashicorp/vault/helper/metricsutil" | ||
"github.com/hashicorp/vault/helper/monitor" | ||
"github.com/hashicorp/vault/helper/namespace" | ||
"github.com/hashicorp/vault/helper/random" | ||
"github.com/hashicorp/vault/helper/versions" | ||
"github.com/hashicorp/vault/sdk/framework" | ||
"github.com/hashicorp/vault/sdk/helper/consts" | ||
"github.com/hashicorp/vault/sdk/helper/jsonutil" | ||
|
@@ -44,6 +43,7 @@ import ( | |
"github.com/hashicorp/vault/sdk/logical" | ||
"github.com/hashicorp/vault/sdk/version" | ||
"github.com/mitchellh/mapstructure" | ||
"golang.org/x/crypto/sha3" | ||
) | ||
|
||
const ( | ||
|
@@ -4828,28 +4828,35 @@ func (b *SystemBackend) handleVersionHistoryList(ctx context.Context, req *logic | |
return logical.ListResponseWithInfo(respKeys, respKeyInfo), nil | ||
} | ||
|
||
// getLogLevel returns the hclog.Level that corresponds with the provided level string. | ||
// This differs hclog.LevelFromString in that it supports additional level strings so | ||
// that in remains consistent with the handling found in the "vault server" command. | ||
func getLogLevel(logLevel string) (log.Level, error) { | ||
var level log.Level | ||
|
||
switch logLevel { | ||
case "trace": | ||
level = log.Trace | ||
case "debug": | ||
level = log.Debug | ||
case "notice", "info", "": | ||
level = log.Info | ||
case "warn", "warning": | ||
level = log.Warn | ||
case "err", "error": | ||
level = log.Error | ||
default: | ||
return level, fmt.Errorf("unrecognized log level %q", logLevel) | ||
func (b *SystemBackend) handleLoggersRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) { | ||
b.Core.allLoggersLock.RLock() | ||
defer b.Core.allLoggersLock.RUnlock() | ||
|
||
loggers := make(map[string]interface{}) | ||
warnings := make([]string, 0) | ||
|
||
for _, logger := range b.Core.allLoggers { | ||
loggerName := logger.Name() | ||
|
||
// ignore base logger | ||
if loggerName == "" { | ||
continue | ||
} | ||
|
||
logLevel, err := logging.TranslateLoggerLevel(logger) | ||
if err != nil { | ||
warnings = append(warnings, fmt.Sprintf("cannot translate level for %q: %s", loggerName, err.Error())) | ||
} else { | ||
loggers[loggerName] = logLevel | ||
} | ||
} | ||
|
||
return level, nil | ||
resp := &logical.Response{ | ||
Data: loggers, | ||
Warnings: warnings, | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (b *SystemBackend) handleLoggersWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) { | ||
|
@@ -4864,7 +4871,7 @@ func (b *SystemBackend) handleLoggersWrite(ctx context.Context, req *logical.Req | |
return logical.ErrorResponse("level is empty"), nil | ||
} | ||
|
||
level, err := getLogLevel(logLevel) | ||
level, err := logging.ParseLogLevel(logLevel) | ||
if err != nil { | ||
return logical.ErrorResponse(fmt.Sprintf("invalid level provided: %s", err.Error())), nil | ||
} | ||
|
@@ -4875,7 +4882,7 @@ func (b *SystemBackend) handleLoggersWrite(ctx context.Context, req *logical.Req | |
} | ||
|
||
func (b *SystemBackend) handleLoggersDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) { | ||
level, err := getLogLevel(b.Core.logLevel) | ||
level, err := logging.ParseLogLevel(b.Core.logLevel) | ||
if err != nil { | ||
return logical.ErrorResponse(fmt.Sprintf("log level from config is invalid: %s", err.Error())), nil | ||
} | ||
|
@@ -4885,12 +4892,63 @@ func (b *SystemBackend) handleLoggersDelete(ctx context.Context, req *logical.Re | |
return nil, nil | ||
} | ||
|
||
func (b *SystemBackend) handleLoggersByNameRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) { | ||
nameRaw, nameOk := d.GetOk("name") | ||
if !nameOk { | ||
return logical.ErrorResponse("name is required"), nil | ||
} | ||
|
||
name := nameRaw.(string) | ||
if name == "" { | ||
return logical.ErrorResponse("name is empty"), nil | ||
} | ||
|
||
b.Core.allLoggersLock.RLock() | ||
defer b.Core.allLoggersLock.RUnlock() | ||
|
||
loggers := make(map[string]interface{}) | ||
warnings := make([]string, 0) | ||
|
||
for _, logger := range b.Core.allLoggers { | ||
loggerName := logger.Name() | ||
|
||
// ignore base logger | ||
if loggerName == "" { | ||
continue | ||
} | ||
|
||
if loggerName == name { | ||
logLevel, err := logging.TranslateLoggerLevel(logger) | ||
|
||
if err != nil { | ||
warnings = append(warnings, fmt.Sprintf("cannot translate level for %q: %s", loggerName, err.Error())) | ||
} else { | ||
loggers[loggerName] = logLevel | ||
} | ||
|
||
break | ||
} | ||
} | ||
|
||
resp := &logical.Response{ | ||
Data: loggers, | ||
Warnings: warnings, | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (b *SystemBackend) handleLoggersByNameWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) { | ||
nameRaw, nameOk := d.GetOk("name") | ||
if !nameOk { | ||
return logical.ErrorResponse("name is required"), nil | ||
} | ||
|
||
name := nameRaw.(string) | ||
if name == "" { | ||
return logical.ErrorResponse("name is empty"), nil | ||
} | ||
|
||
logLevelRaw, logLevelOk := d.GetOk("level") | ||
|
||
if !logLevelOk { | ||
|
@@ -4902,14 +4960,14 @@ func (b *SystemBackend) handleLoggersByNameWrite(ctx context.Context, req *logic | |
return logical.ErrorResponse("level is empty"), nil | ||
} | ||
|
||
level, err := getLogLevel(logLevel) | ||
level, err := logging.ParseLogLevel(logLevel) | ||
if err != nil { | ||
return logical.ErrorResponse(fmt.Sprintf("invalid level provided: %s", err.Error())), nil | ||
} | ||
|
||
err = b.Core.SetLogLevelByName(nameRaw.(string), level) | ||
if err != nil { | ||
return logical.ErrorResponse(fmt.Sprintf("invalid params: %s", err.Error())), nil | ||
success := b.Core.SetLogLevelByName(name, level) | ||
if !success { | ||
return logical.ErrorResponse(fmt.Sprintf("logger %q not found", name)), nil | ||
} | ||
|
||
return nil, nil | ||
|
@@ -4921,14 +4979,19 @@ func (b *SystemBackend) handleLoggersByNameDelete(ctx context.Context, req *logi | |
return logical.ErrorResponse("name is required"), nil | ||
} | ||
|
||
level, err := getLogLevel(b.Core.logLevel) | ||
level, err := logging.ParseLogLevel(b.Core.logLevel) | ||
if err != nil { | ||
return logical.ErrorResponse(fmt.Sprintf("log level from config is invalid: %s", err.Error())), nil | ||
} | ||
|
||
err = b.Core.SetLogLevelByName(nameRaw.(string), level) | ||
if err != nil { | ||
return logical.ErrorResponse(fmt.Sprintf("invalid params: %s", err.Error())), nil | ||
name := nameRaw.(string) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Do we need to check if name is an empty string here the same as above functions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we should. Thanks! |
||
if name == "" { | ||
return logical.ErrorResponse("name is empty"), nil | ||
} | ||
|
||
success := b.Core.SetLogLevelByName(name, level) | ||
if !success { | ||
return logical.ErrorResponse(fmt.Sprintf("logger %q not found", name)), nil | ||
} | ||
|
||
return nil, nil | ||
|
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I took this code and refactored it into a function when adding the log file feature, so this is my bad and thanks for adding comments!
However, I think that the func should probably not include
notice
orwarning
as that hasn't (to my knowledge) been documented on our site.There's also differing info depending on whether you look at the docs for the
server
command or Vault configuration.Trace, Debug, Error, Warn, Info.
vstrace, debug, info, warn, *err*
.My PR I have open at the moment to add rotation to log files has updates to the docs to make them all standardised, and I will also update
ParseLogLevel
to not handlenotice
,warning
or""
which defaults toinfo
.