2019-08-22 16:36:21 +00:00
|
|
|
//+build !windows
|
|
|
|
|
|
|
|
package sshserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/log"
|
|
|
|
"github.com/gliderlabs/ssh"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/sirupsen/logrus/hooks/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-29 20:36:45 +00:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
2019-08-22 16:36:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testDir = "testdata"
|
|
|
|
testUserKeyFilename = "id_rsa.pub"
|
|
|
|
testCAFilename = "ca.pub"
|
|
|
|
testOtherCAFilename = "other_ca.pub"
|
|
|
|
testUserCertFilename = "id_rsa-cert.pub"
|
|
|
|
)
|
|
|
|
|
2019-09-04 15:37:53 +00:00
|
|
|
var (
|
|
|
|
logger, hook = test.NewNullLogger()
|
|
|
|
mockUser = &User{Username: "testUser", HomeDir: testDir}
|
|
|
|
)
|
2019-08-22 16:36:21 +00:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2019-08-28 15:48:30 +00:00
|
|
|
authorizedKeysDir = testUserKeyFilename
|
2019-08-22 16:36:21 +00:00
|
|
|
logger.SetLevel(logrus.DebugLevel)
|
|
|
|
code := m.Run()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublicKeyAuth_Success(t *testing.T) {
|
2019-09-04 15:37:53 +00:00
|
|
|
context, cancel := newMockContext(mockUser)
|
2019-08-22 16:36:21 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-09-04 15:37:53 +00:00
|
|
|
sshServer := SSHServer{logger: logger}
|
2019-08-22 16:36:21 +00:00
|
|
|
|
|
|
|
pubKey := getKey(t, testUserKeyFilename)
|
|
|
|
assert.True(t, sshServer.authorizedKeyHandler(context, pubKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublicKeyAuth_MissingKey(t *testing.T) {
|
2019-09-04 15:37:53 +00:00
|
|
|
context, cancel := newMockContext(mockUser)
|
2019-08-22 16:36:21 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-09-04 15:37:53 +00:00
|
|
|
sshServer := SSHServer{logger: logger}
|
2019-08-22 16:36:21 +00:00
|
|
|
|
|
|
|
pubKey := getKey(t, testOtherCAFilename)
|
|
|
|
assert.False(t, sshServer.authorizedKeyHandler(context, pubKey))
|
|
|
|
assert.Contains(t, hook.LastEntry().Message, "Matching public key not found in")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublicKeyAuth_InvalidUser(t *testing.T) {
|
2019-09-04 15:37:53 +00:00
|
|
|
context, cancel := newMockContext(&User{Username: "notAUser"})
|
2019-08-22 16:36:21 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-09-04 15:37:53 +00:00
|
|
|
sshServer := SSHServer{logger: logger}
|
2019-08-22 16:36:21 +00:00
|
|
|
|
|
|
|
pubKey := getKey(t, testUserKeyFilename)
|
2019-09-04 15:37:53 +00:00
|
|
|
assert.False(t, sshServer.authenticationHandler(context, pubKey))
|
2019-08-22 16:36:21 +00:00
|
|
|
assert.Contains(t, hook.LastEntry().Message, "Invalid user")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublicKeyAuth_MissingFile(t *testing.T) {
|
2019-09-04 15:37:53 +00:00
|
|
|
tempUser, err := user.Current()
|
|
|
|
require.Nil(t, err)
|
|
|
|
currentUser, err := lookupUser(tempUser.Username)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2019-08-22 16:36:21 +00:00
|
|
|
require.Nil(t, err)
|
2019-09-04 15:37:53 +00:00
|
|
|
context, cancel := newMockContext(currentUser)
|
2019-08-22 16:36:21 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-09-04 15:37:53 +00:00
|
|
|
sshServer := SSHServer{Server: ssh.Server{}, logger: logger}
|
2019-08-22 16:36:21 +00:00
|
|
|
|
|
|
|
pubKey := getKey(t, testUserKeyFilename)
|
|
|
|
assert.False(t, sshServer.authorizedKeyHandler(context, pubKey))
|
|
|
|
assert.Contains(t, hook.LastEntry().Message, "not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortLivedCerts_Success(t *testing.T) {
|
2019-09-04 15:37:53 +00:00
|
|
|
context, cancel := newMockContext(mockUser)
|
2019-08-22 16:36:21 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
caCert := getKey(t, testCAFilename)
|
2019-09-04 15:37:53 +00:00
|
|
|
sshServer := SSHServer{logger: log.CreateLogger(), caCert: caCert}
|
2019-08-22 16:36:21 +00:00
|
|
|
|
2019-08-29 20:36:45 +00:00
|
|
|
userCert, ok := getKey(t, testUserCertFilename).(*gossh.Certificate)
|
|
|
|
require.True(t, ok)
|
2019-08-22 16:36:21 +00:00
|
|
|
assert.True(t, sshServer.shortLivedCertHandler(context, userCert))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortLivedCerts_CAsDontMatch(t *testing.T) {
|
2019-09-04 15:37:53 +00:00
|
|
|
context, cancel := newMockContext(mockUser)
|
2019-08-22 16:36:21 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
caCert := getKey(t, testOtherCAFilename)
|
2019-09-04 15:37:53 +00:00
|
|
|
sshServer := SSHServer{logger: logger, caCert: caCert}
|
2019-08-22 16:36:21 +00:00
|
|
|
|
2019-08-29 20:36:45 +00:00
|
|
|
userCert, ok := getKey(t, testUserCertFilename).(*gossh.Certificate)
|
|
|
|
require.True(t, ok)
|
2019-08-22 16:36:21 +00:00
|
|
|
assert.False(t, sshServer.shortLivedCertHandler(context, userCert))
|
|
|
|
assert.Equal(t, "CA certificate does not match user certificate signer", hook.LastEntry().Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortLivedCerts_InvalidPrincipal(t *testing.T) {
|
2019-09-04 15:37:53 +00:00
|
|
|
context, cancel := newMockContext(&User{Username: "NotAUser"})
|
2019-08-22 16:36:21 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
caCert := getKey(t, testCAFilename)
|
2019-09-04 15:37:53 +00:00
|
|
|
sshServer := SSHServer{logger: logger, caCert: caCert}
|
2019-08-22 16:36:21 +00:00
|
|
|
|
2019-08-29 20:36:45 +00:00
|
|
|
userCert, ok := getKey(t, testUserCertFilename).(*gossh.Certificate)
|
|
|
|
require.True(t, ok)
|
2019-08-22 16:36:21 +00:00
|
|
|
assert.False(t, sshServer.shortLivedCertHandler(context, userCert))
|
|
|
|
assert.Contains(t, hook.LastEntry().Message, "not in the set of valid principals for given certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKey(t *testing.T, filename string) ssh.PublicKey {
|
|
|
|
path := path.Join(testDir, filename)
|
|
|
|
bytes, err := ioutil.ReadFile(path)
|
|
|
|
require.Nil(t, err)
|
|
|
|
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(bytes)
|
|
|
|
require.Nil(t, err)
|
|
|
|
return pubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockSSHContext struct {
|
|
|
|
context.Context
|
|
|
|
*sync.Mutex
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:37:53 +00:00
|
|
|
func newMockContext(user *User) (*mockSSHContext, context.CancelFunc) {
|
2019-08-22 16:36:21 +00:00
|
|
|
innerCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
mockCtx := &mockSSHContext{innerCtx, &sync.Mutex{}}
|
2019-09-04 15:37:53 +00:00
|
|
|
mockCtx.SetValue("sshUser", user)
|
|
|
|
|
|
|
|
// This naming is confusing but we cant change it because this mocks the SSHContext struct in gliderlabs/ssh
|
|
|
|
mockCtx.SetValue("user", user.Username)
|
2019-08-22 16:36:21 +00:00
|
|
|
return mockCtx, cancel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *mockSSHContext) SetValue(key, value interface{}) {
|
|
|
|
ctx.Context = context.WithValue(ctx.Context, key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *mockSSHContext) User() string {
|
|
|
|
return ctx.Value("user").(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *mockSSHContext) SessionID() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *mockSSHContext) ClientVersion() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *mockSSHContext) ServerVersion() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *mockSSHContext) RemoteAddr() net.Addr {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *mockSSHContext) LocalAddr() net.Addr {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *mockSSHContext) Permissions() *ssh.Permissions {
|
|
|
|
return nil
|
|
|
|
}
|