Skip to content

Commit

Permalink
Fix Kubespray unsafe conditional (#172)
Browse files Browse the repository at this point in the history
* pkg/utils/file: Add util for appending content to a file

* pkg/cluster/executors/kubespray: Use specific ansible-core in Kubespray requirements

* embed/ansible/kubitect: Update requirements.txt
  • Loading branch information
MusicDin authored Jan 11, 2024
1 parent 4febcce commit da6d9cd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
6 changes: 3 additions & 3 deletions embed/ansible/kubitect/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ansible==5.8.0
ansible-core==2.12.5
ansible==8.5.0
ansible-core==2.16.2
jinja2==3.1.2
netaddr==0.7.19
netaddr==0.9.0
9 changes: 9 additions & 0 deletions pkg/cluster/executors/kubespray/kubespray.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/MusicDin/kubitect/pkg/tools/git"
"github.com/MusicDin/kubitect/pkg/tools/virtualenv"
"github.com/MusicDin/kubitect/pkg/ui"
"github.com/MusicDin/kubitect/pkg/utils/file"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -82,6 +83,14 @@ func (e *kubespray) Init() error {
return err
}

// Patch: There is an issue with unsafe conditionals in
// Kubespray with ansible-core version > 2.14.11.
reqPath := path.Join(dst, "requirements.txt")
reqPatch := []byte("ansible-core==2.14.11")
if err := file.Append(reqPath, reqPatch); err != nil {
return err
}

if err := e.VirtualEnv.Init(); err != nil {
return fmt.Errorf("kubespray exec: initialize virtual environment: %v", err)
}
Expand Down
21 changes: 16 additions & 5 deletions pkg/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package file
import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path"

Expand All @@ -19,7 +18,7 @@ func Exists(path string) bool {

// Read reads file on a given path and returns its content as a string.
func Read(path string) (string, error) {
file, err := ioutil.ReadFile(path)
file, err := os.ReadFile(path)

if err != nil {
return "", fmt.Errorf("read file '%s': %v", path, err)
Expand All @@ -39,11 +38,23 @@ func Copy(srcPath, dstPath string, mode fs.FileMode) error {
return ForceCopy(srcPath, dstPath, mode)
}

// Append appends the provided byte content with an additional new line to the file.
func Append(path string, content []byte) error {
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
defer file.Close()

_, err = file.Write(append(content, '\n'))
return err
}

// ForceCopy reads the file located at the source path and writes it to the
// destination path with the specified permissions. All required subdirectories
// are also created.
func ForceCopy(srcPath, dstPath string, mode fs.FileMode) error {
file, err := ioutil.ReadFile(srcPath)
file, err := os.ReadFile(srcPath)
if err != nil {
return fmt.Errorf("copy file: %v", err)
}
Expand All @@ -63,7 +74,7 @@ func ForceCopy(srcPath, dstPath string, mode fs.FileMode) error {
// ReadYaml reads yaml file on the given path and unmarshals it into the given
// type.
func ReadYaml[T interface{}](path string, typ T) (*T, error) {
yml, err := ioutil.ReadFile(path)
yml, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand All @@ -79,5 +90,5 @@ func WriteYaml(obj interface{}, path string, perm fs.FileMode) error {
return err
}

return ioutil.WriteFile(path, yml, perm)
return os.WriteFile(path, yml, perm)
}
12 changes: 12 additions & 0 deletions pkg/utils/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ func TestForceCopy(t *testing.T) {
assert.Equal(t, "source", out)
}

func TestAppend(t *testing.T) {
src := tmpFile(t, "src.file", "source\n")

require.NoError(t, Append(src, []byte("test")))
require.NoError(t, Append(src, []byte("x=1.2.3")))
require.NoError(t, Append(src, []byte("$ \n \\ ")))

out, err := Read(src)
require.NoError(t, err)
assert.Equal(t, "source\ntest\nx=1.2.3\n$ \n \\ \n", out)
}

func TestWriteYaml(t *testing.T) {
type T struct {
Value int `yaml:"value"`
Expand Down

0 comments on commit da6d9cd

Please sign in to comment.