|
|
@ -22,9 +22,6 @@ import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func checkPermsStderr() (string, error) {
|
|
|
|
func checkPermsStderr() (string, error) {
|
|
|
@ -46,37 +43,3 @@ func checkPermsStderr() (string, error) {
|
|
|
|
io.Copy(&text, r)
|
|
|
|
io.Copy(&text, r)
|
|
|
|
return text.String(), nil
|
|
|
|
return text.String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestCheckPerms(t *testing.T) {
|
|
|
|
|
|
|
|
tdir := t.TempDir()
|
|
|
|
|
|
|
|
tfile := filepath.Join(tdir, "testconfig")
|
|
|
|
|
|
|
|
fh, err := os.OpenFile(tfile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0440)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Errorf("Failed to create temp file: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tconfig := settings.KubeConfig
|
|
|
|
|
|
|
|
settings.KubeConfig = tfile
|
|
|
|
|
|
|
|
defer func() { settings.KubeConfig = tconfig }()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text, err := checkPermsStderr()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("could not read from stderr: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
expectPrefix := "WARNING: Kubernetes configuration file is group-readable. This is insecure. Location:"
|
|
|
|
|
|
|
|
if !strings.HasPrefix(text, expectPrefix) {
|
|
|
|
|
|
|
|
t.Errorf("Expected to get a warning for group perms. Got %q", text)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := fh.Chmod(0404); err != nil {
|
|
|
|
|
|
|
|
t.Errorf("Could not change mode on file: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
text, err = checkPermsStderr()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatalf("could not read from stderr: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
expectPrefix = "WARNING: Kubernetes configuration file is world-readable. This is insecure. Location:"
|
|
|
|
|
|
|
|
if !strings.HasPrefix(text, expectPrefix) {
|
|
|
|
|
|
|
|
t.Errorf("Expected to get a warning for world perms. Got %q", text)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|