-
Notifications
You must be signed in to change notification settings - Fork 53
/
server.go
257 lines (238 loc) · 8.28 KB
/
server.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package bootstrap
import (
"context"
"crypto"
"errors"
"fmt"
"strings"
"sync"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jws"
bootstrapv1 "github.com/rancher/opni/pkg/apis/bootstrap/v1"
capabilityv1 "github.com/rancher/opni/pkg/apis/capability/v1"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/capabilities"
"github.com/rancher/opni/pkg/ecdh"
"github.com/rancher/opni/pkg/health/annotations"
"github.com/rancher/opni/pkg/keyring"
"github.com/rancher/opni/pkg/storage"
"github.com/rancher/opni/pkg/tokens"
"github.com/rancher/opni/pkg/util"
"github.com/rancher/opni/pkg/validation"
"golang.org/x/exp/maps"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
type Server struct {
bootstrapv1.UnsafeBootstrapServer
privateKey crypto.Signer
storage Storage
capBackendStore capabilities.BackendStore
clusterIdLocks util.LockMap[string, *sync.Mutex]
}
func NewServer(storage Storage, privateKey crypto.Signer, capBackendStore capabilities.BackendStore) *Server {
return &Server{
privateKey: privateKey,
storage: storage,
capBackendStore: capBackendStore,
clusterIdLocks: util.NewLockMap[string, *sync.Mutex](),
}
}
func (h *Server) Join(ctx context.Context, _ *bootstrapv1.BootstrapJoinRequest) (*bootstrapv1.BootstrapJoinResponse, error) {
signatures := map[string][]byte{}
tokenList, err := h.storage.ListTokens(ctx)
if err != nil {
return nil, err
}
for _, token := range tokenList {
// Generate a JWS containing the signature of the detached secret token
rawToken, err := tokens.FromBootstrapToken(token)
if err != nil {
return nil, err
}
sig, err := rawToken.SignDetached(h.privateKey)
if err != nil {
return nil, fmt.Errorf("error signing token: %w", err)
}
signatures[rawToken.HexID()] = sig
}
if len(signatures) == 0 {
return nil, status.Error(codes.Unavailable, "server is not accepting bootstrap requests")
}
return &bootstrapv1.BootstrapJoinResponse{
Signatures: signatures,
}, nil
}
func (h *Server) Auth(ctx context.Context, authReq *bootstrapv1.BootstrapAuthRequest) (*bootstrapv1.BootstrapAuthResponse, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, util.StatusError(codes.Unauthenticated)
}
var authHeader string
if v := md.Get("Authorization"); len(v) > 0 {
authHeader = strings.TrimSpace(v[0])
} else {
return nil, util.StatusError(codes.Unauthenticated)
}
if authHeader == "" {
return nil, util.StatusError(codes.Unauthenticated)
}
// Authorization is given, check the authToken
// Remove "Bearer " from the header
bearerToken := strings.TrimSpace(strings.TrimPrefix(authHeader, "Bearer"))
// Verify the token
payload, err := jws.Verify([]byte(bearerToken), jwa.EdDSA, h.privateKey.Public())
if err != nil {
return nil, util.StatusError(codes.PermissionDenied)
}
// The payload should contain the entire token encoded as JSON
token, err := tokens.ParseJSON(payload)
if err != nil {
panic("bug: jws.Verify returned a malformed token")
}
bootstrapToken, err := h.storage.GetToken(ctx, token.Reference())
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return nil, util.StatusError(codes.PermissionDenied)
}
return nil, util.StatusError(codes.Unavailable)
}
// after this point, we can return useful errors
if err := validation.Validate(authReq); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
// lock the mutex associated with the cluster ID
// TODO: when scaling the gateway we need a distributed lock
lock := h.clusterIdLocks.Get(authReq.ClientID)
lock.Lock()
defer lock.Unlock()
// If the cluster with the requested ID does not exist, it can be created
// normally. If it does exist, and the client advertises a capability that
// the cluster does not yet have, and the token has the capability to edit
// this cluster, the cluster will be updated with the new capability.
existing := &corev1.Reference{
Id: authReq.ClientID,
}
var shouldEditExisting bool
if cluster, err := h.storage.GetCluster(ctx, existing); err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return nil, status.Error(codes.Unavailable, err.Error())
}
} else {
if capabilities.Has(cluster, capabilities.Cluster(authReq.Capability)) {
return nil, status.Error(codes.AlreadyExists, "capability is already installed on this cluster")
}
// the cluster capability is new, check if the token can edit it
if capabilities.Has(bootstrapToken, capabilities.JoinExistingCluster.For(existing)) {
shouldEditExisting = true
} else {
return nil, status.Error(codes.PermissionDenied, "insufficient permissions for this cluster")
}
}
ekp := ecdh.NewEphemeralKeyPair()
sharedSecret, err := ecdh.DeriveSharedSecret(ekp, ecdh.PeerPublicKey{
PublicKey: authReq.ClientPubKey,
PeerType: ecdh.PeerTypeClient,
})
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
kr := keyring.New(keyring.NewSharedKeys(sharedSecret))
// Check if the capability exists and can be installed
backendClient, err := h.capBackendStore.Get(authReq.Capability)
if err != nil {
if errors.Is(err, capabilities.ErrBackendNotFound) {
return nil, status.Error(codes.NotFound, err.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
if _, err := backendClient.CanInstall(ctx, &emptypb.Empty{}); err != nil {
return nil, status.Errorf(codes.Unavailable, "capability %q cannot be installed: %v", authReq.Capability, err)
}
if shouldEditExisting {
if err := h.handleEdit(ctx, existing, backendClient, bootstrapToken, kr); err != nil {
return nil, status.Errorf(codes.Internal, "error installing capability %q: %v", authReq.Capability, err)
}
} else {
tokenLabels := maps.Clone(bootstrapToken.GetMetadata().GetLabels())
delete(tokenLabels, corev1.NameLabel)
if tokenLabels == nil {
tokenLabels = map[string]string{}
}
tokenLabels[annotations.AgentVersion] = annotations.Version1
newCluster := &corev1.Cluster{
Id: authReq.ClientID,
Metadata: &corev1.ClusterMetadata{
Labels: tokenLabels,
Capabilities: []*corev1.ClusterCapability{capabilities.Cluster(authReq.Capability)},
},
}
if err := h.handleCreate(ctx, newCluster, backendClient, bootstrapToken, kr); err != nil {
return nil, status.Errorf(codes.Internal, "error installing capability %q: %v", authReq.Capability, err)
}
}
return &bootstrapv1.BootstrapAuthResponse{
ServerPubKey: ekp.PublicKey,
}, nil
}
func (h Server) handleCreate(
ctx context.Context,
newCluster *corev1.Cluster,
newCapability capabilityv1.BackendClient,
token *corev1.BootstrapToken,
kr keyring.Keyring,
) error {
if err := h.storage.CreateCluster(ctx, newCluster); err != nil {
return fmt.Errorf("error creating cluster: %w", err)
}
_, err := h.storage.UpdateToken(ctx, token.Reference(),
storage.NewCompositeMutator(
storage.NewIncrementUsageCountMutator(),
storage.NewAddCapabilityMutator[*corev1.BootstrapToken](&corev1.TokenCapability{
Type: string(capabilities.JoinExistingCluster),
Reference: newCluster.Reference(),
}),
),
)
if err != nil {
return fmt.Errorf("error incrementing usage count: %w", err)
}
krStore := h.storage.KeyringStore("gateway", newCluster.Reference())
if err := krStore.Put(ctx, kr); err != nil {
return fmt.Errorf("error storing keyring: %w", err)
}
newCapability.Install(ctx, &capabilityv1.InstallRequest{
Cluster: newCluster.Reference(),
})
return nil
}
func (h Server) handleEdit(
ctx context.Context,
existingCluster *corev1.Reference,
newCapability capabilityv1.BackendClient,
token *corev1.BootstrapToken,
keyring keyring.Keyring,
) error {
_, err := h.storage.UpdateToken(ctx, token.Reference(),
storage.NewIncrementUsageCountMutator())
if err != nil {
return fmt.Errorf("error incrementing usage count: %w", err)
}
krStore := h.storage.KeyringStore("gateway", existingCluster)
kr, err := krStore.Get(ctx)
if err != nil {
return fmt.Errorf("error getting existing keyring: %w", err)
}
if err := krStore.Put(ctx, keyring.Merge(kr)); err != nil {
return fmt.Errorf("error storing keyring: %w", err)
}
_, err = newCapability.Install(ctx, &capabilityv1.InstallRequest{
Cluster: existingCluster,
})
if err != nil {
return err
}
return nil
}