Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve homedir of sudo user #1412

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 6 additions & 7 deletions clab/authz_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ import (
)

const (
pubKeysGlob = "~/.ssh/*.pub"
pubKeysGlobPart = "~/.ssh/*.pub"
// authorized keys file path on a clab host that is used to create the clabAuthzKeys file.
authzKeysFPath = "~/.ssh/authorized_keys"
authzKeysFPathPart = "~/.ssh/authorized_keys"
hellt marked this conversation as resolved.
Show resolved Hide resolved
)

// CreateAuthzKeysFile creats the authorized_keys file in the lab directory
// if any files ~/.ssh/*.pub found.
func (c *CLab) CreateAuthzKeysFile() error {
b := new(bytes.Buffer)

p := utils.ResolvePath(pubKeysGlob, "")
p := utils.ResolvePath(pubKeysGlobPart, "")

all, err := filepath.Glob(p)
if err != nil {
return fmt.Errorf("failed globbing the path %s", p)
}

f := utils.ResolvePath(authzKeysFPath, "")
f := utils.ResolvePath(authzKeysFPathPart, "")

if utils.FileExists(f) {
log.Debugf("%s found, adding the public keys it contains", f)
Expand All @@ -50,7 +50,7 @@ func (c *CLab) CreateAuthzKeysFile() error {

log.Debugf("extracted %d keys from ssh-agent", len(keys))
for _, k := range keys {
b.WriteString(k + "\n")
addKeyToBuffer(b, k)
}

for _, fn := range all {
Expand Down Expand Up @@ -79,8 +79,7 @@ func addKeyToBuffer(b *bytes.Buffer, key string) {
return
}

key = elems[0] + " " + elems[1]
if !strings.Contains(b.String(), key) {
if !strings.Contains(b.String(), elems[1]) {
b.WriteString(key + "\n")
}
}
Expand Down
14 changes: 12 additions & 2 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"strings"

Expand Down Expand Up @@ -170,9 +171,18 @@ func ReadFileContent(file string) ([]byte, error) {

// ExpandHome expands `~` char in the path to home path of a current user in provided path p.
func ExpandHome(p string) string {
userPath, _ := os.UserHomeDir()
userId, isSet := os.LookupEnv("SUDO_UID")
if !isSet {
userId = "0" // default to root
}

// lookup user to figure out Home Directory
u, err := user.LookupId(userId)
if err != nil {
return p // no idea what else to return other then the string we where called with
}

p = strings.Replace(p, "~", userPath, 1)
p = strings.Replace(p, "~", u.HomeDir, 1)

return p
}
Expand Down