-
Notifications
You must be signed in to change notification settings - Fork 53
/
client.go
165 lines (145 loc) · 4.27 KB
/
client.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package bootstrap
import (
"context"
"crypto/x509"
"errors"
"fmt"
"os"
bootstrapv1 "github.com/rancher/opni/pkg/apis/bootstrap/v1"
"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"
"k8s.io/client-go/rest"
)
type ClientConfig struct {
Capability string
Token *tokens.Token
Endpoint string
DialOpts []grpc.DialOption
K8sConfig *rest.Config
K8sNamespace string
TrustStrategy trust.Strategy
}
func (c *ClientConfig) 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 := bootstrapv1.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 := &bootstrapv1.BootstrapAuthRequest{
ClientID: id,
ClientPubKey: ekp.PublicKey,
Capability: c.Capability,
}
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)
}
sharedSecret, err := ecdh.DeriveSharedSecret(ekp, ecdh.PeerPublicKey{
PublicKey: authResp.ServerPubKey,
PeerType: ecdh.PeerTypeServer,
})
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 *ClientConfig) Finalize(ctx context.Context) error {
if c.K8sConfig == nil {
return nil
}
ns := c.K8sNamespace
if ns == "" {
if nsEnv, ok := os.LookupEnv("POD_NAMESPACE"); ok {
ns = nsEnv
} else {
return errors.New("POD_NAMESPACE not set, and no namespace was explicitly configured")
}
}
return eraseBootstrapTokensFromConfig(ctx, c.K8sConfig, ns)
}
func (c *ClientConfig) bootstrapJoin(ctx context.Context) (*bootstrapv1.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 := bootstrapv1.NewBootstrapClient(cc)
var peer peer.Peer
resp, err := client.Join(ctx, &bootstrapv1.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 *ClientConfig) 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
}