-
Notifications
You must be signed in to change notification settings - Fork 102
NO-ISSUE: add per-scenario PSA compliance audit #1374
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
Open
grokspawn
wants to merge
1
commit into
openshift:main
Choose a base branch
from
grokspawn:psa-audit-framework
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−2
Open
Changes from all commits
Commits
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
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,131 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| g "github.com/onsi/ginkgo/v2" | ||
| e2e "k8s.io/kubernetes/test/e2e/framework" | ||
| ) | ||
|
|
||
| // AuditAndTeardownProject audits the current test namespace before removing | ||
| // the resources created by the test. PSA_CHECK_BIN is supplied by the release | ||
| // step-registry integration. | ||
| func (c *CLI) AuditAndTeardownProject() { | ||
| err := c.auditPSA() | ||
| c.TeardownProject() | ||
| if err != nil { | ||
| g.Fail(err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func (c *CLI) auditPSA() error { | ||
| bin := strings.TrimSpace(os.Getenv("PSA_CHECK_BIN")) | ||
| if bin == "" { | ||
| return nil | ||
| } | ||
|
|
||
| namespace := c.Namespace() | ||
| if namespace == "" { | ||
| e2e.Logf("Skipping PSA audit because the test has no namespace") | ||
| return nil | ||
| } | ||
|
|
||
| if _, err := exec.LookPath(bin); err != nil { | ||
| if psaCheckRequired() { | ||
| return fmt.Errorf("PSA checker %q is unavailable: %w", bin, err) | ||
| } | ||
| e2e.Logf("Skipping PSA audit because checker %q is unavailable: %v", bin, err) | ||
| return nil | ||
| } | ||
|
|
||
| cmd := exec.CommandContext(context.Background(), bin, | ||
| "psa-check", | ||
| "--namespace", namespace, | ||
| "--level", "restricted", | ||
| "--output", "json", | ||
| ) | ||
| configPath := c.adminConfigPath | ||
| if c.tempAdmConfPath != "" { | ||
| configPath = c.tempAdmConfPath | ||
| } | ||
| cmd.Env = append(os.Environ(), fmt.Sprintf("KUBECONFIG=%s", configPath)) | ||
|
|
||
| var stdout, stderr bytes.Buffer | ||
| cmd.Stdout = &stdout | ||
| cmd.Stderr = &stderr | ||
| err := cmd.Run() | ||
|
|
||
| if artifactPath := psaArtifactPath(namespace); artifactPath != "" { | ||
| if writeErr := os.WriteFile(artifactPath, stdout.Bytes(), 0o600); writeErr != nil { | ||
| e2e.Logf("Failed to write PSA audit result: %v", writeErr) | ||
| } | ||
| if stderr.Len() > 0 { | ||
| stderrPath := strings.TrimSuffix(artifactPath, filepath.Ext(artifactPath)) + ".stderr" | ||
| if writeErr := os.WriteFile(stderrPath, stderr.Bytes(), 0o600); writeErr != nil { | ||
| e2e.Logf("Failed to write PSA audit diagnostics: %v", writeErr) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| message := strings.TrimSpace(stdout.String()) | ||
| if stderrMessage := strings.TrimSpace(stderr.String()); stderrMessage != "" { | ||
| if message != "" { | ||
| message += "; " | ||
| } | ||
| message += stderrMessage | ||
| } | ||
| if message == "" { | ||
| message = "no diagnostic output" | ||
| } | ||
| return fmt.Errorf("PSA audit failed for namespace %q: %w: %s", namespace, err, message) | ||
| } | ||
|
|
||
| func psaCheckRequired() bool { | ||
| required, err := strconv.ParseBool(os.Getenv("PSA_CHECK_REQUIRED")) | ||
| return err == nil && required | ||
| } | ||
|
|
||
| func psaArtifactPath(namespace string) string { | ||
| basePath := strings.TrimSpace(os.Getenv("ARTIFACT_DIR")) | ||
| if basePath == "" { | ||
| return "" | ||
| } | ||
|
|
||
| dir := filepath.Join(basePath, "psa", sanitizePSAPath(namespace)) | ||
| if err := os.MkdirAll(dir, 0o755); err != nil { | ||
| e2e.Logf("Failed to create PSA audit artifact directory %q: %v", dir, err) | ||
| return "" | ||
| } | ||
| return filepath.Join(dir, "psa.json") | ||
| } | ||
|
|
||
| func sanitizePSAPath(value string) string { | ||
| var result strings.Builder | ||
| for _, r := range strings.TrimSpace(value) { | ||
| switch { | ||
| case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '-', r == '_', r == '.': | ||
| result.WriteRune(r) | ||
| default: | ||
| result.WriteByte('-') | ||
| } | ||
| } | ||
| if result.Len() == 0 { | ||
| return "unknown" | ||
| } | ||
| safe := strings.Trim(result.String(), "-") | ||
| if safe == "" { | ||
| return "unknown" | ||
| } | ||
| return safe | ||
| } | ||
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,23 @@ | ||
| package util | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestSanitizePSAPath(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want string | ||
| }{ | ||
| {name: "namespace", input: "ns/example-123", want: "ns-example-123"}, | ||
| {name: "empty", input: "", want: "unknown"}, | ||
| {name: "safe characters", input: "namespace_1.test", want: "namespace_1.test"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := sanitizePSAPath(tt.input); got != tt.want { | ||
| t.Fatalf("sanitizePSAPath(%q) = %q, want %q", tt.input, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Bound the PSA checker execution.
Line 48 passes
context.Background()toexec.CommandContext. That context cannot expire or be canceled. If the checker blocks,cmd.Run()blocks theAfterEachcallback andTeardownProjectdoes not start. Use a context with a deadline and cancel it afterRunreturns.As per path instructions, use “context.Context for cancellation and timeouts”.
🤖 Prompt for AI Agents
Source: Path instructions