Skip to content

Commit

Permalink
Fix url when manual install
Browse files Browse the repository at this point in the history
According to docs you can pass a file or an http file to the
manual-install command, but we were not respecting that.

This patch makes it so a local file or an http file is properly working
when doing a manual install.

If its a file, read it directly and pass it to the Scan as a reader

If its an http file, generate a config_url value and pass it to the
reader

Signed-off-by: Itxaka <itxaka@kairos.io>
  • Loading branch information
Itxaka committed Apr 11, 2024
1 parent 15a15a1 commit d95d737
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 82 deletions.
58 changes: 19 additions & 39 deletions internal/agent/install.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package agent

import (
"context"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"strings"
Expand All @@ -28,7 +29,6 @@ import (
qr "github.com/mudler/go-nodepair/qrcode"
"github.com/mudler/go-pluggable"
"github.com/pterm/pterm"
"gopkg.in/yaml.v3"
)

func displayInfo(agentConfig *Config) {
Expand All @@ -54,25 +54,33 @@ func displayInfo(agentConfig *Config) {
}

func ManualInstall(c, sourceImgURL, device string, reboot, poweroff, strictValidations bool) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

configSource, err := prepareConfiguration(ctx, c)
if err != nil {
return err
var cfg io.Reader

// Config can be either a file in the system or an url
// We need to differentiate between the two
// If its a local file, we jsut read it and pass it to the config.Scan
// If its a url, we need to create a configuration with the url and let the config.Scan handle it
if u, err := url.Parse(c); err != nil || u.Scheme == "" {
file, err := os.ReadFile(c)

Check failure

Code scanning / gosec

Potential file inclusion via variable Error

Potential file inclusion via variable
if err != nil {
return err
}
cfg = bytes.NewReader(file)
} else {
cfgUrl := fmt.Sprintf(`config_url: %s`, c)
cfg = strings.NewReader(cfgUrl)
}

cliConf := generateInstallConfForCLIArgs(sourceImgURL)
cliConfManualArgs := generateInstallConfForManualCLIArgs(device, reboot, poweroff)

cc, err := config.Scan(collector.Directories(configSource),
collector.Readers(strings.NewReader(cliConf), strings.NewReader(cliConfManualArgs)),
cc, err := config.Scan(
collector.Readers(cfg, strings.NewReader(cliConf), strings.NewReader(cliConfManualArgs)),
collector.MergeBootLine,
collector.StrictValidation(strictValidations), collector.NoLogs)
if err != nil {
return err
}

return RunInstall(cc)
}

Expand Down Expand Up @@ -324,34 +332,6 @@ func ensureDataSourceReady() {
}
}

func prepareConfiguration(ctx context.Context, source string) (string, error) {
// if the source is not an url it is already a configuration path
if u, err := url.Parse(source); err != nil || u.Scheme == "" {
return source, nil
}

// create a configuration file with the source referenced
f, err := os.CreateTemp(os.TempDir(), "kairos-install-*.yaml")
if err != nil {
return "", err
}

// defer cleanup until after parent is done
go func() {
<-ctx.Done()
_ = os.RemoveAll(f.Name())
}()

cfg := config.Config{
ConfigURL: source,
}
if err = yaml.NewEncoder(f).Encode(cfg); err != nil {
return "", err
}

return f.Name(), nil
}

func generateInstallConfForCLIArgs(sourceImageURL string) string {
if sourceImageURL == "" {
return ""
Expand Down
44 changes: 1 addition & 43 deletions internal/agent/install_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package agent

import (
"context"
"fmt"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"os"
"path/filepath"

"github.com/jaypipes/ghw/pkg/block"
Expand All @@ -13,56 +11,16 @@ import (
"github.com/kairos-io/kairos-agent/v2/pkg/config"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
v1mock "github.com/kairos-io/kairos-agent/v2/tests/mocks"
"github.com/twpayne/go-vfs/v4/vfst"
"gopkg.in/yaml.v3"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/twpayne/go-vfs/v4/vfst"
)

const printOutput = `BYT;
/dev/loop0:50593792s:loopback:512:512:gpt:Loopback device:;`
const partTmpl = `
%d:%ss:%ss:2048s:ext4::type=83;`

var _ = Describe("prepareConfiguration", func() {
path := "/foo/bar"
url := "https://example.com"
ctx, cancel := context.WithCancel(context.Background())

It("returns a file path with no modifications", func() {
source, err := prepareConfiguration(ctx, path)

Expect(err).ToNot(HaveOccurred())
Expect(source).To(Equal(path))
})

It("creates a configuration file containing the given url", func() {
source, err := prepareConfiguration(ctx, url)

Expect(err).ToNot(HaveOccurred())
Expect(source).ToNot(Equal(path))

f, err := os.Open(source)
Expect(err).ToNot(HaveOccurred())

var cfg config.Config
err = yaml.NewDecoder(f).Decode(&cfg)
Expect(err).ToNot(HaveOccurred())

Expect(cfg.ConfigURL).To(Equal(url))
})

It("cleans up the configuration file after context is done", func() {
source, err := prepareConfiguration(ctx, url)
Expect(err).ToNot(HaveOccurred())
cancel()

_, err = os.Stat(source)
Expect(os.IsNotExist(err))
})
})

var _ = Describe("RunInstall", func() {
var options *config.Config
var err error
Expand Down

0 comments on commit d95d737

Please sign in to comment.