-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
687bb19
commit 93d2a36
Showing
15 changed files
with
207 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,45 @@ | ||
package setup | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/canonical/k8s/pkg/snap" | ||
) | ||
|
||
// ExtraNodeConfigFiles writes the file contents to the specified filenames in the snap.ExtraFilesDir directory. | ||
// The files are created with 0400 permissions and owned by root. | ||
// The filenames must not contain any slashes to prevent path traversal. | ||
func ExtraNodeConfigFiles(snap snap.Snap, files map[string]string) error { | ||
for filename, content := range files { | ||
if strings.Contains(filename, "/") { | ||
return fmt.Errorf("file name %q must not contain any slashes (possible path-traversal prevented)", filename) | ||
} | ||
|
||
filePath := path.Join(snap.ServiceExtraConfigDir(), filename) | ||
// Create or truncate the file | ||
file, err := os.Create(filePath) | ||
if err != nil { | ||
return fmt.Errorf("failed to create file %s: %w", filePath, err) | ||
} | ||
defer file.Close() | ||
|
||
// Write the content to the file | ||
_, err = file.WriteString(content) | ||
if err != nil { | ||
return fmt.Errorf("failed to write to file %s: %w", filePath, err) | ||
} | ||
|
||
// Set file owner to root | ||
if err := os.Chown(filePath, snap.UID(), snap.GID()); err != nil { | ||
return fmt.Errorf("failed to change owner of file %s: %w", filePath, err) | ||
} | ||
|
||
if err := os.Chmod(filePath, 0400); err != nil { | ||
return fmt.Errorf("failed to change mode of file %s: %w", filePath, err) | ||
} | ||
} | ||
return nil | ||
} |
This file contains 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,73 @@ | ||
package setup | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/canonical/k8s/pkg/snap/mock" | ||
"github.com/onsi/gomega" | ||
) | ||
|
||
func TestExtraNodeConfigFiles(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
files map[string]string | ||
expectErr bool | ||
errMessage string | ||
}{ | ||
{ | ||
name: "ValidFiles", | ||
files: map[string]string{ | ||
"config1": "content1", | ||
"config2": "content2", | ||
}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "InvalidFilename", | ||
files: map[string]string{ | ||
"invalid/config": "content", | ||
}, | ||
expectErr: true, | ||
errMessage: "file name \"invalid/config\" must not contain any slashes", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := gomega.NewGomegaWithT(t) | ||
|
||
tmpDir := t.TempDir() | ||
snap := &mock.Snap{ | ||
Mock: mock.Mock{ | ||
ServiceExtraConfigDir: tmpDir, | ||
UID: os.Getuid(), | ||
GID: os.Getgid(), | ||
}, | ||
} | ||
|
||
err := ExtraNodeConfigFiles(snap, tt.files) | ||
if tt.expectErr { | ||
g.Expect(err).To(gomega.HaveOccurred()) | ||
g.Expect(err.Error()).To(gomega.ContainSubstring(tt.errMessage)) | ||
} else { | ||
g.Expect(err).ToNot(gomega.HaveOccurred()) | ||
|
||
for filename, content := range tt.files { | ||
filePath := filepath.Join(tmpDir, filename) | ||
|
||
// Verify the file exists | ||
info, err := os.Stat(filePath) | ||
g.Expect(err).ToNot(gomega.HaveOccurred()) | ||
g.Expect(info.Mode().Perm()).To(gomega.Equal(os.FileMode(0400))) | ||
|
||
// Verify the file content | ||
actualContent, err := os.ReadFile(filePath) | ||
g.Expect(err).ToNot(gomega.HaveOccurred()) | ||
g.Expect(string(actualContent)).To(gomega.Equal(content)) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 @@ | ||
# Contains the bootstrap configuration for the session instance of the integration tests. | ||
# The session instance persists over test runs and is used to speed-up the integration tests. | ||
cluster-config: | ||
network: | ||
enabled: true | ||
dns: | ||
enabled: true | ||
ingress: | ||
enabled: true | ||
load-balancer: | ||
enabled: true | ||
local-storage: | ||
enabled: true | ||
gateway: | ||
enabled: true | ||
metrics-server: | ||
enabled: true | ||
extra-node-config-files: | ||
bootstrap-extra-file.yaml: extra-args-test-file-content | ||
extra-node-kube-apiserver-args: | ||
--request-timeout: 2m | ||
extra-node-kube-controller-manager-args: | ||
--leader-elect-retry-period: 3s | ||
extra-node-kube-scheduler-args: | ||
--authorization-webhook-cache-authorized-ttl: 11s | ||
extra-node-kube-proxy-args: | ||
--config-sync-period: 14m | ||
extra-node-kubelet-args: | ||
--authentication-token-webhook-cache-ttl: 3m | ||
extra-node-containerd-args: | ||
--log-level: debug | ||
extra-node-k8s-dqlite-args: | ||
--watch-storage-available-size-interval: 6s |
This file contains 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 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 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