Add user_map setting to docker backend
This commit is contained in:
		| @@ -22,8 +22,6 @@ | ||||
| package backends | ||||
|  | ||||
| import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/zclconf/go-cty/cty" | ||||
| 	"go.elara.ws/seashell/internal/config" | ||||
| 	"go.elara.ws/seashell/internal/router" | ||||
| @@ -84,17 +82,6 @@ func ctyObjToStringMap(o *cty.Value) map[string]string { | ||||
| 	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 | ||||
| // or a default value if v is nil. | ||||
| func valueOr[T any](v *T, or T) T { | ||||
|   | ||||
| @@ -41,6 +41,7 @@ type dockerSettings struct { | ||||
| 	Command    *cty.Value `cty:"command"` | ||||
| 	Privileged *bool      `cty:"privileged"` | ||||
| 	User       *string    `cty:"user"` | ||||
| 	UserMap    *cty.Value `cty:"user_map"` | ||||
| } | ||||
|  | ||||
| // Docker is the docker backend. It returns a handler that connects | ||||
| @@ -62,6 +63,17 @@ func Docker(route config.Route) router.Handler { | ||||
| 		if !ok { | ||||
| 			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( | ||||
| 			client.WithHostFromEnv(), | ||||
| @@ -72,11 +84,6 @@ func Docker(route config.Route) router.Handler { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		if opts.User == nil { | ||||
| 			envUser := sshGetenv(sess.Environ(), "DOCKER_USER") | ||||
| 			opts.User = &envUser | ||||
| 		} | ||||
|  | ||||
| 		cmd := sess.Command() | ||||
| 		if len(cmd) == 0 { | ||||
| 			cmd = ctyTupleToStrings(opts.Command) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user