mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +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()
|
response := allowedResponse()
|
||||||
if enforce {
|
if enforce {
|
||||||
if result := policy.AggregateCheckResults(a.Evaluator.EvaluatePod(nsPolicy.Enforce, podMetadata, podSpec)); !result.Allowed {
|
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)
|
a.Metrics.RecordEvaluation(metrics.DecisionDeny, nsPolicy.Enforce, metrics.ModeEnforce, attrs)
|
||||||
} else {
|
} else {
|
||||||
a.Metrics.RecordEvaluation(metrics.DecisionAllow, nsPolicy.Enforce, metrics.ModeEnforce, attrs)
|
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
|
// 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 {
|
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)
|
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 {
|
if result := policy.AggregateCheckResults(a.Evaluator.EvaluatePod(nsPolicy.Warn, podMetadata, podSpec)); !result.Allowed {
|
||||||
// TODO: Craft a better user-facing warning message
|
// TODO: Craft a better user-facing warning message
|
||||||
response.Warnings = append(response.Warnings, fmt.Sprintf(
|
response.Warnings = append(response.Warnings, fmt.Sprintf(
|
||||||
"would violate %q version of %q PodSecurity profile: %s",
|
"Would violate PodSecurity %q: %s",
|
||||||
nsPolicy.Warn.Version.String(),
|
nsPolicy.Warn.String(),
|
||||||
nsPolicy.Warn.Level,
|
|
||||||
result.ForbiddenDetail(),
|
result.ForbiddenDetail(),
|
||||||
))
|
))
|
||||||
a.Metrics.RecordEvaluation(metrics.DecisionDeny, nsPolicy.Warn, metrics.ModeWarn, attrs)
|
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",
|
desc: "bad deploy creates produce correct user-visible warnings and correct auditAnnotations",
|
||||||
newObject: &badDeploy,
|
newObject: &badDeploy,
|
||||||
gvr: schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"},
|
gvr: schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"},
|
||||||
expectAuditAnnotations: map[string]string{"audit": "forbidden sysctls (unknown)"},
|
expectAuditAnnotations: map[string]string{"audit": "Would violate PodSecurity \"baseline:latest\": forbidden sysctls (unknown)"},
|
||||||
expectWarnings: []string{"would violate \"latest\" version of \"baseline\" PodSecurity profile: 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",
|
desc: "bad spec updates don't block on enforce failures and returns correct information",
|
||||||
newObject: &badDeploy,
|
newObject: &badDeploy,
|
||||||
oldObject: &goodDeploy,
|
oldObject: &goodDeploy,
|
||||||
gvr: schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"},
|
gvr: schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"},
|
||||||
expectAuditAnnotations: map[string]string{"audit": "forbidden sysctls (unknown)"},
|
expectAuditAnnotations: map[string]string{"audit": "Would violate PodSecurity \"baseline:latest\": forbidden sysctls (unknown)"},
|
||||||
expectWarnings: []string{"would violate \"latest\" version of \"baseline\" PodSecurity profile: forbidden sysctls (unknown)"},
|
expectWarnings: []string{"Would violate PodSecurity \"baseline:latest\": forbidden sysctls (unknown)"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user