YAML Plugin add regex match group (#464)

* add support for match group replace

* update doc
This commit is contained in:
Boshi Lian 2024-10-20 02:15:46 -07:00 committed by GitHub
parent 39f5eef98b
commit 4db98899f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 68 additions and 8 deletions

View file

@ -7,7 +7,11 @@ some basic idea of yaml config file:
* first matched `pipe` will be used.
* any `from` in `pipe` fits `downstream` authentication will be considered as the `pipe` matched.
* `username_regex_match` can be used to match with regex
* `authorized_keys`, `private_key`, `known_hosts` are `path/to/target/file`, but there are also `authorized_keys_data`, `private_key_data`, `known_hosts_data` accepting base64 inline data
* to.Username can be template of regex match groups, example: `from.username: "^password_(.*?)_regex$"` and `to.username: $1"`, will match `password_user_regex` to `user`, more sytax see <https://pkg.go.dev/regexp#Regexp.Expand>
* `authorized_keys`, `known_hosts` are array `path/to/target/file` or single string, but there are also `authorized_keys_data`, `known_hosts_data` accepting base64 inline data, file and data will be merged if both are set
* `private_key` is `path/to/target/file`, but there are also `private_key_data` accepting base64 inline data, file wins if both are set
* magic placeholders in path, example usage: `/path/to/$UPSTREAM_USER/file`
* `DOWNSTREAM_USER`: supported in `private_key`, `known_hosts`
* `UPSTREAM_USER`: supported in `authorized_keys`, `private_key`, `known_hosts`
@ -39,20 +43,24 @@ pipes:
username: "user"
ignore_hostkey: true
- from:
- username: "password_.*_regex"
- username: "^password_(.*?)_regex$"
username_regex_match: true
to:
host: host-password:2222
username: "user"
username: "$1"
ignore_hostkey: true
- from:
- username: "publickey_simple"
authorized_keys: /path/to/publickey_simple/authorized_keys
authorized_keys:
- /path/to/publickey_simple/authorized_keys
- /path/to/publickey_simple/authorized_keys2
to:
host: host-publickey:2222
username: "user"
private_key: /path/to/host-publickey/id_rsa
known_hosts_data: "base64_known_hosts_data"
known_hosts_data:
- "base64_known_hosts_data"
- "base64_known_hosts_data2"
- from:
- username: ".*" # catch all
username_regex_match: true

View file

@ -290,8 +290,21 @@ func (p *plugin) findAndCreateUpstream(conn libplugin.ConnMetadata, password str
for _, from := range pipe.From {
matched := from.Username == user
if pipe.To.Username == "" {
pipe.To.Username = user
}
if from.UsernameRegexMatch {
matched, _ = regexp.MatchString(from.Username, user)
re, err := regexp.Compile(from.Username)
if err != nil {
return nil, err
}
matched = re.MatchString(user)
if matched {
pipe.To.Username = re.ReplaceAllString(user, pipe.To.Username)
}
}
if !matched {