forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_replication.go
73 lines (57 loc) · 2.01 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
package bertyprotocol
import (
"context"
"encoding/base64"
"fmt"
"go.uber.org/zap"
"google.golang.org/grpc"
"berty.tech/berty/v2/go/internal/grpcutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
func (s *service) ReplicationServiceRegisterGroup(ctx context.Context, request *protocoltypes.ReplicationServiceRegisterGroup_Request) (*protocoltypes.ReplicationServiceRegisterGroup_Reply, error) {
gc, err := s.getContextGroupForID(request.GroupPK)
if err != nil {
return nil, errcode.ErrInvalidInput.Wrap(err)
}
replGroup, err := gc.group.FilterForReplication()
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 != ServiceReplicationID {
continue
}
endpoint = t.ServiceEndpoint
break
}
if endpoint == "" {
return nil, errcode.ErrServiceReplicationMissingEndpoint
}
cc, err := grpc.Dial(endpoint, []grpc.DialOption{
grpc.WithPerRPCCredentials(grpcutil.NewUnsecureSimpleAuthAccess("bearer", token.Token)),
grpc.WithInsecure(), // TODO: remove this, enforce security
}...)
if err != nil {
return nil, errcode.ErrStreamWrite.Wrap(err)
}
client := NewReplicationServiceClient(cc)
if _, err = client.ReplicateGroup(ctx, &protocoltypes.ReplicationServiceReplicateGroup_Request{
Group: replGroup,
}); err != nil {
return nil, errcode.ErrServiceReplicationServer.Wrap(err)
}
s.logger.Info("group will be replicated", zap.String("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
}