Skip to content

Commit

Permalink
Add write-kubeconfig-group flag to server (#9233)
Browse files Browse the repository at this point in the history
* Add write-kubeconfig-group flag to server
* update kubectl unable to read config message for kubeconfig mode/group

Signed-off-by: Katherine Pata <me@kitty.sh>
(cherry picked from commit 7a0ea3c)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
kittydoor authored and brandond committed May 31, 2024
1 parent 7b53f3b commit d065c98
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/cli/cmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Server struct {
DisableAgent bool
KubeConfigOutput string
KubeConfigMode string
KubeConfigGroup string
HelmJobImage string
TLSSan cli.StringSlice
TLSSanSecurity bool
Expand Down Expand Up @@ -256,6 +257,12 @@ var ServerFlags = []cli.Flag{
Destination: &ServerConfig.KubeConfigMode,
EnvVar: version.ProgramUpper + "_KUBECONFIG_MODE",
},
&cli.StringFlag{
Name: "write-kubeconfig-group",
Usage: "(client) Write kubeconfig with this group",
Destination: &ServerConfig.KubeConfigGroup,
EnvVar: version.ProgramUpper + "_KUBECONFIG_GROUP",
},
&cli.StringFlag{
Name: "helm-job-image",
Usage: "(helm) Default image to use for helm jobs",
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
serverConfig.ControlConfig.DataDir = cfg.DataDir
serverConfig.ControlConfig.KubeConfigOutput = cfg.KubeConfigOutput
serverConfig.ControlConfig.KubeConfigMode = cfg.KubeConfigMode
serverConfig.ControlConfig.KubeConfigGroup = cfg.KubeConfigGroup
serverConfig.ControlConfig.HelmJobImage = cfg.HelmJobImage
serverConfig.ControlConfig.Rootless = cfg.Rootless
serverConfig.ControlConfig.ServiceLBNamespace = cfg.ServiceLBNamespace
Expand Down
1 change: 1 addition & 0 deletions pkg/daemons/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ type Control struct {
ServiceNodePortRange *utilnet.PortRange
KubeConfigOutput string
KubeConfigMode string
KubeConfigGroup string
HelmJobImage string
DataDir string
KineTLS bool
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func checkReadConfigPermissions(configFile string) error {
if err != nil {
if os.IsPermission(err) {
return fmt.Errorf("Unable to read %s, please start server "+
"with --write-kubeconfig-mode to modify kube config permissions", configFile)
"with --write-kubeconfig-mode or --write-kubeconfig-group "+
"to modify kube config permissions", configFile)
}
}
file.Close()
Expand Down
7 changes: 7 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,13 @@ func writeKubeConfig(certs string, config *Config) error {
util.SetFileModeForPath(kubeConfig, os.FileMode(0600))
}

if config.ControlConfig.KubeConfigGroup != "" {
err := util.SetFileGroupForPath(kubeConfig, config.ControlConfig.KubeConfigGroup)
if err != nil {
logrus.Errorf("Failed to set %s to group %s: %v", kubeConfig, config.ControlConfig.KubeConfigGroup, err)
}
}

if kubeConfigSymlink != kubeConfig {
if err := writeConfigSymlink(kubeConfig, kubeConfigSymlink); err != nil {
logrus.Errorf("Failed to write kubeconfig symlink: %v", err)
Expand Down
23 changes: 23 additions & 0 deletions pkg/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package util

import (
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -14,6 +16,27 @@ func SetFileModeForPath(name string, mode os.FileMode) error {
return os.Chmod(name, mode)
}

func SetFileGroupForPath(name string, group string) error {
// Try to use as group id
gid, err := strconv.Atoi(group)
if err == nil {
return os.Chown(name, -1, gid)
}

// Otherwise, it must be a group name
g, err := user.LookupGroup(group)
if err != nil {
return err
}

gid, err = strconv.Atoi(g.Gid)
if err != nil {
return err
}

return os.Chown(name, -1, gid)
}

func SetFileModeForFile(file *os.File, mode os.FileMode) error {
return file.Chmod(mode)
}
Expand Down

0 comments on commit d065c98

Please sign in to comment.