Skip to content
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

Handle permission issue on pki health-check tune checkers #19276

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions command/healthcheck/pki_allow_if_modified_since.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type AllowIfModifiedSince struct {
Enabled bool
UnsupportedVersion bool

TuneData map[string]interface{}
TuneData *PathFetch
}

func NewAllowIfModifiedSinceCheck() Check {
Expand Down Expand Up @@ -42,15 +42,14 @@ func (h *AllowIfModifiedSince) LoadConfig(config map[string]interface{}) error {
}

func (h *AllowIfModifiedSince) FetchResources(e *Executor) error {
exit, _, data, err := fetchMountTune(e, func() {
pathFetch, err := fetchMountTune(e, func() {
h.UnsupportedVersion = true
})
if exit {
if err != nil {
return err
}

h.TuneData = data

h.TuneData = pathFetch
return nil
}

Expand All @@ -59,30 +58,52 @@ func (h *AllowIfModifiedSince) Evaluate(e *Executor) (results []*Result, err err
ret := Result{
Status: ResultInvalidVersion,
Endpoint: "/sys/mounts/{{mount}}/tune",
Message: "This health check requires Vault 1.9+ but an earlier version of Vault Server was contacted, preventing this health check from running.",
Message: "This health check requires Vault 1.12+ but an earlier version of Vault Server was contacted, preventing this health check from running.",
}
return []*Result{&ret}, nil
}

req, err := StringList(h.TuneData["passthrough_request_headers"])
if h.TuneData.IsSecretPermissionsError() {
ret := Result{
Status: ResultInsufficientPermissions,
Endpoint: "/sys/mounts/{{mount}}/tune",
Message: "Without this information, this health check is unable to function.",
}

if e.Client.Token() == "" {
ret.Message = "No token available so unable read the tune endpoint for this mount. " + ret.Message
} else {
ret.Message = "This token lacks permission to read the tune endpoint for this mount. " + ret.Message
}

results = append(results, &ret)
return
}

var tuneData map[string]interface{} = nil
if len(h.TuneData.Secret.Data) > 0 {
tuneData = h.TuneData.Secret.Data
}

req, err := StringList(tuneData["passthrough_request_headers"])
if err != nil {
return nil, fmt.Errorf("unable to parse value from server for passthrough_request_headers: %w", err)
}

resp, err := StringList(h.TuneData["allowed_response_headers"])
resp, err := StringList(tuneData["allowed_response_headers"])
if err != nil {
return nil, fmt.Errorf("unable to parse value from server for allowed_response_headers: %w", err)
}

var foundIMS bool = false
foundIMS := false
for _, param := range req {
if strings.EqualFold(param, "If-Modified-Since") {
foundIMS = true
break
}
}

var foundLM bool = false
foundLM := false
for _, param := range resp {
if strings.EqualFold(param, "Last-Modified") {
foundLM = true
Expand Down
40 changes: 30 additions & 10 deletions command/healthcheck/pki_audit_visibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type AuditVisibility struct {
UnsupportedVersion bool

IgnoredParameters map[string]bool
TuneData map[string]interface{}
TuneData *PathFetch
}

func NewAuditVisibilityCheck() Check {
Expand Down Expand Up @@ -100,35 +100,55 @@ func (h *AuditVisibility) LoadConfig(config map[string]interface{}) error {
}

func (h *AuditVisibility) FetchResources(e *Executor) error {
exit, _, data, err := fetchMountTune(e, func() {
pathFetch, err := fetchMountTune(e, func() {
h.UnsupportedVersion = true
})
if exit {
if err != nil {
return err
}

h.TuneData = data

h.TuneData = pathFetch
return nil
}

func (h *AuditVisibility) Evaluate(e *Executor) (results []*Result, err error) {
if h.UnsupportedVersion {
// Shouldn't happen; /certs has been around forever.
ret := Result{
Status: ResultInvalidVersion,
Endpoint: "/{{mount}}/certs",
Message: "This health check requires Vault 1.11+ but an earlier version of Vault Server was contacted, preventing this health check from running.",
Endpoint: "/sys/mounts/{{mount}}/tune",
Message: "This health check requires Vault 1.9+ but an earlier version of Vault Server was contacted, preventing this health check from running.",
}
return []*Result{&ret}, nil
}

if h.TuneData.IsSecretPermissionsError() {
ret := Result{
Status: ResultInsufficientPermissions,
Endpoint: "/sys/mounts/{{mount}}/tune",
Message: "Without this information, this health check is unable to function.",
}

if e.Client.Token() == "" {
ret.Message = "No token available so unable read the tune endpoint for this mount. " + ret.Message
} else {
ret.Message = "This token lacks permission to read the tune endpoint for this mount. " + ret.Message
}

results = append(results, &ret)
return
}

var tuneData map[string]interface{} = nil
if len(h.TuneData.Secret.Data) > 0 {
tuneData = h.TuneData.Secret.Data
}

sourceMap := map[string][]string{
"audit_non_hmac_request_keys": VisibleReqParams,
"audit_non_hmac_response_keys": VisibleRespParams,
}
for source, visibleList := range sourceMap {
actual, err := StringList(h.TuneData[source])
actual, err := StringList(tuneData[source])
if err != nil {
return nil, fmt.Errorf("error parsing %v from server: %v", source, err)
}
Expand Down Expand Up @@ -158,7 +178,7 @@ func (h *AuditVisibility) Evaluate(e *Executor) (results []*Result, err error) {
"audit_non_hmac_response_keys": HiddenRespParams,
}
for source, hiddenList := range sourceMap {
actual, err := StringList(h.TuneData[source])
actual, err := StringList(tuneData[source])
if err != nil {
return nil, fmt.Errorf("error parsing %v from server: %v", source, err)
}
Expand Down
2 changes: 1 addition & 1 deletion command/healthcheck/pki_tidy_last_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (h *TidyLastRun) Evaluate(e *Executor) (results []*Result, err error) {
ret := Result{
Status: ResultInsufficientPermissions,
Endpoint: "/{{mount}}/tidy-status",
Message: "Without this information, this health check is unable tof unction.",
Message: "Without this information, this health check is unable to function.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆

}

if e.Client.Token() == "" {
Expand Down
13 changes: 3 additions & 10 deletions command/healthcheck/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,17 @@ func StringList(source interface{}) ([]string, error) {
return nil, fmt.Errorf("unknown source type for []string coercion: %T", source)
}

func fetchMountTune(e *Executor, versionError func()) (bool, *PathFetch, map[string]interface{}, error) {
func fetchMountTune(e *Executor, versionError func()) (*PathFetch, error) {
tuneRet, err := e.FetchIfNotFetched(logical.ReadOperation, "/sys/mounts/{{mount}}/tune")
if err != nil {
return true, nil, nil, err
return nil, fmt.Errorf("failed to fetch mount tune information: %w", err)
}

if !tuneRet.IsSecretOK() {
if tuneRet.IsUnsupportedPathError() {
versionError()
}

return true, nil, nil, nil
cipherboy marked this conversation as resolved.
Show resolved Hide resolved
}

var data map[string]interface{} = nil
if len(tuneRet.Secret.Data) > 0 {
data = tuneRet.Secret.Data
}

return false, tuneRet, data, nil
return tuneRet, nil
}