validateAttestationOutputPath validates and returns the absolute path for attestation output. If path is empty, defaults to a temp directory under /tmp with "vsa-" prefix. If path is provided, validates it's under /tmp or current working directory.
(path string)
| 594 | // If path is empty, defaults to a temp directory under /tmp with "vsa-" prefix. |
| 595 | // If path is provided, validates it's under /tmp or current working directory. |
| 596 | func validateAttestationOutputPath(path string) (string, error) { |
| 597 | // Default to temp directory if not provided |
| 598 | if path == "" { |
| 599 | return "vsa-", nil |
| 600 | } |
| 601 | |
| 602 | // Clean and get absolute path |
| 603 | cleanPath := filepath.Clean(path) |
| 604 | absPath, err := filepath.Abs(cleanPath) |
| 605 | if err != nil { |
| 606 | return "", fmt.Errorf("failed to get absolute path for %s: %w", path, err) |
| 607 | } |
| 608 | |
| 609 | // Get current working directory |
| 610 | cwd, err := os.Getwd() |
| 611 | if err != nil { |
| 612 | return "", fmt.Errorf("failed to get current working directory: %w", err) |
| 613 | } |
| 614 | |
| 615 | // Check if path is under /tmp |
| 616 | tmpDir := filepath.Clean("/tmp") |
| 617 | if strings.HasPrefix(absPath, tmpDir+string(filepath.Separator)) || absPath == tmpDir { |
| 618 | return absPath, nil |
| 619 | } |
| 620 | |
| 621 | // Check if path is under current working directory |
| 622 | if strings.HasPrefix(absPath, cwd+string(filepath.Separator)) || absPath == cwd { |
| 623 | return absPath, nil |
| 624 | } |
| 625 | |
| 626 | return "", fmt.Errorf("attestation output directory must be under /tmp or current working directory, got: %s", absPath) |
| 627 | } |
| 628 | |
| 629 | // imageData is the struct that holds all image validation command data |
| 630 | type imageData struct { |