2022-02-18 22:10:26 +00:00
|
|
|
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-02-24 20:27:42 +00:00
|
|
|
//go:build linux || darwin
|
|
|
|
// +build linux darwin
|
2022-02-18 22:10:26 +00:00
|
|
|
|
|
|
|
package tailssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMatchRule(t *testing.T) {
|
|
|
|
someAction := new(tailcfg.SSHAction)
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
rule *tailcfg.SSHRule
|
2022-02-24 16:58:53 +00:00
|
|
|
ci *sshConnInfo
|
2022-02-18 22:10:26 +00:00
|
|
|
wantErr error
|
|
|
|
wantUser string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nil-rule",
|
|
|
|
rule: nil,
|
|
|
|
wantErr: errNilRule,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil-action",
|
|
|
|
rule: &tailcfg.SSHRule{},
|
|
|
|
wantErr: errNilAction,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "expired",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Action: someAction,
|
|
|
|
RuleExpires: timePtr(time.Unix(100, 0)),
|
|
|
|
},
|
2022-02-24 16:58:53 +00:00
|
|
|
ci: &sshConnInfo{now: time.Unix(200, 0)},
|
2022-02-18 22:10:26 +00:00
|
|
|
wantErr: errRuleExpired,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-principal",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Action: someAction,
|
|
|
|
},
|
|
|
|
wantErr: errPrincipalMatch,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-user-match",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Action: someAction,
|
|
|
|
Principals: []*tailcfg.SSHPrincipal{{Any: true}},
|
|
|
|
},
|
2022-02-24 16:58:53 +00:00
|
|
|
ci: &sshConnInfo{sshUser: "alice"},
|
2022-02-18 22:10:26 +00:00
|
|
|
wantErr: errUserMatch,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok-wildcard",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Action: someAction,
|
|
|
|
Principals: []*tailcfg.SSHPrincipal{{Any: true}},
|
|
|
|
SSHUsers: map[string]string{
|
|
|
|
"*": "ubuntu",
|
|
|
|
},
|
|
|
|
},
|
2022-02-24 16:58:53 +00:00
|
|
|
ci: &sshConnInfo{sshUser: "alice"},
|
2022-02-18 22:10:26 +00:00
|
|
|
wantUser: "ubuntu",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok-wildcard-and-nil-principal",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Action: someAction,
|
|
|
|
Principals: []*tailcfg.SSHPrincipal{
|
|
|
|
nil, // don't crash on this
|
|
|
|
{Any: true},
|
|
|
|
},
|
|
|
|
SSHUsers: map[string]string{
|
|
|
|
"*": "ubuntu",
|
|
|
|
},
|
|
|
|
},
|
2022-02-24 16:58:53 +00:00
|
|
|
ci: &sshConnInfo{sshUser: "alice"},
|
2022-02-18 22:10:26 +00:00
|
|
|
wantUser: "ubuntu",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok-exact",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Action: someAction,
|
|
|
|
Principals: []*tailcfg.SSHPrincipal{{Any: true}},
|
|
|
|
SSHUsers: map[string]string{
|
|
|
|
"*": "ubuntu",
|
|
|
|
"alice": "thealice",
|
|
|
|
},
|
|
|
|
},
|
2022-02-24 16:58:53 +00:00
|
|
|
ci: &sshConnInfo{sshUser: "alice"},
|
2022-02-18 22:10:26 +00:00
|
|
|
wantUser: "thealice",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-users-for-reject",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Principals: []*tailcfg.SSHPrincipal{{Any: true}},
|
|
|
|
Action: &tailcfg.SSHAction{Reject: true},
|
|
|
|
},
|
2022-02-24 16:58:53 +00:00
|
|
|
ci: &sshConnInfo{sshUser: "alice"},
|
2022-02-18 22:10:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "match-principal-node-ip",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Action: someAction,
|
|
|
|
Principals: []*tailcfg.SSHPrincipal{{NodeIP: "1.2.3.4"}},
|
|
|
|
SSHUsers: map[string]string{"*": "ubuntu"},
|
|
|
|
},
|
2022-02-24 16:58:53 +00:00
|
|
|
ci: &sshConnInfo{srcIP: netaddr.MustParseIP("1.2.3.4")},
|
2022-02-18 22:10:26 +00:00
|
|
|
wantUser: "ubuntu",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "match-principal-node-id",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Action: someAction,
|
|
|
|
Principals: []*tailcfg.SSHPrincipal{{Node: "some-node-ID"}},
|
|
|
|
SSHUsers: map[string]string{"*": "ubuntu"},
|
|
|
|
},
|
2022-02-24 16:58:53 +00:00
|
|
|
ci: &sshConnInfo{node: &tailcfg.Node{StableID: "some-node-ID"}},
|
2022-02-18 22:10:26 +00:00
|
|
|
wantUser: "ubuntu",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "match-principal-userlogin",
|
|
|
|
rule: &tailcfg.SSHRule{
|
|
|
|
Action: someAction,
|
|
|
|
Principals: []*tailcfg.SSHPrincipal{{UserLogin: "foo@bar.com"}},
|
|
|
|
SSHUsers: map[string]string{"*": "ubuntu"},
|
|
|
|
},
|
2022-02-24 16:58:53 +00:00
|
|
|
ci: &sshConnInfo{uprof: &tailcfg.UserProfile{LoginName: "foo@bar.com"}},
|
2022-02-18 22:10:26 +00:00
|
|
|
wantUser: "ubuntu",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-02-24 16:58:53 +00:00
|
|
|
got, gotUser, err := matchRule(tt.rule, tt.ci)
|
2022-02-18 22:10:26 +00:00
|
|
|
if err != tt.wantErr {
|
|
|
|
t.Errorf("err = %v; want %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if gotUser != tt.wantUser {
|
|
|
|
t.Errorf("user = %q; want %q", gotUser, tt.wantUser)
|
|
|
|
}
|
|
|
|
if err == nil && got == nil {
|
|
|
|
t.Errorf("expected non-nil action on success")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func timePtr(t time.Time) *time.Time { return &t }
|