Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Context conform to "x/crypto/ssh".ConnMetadata interface #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ssh

import (
"context"
"encoding/hex"
"net"
"sync"

Expand Down Expand Up @@ -70,13 +69,13 @@ type Context interface {
User() string

// SessionID returns the session hash.
SessionID() string
SessionID() []byte

// ClientVersion returns the version reported by the client.
ClientVersion() string
ClientVersion() []byte

// ServerVersion returns the version reported by the server.
ServerVersion() string
ServerVersion() []byte

// RemoteAddr returns the remote address for this connection.
RemoteAddr() net.Addr
Expand Down Expand Up @@ -111,9 +110,9 @@ func applyConnMetadata(ctx Context, conn gossh.ConnMetadata) {
if ctx.Value(ContextKeySessionID) != nil {
return
}
ctx.SetValue(ContextKeySessionID, hex.EncodeToString(conn.SessionID()))
ctx.SetValue(ContextKeyClientVersion, string(conn.ClientVersion()))
ctx.SetValue(ContextKeyServerVersion, string(conn.ServerVersion()))
ctx.SetValue(ContextKeySessionID, conn.SessionID())
ctx.SetValue(ContextKeyClientVersion, conn.ClientVersion())
ctx.SetValue(ContextKeyServerVersion, conn.ServerVersion())
ctx.SetValue(ContextKeyUser, conn.User())
ctx.SetValue(ContextKeyLocalAddr, conn.LocalAddr())
ctx.SetValue(ContextKeyRemoteAddr, conn.RemoteAddr())
Expand All @@ -127,16 +126,16 @@ func (ctx *sshContext) User() string {
return ctx.Value(ContextKeyUser).(string)
}

func (ctx *sshContext) SessionID() string {
return ctx.Value(ContextKeySessionID).(string)
func (ctx *sshContext) SessionID() []byte {
return ctx.Value(ContextKeySessionID).([]byte)
}

func (ctx *sshContext) ClientVersion() string {
return ctx.Value(ContextKeyClientVersion).(string)
func (ctx *sshContext) ClientVersion() []byte {
return ctx.Value(ContextKeyClientVersion).([]byte)
}

func (ctx *sshContext) ServerVersion() string {
return ctx.Value(ContextKeyServerVersion).(string)
func (ctx *sshContext) ServerVersion() []byte {
return ctx.Value(ContextKeyServerVersion).([]byte)
}

func (ctx *sshContext) RemoteAddr() net.Addr {
Expand Down
13 changes: 12 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ssh

import "testing"
import (
"testing"

gossh "golang.org/x/crypto/ssh"
)

func TestSetPermissions(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -45,3 +49,10 @@ func TestSetValue(t *testing.T) {
t.Fatal(err)
}
}

func TestContextAsConnMetadata (t *testing.T) {
var s Context = &sshContext{}
if _, ok := s.(gossh.ConnMetadata); !ok {
t.Error("Context unusable as ConnMetadata")
}
}