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
|
package helper
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"gitlab.com/gitlab-org/gitaly/internal/storage"
"google.golang.org/grpc/metadata"
)
// ExtractGitalyServers extracts `storage.GitalyServers` from an incoming context.
func ExtractGitalyServers(ctx context.Context) (gitalyServersInfo storage.GitalyServers, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, fmt.Errorf("empty metadata")
}
gitalyServersJSONEncoded := md["gitaly-servers"]
if len(gitalyServersJSONEncoded) == 0 {
return nil, fmt.Errorf("empty gitaly-servers metadata")
}
gitalyServersJSON, err := base64.StdEncoding.DecodeString(gitalyServersJSONEncoded[0])
if err != nil {
return nil, fmt.Errorf("failed decoding base64: %v", err)
}
if err := json.Unmarshal(gitalyServersJSON, &gitalyServersInfo); err != nil {
return nil, fmt.Errorf("failed unmarshalling json: %v", err)
}
return
}
// IncomingToOutgoing creates an outgoing context out of an incoming context with the same storage metadata
func IncomingToOutgoing(ctx context.Context) context.Context {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return ctx
}
return metadata.NewOutgoingContext(ctx, md)
}
// InjectGitalyServers injects gitaly-servers metadata into an outgoing context
func InjectGitalyServers(ctx context.Context, name, address, token string) (context.Context, error) {
gitalyServers := storage.GitalyServers{
name: {
"address": address,
"token": token,
},
}
gitalyServersJSON, err := json.Marshal(gitalyServers)
if err != nil {
return nil, err
}
return metadata.NewOutgoingContext(ctx, metadata.Pairs("gitaly-servers", base64.StdEncoding.EncodeToString(gitalyServersJSON))), nil
}
|