open-vault/builtin/logical/ssh/backend_test.go

451 lines
13 KiB
Go
Raw Normal View History

2015-07-10 15:56:14 +00:00
package ssh
import (
"fmt"
"os/user"
2015-08-30 18:17:50 +00:00
"reflect"
2015-08-18 22:40:52 +00:00
"strconv"
2015-07-10 15:56:14 +00:00
"strings"
"testing"
"golang.org/x/crypto/ssh"
2015-08-18 23:48:50 +00:00
"github.com/hashicorp/vault/api"
2015-07-10 15:56:14 +00:00
"github.com/hashicorp/vault/logical"
logicaltest "github.com/hashicorp/vault/logical/testing"
2015-07-10 22:18:02 +00:00
"github.com/hashicorp/vault/vault"
2015-07-10 15:56:14 +00:00
"github.com/mitchellh/mapstructure"
)
const (
2015-07-31 17:24:23 +00:00
testOTPKeyType = "otp"
testDynamicKeyType = "dynamic"
2015-08-13 15:46:55 +00:00
testCIDRList = "127.0.0.1/32"
2015-07-31 17:24:23 +00:00
testDynamicRoleName = "testDynamicRoleName"
testOTPRoleName = "testOTPRoleName"
2015-07-31 19:17:40 +00:00
testKeyName = "testKeyName"
2015-07-10 22:18:02 +00:00
testSharedPrivateKey = `
2015-07-10 15:56:14 +00:00
-----BEGIN RSA PRIVATE KEY-----
2015-07-10 22:18:02 +00:00
MIIEogIBAAKCAQEAvYvoRcWRxqOim5VZnuM6wHCbLUeiND0yaM1tvOl+Fsrz55DG
A0OZp4RGAu1Fgr46E1mzxFz1+zY4UbcEExg+u21fpa8YH8sytSWW1FyuD8ICib0A
/l8slmDMw4BkkGOtSlEqgscpkpv/TWZD1NxJWkPcULk8z6c7TOETn2/H9mL+v2RE
mbE6NDEwJKfD3MvlpIqCP7idR+86rNBAODjGOGgyUbtFLT+K01XmDRALkV3V/nh+
GltyjL4c6RU4zG2iRyV5RHlJtkml+UzUMkzr4IQnkCC32CC/wmtoo/IsAprpcHVe
nkBn3eFQ7uND70p5n6GhN/KOh2j519JFHJyokwIDAQABAoIBAHX7VOvBC3kCN9/x
+aPdup84OE7Z7MvpX6w+WlUhXVugnmsAAVDczhKoUc/W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VFLVgSwwm6owwsouZ0+D/LasbuHqYyqYqdyPJQYzWA2Y+F
+B6f4RoPdSXj24JHPg/ioRxjaj094UXJxua2yfkcecGNEuBQHSs=
2015-07-10 15:56:14 +00:00
-----END RSA PRIVATE KEY-----
`
)
var testIP string
2015-08-04 19:30:24 +00:00
var testOTP string
2015-08-13 15:46:55 +00:00
var testPort int
2015-07-10 15:56:14 +00:00
var testUserName string
var testAdminUser string
2015-08-30 18:30:59 +00:00
var testOTPRoleData map[string]interface{}
var testDynamicRoleData map[string]interface{}
2015-07-10 15:56:14 +00:00
2015-07-10 23:27:21 +00:00
// Starts the server and initializes the servers IP address,
// port and usernames to be used by the test cases.
2015-07-10 15:56:14 +00:00
func init() {
2015-08-18 23:48:50 +00:00
addr, err := vault.StartSSHHostTestServer()
2015-07-10 15:56:14 +00:00
if err != nil {
panic(fmt.Sprintf("error starting mock server:%s", err))
2015-07-10 15:56:14 +00:00
}
input := strings.Split(addr, ":")
testIP = input[0]
2015-08-18 22:40:52 +00:00
testPort, err = strconv.Atoi(input[1])
if err != nil {
panic(fmt.Sprintf("error parsing port number:%s", err))
}
2015-07-10 15:56:14 +00:00
u, err := user.Current()
if err != nil {
panic(fmt.Sprintf("error getting current username: '%s'", err))
2015-07-10 15:56:14 +00:00
}
testUserName = u.Username
testAdminUser = u.Username
2015-08-30 18:30:59 +00:00
testOTPRoleData = map[string]interface{}{
2015-08-03 20:18:14 +00:00
"key_type": testOTPKeyType,
"default_user": testUserName,
2015-08-13 15:46:55 +00:00
"cidr_list": testCIDRList,
2015-08-03 20:18:14 +00:00
}
2015-08-30 18:30:59 +00:00
testDynamicRoleData = map[string]interface{}{
2015-08-30 18:17:50 +00:00
"key_type": testDynamicKeyType,
"key": testKeyName,
"admin_user": testAdminUser,
"default_user": testAdminUser,
"cidr_list": testCIDRList,
2015-08-03 20:18:14 +00:00
}
2015-08-30 18:30:59 +00:00
}
func TestSSHBackend_Lookup(t *testing.T) {
data := map[string]interface{}{
"ip": testIP,
}
2015-08-03 20:18:14 +00:00
logicaltest.Test(t, logicaltest.TestCase{
Factory: Factory,
Steps: []logicaltest.TestStep{
testLookupRead(t, data, 0),
2015-08-30 18:30:59 +00:00
testRoleWrite(t, testOTPRoleName, testOTPRoleData),
2015-08-03 20:18:14 +00:00
testLookupRead(t, data, 1),
testNamedKeysWrite(t),
2015-08-30 18:30:59 +00:00
testRoleWrite(t, testDynamicRoleName, testDynamicRoleData),
2015-08-03 20:18:14 +00:00
testLookupRead(t, data, 2),
testRoleDelete(t, testOTPRoleName),
testLookupRead(t, data, 1),
testRoleDelete(t, testDynamicRoleName),
testLookupRead(t, data, 0),
},
})
}
2015-08-03 15:22:00 +00:00
func TestSSHBackend_DynamicKeyCreate(t *testing.T) {
2015-07-10 15:56:14 +00:00
logicaltest.Test(t, logicaltest.TestCase{
2015-07-24 16:13:26 +00:00
Factory: Factory,
2015-07-10 15:56:14 +00:00
Steps: []logicaltest.TestStep{
2015-08-03 20:18:14 +00:00
testNamedKeysWrite(t),
2015-07-24 16:13:26 +00:00
testNewDynamicKeyRole(t),
testDynamicKeyCredsCreate(t),
2015-07-10 15:56:14 +00:00
},
})
}
2015-07-31 17:24:23 +00:00
func TestSSHBackend_OTPRoleCrud(t *testing.T) {
2015-07-31 19:17:40 +00:00
logicaltest.Test(t, logicaltest.TestCase{
Factory: Factory,
Steps: []logicaltest.TestStep{
2015-08-30 18:30:59 +00:00
testRoleWrite(t, testOTPRoleName, testOTPRoleData),
testRoleRead(t, testOTPRoleName, testOTPRoleData),
2015-07-31 19:17:40 +00:00
testRoleDelete(t, testOTPRoleName),
testRoleRead(t, testOTPRoleName, nil),
},
})
}
func TestSSHBackend_DynamicRoleCrud(t *testing.T) {
2015-07-31 17:24:23 +00:00
logicaltest.Test(t, logicaltest.TestCase{
Factory: Factory,
Steps: []logicaltest.TestStep{
2015-08-03 20:18:14 +00:00
testNamedKeysWrite(t),
2015-08-30 18:30:59 +00:00
testRoleWrite(t, testDynamicRoleName, testDynamicRoleData),
testRoleRead(t, testDynamicRoleName, testDynamicRoleData),
2015-07-31 19:17:40 +00:00
testRoleDelete(t, testDynamicRoleName),
testRoleRead(t, testDynamicRoleName, nil),
2015-07-31 17:24:23 +00:00
},
})
}
2015-08-03 20:18:14 +00:00
func TestSSHBackend_NamedKeysCrud(t *testing.T) {
2015-08-03 15:22:00 +00:00
logicaltest.Test(t, logicaltest.TestCase{
Factory: Factory,
Steps: []logicaltest.TestStep{
2015-08-03 20:18:14 +00:00
testNamedKeysRead(t, ""),
testNamedKeysWrite(t),
testNamedKeysRead(t, testSharedPrivateKey),
testNamedKeysDelete(t),
2015-08-03 15:22:00 +00:00
},
})
}
2015-08-03 23:04:07 +00:00
func TestSSHBackend_OTPCreate(t *testing.T) {
logicaltest.Test(t, logicaltest.TestCase{
Factory: Factory,
Steps: []logicaltest.TestStep{
2015-08-30 18:30:59 +00:00
testRoleWrite(t, testOTPRoleName, testOTPRoleData),
2015-08-04 19:30:24 +00:00
testCredsWrite(t, testOTPRoleName),
2015-08-03 23:04:07 +00:00
},
})
}
2015-08-18 23:48:50 +00:00
func TestSSHBackend_VerifyEcho(t *testing.T) {
verifyData := map[string]interface{}{
"otp": api.VerifyEchoRequest,
}
expectedData := map[string]interface{}{
"message": api.VerifyEchoResponse,
}
logicaltest.Test(t, logicaltest.TestCase{
Factory: Factory,
Steps: []logicaltest.TestStep{
testVerifyWrite(t, verifyData, expectedData),
},
})
}
2015-08-30 18:17:50 +00:00
func TestSSHBackend_ConfigZeroAddressCRUD(t *testing.T) {
zeroAddressData1 := map[string]interface{}{
"roles": testOTPRoleName,
}
zeroAddressData2 := map[string]interface{}{
"roles": fmt.Sprintf("%s,%s", testOTPRoleName, testDynamicRoleName),
}
zeroAddressData3 := map[string]interface{}{
"roles": "",
}
logicaltest.Test(t, logicaltest.TestCase{
Factory: Factory,
Steps: []logicaltest.TestStep{
2015-08-30 18:30:59 +00:00
testRoleWrite(t, testOTPRoleName, testOTPRoleData),
2015-08-30 18:17:50 +00:00
testConfigZeroAddressWrite(t, zeroAddressData1),
testConfigZeroAddressRead(t, zeroAddressData1),
testNamedKeysWrite(t),
2015-08-30 18:30:59 +00:00
testRoleWrite(t, testDynamicRoleName, testDynamicRoleData),
2015-08-30 18:17:50 +00:00
testConfigZeroAddressWrite(t, zeroAddressData2),
testConfigZeroAddressRead(t, zeroAddressData2),
testRoleDelete(t, testDynamicRoleName),
testConfigZeroAddressRead(t, zeroAddressData1),
testRoleDelete(t, testOTPRoleName),
testConfigZeroAddressRead(t, zeroAddressData3),
testConfigZeroAddressDelete(t),
},
})
}
func testConfigZeroAddressDelete(t *testing.T) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.DeleteOperation,
Path: "config/zeroaddress",
}
}
func testConfigZeroAddressWrite(t *testing.T, d map[string]interface{}) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.WriteOperation,
Path: "config/zeroaddress",
Data: d,
}
}
func testConfigZeroAddressRead(t *testing.T, expected map[string]interface{}) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.ReadOperation,
Path: "config/zeroaddress",
Check: func(resp *logical.Response) error {
var d zeroAddressRoles
if err := mapstructure.Decode(resp.Data, &d); err != nil {
return err
}
var ex zeroAddressRoles
if err := mapstructure.Decode(expected, &ex); err != nil {
return err
}
if !reflect.DeepEqual(d, ex) {
return fmt.Errorf("Response mismatch:\nActual:%#v\nExpected:%#v", d, ex)
}
return nil
},
}
}
2015-08-18 23:48:50 +00:00
func testVerifyWrite(t *testing.T, d map[string]interface{}, expected map[string]interface{}) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.WriteOperation,
Path: fmt.Sprintf("verify"),
Data: d,
Check: func(resp *logical.Response) error {
var ac api.SSHVerifyResponse
if err := mapstructure.Decode(resp.Data, &ac); err != nil {
return err
}
var ex api.SSHVerifyResponse
if err := mapstructure.Decode(expected, &ex); err != nil {
return err
}
2015-08-30 18:17:50 +00:00
if !reflect.DeepEqual(ac, ex) {
2015-08-18 23:48:50 +00:00
return fmt.Errorf("Invalid response")
}
return nil
},
}
}
2015-08-04 19:30:24 +00:00
func testCredsWrite(t *testing.T, name string) logicaltest.TestStep {
2015-08-03 23:04:07 +00:00
data := map[string]interface{}{
"ip": testIP,
}
return logicaltest.TestStep{
Operation: logical.WriteOperation,
Path: fmt.Sprintf("creds/%s", name),
Data: data,
Check: func(resp *logical.Response) error {
if resp == nil {
return fmt.Errorf("response is nil")
}
if resp.Data == nil {
return fmt.Errorf("data is nil")
}
if resp.Data["key_type"] != KeyTypeOTP {
return fmt.Errorf("Incorrect key_type")
}
if resp.Data["key"] == nil {
return fmt.Errorf("Invalid key")
}
2015-08-04 19:30:24 +00:00
testOTP = resp.Data["key"].(string)
2015-08-03 23:04:07 +00:00
return nil
},
}
}
2015-08-03 20:18:14 +00:00
func testNamedKeysRead(t *testing.T, key string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.ReadOperation,
Path: fmt.Sprintf("keys/%s", testKeyName),
Check: func(resp *logical.Response) error {
if key != "" {
if resp == nil || resp.Data == nil {
return fmt.Errorf("Key missing in response")
}
var d struct {
Key string `mapstructure:"key"`
}
if err := mapstructure.Decode(resp.Data, &d); err != nil {
return err
}
if d.Key != key {
return fmt.Errorf("Key mismatch")
}
}
return nil
},
}
}
func testNamedKeysWrite(t *testing.T) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.WriteOperation,
Path: fmt.Sprintf("keys/%s", testKeyName),
Data: map[string]interface{}{
"key": testSharedPrivateKey,
},
}
}
func testNamedKeysDelete(t *testing.T) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.DeleteOperation,
Path: fmt.Sprintf("keys/%s", testKeyName),
}
}
2015-08-03 15:22:00 +00:00
func testLookupRead(t *testing.T, data map[string]interface{}, length int) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.WriteOperation,
Path: "lookup",
Data: data,
Check: func(resp *logical.Response) error {
if resp.Data == nil || resp.Data["roles"] == nil {
return fmt.Errorf("Missing roles information")
}
if len(resp.Data["roles"].([]string)) != length {
return fmt.Errorf("Role information incorrect")
}
return nil
},
}
}
2015-07-31 19:17:40 +00:00
func testRoleWrite(t *testing.T, name string, data map[string]interface{}) logicaltest.TestStep {
2015-07-31 17:24:23 +00:00
return logicaltest.TestStep{
Operation: logical.WriteOperation,
2015-07-31 19:17:40 +00:00
Path: "roles/" + name,
2015-07-31 17:24:23 +00:00
Data: data,
}
}
2015-07-31 19:17:40 +00:00
func testRoleRead(t *testing.T, name string, data map[string]interface{}) logicaltest.TestStep {
2015-07-31 17:24:23 +00:00
return logicaltest.TestStep{
Operation: logical.ReadOperation,
2015-07-31 19:17:40 +00:00
Path: "roles/" + name,
2015-07-31 17:24:23 +00:00
Check: func(resp *logical.Response) error {
if resp == nil {
if data == nil {
return nil
}
return fmt.Errorf("bad: %#v", resp)
}
2015-07-31 19:17:40 +00:00
var d sshRole
2015-07-31 17:24:23 +00:00
if err := mapstructure.Decode(resp.Data, &d); err != nil {
2015-07-31 19:17:40 +00:00
return fmt.Errorf("error decoding response:%s", err)
2015-07-31 17:24:23 +00:00
}
2015-07-31 19:17:40 +00:00
if name == testOTPRoleName {
2015-08-13 15:46:55 +00:00
if d.KeyType != data["key_type"] || d.DefaultUser != data["default_user"] || d.CIDRList != data["cidr_list"] {
2015-07-31 19:17:40 +00:00
return fmt.Errorf("data mismatch. bad: %#v", resp)
}
} else {
2015-08-13 15:46:55 +00:00
if d.AdminUser != data["admin_user"] || d.CIDRList != data["cidr_list"] || d.KeyName != data["key"] || d.KeyType != data["key_type"] {
2015-07-31 19:17:40 +00:00
return fmt.Errorf("data mismatch. bad: %#v", resp)
}
2015-07-31 17:24:23 +00:00
}
return nil
},
}
}
2015-07-31 19:17:40 +00:00
func testRoleDelete(t *testing.T, name string) logicaltest.TestStep {
2015-07-31 17:24:23 +00:00
return logicaltest.TestStep{
Operation: logical.DeleteOperation,
2015-07-31 19:17:40 +00:00
Path: "roles/" + name,
2015-07-31 17:24:23 +00:00
}
}
2015-07-24 16:13:26 +00:00
func testNewDynamicKeyRole(t *testing.T) logicaltest.TestStep {
2015-07-10 23:27:21 +00:00
return logicaltest.TestStep{
Operation: logical.WriteOperation,
2015-07-31 17:24:23 +00:00
Path: fmt.Sprintf("roles/%s", testDynamicRoleName),
2015-08-30 18:30:59 +00:00
Data: testDynamicRoleData,
2015-07-10 23:27:21 +00:00
}
}
2015-07-24 16:13:26 +00:00
func testDynamicKeyCredsCreate(t *testing.T) logicaltest.TestStep {
2015-07-10 15:56:14 +00:00
return logicaltest.TestStep{
Operation: logical.WriteOperation,
2015-07-31 17:24:23 +00:00
Path: fmt.Sprintf("creds/%s", testDynamicRoleName),
2015-07-10 15:56:14 +00:00
Data: map[string]interface{}{
"username": testUserName,
"ip": testIP,
},
Check: func(resp *logical.Response) error {
var d struct {
Key string `mapstructure:"key"`
}
if err := mapstructure.Decode(resp.Data, &d); err != nil {
return err
}
if d.Key == "" {
return fmt.Errorf("Generated key is an empty string")
}
_, err := ssh.ParsePrivateKey([]byte(d.Key))
if err != nil {
return fmt.Errorf("Generated key is invalid")
}
return nil
},
}
}