-
Notifications
You must be signed in to change notification settings - Fork 53
/
client_v2.go
145 lines (127 loc) · 3.84 KB
/
client_v2.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
141
142
143
144
145
package bootstrap
import (
"context"
"crypto/x509"
"fmt"
bootstrapv2 "github.com/rancher/opni/pkg/apis/bootstrap/v2"
"github.com/rancher/opni/pkg/auth"
"github.com/rancher/opni/pkg/ecdh"
"github.com/rancher/opni/pkg/ident"
"github.com/rancher/opni/pkg/keyring"
"github.com/rancher/opni/pkg/tokens"
"github.com/rancher/opni/pkg/trust"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
)
type ClientConfigV2 struct {
Token *tokens.Token
Endpoint string
DialOpts []grpc.DialOption
TrustStrategy trust.Strategy
FriendlyName *string
}
func (c *ClientConfigV2) Bootstrap(
ctx context.Context,
ident ident.Provider,
) (keyring.Keyring, error) {
if c.Token == nil {
return nil, ErrNoToken
}
response, serverLeafCert, err := c.bootstrapJoin(ctx)
if err != nil {
return nil, err
}
completeJws, err := c.findValidSignature(
response.Signatures, serverLeafCert.PublicKey)
if err != nil {
return nil, err
}
tlsConfig, err := c.TrustStrategy.TLSConfig()
if err != nil {
return nil, err
}
cc, err := grpc.DialContext(ctx, c.Endpoint,
append(c.DialOpts,
grpc.WithBlock(),
grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
grpc.WithChainStreamInterceptor(otelgrpc.StreamClientInterceptor()),
grpc.WithChainUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
)...,
)
if err != nil {
return nil, fmt.Errorf("failed to dial gateway: %w", err)
}
defer cc.Close()
client := bootstrapv2.NewBootstrapClient(cc)
ekp := ecdh.NewEphemeralKeyPair()
id, err := ident.UniqueIdentifier(ctx)
if err != nil {
return nil, fmt.Errorf("failed to obtain unique identifier: %w", err)
}
authReq := &bootstrapv2.BootstrapAuthRequest{
ClientId: id,
ClientPubKey: ekp.PublicKey.Bytes(),
FriendlyName: c.FriendlyName,
}
authResp, err := client.Auth(metadata.NewOutgoingContext(ctx, metadata.Pairs(
auth.AuthorizationKey, "Bearer "+string(completeJws),
)), authReq)
if err != nil {
return nil, fmt.Errorf("auth request failed: %w", err)
}
serverPubKey, err := ecdh.ServerPubKey(authResp)
if err != nil {
return nil, err
}
sharedSecret, err := ecdh.DeriveSharedSecret(ekp, serverPubKey)
if err != nil {
return nil, err
}
keys := []any{keyring.NewSharedKeys(sharedSecret)}
if k := c.TrustStrategy.PersistentKey(); k != nil {
keys = append(keys, k)
}
return keyring.New(keys...), nil
}
func (c *ClientConfigV2) bootstrapJoin(ctx context.Context) (*bootstrapv2.BootstrapJoinResponse, *x509.Certificate, error) {
tlsConfig, err := c.TrustStrategy.TLSConfig()
if err != nil {
return nil, nil, err
}
cc, err := grpc.DialContext(ctx, c.Endpoint,
append(c.DialOpts,
grpc.WithBlock(),
grpc.FailOnNonTempDialError(true),
grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
grpc.WithChainStreamInterceptor(otelgrpc.StreamClientInterceptor()),
grpc.WithChainUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
)...,
)
if err != nil {
return nil, nil, fmt.Errorf("failed to dial gateway: %w", err)
}
defer cc.Close()
client := bootstrapv2.NewBootstrapClient(cc)
var peer peer.Peer
resp, err := client.Join(ctx, &bootstrapv2.BootstrapJoinRequest{}, grpc.Peer(&peer), grpc.WaitForReady(true))
if err != nil {
return nil, nil, fmt.Errorf("join request failed: %w", err)
}
tlsInfo, ok := peer.AuthInfo.(credentials.TLSInfo)
if !ok {
return nil, nil, fmt.Errorf("unexpected type for peer TLS info: %#T", peer.AuthInfo)
}
return resp, tlsInfo.State.PeerCertificates[0], nil
}
func (c *ClientConfigV2) findValidSignature(
signatures map[string][]byte,
pubKey interface{},
) ([]byte, error) {
if sig, ok := signatures[c.Token.HexID()]; ok {
return c.Token.VerifyDetached(sig, pubKey)
}
return nil, ErrNoValidSignature
}