Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ var bootstrapCmd = &cobra.Command{
if err := templates.ValidateK3SVersion(k3sVersion); err != nil {
return err
}
registryMirror, _ := cmd.Flags().GetString("registry-mirror")
registryCredFile, _ := cmd.Flags().GetString("registry-credential-file")
// Fail on a bad mirror or an unusable credential file before the platform is asked for anything.
if err := checkRegistryFlags(registryMirror, registryCredFile); err != nil {
return err
}
nodeOpts, err := nodeOptionsFromFlags(cmd)
if err != nil {
return err
Expand Down Expand Up @@ -66,6 +72,11 @@ var bootstrapCmd = &cobra.Command{
return fmt.Errorf("--force only applies together with --k3s-upgrade")
}

registry, err := registryFromFlags(registryMirror, registryCredFile, bl.registry)
if err != nil {
return err
}

data := templates.TemplateData{
Domain: domain,
Namespace: namespace,
Expand All @@ -81,6 +92,9 @@ var bootstrapCmd = &cobra.Command{
K3SVersionExplicit: cmd.Flags().Changed("k3s-version"),
K3SUpgrade: k3sUpgrade,
K3SForce: force,
RegistryMirror: registry.Mirror,
RegistryUsername: registry.Username,
RegistryPassword: registry.Password,
}

if err := templates.ValidateTemplateData(data); err != nil {
Expand All @@ -89,14 +103,7 @@ var bootstrapCmd = &cobra.Command{

serverData, agentData := nodes.apply(data)

generate := func(tmpl *template.Template, filename string, data templates.TemplateData) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
return tmpl.Execute(f, data)
}
generate := writeBootstrapScript

agentFile := fmt.Sprintf("bootstrap_agent_%s.sh", deviceName)
serverFile := fmt.Sprintf("bootstrap_server_%s.sh", deviceName)
Expand All @@ -117,6 +124,13 @@ var bootstrapCmd = &cobra.Command{
fmt.Printf("- %s\n", serverFile)
fmt.Printf("- %s\n", dockerServerFile)
fmt.Print(nodes.describe(agentFile, serverFile))
switch registry.Mirror {
case "":
case templates.RegistryMirrorNone:
fmt.Printf("%s and %s remove a registries.yaml that an earlier script wrote (registry mirror none)\n", agentFile, serverFile)
default:
fmt.Printf("%s and %s configure the registry mirror %s (/etc/rancher/k3s/registries.yaml); they contain its credential, keep them private\n", agentFile, serverFile, registry.Mirror)
}

skipUpload, _ := cmd.Flags().GetBool("skip-upload")
if !skipUpload {
Expand All @@ -130,6 +144,8 @@ var bootstrapCmd = &cobra.Command{
// bootstrapLogin is the decoded login of a device that the bootstrap scripts install.
type bootstrapLogin struct {
rsaPrivate, rsaPublic, username, password, publicSSHKey string
// registry is the registry mirror the platform offers the device, nil when it sends none.
registry *api.FleetRegistry
}

// loadBootstrapLogin asks the platform for the login of a device and decodes it. The platform gives
Expand Down Expand Up @@ -170,6 +186,7 @@ func loadBootstrapLogin(ctx context.Context, fl *api.Fleet, namespace, deviceNam
if bl.publicSSHKey, err = decode("publicsshkey", c.PublicSSHKey); err != nil {
return nil, err
}
bl.registry = c.Registry
return &bl, nil
}

Expand All @@ -191,5 +208,24 @@ func init() {
"with --k3s-upgrade: allow an upgrade over more than one minor version (TN_K3S_FORCE=true is the default in the scripts)")
addNodeFlags(bootstrapCmd)
bootstrapCmd.Flags().String("docker-secret", "", "The contents of the .dockerconfigjson")
bootstrapCmd.Flags().String("registry-mirror", "",
"pull images through this Harbor host (pull-through cache projects dockerhub, ghcr, gcr, quay, k8s, nvcr), with the upstream registry as the fallback: the scripts write /etc/rancher/k3s/registries.yaml before the k3s installer. \"none\" removes a file the scripts wrote. Without it the file is left alone (or follows the registry that the platform sends with the device credentials, when it sends one)")
bootstrapCmd.Flags().String("registry-credential-file", "",
"file with the Harbor credential for --registry-mirror, one line username:password (a robot account of the teamspace), readable by you only. The password goes into the generated scripts and is never printed")
rootCmd.AddCommand(bootstrapCmd)
}

// writeBootstrapScript renders a bootstrap script to filename with mode 0600: the scripts hold the device
// password, the k3s token and the teamspace's Harbor robot password. A file that already exists (written with
// 0644 by an earlier tnctl) is chmodded too, because OpenFile's mode applies to new files only.
func writeBootstrapScript(tmpl *template.Template, filename string, data templates.TemplateData) error {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if err != nil {
return err
}
defer f.Close()
if err := f.Chmod(0o600); err != nil {
return err
}
return tmpl.Execute(f, data)
}
48 changes: 48 additions & 0 deletions cmd/bootstrap_perm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"os"
"path/filepath"
"runtime"
"testing"
"text/template"

"github.com/teknoir/cli/pkg/templates"
)

// The bootstrap scripts hold the teamspace's Harbor robot password: new files are 0600, and a file an earlier
// tnctl wrote with 0644 is tightened and truncated, not appended to.
func TestWriteBootstrapScriptIsPrivate(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("POSIX modes")
}
tmpl := template.Must(template.New("t").Parse("password={{.RegistryPassword}}\n"))
data := templates.TemplateData{RegistryPassword: "s3cret"}
dir := t.TempDir()

fresh := filepath.Join(dir, "fresh.sh")
if err := writeBootstrapScript(tmpl, fresh, data); err != nil {
t.Fatal(err)
}
if fi, err := os.Stat(fresh); err != nil || fi.Mode().Perm() != 0o600 {
t.Errorf("fresh file: %v %v, want 0600", fi.Mode().Perm(), err)
}

old := filepath.Join(dir, "old.sh")
if err := os.WriteFile(old, []byte("an older, much longer script body that must not survive\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Chmod(old, 0o644); err != nil { // umask may have cleared bits
t.Fatal(err)
}
if err := writeBootstrapScript(tmpl, old, data); err != nil {
t.Fatal(err)
}
fi, err := os.Stat(old)
if err != nil || fi.Mode().Perm() != 0o600 {
t.Errorf("overwritten file: %v %v, want 0600", fi.Mode().Perm(), err)
}
if b, _ := os.ReadFile(old); string(b) != "password=s3cret\n" {
t.Errorf("content %q", b)
}
}
116 changes: 116 additions & 0 deletions cmd/bootstrap_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd

import (
"fmt"
"os"
"runtime"
"strings"

"github.com/teknoir/cli/pkg/api"
"github.com/teknoir/cli/pkg/templates"
)

// registrySettings is what `tnctl bootstrap` puts into TemplateData for the registry mirror.
type registrySettings struct {
Mirror, Username, Password string
}

// registryFromFlags decides the registry mirror of the generated scripts (plans/harbor-proxy-staging.md):
//
// - --registry-mirror none removes a registries.yaml that an earlier script wrote; no credential;
// - --registry-mirror HOST uses HOST with the credential of --registry-credential-file
// (one line username:password), or the platform's credential when the device's credentials
// response names the same host;
// - no --registry-mirror: the platform's registry of the device credentials response when it sends
// one (not yet), otherwise nothing: registries.yaml is left alone.
//
// Errors never contain the credential.
func registryFromFlags(mirrorFlag, credFile string, platform *api.FleetRegistry) (registrySettings, error) {
mirror := strings.TrimSpace(mirrorFlag)
if err := templates.ValidateRegistryMirror(mirror); err != nil {
return registrySettings{}, err
}
fromPlatform := false
if mirror == "" && platform != nil && platform.Host != "" {
mirror = platform.Host
fromPlatform = true
if err := templates.ValidateRegistryMirror(mirror); err != nil {
return registrySettings{}, fmt.Errorf("the registry mirror that the platform sent is unusable: %w", err)
}
}
switch mirror {
case "":
if credFile != "" {
return registrySettings{}, fmt.Errorf("--registry-credential-file needs --registry-mirror HOST")
}
return registrySettings{}, nil
case templates.RegistryMirrorNone:
return registrySettings{Mirror: mirror}, nil
}
s := registrySettings{Mirror: mirror}
switch {
case credFile != "":
user, pass, err := readRegistryCredentialFile(credFile)
if err != nil {
return registrySettings{}, err
}
s.Username, s.Password = user, pass
case platform != nil && platform.Host == mirror && platform.Username != "" && platform.Password != "":
s.Username, s.Password = platform.Username, platform.Password
default:
what := "--registry-mirror " + mirror
if fromPlatform {
what = "the registry mirror " + mirror + " that the platform sent"
}
return registrySettings{}, fmt.Errorf("%s needs a credential: pass --registry-credential-file FILE (one line username:password; the Harbor proxy projects are private)", what)
}
return s, nil
}

// checkRegistryFlags checks the two flags on their own, before the platform is asked for anything: the
// mirror syntax and, when given, the credential file. Whether a mirror and a credential are both there
// is decided by registryFromFlags once the platform's answer is known.
func checkRegistryFlags(mirrorFlag, credFile string) error {
if err := templates.ValidateRegistryMirror(strings.TrimSpace(mirrorFlag)); err != nil {
return err
}
if credFile != "" {
if _, _, err := readRegistryCredentialFile(credFile); err != nil {
return err
}
}
return nil
}

// readRegistryCredentialFile reads `username:password` from the first line of path. Where file modes
// mean something, a file that group or others can read is refused: it holds a password. The password
// is never part of an error.
func readRegistryCredentialFile(path string) (user, pass string, err error) {
fi, err := os.Stat(path)
if err != nil {
return "", "", fmt.Errorf("--registry-credential-file: %w", err)
}
if !fi.Mode().IsRegular() {
return "", "", fmt.Errorf("--registry-credential-file %s is not a regular file", path)
}
if runtime.GOOS != "windows" && fi.Mode().Perm()&0o077 != 0 {
return "", "", fmt.Errorf("--registry-credential-file %s has mode %04o: it holds a password, make it readable by you only (chmod 600)", path, fi.Mode().Perm())
}
b, err := os.ReadFile(path)
if err != nil {
return "", "", fmt.Errorf("--registry-credential-file: %w", err)
}
if len(strings.TrimSpace(string(b))) == 0 {
return "", "", fmt.Errorf("--registry-credential-file %s is empty", path)
}
lines := strings.Split(strings.TrimRight(string(b), "\r\n"), "\n")
if len(lines) != 1 {
return "", "", fmt.Errorf("--registry-credential-file %s must hold exactly one line, username:password", path)
}
line := strings.TrimSuffix(lines[0], "\r")
user, pass, ok := strings.Cut(line, ":")
if !ok || user == "" || pass == "" {
return "", "", fmt.Errorf("--registry-credential-file %s must hold one line username:password", path)
}
return user, pass, nil
}
Loading
Loading