Add user_map setting to docker backend

This commit is contained in:
Elara 2024-08-04 18:27:21 -07:00
parent 4f7a8f0b04
commit 792dfdba78
2 changed files with 12 additions and 18 deletions

View File

@ -22,8 +22,6 @@
package backends package backends
import ( import (
"strings"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"go.elara.ws/seashell/internal/config" "go.elara.ws/seashell/internal/config"
"go.elara.ws/seashell/internal/router" "go.elara.ws/seashell/internal/router"
@ -84,17 +82,6 @@ func ctyObjToStringMap(o *cty.Value) map[string]string {
return out return out
} }
// sshGetenv gets an environment variable from the SSH session
func sshGetenv(env []string, key string) string {
for _, kv := range env {
before, after, ok := strings.Cut(kv, "=")
if ok && before == key {
return after
}
}
return ""
}
// valueOr returns the value that v points to // valueOr returns the value that v points to
// or a default value if v is nil. // or a default value if v is nil.
func valueOr[T any](v *T, or T) T { func valueOr[T any](v *T, or T) T {

View File

@ -41,6 +41,7 @@ type dockerSettings struct {
Command *cty.Value `cty:"command"` Command *cty.Value `cty:"command"`
Privileged *bool `cty:"privileged"` Privileged *bool `cty:"privileged"`
User *string `cty:"user"` User *string `cty:"user"`
UserMap *cty.Value `cty:"user_map"`
} }
// Docker is the docker backend. It returns a handler that connects // Docker is the docker backend. It returns a handler that connects
@ -63,6 +64,17 @@ func Docker(route config.Route) router.Handler {
return errors.New("this route only accepts pty sessions (try adding the -t flag)") return errors.New("this route only accepts pty sessions (try adding the -t flag)")
} }
if opts.User == nil {
userMap := ctyObjToStringMap(opts.UserMap)
user, _ := sshctx.GetUser(sess.Context())
if muser, ok := userMap[user.Name]; ok {
opts.User = &muser
} else {
opts.User = &user.Name
}
}
c, err := client.NewClientWithOpts( c, err := client.NewClientWithOpts(
client.WithHostFromEnv(), client.WithHostFromEnv(),
client.WithVersionFromEnv(), client.WithVersionFromEnv(),
@ -72,11 +84,6 @@ func Docker(route config.Route) router.Handler {
return err return err
} }
if opts.User == nil {
envUser := sshGetenv(sess.Environ(), "DOCKER_USER")
opts.User = &envUser
}
cmd := sess.Command() cmd := sess.Command()
if len(cmd) == 0 { if len(cmd) == 0 {
cmd = ctyTupleToStrings(opts.Command) cmd = ctyTupleToStrings(opts.Command)