feat: Decide upstream based on user's unix group membership (#536)
* feat: Decide upstream based on user's unix group membership If a username is not defined, groupname is parsed. Check if the user is part of that group and route them to the associated host defined in the config file for the yaml plugin * style: fix formatting with gofmt * feat: Look up user groups only when groupname defined in yaml config * fix: inefficient assignment because of unused var * feat: fallback to next rule on group lookup failure Instead of failing on group lookup errors, the matcher now skips the groupname rule and proceeds to the next, eventually failing through to the catchall rule. * feat: test cases for group based routing in yaml plugin * Revert "feat: fallback to next rule on group lookup failure" This reverts commit 622ee9f1eb3157d04f57c068179bb74de8db3a1f. Handles the error returned by getUserGroups instead of ignoring it, to prevent potential runtime issues when user lookup fail * feat: Check if a user is known to the system before group lookup This will let the rule matching logic skip to the next pipe in the yaml config when a user is not found on the system. Note the variable name change from user to username to avoid ambiguity dur to name collision with os/user package. * feat: Avoid redundant user lookup * feat: Improve error handling for user and group lookup failures * feat: Use appropriate test user name for group routing
This commit is contained in:
parent
4ddeee8048
commit
4fce1dcb45
5 changed files with 151 additions and 22 deletions
|
|
@ -50,6 +50,9 @@
|
|||
"username_regex_match": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"groupname": {
|
||||
"type": "string"
|
||||
},
|
||||
"authorized_keys": {
|
||||
"oneOf": [
|
||||
{
|
||||
|
|
@ -161,4 +164,4 @@
|
|||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os/user"
|
||||
"regexp"
|
||||
"slices"
|
||||
|
||||
"github.com/tg123/sshpiper/libplugin"
|
||||
)
|
||||
|
|
@ -84,26 +88,46 @@ func (s *skelpipeToWrapper) KnownHosts(conn libplugin.ConnMetadata) ([]byte, err
|
|||
}
|
||||
|
||||
func (s *skelpipeFromWrapper) MatchConn(conn libplugin.ConnMetadata) (libplugin.SkelPipeTo, error) {
|
||||
user := conn.User()
|
||||
username := conn.User()
|
||||
|
||||
matched := s.from.Username == user
|
||||
targetuser := s.to.Username
|
||||
|
||||
if targetuser == "" {
|
||||
targetuser = user
|
||||
}
|
||||
var matched bool
|
||||
if s.from.Username != "" {
|
||||
matched = s.from.Username == username
|
||||
if s.from.UsernameRegexMatch {
|
||||
re, err := regexp.Compile(s.from.Username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if s.from.UsernameRegexMatch {
|
||||
re, err := regexp.Compile(s.from.Username)
|
||||
matched = re.MatchString(username)
|
||||
|
||||
if matched {
|
||||
targetuser = re.ReplaceAllString(username, s.to.Username)
|
||||
}
|
||||
}
|
||||
} else if s.from.Groupname != "" {
|
||||
// check user is known to the system before grouplookup
|
||||
usr, err := user.Lookup(username)
|
||||
if err != nil {
|
||||
var unknownUser user.UnknownUserError
|
||||
if errors.As(err, &unknownUser) {
|
||||
return nil, nil
|
||||
}
|
||||
log.Errorf("[ERROR] Matchconn(): Failure looking up user %q: %T - %v", username, err, err)
|
||||
return nil, err
|
||||
}
|
||||
userGroups, err := getUserGroups(usr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fromPipeGroup := s.from.Groupname
|
||||
matched = slices.Contains(userGroups, fromPipeGroup)
|
||||
}
|
||||
|
||||
matched = re.MatchString(user)
|
||||
|
||||
if matched {
|
||||
targetuser = re.ReplaceAllString(user, s.to.Username)
|
||||
}
|
||||
if targetuser == "" {
|
||||
targetuser = username
|
||||
}
|
||||
|
||||
if matched {
|
||||
|
|
@ -183,3 +207,23 @@ func (p *plugin) listPipe(_ libplugin.ConnMetadata) ([]libplugin.SkelPipe, error
|
|||
|
||||
return pipes, nil
|
||||
}
|
||||
|
||||
func getUserGroups(usr *user.User) ([]string, error) {
|
||||
groupIds, err := usr.GroupIds()
|
||||
if err != nil {
|
||||
log.Errorf("[ERROR] getUserGroups(): Failure retrieving group IDs for %q: %T - %v", usr.Username, err, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var groups []string
|
||||
for _, groupId := range groupIds {
|
||||
grp, err := user.LookupGroupId(groupId)
|
||||
if err != nil {
|
||||
log.Errorf("[ERROR] getUserGroups(): Failure retrieving group name for %q: %T - %v", usr.Username, err, err)
|
||||
return nil, err
|
||||
}
|
||||
groups = append(groups, grp.Name)
|
||||
}
|
||||
|
||||
return groups, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,8 @@ import (
|
|||
)
|
||||
|
||||
type yamlPipeFrom struct {
|
||||
Username string `yaml:"username"`
|
||||
Username string `yaml:"username,omitempty"`
|
||||
Groupname string `yaml:"groupname,omitempty"`
|
||||
UsernameRegexMatch bool `yaml:"username_regex_match,omitempty"`
|
||||
AuthorizedKeys listOrString `yaml:"authorized_keys,omitempty"`
|
||||
AuthorizedKeysData listOrString `yaml:"authorized_keys_data,omitempty"`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue