Skip to content

Commit a4c999a

Browse files
cyrilstCyril Servant
authored andcommitted
feature: read force-command from client certificate
1 parent 0e47d77 commit a4c999a

File tree

110 files changed

+18392
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+18392
-1
lines changed

cmd/sshproxy/sshproxy.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package main
1212

1313
import (
14+
"bytes"
1415
"context"
1516
"errors"
1617
"flag"
@@ -34,6 +35,7 @@ import (
3435
"github.com/moby/term"
3536
"github.com/op/go-logging"
3637
"go.etcd.io/etcd/client/v3"
38+
"golang.org/x/crypto/ssh"
3739
)
3840

3941
var (
@@ -206,6 +208,31 @@ type ConnInfo struct {
206208
SSH *SSHInfo // SSH source and destination (from SSH_CONNECTION)
207209
}
208210

211+
// GetOriginalCommand returns the force-command included in the client ssh
212+
// certificate, if any. Otherwise, it returns the content of the environment
213+
// variable SSH_ORIGINAL_COMMAND. No error is returned. In case of any error,
214+
// the content of SSH_ORIGINAL_COMMAND will be returned.
215+
func getOriginalCommand() string {
216+
userAuthFile := os.Getenv("SSH_USER_AUTH")
217+
if userAuthFile != "" {
218+
content, err := os.ReadFile(userAuthFile)
219+
if err == nil {
220+
prefix := []byte("publickey ")
221+
key, found := bytes.CutPrefix(content, prefix)
222+
if found {
223+
out, comment, options, rest, err := ssh.ParseAuthorizedKey(key)
224+
if err == nil {
225+
fmt.Println("out: %v", out)
226+
fmt.Println("comment: %v", comment)
227+
fmt.Println("options: %v", options)
228+
fmt.Println("rest: %v", rest)
229+
}
230+
}
231+
}
232+
}
233+
return os.Getenv("SSH_ORIGINAL_COMMAND")
234+
}
235+
209236
func main() {
210237
os.Exit(mainExitCode())
211238
}
@@ -428,7 +455,7 @@ func mainExitCode() int {
428455
}
429456
}()
430457

431-
originalCmd := os.Getenv("SSH_ORIGINAL_COMMAND")
458+
originalCmd := getOriginalCommand()
432459
log.Debugf("original command = %s", originalCmd)
433460

434461
interactiveCommand := term.IsTerminal(os.Stdout.Fd())

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
go.etcd.io/etcd/api/v3 v3.6.5
1515
go.etcd.io/etcd/client/v3 v3.6.5
1616
go.uber.org/zap v1.27.0
17+
golang.org/x/crypto v0.43.0
1718
google.golang.org/grpc v1.76.0
1819
gopkg.in/yaml.v2 v2.4.0
1920
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
9696
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
9797
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
9898
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
99+
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
100+
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
99101
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
100102
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
101103
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -114,6 +116,8 @@ golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBc
114116
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
115117
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
116118
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
119+
golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q=
120+
golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss=
117121
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
118122
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
119123
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=

vendor/golang.org/x/crypto/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/crypto/PATENTS

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/crypto/blowfish/block.go

Lines changed: 159 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/crypto/blowfish/cipher.go

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)