-
Notifications
You must be signed in to change notification settings - Fork 75
restore: add command to restore withdrawn packages #1743
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
sil2100
wants to merge
2
commits into
wolfi-dev:main
Choose a base branch
from
sil2100:add-restore-cmd
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.
+447
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,213 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/chainguard-dev/clog" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type BulkRestoreRequest struct { | ||
| APKs []string `json:"apks"` | ||
| } | ||
|
|
||
| type BulkRestoreResponse struct { | ||
| RestoredAPKs []string `json:"restored_apks"` | ||
| FailedRestores []FailedRestore `json:"failed_restores"` | ||
| } | ||
|
|
||
| type FailedRestore struct { | ||
| Name string `json:"name"` | ||
| ErrorMessage string `json:"error_message"` | ||
| } | ||
|
|
||
| func cmdRestore() *cobra.Command { | ||
| var arch string | ||
| var packagesFile string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "restore [packages...]", | ||
| Short: "Restore withdrawn packages in apk.cgr.dev", | ||
| Example: "restore example-pkg-1.2.3-r4 another-pkg-2.0.0-r1", | ||
| SilenceErrors: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
| log := clog.FromContext(ctx) | ||
|
|
||
| var packages []string | ||
|
|
||
| packages = append(packages, args...) | ||
| if packagesFile != "" { | ||
| filePackages, err := readPackagesFromFile(packagesFile) | ||
| if err != nil { | ||
| return fmt.Errorf("reading packages file: %w", err) | ||
| } | ||
| packages = append(packages, filePackages...) | ||
| } | ||
|
|
||
| if len(packages) == 0 { | ||
| return fmt.Errorf("no packages specified") | ||
| } | ||
|
|
||
| // Validate package format | ||
| for _, pkg := range packages { | ||
| if !isValidPackageFormat(pkg) { | ||
| return fmt.Errorf("invalid package format: %s (expected format: package-name-version, e.g., example-pkg-1.2.3-r4)", pkg) | ||
| } | ||
| } | ||
|
|
||
| // Determine architectures to process, default means both | ||
| var architectures []string | ||
| if arch == "" { | ||
| architectures = []string{"x86_64", "aarch64"} | ||
| } else { | ||
| architectures = []string{arch} | ||
| } | ||
|
|
||
| // Process each architecture | ||
| for _, targetArch := range architectures { | ||
| log.Infof("Processing architecture: %s", targetArch) | ||
| if err := restorePackages(ctx, targetArch, packages); err != nil { | ||
| return fmt.Errorf("failed to restore packages for %s: %w", targetArch, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&arch, "arch", "", "architecture to restore packages for (x86_64 or aarch64, defaults to both if not specified)") | ||
| cmd.Flags().StringVar(&packagesFile, "packages-file", "", "file containing list of packages to restore (one per line, supports comments with #)") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func restorePackages(ctx context.Context, arch string, packages []string) error { | ||
| // Get authentication token from environment | ||
| authToken := os.Getenv("HTTP_AUTH") | ||
| if authToken == "" { | ||
| return fmt.Errorf("HTTP_AUTH environment variable is required") | ||
| } | ||
|
|
||
| if len(packages) == 1 { | ||
| // Single package: use PATCH request | ||
| url := fmt.Sprintf("https://apk.cgr.dev/chainguard/%s/%s", arch, packages[0]) | ||
| return makePatchRequest(ctx, url, authToken) | ||
| } | ||
| // Multiple packages: use POST request (bulk restore) | ||
| url := fmt.Sprintf("https://apk.cgr.dev/chainguard/%s/restore", arch) | ||
| return makePostRequest(ctx, url, authToken, packages) | ||
| } | ||
|
|
||
| func makePatchRequest(ctx context.Context, url, authToken string) error { | ||
| log := clog.FromContext(ctx) | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, "PATCH", url, nil) | ||
| if err != nil { | ||
| return fmt.Errorf("creating PATCH request: %w", err) | ||
| } | ||
|
|
||
| req.SetBasicAuth("user", authToken) | ||
|
|
||
| client := &http.Client{} | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return fmt.Errorf("making PATCH request: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return fmt.Errorf("PATCH request failed with status %d and failed to read response body: %w", resp.StatusCode, err) | ||
| } | ||
| return fmt.Errorf("PATCH request failed with status %d: %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| log.Infof("Successfully restored package via PATCH") | ||
| return nil | ||
| } | ||
|
|
||
| func makePostRequest(ctx context.Context, url, authToken string, packages []string) error { | ||
| log := clog.FromContext(ctx) | ||
|
|
||
| requestBody := BulkRestoreRequest{ | ||
| APKs: packages, | ||
| } | ||
|
|
||
| jsonBody, err := json.Marshal(requestBody) | ||
| if err != nil { | ||
| return fmt.Errorf("marshaling request body: %w", err) | ||
| } | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonBody)) | ||
| if err != nil { | ||
| return fmt.Errorf("creating POST request: %w", err) | ||
| } | ||
|
|
||
| req.Header.Set("Content-Type", "application/json") | ||
| req.SetBasicAuth("user", authToken) | ||
|
|
||
| client := &http.Client{} | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return fmt.Errorf("making POST request: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return fmt.Errorf("reading response body: %w", err) | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return fmt.Errorf("POST request failed with status %d: %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| // Parse and display response | ||
| var response BulkRestoreResponse | ||
| if err := json.Unmarshal(body, &response); err != nil { | ||
| return fmt.Errorf("unmarshaling response: %w", err) | ||
| } | ||
|
|
||
| log.Infof("Restore operation completed:") | ||
| log.Infof("Successfully restored: %v", response.RestoredAPKs) | ||
| if len(response.FailedRestores) > 0 { | ||
| for _, failed := range response.FailedRestores { | ||
| log.Warnf("Failed to restore package %s: %s", failed.Name, failed.ErrorMessage) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // isValidPackageFormat checks if the package name follows the expected format: package-name-version | ||
| // where version typically ends with -r<number> | ||
| func isValidPackageFormat(pkg string) bool { | ||
| // Basic validation: should contain at least one hyphen and end with something like -r<number> | ||
| parts := strings.Split(pkg, "-") | ||
| if len(parts) < 3 { | ||
| return false | ||
| } | ||
|
|
||
| // Check if the last part looks like a revision (starts with 'r' followed by numbers) | ||
| lastPart := parts[len(parts)-1] | ||
| if len(lastPart) < 2 || lastPart[0] != 'r' { | ||
| return false | ||
| } | ||
|
|
||
| for i := 1; i < len(lastPart); i++ { | ||
| if lastPart[i] < '0' || lastPart[i] > '9' { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
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.
We need to be able to restore chainguard (aka wolfi), chainguard-private (enterprise-package) and extra-packages so the first part of the path needs to be driven from a command line option.