2018-01-18 21:49:20 +00:00
|
|
|
package pb
|
|
|
|
|
|
|
|
import (
|
2021-10-07 12:06:09 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2018-01-18 21:49:20 +00:00
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/errutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/wrapping"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-01-18 21:49:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTranslation_Errors(t *testing.T) {
|
|
|
|
errs := []error{
|
|
|
|
nil,
|
|
|
|
errors.New("test"),
|
|
|
|
errutil.UserError{Err: "test"},
|
|
|
|
errutil.InternalError{Err: "test"},
|
|
|
|
logical.CodedError(403, "test"),
|
|
|
|
&logical.StatusBadRequest{Err: "test"},
|
|
|
|
logical.ErrUnsupportedOperation,
|
|
|
|
logical.ErrUnsupportedPath,
|
|
|
|
logical.ErrInvalidRequest,
|
|
|
|
logical.ErrPermissionDenied,
|
|
|
|
logical.ErrMultiAuthzPending,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, err := range errs {
|
|
|
|
pe := ErrToProtoErr(err)
|
|
|
|
e := ProtoErrToErr(pe)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(e, err) {
|
2018-02-05 01:37:57 +00:00
|
|
|
t.Fatalf("Errs did not match: %#v, %#v", e, err)
|
2018-01-18 21:49:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTranslation_StorageEntry(t *testing.T) {
|
|
|
|
tCases := []*logical.StorageEntry{
|
|
|
|
nil,
|
2021-04-08 16:43:39 +00:00
|
|
|
{Key: "key", Value: []byte("value")},
|
|
|
|
{Key: "key1", Value: []byte("value1"), SealWrap: true},
|
|
|
|
{Key: "key1", SealWrap: true},
|
2018-01-18 21:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range tCases {
|
|
|
|
p := LogicalStorageEntryToProtoStorageEntry(c)
|
|
|
|
e := ProtoStorageEntryToLogicalStorageEntry(p)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(c, e) {
|
2018-02-05 01:37:57 +00:00
|
|
|
t.Fatalf("Entries did not match: %#v, %#v", e, c)
|
2018-01-18 21:49:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTranslation_Request(t *testing.T) {
|
2021-10-07 12:06:09 +00:00
|
|
|
certs, err := peerCertificates()
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("No test certificates were generated: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:49:20 +00:00
|
|
|
tCases := []*logical.Request{
|
|
|
|
nil,
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2018-01-18 21:49:20 +00:00
|
|
|
ID: "ID",
|
|
|
|
ReplicationCluster: "RID",
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "test/foo",
|
|
|
|
ClientToken: "token",
|
|
|
|
ClientTokenAccessor: "accessor",
|
|
|
|
DisplayName: "display",
|
|
|
|
MountPoint: "test",
|
|
|
|
MountType: "secret",
|
|
|
|
MountAccessor: "test-231234",
|
|
|
|
ClientTokenRemainingUses: 1,
|
|
|
|
EntityID: "tester",
|
|
|
|
PolicyOverride: true,
|
|
|
|
Unauthenticated: true,
|
2018-02-17 01:41:01 +00:00
|
|
|
Connection: &logical.Connection{
|
|
|
|
RemoteAddr: "localhost",
|
2021-10-07 12:06:09 +00:00
|
|
|
ConnState: &tls.ConnectionState{
|
|
|
|
Version: tls.VersionTLS12,
|
|
|
|
HandshakeComplete: true,
|
|
|
|
PeerCertificates: certs,
|
|
|
|
},
|
2018-02-17 01:41:01 +00:00
|
|
|
},
|
2018-01-18 21:49:20 +00:00
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2018-01-18 21:49:20 +00:00
|
|
|
ID: "ID",
|
|
|
|
ReplicationCluster: "RID",
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "test/foo",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"string": "string",
|
|
|
|
"bool": true,
|
|
|
|
"array": []interface{}{"1", "2"},
|
|
|
|
"map": map[string]interface{}{
|
|
|
|
"key": "value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: time.Second,
|
2018-04-03 16:20:20 +00:00
|
|
|
MaxTTL: time.Second,
|
2018-01-18 21:49:20 +00:00
|
|
|
Renewable: true,
|
|
|
|
Increment: time.Second,
|
|
|
|
IssueTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"role": "test",
|
|
|
|
},
|
|
|
|
LeaseID: "LeaseID",
|
|
|
|
},
|
|
|
|
Auth: &logical.Auth{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: time.Second,
|
2018-04-03 16:20:20 +00:00
|
|
|
MaxTTL: time.Second,
|
2018-01-18 21:49:20 +00:00
|
|
|
Renewable: true,
|
|
|
|
Increment: time.Second,
|
|
|
|
IssueTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"role": "test",
|
|
|
|
},
|
|
|
|
DisplayName: "test",
|
|
|
|
Policies: []string{"test", "Test"},
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"test": "test",
|
|
|
|
},
|
|
|
|
ClientToken: "token",
|
|
|
|
Accessor: "accessor",
|
|
|
|
Period: 5 * time.Second,
|
|
|
|
NumUses: 1,
|
|
|
|
EntityID: "id",
|
|
|
|
Alias: &logical.Alias{
|
|
|
|
MountType: "type",
|
|
|
|
MountAccessor: "accessor",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
GroupAliases: []*logical.Alias{
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2018-01-18 21:49:20 +00:00
|
|
|
MountType: "type",
|
|
|
|
MountAccessor: "accessor",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Headers: map[string][]string{
|
2021-04-08 16:43:39 +00:00
|
|
|
"X-Vault-Test": {"test"},
|
2018-01-18 21:49:20 +00:00
|
|
|
},
|
|
|
|
ClientToken: "token",
|
|
|
|
ClientTokenAccessor: "accessor",
|
|
|
|
DisplayName: "display",
|
|
|
|
MountPoint: "test",
|
|
|
|
MountType: "secret",
|
|
|
|
MountAccessor: "test-231234",
|
|
|
|
WrapInfo: &logical.RequestWrapInfo{
|
|
|
|
TTL: time.Second,
|
|
|
|
Format: "token",
|
|
|
|
SealWrap: true,
|
|
|
|
},
|
|
|
|
ClientTokenRemainingUses: 1,
|
|
|
|
EntityID: "tester",
|
|
|
|
PolicyOverride: true,
|
|
|
|
Unauthenticated: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range tCases {
|
|
|
|
p, err := LogicalRequestToProtoRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, err := ProtoRequestToLogicalRequest(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(c, r) {
|
|
|
|
t.Fatalf("Requests did not match: \n%#v, \n%#v", c, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTranslation_Response(t *testing.T) {
|
|
|
|
tCases := []*logical.Response{
|
|
|
|
nil,
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2018-01-18 21:49:20 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"data": "blah",
|
|
|
|
},
|
|
|
|
Warnings: []string{"warning"},
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2018-01-18 21:49:20 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"string": "string",
|
|
|
|
"bool": true,
|
|
|
|
"array": []interface{}{"1", "2"},
|
|
|
|
"map": map[string]interface{}{
|
|
|
|
"key": "value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: time.Second,
|
2018-04-03 16:20:20 +00:00
|
|
|
MaxTTL: time.Second,
|
2018-01-18 21:49:20 +00:00
|
|
|
Renewable: true,
|
|
|
|
Increment: time.Second,
|
|
|
|
IssueTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"role": "test",
|
|
|
|
},
|
|
|
|
LeaseID: "LeaseID",
|
|
|
|
},
|
|
|
|
Auth: &logical.Auth{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: time.Second,
|
2018-04-03 16:20:20 +00:00
|
|
|
MaxTTL: time.Second,
|
2018-01-18 21:49:20 +00:00
|
|
|
Renewable: true,
|
|
|
|
Increment: time.Second,
|
|
|
|
IssueTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"role": "test",
|
|
|
|
},
|
|
|
|
DisplayName: "test",
|
|
|
|
Policies: []string{"test", "Test"},
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"test": "test",
|
|
|
|
},
|
|
|
|
ClientToken: "token",
|
|
|
|
Accessor: "accessor",
|
|
|
|
Period: 5 * time.Second,
|
|
|
|
NumUses: 1,
|
|
|
|
EntityID: "id",
|
|
|
|
Alias: &logical.Alias{
|
|
|
|
MountType: "type",
|
|
|
|
MountAccessor: "accessor",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
GroupAliases: []*logical.Alias{
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2018-01-18 21:49:20 +00:00
|
|
|
MountType: "type",
|
|
|
|
MountAccessor: "accessor",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
WrapInfo: &wrapping.ResponseWrapInfo{
|
|
|
|
TTL: time.Second,
|
|
|
|
Token: "token",
|
|
|
|
Accessor: "accessor",
|
|
|
|
CreationTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
WrappedAccessor: "wrapped-accessor",
|
|
|
|
WrappedEntityID: "id",
|
|
|
|
Format: "token",
|
|
|
|
CreationPath: "test/foo",
|
|
|
|
SealWrap: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range tCases {
|
|
|
|
p, err := LogicalResponseToProtoResponse(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, err := ProtoResponseToLogicalResponse(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(c, r) {
|
|
|
|
t.Fatalf("Requests did not match: \n%#v, \n%#v", c, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 12:06:09 +00:00
|
|
|
|
|
|
|
// This is the contents of $GOROOT/src/crypto/tls/testdata/example-cert.pem
|
|
|
|
// If it's good enough for testing the crypto/tls package it's good enough
|
|
|
|
// for Vault.
|
|
|
|
const exampleCert = `
|
|
|
|
-----BEGIN CERTIFICATE-----
|
|
|
|
MIIBhTCCASugAwIBAgIQIRi6zePL6mKjOipn+dNuaTAKBggqhkjOPQQDAjASMRAw
|
|
|
|
DgYDVQQKEwdBY21lIENvMB4XDTE3MTAyMDE5NDMwNloXDTE4MTAyMDE5NDMwNlow
|
|
|
|
EjEQMA4GA1UEChMHQWNtZSBDbzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABD0d
|
|
|
|
7VNhbWvZLWPuj/RtHFjvtJBEwOkhbN/BnnE8rnZR8+sbwnc/KhCk3FhnpHZnQz7B
|
|
|
|
5aETbbIgmuvewdjvSBSjYzBhMA4GA1UdDwEB/wQEAwICpDATBgNVHSUEDDAKBggr
|
|
|
|
BgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdEQQiMCCCDmxvY2FsaG9zdDo1
|
|
|
|
NDUzgg4xMjcuMC4wLjE6NTQ1MzAKBggqhkjOPQQDAgNIADBFAiEA2zpJEPQyz6/l
|
|
|
|
Wf86aX6PepsntZv2GYlA5UpabfT2EZICICpJ5h/iI+i341gBmLiAFQOyTDT+/wQc
|
|
|
|
6MF9+Yw1Yy0t
|
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
|
|
|
|
func peerCertificates() ([]*x509.Certificate, error) {
|
|
|
|
blk, _ := pem.Decode([]byte(exampleCert))
|
|
|
|
if blk == nil {
|
|
|
|
return nil, errors.New("cannot decode example certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(blk.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*x509.Certificate{cert}, nil
|
|
|
|
}
|