-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add violet config + exec env allowlist + helper functions (PR 1/3) #13
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
Merged
Merged
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
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,116 @@ | ||
| package operatorhub | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Environment variables consumed when assembling `violet push`. They are | ||
| // intentionally not part of OperatorConfig: pipelines inject them as secrets | ||
| // and the upgrade CLI must not persist them to disk or echo them back. | ||
| const ( | ||
| EnvVioletRegistryUsername = "VIOLET_REGISTRY_USERNAME" | ||
| EnvVioletRegistryPassword = "VIOLET_REGISTRY_PASSWORD" | ||
| ) | ||
|
|
||
| // violet CLI flag names. Kept private so the consuming code stays expressive | ||
| // (`flagSkipPush` rather than the raw string everywhere). | ||
| const ( | ||
| flagSkipPush = "--skip-push" | ||
| flagTargetCatalogSource = "--target-catalog-source" | ||
| flagUsername = "--username" | ||
| flagPassword = "--password" | ||
| ) | ||
|
|
||
| // BuildPackageURL composes the .tgz URL by the agreed MinIO convention: | ||
| // | ||
| // <prefix>/<name>/<channel>/<name>.latest.ALL.<bundleVersion>.tgz | ||
| // | ||
| // A trailing slash on prefix is tolerated. All four inputs are required; any | ||
| // empty value returns an error so callers fail loudly instead of producing a | ||
| // malformed URL that 404s later. | ||
| func BuildPackageURL(prefix, name, channel, bundleVersion string) (string, error) { | ||
| switch { | ||
| case prefix == "": | ||
| return "", fmt.Errorf("packagePrefix is empty") | ||
| case name == "": | ||
| return "", fmt.Errorf("operator name is empty") | ||
| case channel == "": | ||
| return "", fmt.Errorf("channel is empty (Version.Channel is required when using violet)") | ||
| case bundleVersion == "": | ||
| return "", fmt.Errorf("bundleVersion is empty") | ||
| } | ||
| p := strings.TrimRight(prefix, "/") | ||
| return fmt.Sprintf("%s/%s/%s/%s.latest.ALL.%s.tgz", p, name, channel, name, bundleVersion), nil | ||
| } | ||
|
|
||
| // BuildVioletPushArgs assembles the argv for `violet push <tgz>`. Credentials | ||
| // are read from EnvVioletRegistryUsername / EnvVioletRegistryPassword and | ||
| // injected as --username / --password when non-empty; pushArgs is appended | ||
| // verbatim. The function is a pure transform — it never touches the filesystem | ||
| // or starts a process — so callers can table-test the exact argv shape. | ||
| // | ||
| // The decision of skipPush belongs to the caller (it has already deferenced | ||
| // VioletConfig.SkipPush and applied the "nil == true" default in config), so | ||
| // this function takes a plain bool. | ||
| func BuildVioletPushArgs(tgzPath string, skipPush bool, pushArgs []string) []string { | ||
| args := []string{"push", tgzPath, flagTargetCatalogSource, targetCatalogSource} | ||
| if skipPush { | ||
| args = append(args, flagSkipPush) | ||
| } | ||
| if u := os.Getenv(EnvVioletRegistryUsername); u != "" { | ||
| args = append(args, flagUsername, u) | ||
| } | ||
| if p := os.Getenv(EnvVioletRegistryPassword); p != "" { | ||
| args = append(args, flagPassword, p) | ||
| } | ||
| args = append(args, pushArgs...) | ||
| return args | ||
| } | ||
|
|
||
| // MaskCommand renders the command for logging, replacing the token following | ||
| // --password with `***`. This only protects log output — the credential is | ||
| // still visible to OS-level inspection (e.g. `ps auxe`) once the child process | ||
| // runs. The README must document that risk for shared CI runners. | ||
| func MaskCommand(name string, args []string) string { | ||
| parts := make([]string, 0, len(args)+1) | ||
| parts = append(parts, name) | ||
| for i := 0; i < len(args); i++ { | ||
| if args[i] == flagPassword && i+1 < len(args) { | ||
| parts = append(parts, args[i], "***") | ||
| i++ | ||
| continue | ||
| } | ||
| parts = append(parts, args[i]) | ||
| } | ||
| return strings.Join(parts, " ") | ||
| } | ||
|
|
||
| // VerifySha256 streams the file at filePath and compares its SHA-256 against | ||
| // expected (hex, case-insensitive). An empty expected disables verification — | ||
| // callers can pass Version.ExpectedSha256 directly without nil-checking. The | ||
| // returned error names the path so failures are actionable without extra | ||
| // wrapping at the call site. | ||
| func VerifySha256(filePath, expected string) error { | ||
| if expected == "" { | ||
| return nil | ||
| } | ||
| f, err := os.Open(filePath) | ||
| if err != nil { | ||
| return fmt.Errorf("open %s: %w", filePath, err) | ||
| } | ||
| defer f.Close() | ||
| h := sha256.New() | ||
| if _, err := io.Copy(h, f); err != nil { | ||
| return fmt.Errorf("read %s: %w", filePath, err) | ||
| } | ||
| actual := hex.EncodeToString(h.Sum(nil)) | ||
| if !strings.EqualFold(actual, expected) { | ||
| return fmt.Errorf("sha256 mismatch for %s: expected %s, got %s", filePath, expected, actual) | ||
| } | ||
| return nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
不要硬编码在代码里