mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #105314 from calvin0327/issue-podsecurity-errormessage
[PodSecurity]Add context to failure message
This commit is contained in:
commit
a6ffd29e5f
@ -414,7 +414,11 @@ func (a *Admission) EvaluatePod(ctx context.Context, nsPolicy api.Policy, nsPoli
|
||||
response := allowedResponse()
|
||||
if enforce {
|
||||
if result := policy.AggregateCheckResults(a.Evaluator.EvaluatePod(nsPolicy.Enforce, podMetadata, podSpec)); !result.Allowed {
|
||||
response = forbiddenResponse(result.ForbiddenDetail())
|
||||
response = forbiddenResponse(fmt.Sprintf(
|
||||
"Pod violates PodSecurity %q: %s",
|
||||
nsPolicy.Enforce.String(),
|
||||
result.ForbiddenDetail(),
|
||||
))
|
||||
a.Metrics.RecordEvaluation(metrics.DecisionDeny, nsPolicy.Enforce, metrics.ModeEnforce, attrs)
|
||||
} else {
|
||||
a.Metrics.RecordEvaluation(metrics.DecisionAllow, nsPolicy.Enforce, metrics.ModeEnforce, attrs)
|
||||
@ -423,7 +427,11 @@ func (a *Admission) EvaluatePod(ctx context.Context, nsPolicy api.Policy, nsPoli
|
||||
|
||||
// TODO: reuse previous evaluation if audit level+version is the same as enforce level+version
|
||||
if result := policy.AggregateCheckResults(a.Evaluator.EvaluatePod(nsPolicy.Audit, podMetadata, podSpec)); !result.Allowed {
|
||||
auditAnnotations["audit"] = result.ForbiddenDetail()
|
||||
auditAnnotations["audit"] = fmt.Sprintf(
|
||||
"Would violate PodSecurity %q: %s",
|
||||
nsPolicy.Audit.String(),
|
||||
result.ForbiddenDetail(),
|
||||
)
|
||||
a.Metrics.RecordEvaluation(metrics.DecisionDeny, nsPolicy.Audit, metrics.ModeAudit, attrs)
|
||||
}
|
||||
|
||||
@ -433,9 +441,8 @@ func (a *Admission) EvaluatePod(ctx context.Context, nsPolicy api.Policy, nsPoli
|
||||
if result := policy.AggregateCheckResults(a.Evaluator.EvaluatePod(nsPolicy.Warn, podMetadata, podSpec)); !result.Allowed {
|
||||
// TODO: Craft a better user-facing warning message
|
||||
response.Warnings = append(response.Warnings, fmt.Sprintf(
|
||||
"would violate %q version of %q PodSecurity profile: %s",
|
||||
nsPolicy.Warn.Version.String(),
|
||||
nsPolicy.Warn.Level,
|
||||
"Would violate PodSecurity %q: %s",
|
||||
nsPolicy.Warn.String(),
|
||||
result.ForbiddenDetail(),
|
||||
))
|
||||
a.Metrics.RecordEvaluation(metrics.DecisionDeny, nsPolicy.Warn, metrics.ModeWarn, attrs)
|
||||
|
@ -565,16 +565,16 @@ func TestValidatePodController(t *testing.T) {
|
||||
desc: "bad deploy creates produce correct user-visible warnings and correct auditAnnotations",
|
||||
newObject: &badDeploy,
|
||||
gvr: schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"},
|
||||
expectAuditAnnotations: map[string]string{"audit": "forbidden sysctls (unknown)"},
|
||||
expectWarnings: []string{"would violate \"latest\" version of \"baseline\" PodSecurity profile: forbidden sysctls (unknown)"},
|
||||
expectAuditAnnotations: map[string]string{"audit": "Would violate PodSecurity \"baseline:latest\": forbidden sysctls (unknown)"},
|
||||
expectWarnings: []string{"Would violate PodSecurity \"baseline:latest\": forbidden sysctls (unknown)"},
|
||||
},
|
||||
{
|
||||
desc: "bad spec updates don't block on enforce failures and returns correct information",
|
||||
newObject: &badDeploy,
|
||||
oldObject: &goodDeploy,
|
||||
gvr: schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"},
|
||||
expectAuditAnnotations: map[string]string{"audit": "forbidden sysctls (unknown)"},
|
||||
expectWarnings: []string{"would violate \"latest\" version of \"baseline\" PodSecurity profile: forbidden sysctls (unknown)"},
|
||||
expectAuditAnnotations: map[string]string{"audit": "Would violate PodSecurity \"baseline:latest\": forbidden sysctls (unknown)"},
|
||||
expectWarnings: []string{"Would violate PodSecurity \"baseline:latest\": forbidden sysctls (unknown)"},
|
||||
},
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user