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
contexts #29
Merged
contexts #29
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c90e417
context: working mostly tested context implementation and refactoring…
progrium 46a8a4f
_example/ssh-publickey: updating new context based callbacks
progrium b8ac33b
godocs related to public api changes for contexts
progrium 0e5e510
context: converting []bytes to strings before putting into context
progrium File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package ssh | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
gossh "golang.org/x/crypto/ssh" | ||
) | ||
|
||
// contextKey is a value for use with context.WithValue. It's used as | ||
// a pointer so it fits in an interface{} without allocation. | ||
type contextKey struct { | ||
name string | ||
} | ||
|
||
var ( | ||
ContextKeyUser = &contextKey{"user"} | ||
ContextKeySessionID = &contextKey{"session-id"} | ||
ContextKeyPermissions = &contextKey{"permissions"} | ||
ContextKeyClientVersion = &contextKey{"client-version"} | ||
ContextKeyServerVersion = &contextKey{"server-version"} | ||
ContextKeyLocalAddr = &contextKey{"local-addr"} | ||
ContextKeyRemoteAddr = &contextKey{"remote-addr"} | ||
ContextKeyServer = &contextKey{"ssh-server"} | ||
ContextKeyPublicKey = &contextKey{"public-key"} | ||
) | ||
|
||
type Context interface { | ||
context.Context | ||
User() string | ||
SessionID() string | ||
ClientVersion() string | ||
ServerVersion() string | ||
RemoteAddr() net.Addr | ||
LocalAddr() net.Addr | ||
Permissions() *Permissions | ||
SetValue(key, value interface{}) | ||
} | ||
|
||
type sshContext struct { | ||
context.Context | ||
} | ||
|
||
func newContext(srv *Server) *sshContext { | ||
ctx := &sshContext{context.Background()} | ||
ctx.SetValue(ContextKeyServer, srv) | ||
perms := &Permissions{&gossh.Permissions{}} | ||
ctx.SetValue(ContextKeyPermissions, perms) | ||
return ctx | ||
} | ||
|
||
// this is separate from newContext because we will get ConnMetadata | ||
// at different points so it needs to be applied separately | ||
func (ctx *sshContext) applyConnMetadata(conn gossh.ConnMetadata) { | ||
if ctx.Value(ContextKeySessionID) != nil { | ||
return | ||
} | ||
// for most of these, instead of converting to strings now, storing the byte | ||
// slices means allocations only happen when accessing, not when contexts | ||
// are being copied around | ||
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()) | ||
} | ||
|
||
func (ctx *sshContext) SetValue(key, value interface{}) { | ||
ctx.Context = context.WithValue(ctx.Context, key, value) | ||
} | ||
|
||
func (ctx *sshContext) User() string { | ||
return ctx.Value(ContextKeyUser).(string) | ||
} | ||
|
||
func (ctx *sshContext) SessionID() string { | ||
id, _ := ctx.Value(ContextKeySessionID).([]byte) | ||
return string(id) | ||
} | ||
|
||
func (ctx *sshContext) ClientVersion() string { | ||
version, _ := ctx.Value(ContextKeyClientVersion).([]byte) | ||
return string(version) | ||
} | ||
|
||
func (ctx *sshContext) ServerVersion() string { | ||
version, _ := ctx.Value(ContextKeyServerVersion).([]byte) | ||
return string(version) | ||
} | ||
|
||
func (ctx *sshContext) RemoteAddr() net.Addr { | ||
return ctx.Value(ContextKeyRemoteAddr).(net.Addr) | ||
} | ||
|
||
func (ctx *sshContext) LocalAddr() net.Addr { | ||
return ctx.Value(ContextKeyLocalAddr).(net.Addr) | ||
} | ||
|
||
func (ctx *sshContext) Permissions() *Permissions { | ||
return ctx.Value(ContextKeyPermissions).(*Permissions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ssh | ||
|
||
import "testing" | ||
|
||
func TestSetPermissions(t *testing.T) { | ||
t.Parallel() | ||
permsExt := map[string]string{ | ||
"foo": "bar", | ||
} | ||
session, cleanup := newTestSessionWithOptions(t, &Server{ | ||
Handler: func(s Session) { | ||
if _, ok := s.Permissions().Extensions["foo"]; !ok { | ||
t.Fatalf("got %#v; want %#v", s.Permissions().Extensions, permsExt) | ||
} | ||
}, | ||
}, nil, PasswordAuth(func(ctx Context, password string) bool { | ||
ctx.Permissions().Extensions = permsExt | ||
return true | ||
})) | ||
defer cleanup() | ||
if err := session.Run(""); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestSetValue(t *testing.T) { | ||
t.Parallel() | ||
value := map[string]string{ | ||
"foo": "bar", | ||
} | ||
key := "testValue" | ||
session, cleanup := newTestSessionWithOptions(t, &Server{ | ||
Handler: func(s Session) { | ||
v := s.Context().Value(key).(map[string]string) | ||
if v["foo"] != value["foo"] { | ||
t.Fatalf("got %#v; want %#v", v, value) | ||
} | ||
}, | ||
}, nil, PasswordAuth(func(ctx Context, password string) bool { | ||
ctx.SetValue(key, value) | ||
return true | ||
})) | ||
defer cleanup() | ||
if err := session.Run(""); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ssh | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
gossh "golang.org/x/crypto/ssh" | ||
) | ||
|
||
func newTestSessionWithOptions(t *testing.T, srv *Server, cfg *gossh.ClientConfig, options ...Option) (*gossh.Session, func()) { | ||
for _, option := range options { | ||
if err := srv.SetOption(option); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
return newTestSession(t, srv, cfg) | ||
} | ||
|
||
func TestPasswordAuth(t *testing.T) { | ||
t.Parallel() | ||
testUser := "testuser" | ||
testPass := "testpass" | ||
session, cleanup := newTestSessionWithOptions(t, &Server{ | ||
Handler: func(s Session) { | ||
// noop | ||
}, | ||
}, &gossh.ClientConfig{ | ||
User: testUser, | ||
Auth: []gossh.AuthMethod{ | ||
gossh.Password(testPass), | ||
}, | ||
}, PasswordAuth(func(ctx Context, password string) bool { | ||
if ctx.User() != testUser { | ||
t.Fatalf("user = %#v; want %#v", ctx.User(), testUser) | ||
} | ||
if password != testPass { | ||
t.Fatalf("user = %#v; want %#v", password, testPass) | ||
} | ||
return true | ||
})) | ||
defer cleanup() | ||
if err := session.Run(""); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestPasswordAuthBadPass(t *testing.T) { | ||
t.Parallel() | ||
l := newLocalListener() | ||
srv := &Server{Handler: func(s Session) {}} | ||
srv.SetOption(PasswordAuth(func(ctx Context, password string) bool { | ||
return false | ||
})) | ||
go srv.serveOnce(l) | ||
_, err := gossh.Dial("tcp", l.Addr().String(), &gossh.ClientConfig{ | ||
User: "testuser", | ||
Auth: []gossh.AuthMethod{ | ||
gossh.Password("testpass"), | ||
}, | ||
}) | ||
if err != nil { | ||
if !strings.Contains(err.Error(), "unable to authenticate") { | ||
t.Fatal(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though after I saw how context.WithValue is implemented this might not be an issue.