-
Notifications
You must be signed in to change notification settings - Fork 73
/
expect.go
62 lines (55 loc) · 1.64 KB
/
expect.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
package spiffe
import (
"crypto/x509"
"fmt"
"net/url"
)
// ExpectPeerFunc is invoked after peer SVID verification to validate the SVID
// belongs to th indended peer. If an error is returned, verification (i.e. TLS
// handshake) fails.
type ExpectPeerFunc func(peerID string, verifiedChains [][]*x509.Certificate) error
// ExpectAnyPeer allows any peer
func ExpectAnyPeer() ExpectPeerFunc {
return func(string, [][]*x509.Certificate) error {
return nil
}
}
// ExpectPeer allows a peer matching the specified peer ID
func ExpectPeer(expectedID string) ExpectPeerFunc {
return func(peerID string, _ [][]*x509.Certificate) error {
if peerID != expectedID {
return fmt.Errorf("unexpected peer ID %q", peerID)
}
return nil
}
}
// ExpectPeers allows any peer to belong to the provided set.
func ExpectPeers(expectedIDs ...string) ExpectPeerFunc {
m := make(map[string]struct{}, len(expectedIDs))
for _, id := range expectedIDs {
m[id] = struct{}{}
}
return func(peerID string, _ [][]*x509.Certificate) error {
if _, ok := m[peerID]; !ok {
return fmt.Errorf("unexpected peer ID %q", peerID)
}
return nil
}
}
// ExpectPeerInDomain returns a callback that asserts that the peer ID belongs
// to the provided trust domain (i.e. "domain.test")
func ExpectPeerInDomain(expectedDomain string) ExpectPeerFunc {
return func(peerID string, _ [][]*x509.Certificate) error {
if domain := getPeerTrustDomain(peerID); domain != expectedDomain {
return fmt.Errorf("unexpected peer trust domain %q", domain)
}
return nil
}
}
func getPeerTrustDomain(peerID string) string {
u, _ := url.Parse(peerID)
if u != nil {
return u.Host
}
return ""
}