-
Notifications
You must be signed in to change notification settings - Fork 476
/
server_test.go
140 lines (116 loc) · 4.37 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package server
import (
"bytes"
"context"
"fmt"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/spiffe/spire/proto/spire/common"
"github.com/spiffe/spire/test/fakes/fakedatastore"
"github.com/stretchr/testify/suite"
)
type ServerTestSuite struct {
suite.Suite
server *Server
ds *fakedatastore.DataStore
stdout *bytes.Buffer
}
func (suite *ServerTestSuite) SetupTest() {
suite.ds = fakedatastore.New(suite.T())
suite.stdout = new(bytes.Buffer)
logrusLevel, err := logrus.ParseLevel("DEBUG")
suite.Nil(err)
logger := logrus.New()
logger.Out = suite.stdout
logger.Level = logrusLevel
suite.server = New(Config{
Log: logger,
TrustDomain: spiffeid.RequireTrustDomainFromString("example.org"),
})
}
func TestServerTestSuite(t *testing.T) {
suite.Run(t, new(ServerTestSuite))
}
func (suite *ServerTestSuite) TestValidateTrustDomain() {
ctx := context.Background()
ds := suite.ds
// Create default trust domain
trustDomain, err := spiffeid.TrustDomainFromString("spiffe://test.com")
suite.NoError(err)
// Create new trust domain
newTrustDomain, err := spiffeid.TrustDomainFromString("spiffe://new_test.com")
suite.NoError(err)
// Set trust domain to server
suite.server.config.TrustDomain = trustDomain
suite.NoError(err)
// No attested nodes, not error expected
err = suite.server.validateTrustDomain(ctx, ds)
suite.NoError(err)
// create attested node with current trust domain
attestedNode, err := ds.CreateAttestedNode(ctx, &common.AttestedNode{
SpiffeId: "spiffe://test.com/host",
AttestationDataType: "fake_nodeattestor_1",
CertNotAfter: time.Now().Add(time.Hour).Unix(),
CertSerialNumber: "18392437442709699290",
})
suite.NoError(err)
// Validate created trust domain, no error expected
err = suite.server.validateTrustDomain(ctx, ds)
suite.NoError(err)
// Update server trust domain to force errors
suite.server.config.TrustDomain = newTrustDomain
// Validate new trust domain
err = suite.server.validateTrustDomain(ctx, ds)
// no error expected, warning is displaying in this case
suite.NoError(err)
suite.Require().Contains(suite.stdout.String(), fmt.Sprintf(invalidTrustDomainAttestedNode, "test.com", "new_test.com"))
// Restore original trust domain
suite.server.config.TrustDomain = trustDomain
// Create a registration entry with original trust domain
registrationEntry, err := ds.CreateRegistrationEntry(ctx, &common.RegistrationEntry{
SpiffeId: "spiffe://test.com/foo",
Selectors: []*common.Selector{{Type: "TYPE", Value: "VALUE"}},
})
suite.NoError(err)
// Attested node and registration entry have the same trust domain as server, no error expected
err = suite.server.validateTrustDomain(ctx, ds)
suite.NoError(err)
// Update server's trust domain, error expected because invalid trust domain
suite.server.config.TrustDomain = newTrustDomain
err = suite.server.validateTrustDomain(ctx, ds)
suite.EqualError(err, fmt.Sprintf(invalidTrustDomainRegistrationEntry, "test.com", "new_test.com"))
// Create a registration entry with an invalid url
_, err = ds.DeleteRegistrationEntry(ctx, registrationEntry.EntryId)
suite.NoError(err)
suite.server.config.TrustDomain = trustDomain
registrationEntry, err = ds.CreateRegistrationEntry(ctx, &common.RegistrationEntry{
SpiffeId: "spiffe://inv%ild/test",
Selectors: []*common.Selector{{Type: "TYPE", Value: "VALUE"}},
})
suite.NoError(err)
err = suite.server.validateTrustDomain(ctx, ds)
expectedError := fmt.Sprintf(invalidSpiffeIDRegistrationEntry, registrationEntry.EntryId, "")
if suite.Error(err) {
suite.Contains(err.Error(), expectedError)
}
// remove entry to solve error
_, err = ds.DeleteRegistrationEntry(ctx, registrationEntry.EntryId)
suite.NoError(err)
// create attested node with current trust domain
// drop resp
_, err = ds.DeleteAttestedNode(ctx, attestedNode.SpiffeId)
suite.NoError(err)
_, err = ds.CreateAttestedNode(ctx, &common.AttestedNode{
SpiffeId: "spiffe://inv%ild/host",
AttestationDataType: "fake_nodeattestor_1",
CertNotAfter: time.Now().Add(time.Hour).Unix(),
CertSerialNumber: "18392437442709699290",
})
suite.NoError(err)
// Attested now with same trust domain created, no error expected
err = suite.server.validateTrustDomain(ctx, ds)
suite.NoError(err)
suite.Require().Contains(suite.stdout.String(), invalidSpiffeIDAttestedNode)
}