-
Notifications
You must be signed in to change notification settings - Fork 112
Add native airgap bundle support #1080
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
kke
wants to merge
11
commits into
main
Choose a base branch
from
airgap-mvp
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.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c4e0cca
Add native airgap bundle support
kke e5be617
fix: tighten airgap checksum handling
kke 2f49f22
fix: share bundle download handling
kke 7c66218
fix: allow defaulted airgap version
kke 9ad72f4
test: harden airgap phase ordering check
kke b379398
fix: validate airgap artifact paths
kke 5490819
fix: tighten airgap artifact name validation
kke 1a78153
perf: memoize airgap checksum verification
kke 3329add
fix: validate local airgap bundle directories
kke 9c179e3
fix: redact airgap download URLs
kke e9748d4
fix: redact downloader request errors
kke 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package action | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/k0sproject/k0sctl/phase" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestApplyIncludesAirgapBeforeWorkerPhases(t *testing.T) { | ||
| apply := NewApply(ApplyOptions{}) | ||
| airgapPhase := (&phase.AirgapBundles{}).Title() | ||
| initializeK0s := (&phase.InitializeK0s{}).Title() | ||
| installControllers := (&phase.InstallControllers{}).Title() | ||
| installWorkers := (&phase.InstallWorkers{}).Title() | ||
| upgradeWorkers := (&phase.UpgradeWorkers{}).Title() | ||
|
|
||
| airgapIndex := apply.Phases.Index(airgapPhase) | ||
| initializeIndex := apply.Phases.Index(initializeK0s) | ||
| installControllersIndex := apply.Phases.Index(installControllers) | ||
| installWorkersIndex := apply.Phases.Index(installWorkers) | ||
| upgradeWorkersIndex := apply.Phases.Index(upgradeWorkers) | ||
|
|
||
| require.NotEqual(t, -1, airgapIndex) | ||
| require.NotEqual(t, -1, initializeIndex) | ||
| require.NotEqual(t, -1, installControllersIndex) | ||
| require.NotEqual(t, -1, installWorkersIndex) | ||
| require.NotEqual(t, -1, upgradeWorkersIndex) | ||
| require.Less(t, airgapIndex, initializeIndex) | ||
| require.Less(t, airgapIndex, installControllersIndex) | ||
| require.Less(t, airgapIndex, installWorkersIndex) | ||
| require.Less(t, airgapIndex, upgradeWorkersIndex) | ||
| } | ||
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,112 @@ | ||
| package download | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| var httpClient = &http.Client{ | ||
| Timeout: 10 * time.Minute, | ||
| Transport: &http.Transport{ | ||
| Proxy: http.ProxyFromEnvironment, | ||
| DialContext: (&net.Dialer{ | ||
| Timeout: 30 * time.Second, | ||
| KeepAlive: 30 * time.Second, | ||
| }).DialContext, | ||
| ResponseHeaderTimeout: 30 * time.Second, | ||
| TLSHandshakeTimeout: 10 * time.Second, | ||
| ExpectContinueTimeout: time.Second, | ||
| }, | ||
| } | ||
|
|
||
| // ToFile downloads rawURL to dest using a temporary file in the destination directory. | ||
| func ToFile(ctx context.Context, rawURL, dest string) (retErr error) { | ||
| dir := filepath.Dir(dest) | ||
| if err := os.MkdirAll(dir, 0o755); err != nil { | ||
| return err | ||
| } | ||
| tmpFile, err := os.CreateTemp(dir, filepath.Base(dest)+".tmp-") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| tmpPath := tmpFile.Name() | ||
| defer func() { | ||
| if tmpFile != nil { | ||
| if err := tmpFile.Close(); err != nil && retErr == nil { | ||
| retErr = err | ||
| } | ||
| } | ||
| if retErr != nil { | ||
| if err := os.Remove(tmpPath); err != nil && !os.IsNotExist(err) { | ||
| log.Warnf("failed to remove partial download at %s: %v", tmpPath, err) | ||
| } | ||
| } | ||
| }() | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) | ||
| if err != nil { | ||
| return fmt.Errorf("create download request for %s: %w", RedactedURL(rawURL), redactedURLError(rawURL, err)) | ||
| } | ||
| resp, err := httpClient.Do(req) | ||
| if err != nil { | ||
| return fmt.Errorf("download %s: %w", RedactedURL(rawURL), redactedURLError(rawURL, err)) | ||
| } | ||
|
kke marked this conversation as resolved.
|
||
| defer func() { | ||
| if err := resp.Body.Close(); err != nil && retErr == nil { | ||
| retErr = err | ||
| } | ||
| }() | ||
| if resp.StatusCode != http.StatusOK { | ||
| return fmt.Errorf("unexpected http status %s from %s", resp.Status, RedactedURL(rawURL)) | ||
| } | ||
|
kke marked this conversation as resolved.
|
||
| if _, err := io.Copy(tmpFile, resp.Body); err != nil { | ||
| return err | ||
| } | ||
| if err := tmpFile.Sync(); err != nil { | ||
| return err | ||
| } | ||
| if err := tmpFile.Close(); err != nil { | ||
| tmpFile = nil | ||
| return err | ||
| } | ||
| tmpFile = nil | ||
| // os.Rename is atomic on Unix (replaces dest if it exists), so concurrent runs are safe. | ||
| // On Windows it fails if dest already exists; two simultaneous k0sctl processes targeting | ||
| // the same destination could race here. We intentionally propagate that error rather than | ||
| // silently accepting whatever file is at dest, which would be a TOCTOU risk. | ||
| if err := os.Rename(tmpPath, dest); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // RedactedURL returns a URL string suitable for error messages. | ||
| func RedactedURL(rawURL string) string { | ||
| parsed, err := url.Parse(rawURL) | ||
| if err != nil { | ||
| return "<redacted>" | ||
| } | ||
| parsed.User = nil | ||
| parsed.RawQuery = "" | ||
| parsed.ForceQuery = false | ||
| parsed.Fragment = "" | ||
| return parsed.String() | ||
| } | ||
|
|
||
| func redactedURLError(rawURL string, err error) error { | ||
| var urlErr *url.Error | ||
| if errors.As(err, &urlErr) && urlErr.Err != nil { | ||
| return urlErr.Err | ||
| } | ||
| return errors.New(strings.ReplaceAll(err.Error(), rawURL, RedactedURL(rawURL))) | ||
| } | ||
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,110 @@ | ||
| package download | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestToFileDownloadsToDestination(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| _, err := fmt.Fprint(w, "downloaded") | ||
| require.NoError(t, err) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| dest := filepath.Join(t.TempDir(), "bundle") | ||
| require.NoError(t, ToFile(context.Background(), server.URL+"/bundle", dest)) | ||
|
|
||
| content, err := os.ReadFile(dest) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "downloaded", string(content)) | ||
| require.Empty(t, tempFiles(t, dest)) | ||
| } | ||
|
|
||
| func TestToFileRedactsURLOnHTTPStatusError(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| http.Error(w, "nope", http.StatusUnauthorized) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| dest := filepath.Join(t.TempDir(), "bundle") | ||
| err := ToFile(context.Background(), authenticatedURL(server.URL)+"/bundle?token=secret", dest) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "unexpected http status 401 Unauthorized") | ||
| require.NotContains(t, err.Error(), "token=secret") | ||
| require.NotContains(t, err.Error(), "user:pass") | ||
| require.NoFileExists(t, dest) | ||
| require.Empty(t, tempFiles(t, dest)) | ||
| } | ||
|
|
||
| func TestToFileRedactsURLOnRequestError(t *testing.T) { | ||
| dest := filepath.Join(t.TempDir(), "bundle") | ||
| err := ToFile(context.Background(), "http://user:pass@example.invalid/\n?token=secret", dest) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "create download request for <redacted>") | ||
| require.NotContains(t, err.Error(), "token=secret") | ||
| require.NotContains(t, err.Error(), "user:pass") | ||
| require.NoFileExists(t, dest) | ||
| require.Empty(t, tempFiles(t, dest)) | ||
| } | ||
|
|
||
| func TestToFileRedactsURLOnTransportError(t *testing.T) { | ||
| dest := filepath.Join(t.TempDir(), "bundle") | ||
| err := ToFile(context.Background(), "http://user:pass@127.0.0.1:1/bundle?token=secret", dest) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "download http://127.0.0.1:1/bundle") | ||
| require.NotContains(t, err.Error(), "token=secret") | ||
| require.NotContains(t, err.Error(), "user:pass") | ||
| require.NoFileExists(t, dest) | ||
| require.Empty(t, tempFiles(t, dest)) | ||
| } | ||
|
|
||
| func TestToFileRemovesPartialDownloadOnCopyError(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.Header().Set("Content-Length", "10") | ||
| _, err := fmt.Fprint(w, "part") | ||
| require.NoError(t, err) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| dest := filepath.Join(t.TempDir(), "bundle") | ||
| err := ToFile(context.Background(), server.URL+"/bundle", dest) | ||
| require.Error(t, err) | ||
| require.NoFileExists(t, dest) | ||
| require.Empty(t, tempFiles(t, dest)) | ||
| } | ||
|
|
||
| func TestToFileRemovesTempFileOnCanceledContext(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() | ||
|
|
||
| dest := filepath.Join(t.TempDir(), "bundle") | ||
| err := ToFile(ctx, "http://127.0.0.1/bundle", dest) | ||
| require.Error(t, err) | ||
| require.NoFileExists(t, dest) | ||
| require.Empty(t, tempFiles(t, dest)) | ||
| } | ||
|
|
||
| func TestRedactedURLRemovesCredentialsAndQuery(t *testing.T) { | ||
| got := RedactedURL("https://user:pass@example.invalid/path/to/bundle?token=secret#fragment") | ||
| require.Equal(t, "https://example.invalid/path/to/bundle", got) | ||
| } | ||
|
|
||
| func authenticatedURL(rawURL string) string { | ||
| return strings.Replace(rawURL, "http://", "http://user:pass@", 1) | ||
| } | ||
|
|
||
| func tempFiles(t *testing.T, dest string) []string { | ||
| t.Helper() | ||
| matches, err := filepath.Glob(filepath.Join(filepath.Dir(dest), filepath.Base(dest)+".tmp-*")) | ||
| require.NoError(t, err) | ||
| return matches | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.