-
Notifications
You must be signed in to change notification settings - Fork 86
immutable gateway: skip ..-prefixed dirs in artifact walker
#2640
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8cd73a4
fix: skip ..-prefixed dirs in artifact walker
tricktron 9dfe2e1
refactor: wire collectArtifacts into LoadArtifacts
tricktron 02c5c65
test: add edge case tests for collectArtifacts
tricktron 35f8b86
fix: follow directory symlinks in ConfigMap nested key layout
tricktron 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
141 changes: 141 additions & 0 deletions
141
gateway/gateway-controller/pkg/immutable/loader_test.go
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,141 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package immutable | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| const petstoreArtifact = `apiVersion: gateway.api-platform.wso2.com/v1 | ||
| kind: RestApi | ||
| metadata: | ||
| name: petstore-v1 | ||
| spec: | ||
| displayName: Petstore | ||
| version: v1.0 | ||
| context: /petstore/$version | ||
| upstream: | ||
| main: | ||
| url: https://petstore3.swagger.io/api/v3 | ||
| operations: | ||
| - method: GET | ||
| path: /pet/{petId} | ||
| ` | ||
|
|
||
| func TestCollectArtifacts_ConfigMapMountYieldsFileOnce(t *testing.T) { | ||
| // Kubernetes ConfigMap mount layout: | ||
| // <dir>/ | ||
| // ..2026_07_13_.../petstore.yaml (real file in timestamped dir) | ||
| // ..data -> ..2026_07_13_.../ (symlink to current revision) | ||
| // petstore.yaml -> ..data/petstore.yaml (per-key symlink) | ||
| dir := t.TempDir() | ||
|
|
||
| tsDir := filepath.Join(dir, "..2026_07_13_10_00_00.123456") | ||
| if err := os.Mkdir(tsDir, 0o700); err != nil { | ||
| t.Fatalf("setup: mkdir %s: %v", tsDir, err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(tsDir, "petstore.yaml"), []byte(petstoreArtifact), 0o600); err != nil { | ||
| t.Fatalf("setup: write real file: %v", err) | ||
| } | ||
|
|
||
| dotData := filepath.Join(dir, "..data") | ||
| if err := os.Symlink(tsDir, dotData); err != nil { | ||
| t.Fatalf("setup: symlink ..data: %v", err) | ||
| } | ||
|
|
||
| topLevel := filepath.Join(dir, "petstore.yaml") | ||
| if err := os.Symlink(filepath.Join(dotData, "petstore.yaml"), topLevel); err != nil { | ||
| t.Fatalf("setup: symlink petstore.yaml: %v", err) | ||
| } | ||
|
|
||
| paths, err := collectArtifacts(dir) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("collectArtifacts: unexpected error: %v", err) | ||
| } | ||
| if len(paths) != 1 { | ||
| t.Fatalf("got %d path(s) %v; want exactly 1", len(paths), paths) | ||
| } | ||
| if paths[0] != topLevel { | ||
| t.Errorf("got %q; want top-level symlink %q", paths[0], topLevel) | ||
| } | ||
| } | ||
|
|
||
| func TestCollectArtifacts_ConfigMapNestedDirSymlinkYieldsFileOnce(t *testing.T) { | ||
| // Kubernetes ConfigMap mount layout with a nested key (rest-apis/petstore.yaml): | ||
| // <dir>/ | ||
| // ..2026_07_13_.../rest-apis/petstore.yaml (real file in timestamped dir) | ||
| // ..data -> ..2026_07_13_.../ (symlink to current revision) | ||
| // rest-apis -> ..data/rest-apis (top-level symlink to DIRECTORY) | ||
| dir := t.TempDir() | ||
|
|
||
| tsDir := filepath.Join(dir, "..2026_07_13_10_00_00.123456") | ||
| if err := os.MkdirAll(filepath.Join(tsDir, "rest-apis"), 0o700); err != nil { | ||
| t.Fatalf("setup: mkdirall: %v", err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(tsDir, "rest-apis", "petstore.yaml"), []byte(petstoreArtifact), 0o600); err != nil { | ||
| t.Fatalf("setup: write real file: %v", err) | ||
| } | ||
|
|
||
| dotData := filepath.Join(dir, "..data") | ||
| if err := os.Symlink(tsDir, dotData); err != nil { | ||
| t.Fatalf("setup: symlink ..data: %v", err) | ||
| } | ||
|
|
||
| // Top-level entry is a symlink to a directory, not a file. | ||
| if err := os.Symlink(filepath.Join(dotData, "rest-apis"), filepath.Join(dir, "rest-apis")); err != nil { | ||
| t.Fatalf("setup: symlink rest-apis: %v", err) | ||
| } | ||
|
|
||
| want := filepath.Join(dir, "rest-apis", "petstore.yaml") | ||
| paths, err := collectArtifacts(dir) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("collectArtifacts: unexpected error: %v", err) | ||
| } | ||
| if len(paths) != 1 { | ||
| t.Fatalf("got %d path(s) %v; want exactly 1", len(paths), paths) | ||
| } | ||
| if paths[0] != want { | ||
| t.Errorf("got %q; want %q", paths[0], want) | ||
| } | ||
| } | ||
|
|
||
| func TestCollectArtifacts_SkipsDotPrefixedEntries(t *testing.T) { | ||
| dir := t.TempDir() | ||
| hidden := filepath.Join(dir, ".hidden") | ||
| if err := os.Mkdir(hidden, 0o700); err != nil { | ||
| t.Fatalf("setup: %v", err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(hidden, "petstore.yaml"), []byte(petstoreArtifact), 0o600); err != nil { | ||
| t.Fatalf("setup: %v", err) | ||
| } | ||
|
|
||
| paths, err := collectArtifacts(dir) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("collectArtifacts: %v", err) | ||
| } | ||
| if len(paths) != 0 { | ||
| t.Fatalf("got %d path(s) %v; want 0 (dot-prefixed entries should be skipped)", len(paths), paths) | ||
| } | ||
| } | ||
|
|
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.