2017-03-03 15:19:45 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2022-02-15 19:14:05 +00:00
|
|
|
"strings"
|
2017-03-03 15:19:45 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-03-03 15:19:45 +00:00
|
|
|
)
|
|
|
|
|
2017-03-08 22:36:21 +00:00
|
|
|
func TestSSH_ConfigCAStorageUpgrade(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
|
|
|
|
b, err := Backend(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = b.Setup(context.Background(), config)
|
2017-03-08 22:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store at an older path
|
2018-01-19 06:44:44 +00:00
|
|
|
err = config.StorageView.Put(context.Background(), &logical.StorageEntry{
|
2017-03-08 22:36:21 +00:00
|
|
|
Key: caPrivateKeyStoragePathDeprecated,
|
2020-06-11 12:10:13 +00:00
|
|
|
Value: []byte(testCAPrivateKey),
|
2017-03-08 22:36:21 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading it should return the key as well as upgrade the storage path
|
2018-01-19 06:44:44 +00:00
|
|
|
privateKeyEntry, err := caKey(context.Background(), config.StorageView, caPrivateKey)
|
2017-03-08 22:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if privateKeyEntry == nil || privateKeyEntry.Key == "" {
|
|
|
|
t.Fatalf("failed to read the stored private key")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := config.StorageView.Get(context.Background(), caPrivateKeyStoragePathDeprecated)
|
2017-03-08 22:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entry != nil {
|
|
|
|
t.Fatalf("bad: expected a nil entry after upgrade")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err = config.StorageView.Get(context.Background(), caPrivateKeyStoragePath)
|
2017-03-08 22:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
t.Fatalf("bad: expected a non-nil entry after upgrade")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store at an older path
|
2018-01-19 06:44:44 +00:00
|
|
|
err = config.StorageView.Put(context.Background(), &logical.StorageEntry{
|
2017-03-08 22:36:21 +00:00
|
|
|
Key: caPublicKeyStoragePathDeprecated,
|
2020-06-11 12:10:13 +00:00
|
|
|
Value: []byte(testCAPublicKey),
|
2017-03-08 22:36:21 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading it should return the key as well as upgrade the storage path
|
2018-01-19 06:44:44 +00:00
|
|
|
publicKeyEntry, err := caKey(context.Background(), config.StorageView, caPublicKey)
|
2017-03-08 22:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if publicKeyEntry == nil || publicKeyEntry.Key == "" {
|
|
|
|
t.Fatalf("failed to read the stored public key")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err = config.StorageView.Get(context.Background(), caPublicKeyStoragePathDeprecated)
|
2017-03-08 22:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entry != nil {
|
|
|
|
t.Fatalf("bad: expected a nil entry after upgrade")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err = config.StorageView.Get(context.Background(), caPublicKeyStoragePath)
|
2017-03-08 22:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
t.Fatalf("bad: expected a non-nil entry after upgrade")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 15:19:45 +00:00
|
|
|
func TestSSH_ConfigCAUpdateDelete(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2017-03-03 15:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create backend: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caReq := &logical.Request{
|
|
|
|
Path: "config/ca",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Storage: config.StorageView,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auto-generate the keys
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), caReq)
|
2017-03-03 15:19:45 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v, resp:%v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail to overwrite it
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), caReq)
|
2018-12-11 20:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error, got %#v", *resp)
|
2017-03-03 15:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
caReq.Operation = logical.DeleteOperation
|
|
|
|
// Delete the configured keys
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), caReq)
|
2017-03-03 15:19:45 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v, resp:%v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
caReq.Operation = logical.UpdateOperation
|
|
|
|
caReq.Data = map[string]interface{}{
|
2020-06-11 12:10:13 +00:00
|
|
|
"public_key": testCAPublicKey,
|
|
|
|
"private_key": testCAPrivateKey,
|
2017-03-03 15:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Successfully create a new one
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), caReq)
|
2017-03-03 15:19:45 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v, resp:%v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail to overwrite it
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), caReq)
|
2018-12-11 20:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error, got %#v", *resp)
|
2017-03-03 15:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
caReq.Operation = logical.DeleteOperation
|
|
|
|
// Delete the configured keys
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), caReq)
|
2017-03-03 15:19:45 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v, resp:%v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
caReq.Operation = logical.UpdateOperation
|
|
|
|
caReq.Data = nil
|
|
|
|
|
|
|
|
// Successfully create a new one
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), caReq)
|
2017-03-03 15:19:45 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v, resp:%v", err, resp)
|
|
|
|
}
|
2022-02-15 19:14:05 +00:00
|
|
|
|
|
|
|
// Delete the configured keys
|
|
|
|
caReq.Operation = logical.DeleteOperation
|
|
|
|
resp, err = b.HandleRequest(context.Background(), caReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v, resp:%v", err, resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDeleteHelper(t *testing.T, b logical.Backend, config *logical.BackendConfig, index int, keyType string, keyBits int) {
|
|
|
|
// Check that we can create a new key of the specified type
|
|
|
|
caReq := &logical.Request{
|
|
|
|
Path: "config/ca",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Storage: config.StorageView,
|
|
|
|
}
|
|
|
|
caReq.Data = map[string]interface{}{
|
|
|
|
"generate_signing_key": true,
|
|
|
|
"key_type": keyType,
|
|
|
|
"key_bits": keyBits,
|
|
|
|
}
|
|
|
|
resp, err := b.HandleRequest(context.Background(), caReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
2022-02-17 20:17:59 +00:00
|
|
|
t.Fatalf("bad case %v: err: %v, resp: %v", index, err, resp)
|
2022-02-15 19:14:05 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(resp.Data["public_key"].(string), caReq.Data["key_type"].(string)) {
|
|
|
|
t.Fatalf("bad case %v: expected public key of type %v but was %v", index, caReq.Data["key_type"], resp.Data["public_key"])
|
|
|
|
}
|
|
|
|
|
2022-02-17 20:17:59 +00:00
|
|
|
issueOptions := map[string]interface{}{
|
|
|
|
"public_key": testCAPublicKeyEd25519,
|
|
|
|
}
|
|
|
|
issueReq := &logical.Request{
|
|
|
|
Path: "sign/ca-issuance",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: issueOptions,
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), issueReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad case %v: err: %v, resp: %v", index, err, resp)
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:14:05 +00:00
|
|
|
// Delete the configured keys
|
|
|
|
caReq.Operation = logical.DeleteOperation
|
|
|
|
resp, err = b.HandleRequest(context.Background(), caReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
2022-02-17 20:17:59 +00:00
|
|
|
t.Fatalf("bad case %v: err: %v, resp: %v", index, err, resp)
|
2022-02-15 19:14:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSH_ConfigCAKeyTypes(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
|
|
|
|
b, err := Factory(context.Background(), config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create backend: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
keyType string
|
|
|
|
keyBits int
|
|
|
|
}{
|
|
|
|
{"ssh-rsa", 2048},
|
|
|
|
{"ssh-rsa", 4096},
|
|
|
|
{"ssh-rsa", 0},
|
|
|
|
{"rsa", 2048},
|
|
|
|
{"rsa", 4096},
|
|
|
|
{"ecdsa-sha2-nistp256", 0},
|
|
|
|
{"ecdsa-sha2-nistp384", 0},
|
|
|
|
{"ecdsa-sha2-nistp521", 0},
|
|
|
|
{"ec", 256},
|
|
|
|
{"ec", 384},
|
|
|
|
{"ec", 521},
|
|
|
|
{"ec", 0},
|
|
|
|
{"ssh-ed25519", 0},
|
|
|
|
{"ed25519", 0},
|
|
|
|
}
|
|
|
|
|
2022-02-17 20:17:59 +00:00
|
|
|
// Create a role for ssh signing.
|
|
|
|
roleOptions := map[string]interface{}{
|
|
|
|
"allow_user_certificates": true,
|
|
|
|
"allowed_users": "*",
|
|
|
|
"key_type": "ca",
|
|
|
|
"ttl": "30s",
|
|
|
|
}
|
|
|
|
roleReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/ca-issuance",
|
|
|
|
Data: roleOptions,
|
|
|
|
Storage: config.StorageView,
|
|
|
|
}
|
|
|
|
_, err = b.HandleRequest(context.Background(), roleReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create role to issue against: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:14:05 +00:00
|
|
|
for index, scenario := range cases {
|
|
|
|
createDeleteHelper(t, b, config, index, scenario.keyType, scenario.keyBits)
|
|
|
|
}
|
2017-03-03 15:19:45 +00:00
|
|
|
}
|