forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_replication.go
117 lines (94 loc) · 3.2 KB
/
api_replication.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
package bertyprotocol
import (
"context"
"crypto/tls"
"encoding/base64"
"fmt"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/internal/grpcutil"
"berty.tech/berty/v2/go/internal/logutil"
"berty.tech/berty/v2/go/pkg/authtypes"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
"berty.tech/berty/v2/go/pkg/replicationtypes"
"berty.tech/berty/v2/go/pkg/tyber"
)
func FilterGroupForReplication(m *protocoltypes.Group) (*protocoltypes.Group, error) {
groupSigPK, err := m.GetSigningPubKey()
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
groupSigPKBytes, err := groupSigPK.Raw()
if err != nil {
return nil, errcode.ErrSerialization.Wrap(err)
}
linkKey, err := cryptoutil.GetLinkKeyArray(m)
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
return &protocoltypes.Group{
PublicKey: m.PublicKey,
SignPub: groupSigPKBytes,
LinkKey: linkKey[:],
LinkKeySig: m.LinkKeySig,
}, nil
}
func (s *service) ReplicationServiceRegisterGroup(ctx context.Context, request *protocoltypes.ReplicationServiceRegisterGroup_Request) (_ *protocoltypes.ReplicationServiceRegisterGroup_Reply, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Registering replication service for group")
defer func() { endSection(err, "") }()
gc, err := s.GetContextGroupForID(request.GroupPK)
if err != nil {
return nil, errcode.ErrInvalidInput.Wrap(err)
}
replGroup, err := FilterGroupForReplication(gc.group)
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
token, err := s.accountGroup.metadataStore.getServiceToken(request.TokenID)
if err != nil {
return nil, errcode.ErrInvalidInput.Wrap(err)
}
if token == nil {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("invalid token"))
}
endpoint := ""
for _, t := range token.SupportedServices {
if t.ServiceType != authtypes.ServiceReplicationID {
continue
}
endpoint = t.ServiceEndpoint
break
}
if endpoint == "" {
return nil, errcode.ErrServiceReplicationMissingEndpoint
}
gopts := []grpc.DialOption{
grpc.WithPerRPCCredentials(grpcutil.NewUnsecureSimpleAuthAccess("bearer", token.Token)),
}
if s.grpcInsecure {
gopts = append(gopts, grpc.WithInsecure())
} else {
tlsconfig := credentials.NewTLS(&tls.Config{
MinVersion: tls.VersionTLS12,
})
gopts = append(gopts, grpc.WithTransportCredentials(tlsconfig))
}
cc, err := grpc.DialContext(context.Background(), endpoint, gopts...)
if err != nil {
return nil, errcode.ErrStreamWrite.Wrap(err)
}
client := replicationtypes.NewReplicationServiceClient(cc)
if _, err = client.ReplicateGroup(ctx, &replicationtypes.ReplicationServiceReplicateGroup_Request{
Group: replGroup,
}); err != nil {
return nil, errcode.ErrServiceReplicationServer.Wrap(err)
}
s.logger.Info("group will be replicated", logutil.PrivateString("public-key", base64.RawURLEncoding.EncodeToString(request.GroupPK)))
if _, err := gc.metadataStore.SendGroupReplicating(ctx, token, endpoint); err != nil {
s.logger.Error("error while notifying group about replication", zap.Error(err))
}
return &protocoltypes.ReplicationServiceRegisterGroup_Reply{}, nil
}