Skip to content

Commit 789758a

Browse files
committed
PRODENG-2595 host command sudo
- MKE Host configuration for commands that should use sudo - MKE Host now wraps rig Connection Exec commands, to check if sudo is needed - Phases that use configurer.AuthenticateDocker now skips that if docker runs with sudo. ALSO - update example TF lock - some small linting control changes for latest golangci-lint - skipped some linting on wrapped/decorated functions Signed-off-by: James Nesbitt <[email protected]>
1 parent 0156872 commit 789758a

File tree

7 files changed

+78
-27
lines changed

7 files changed

+78
-27
lines changed

.golangci.yml

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
run:
22
timeout: 8m
33

4-
skip-dirs-use-default: false
5-
skip-files:
4+
tests: false
5+
allow-parallel-runners: true
6+
7+
issues:
8+
exclude-dirs-use-default: false
9+
exclude-files:
610
- ".*\\.gen\\.go"
711
- examples/*
812
- test/*
913
- logo.go
1014
- logo_windows.go
11-
tests: false
12-
allow-parallel-runners: true
15+
max-issues-per-linter: 0
16+
max-same-issues: 0
1317

1418
linters:
1519
enable:
@@ -83,6 +87,3 @@ linters-settings:
8387
- ok bool
8488
- s string
8589

86-
issues:
87-
max-issues-per-linter: 0
88-
max-same-issues: 0

examples/tf-aws/launchpad/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
terraform.tfstate*
99
*.tfvars
1010
# SSH-KEYS
11-
./ssh-keys/
11+
ssh-keys/
1212

1313
.terraform
1414

examples/tf-aws/launchpad/.terraform.lock.hcl

+19-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/product/mke/api/host.go

+47
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io"
78
"os"
89
"reflect"
910
"strings"
@@ -15,6 +16,7 @@ import (
1516
"github.com/creasty/defaults"
1617
"github.com/k0sproject/dig"
1718
"github.com/k0sproject/rig"
19+
"github.com/k0sproject/rig/exec"
1820
"github.com/k0sproject/rig/os/registry"
1921
log "github.com/sirupsen/logrus"
2022
)
@@ -75,6 +77,7 @@ type Host struct {
7577
Environment map[string]string `yaml:"environment,flow,omitempty" default:"{}"`
7678
Hooks common.Hooks `yaml:"hooks,omitempty" validate:"dive,keys,oneof=apply reset,endkeys,dive,keys,oneof=before after,endkeys,omitempty"`
7779
ImageDir string `yaml:"imageDir,omitempty"`
80+
SudoCommands []string `yaml:"sudocmds,omitempty"`
7881

7982
Metadata *HostMetadata `yaml:"-"`
8083
MSRMetadata *MSRMetadata `yaml:"-"`
@@ -106,6 +109,34 @@ func (h *Host) IsLocal() bool {
106109
return h.Protocol() == "Local"
107110
}
108111

112+
// IsSudoCommand is a particluar string command supposed to use Sudo.
113+
func (h *Host) IsSudoCommand(cmd string) bool {
114+
for _, sudocmd := range h.SudoCommands {
115+
if strings.HasPrefix(cmd, sudocmd) {
116+
return true
117+
}
118+
}
119+
return false
120+
}
121+
122+
// AuthorizeDocker if needed.
123+
func (h *Host) AuthorizeDocker() error {
124+
if h.IsSudoCommand("docker") {
125+
log.Debugf("%s: not authorizing docker, as docker is meant to be run with sudo", h)
126+
return nil
127+
}
128+
129+
return h.Configurer.AuthorizeDocker(h) //nolint:wrapcheck
130+
}
131+
132+
func (h *Host) sudoCommandOptions(cmd string, opts []exec.Option) []exec.Option {
133+
if h.IsSudoCommand(cmd) {
134+
log.Debugf("%s: Exec is getting SUDOed as the command is in the host sudo list: %s", h, cmd)
135+
opts = append(opts, exec.Sudo(h))
136+
}
137+
return opts
138+
}
139+
109140
// ExecAll execs a slice of commands on the host.
110141
func (h *Host) ExecAll(cmds []string) error {
111142
for _, cmd := range cmds {
@@ -122,6 +153,22 @@ func (h *Host) ExecAll(cmds []string) error {
122153
return nil
123154
}
124155

156+
// ExecStreams executes a command on the remote host and uses the passed in streams for stdin, stdout and stderr. It returns a Waiter with a .Wait() function that
157+
// blocks until the command finishes and returns an error if the exit code is not zero.
158+
func (h *Host) ExecStreams(cmd string, stdin io.ReadCloser, stdout, stderr io.Writer, opts ...exec.Option) (exec.Waiter, error) { //nolint:ireturn
159+
return h.Connection.ExecStreams(cmd, stdin, stdout, stderr, h.sudoCommandOptions(cmd, opts)...) //nolint:wrapcheck
160+
}
161+
162+
// Exec runs a command on the host.
163+
func (h *Host) Exec(cmd string, opts ...exec.Option) error {
164+
return h.Connection.Exec(cmd, h.sudoCommandOptions(cmd, opts)...) //nolint:wrapcheck
165+
}
166+
167+
// ExecOutput runs a command on the host and returns the output as a String.
168+
func (h *Host) ExecOutput(cmd string, opts ...exec.Option) (string, error) {
169+
return h.Connection.ExecOutput(cmd, h.sudoCommandOptions(cmd, opts)...) //nolint:wrapcheck
170+
}
171+
125172
var errAuthFailed = errors.New("authentication failed")
126173

127174
// AuthenticateDocker performs a docker login on the host using local REGISTRY_USERNAME

pkg/product/mke/phase/install_mcr.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (p *InstallMCR) installMCR(h *api.Host) error {
6969
return fmt.Errorf("retry count exceeded: %w", err)
7070
}
7171

72-
if err := h.Configurer.AuthorizeDocker(h); err != nil {
72+
if err := h.AuthorizeDocker(); err != nil {
7373
return fmt.Errorf("%s: failed to authorize docker: %w", h, err)
7474
}
7575

pkg/product/mke/phase/prepare_host.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (p *PrepareHost) fixContainerized(h *api.Host, _ *api.ClusterConfig) error
9797
}
9898

9999
func (p *PrepareHost) authorizeDocker(h *api.Host, _ *api.ClusterConfig) error {
100-
if err := h.Configurer.AuthorizeDocker(h); err != nil {
100+
if err := h.AuthorizeDocker(); err != nil {
101101
return fmt.Errorf("failed to authorize docker: %w", err)
102102
}
103103
return nil

test/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"math/rand"
55
)
66

7-
// GenerateRandomAlphaNumericString generates a random string of a given length with only alphanumeric values
7+
// GenerateRandomAlphaNumericString generates a random string of a given length with only alphanumeric values.
88
func GenerateRandomAlphaNumericString(length int) string {
99
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
1010
result := make([]byte, length)

0 commit comments

Comments
 (0)