-
Notifications
You must be signed in to change notification settings - Fork 155
/
registry.go
110 lines (101 loc) · 3.48 KB
/
registry.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
// Copyright 2020 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grpc
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"github.com/scionproto/scion/pkg/addr"
"github.com/scionproto/scion/pkg/experimental/hiddenpath"
"github.com/scionproto/scion/pkg/log"
"github.com/scionproto/scion/pkg/private/serrors"
hspb "github.com/scionproto/scion/pkg/proto/hidden_segment"
seg "github.com/scionproto/scion/pkg/segment"
"github.com/scionproto/scion/pkg/snet"
infra "github.com/scionproto/scion/private/segment/verifier"
)
// RegistrationServer handles gRPC segment registration requests.
type RegistrationServer struct {
Registry hiddenpath.Registry
Verifier infra.Verifier
}
// HiddenSegmentRegistration handles the gRPC hidden segment registration
// request.
func (s RegistrationServer) HiddenSegmentRegistration(ctx context.Context,
req *hspb.HiddenSegmentRegistrationRequest) (*hspb.HiddenSegmentRegistrationResponse, error) {
logger := log.FromCtx(ctx)
p, peerIA, err := getPeer(ctx)
if err != nil {
logger.Debug("Failed to extract peer", "err", err)
return nil, err
}
msg, err := s.Verifier.WithIA(peerIA).WithServer(p).Verify(ctx, req.SignedRequest)
if err != nil {
logger.Debug("Failed to verify signature", "err", err)
return nil, status.Error(codes.Unauthenticated, "verifying signature")
}
var reqBody hspb.HiddenSegmentRegistrationRequestBody
if err := proto.Unmarshal(msg.Body, &reqBody); err != nil {
logger.Debug("Failed to parse body", "err", err)
return nil, status.Error(codes.InvalidArgument, "parsing body")
}
id := hiddenpath.GroupIDFromUint64(reqBody.GroupId)
var segs []*seg.Meta
for rawType, rawSegs := range reqBody.Segments {
for i, rawSeg := range rawSegs.Segments {
s, err := seg.SegmentFromPB(rawSeg)
if err != nil {
return nil, status.Error(codes.InvalidArgument,
fmt.Sprintf("invalid segment %d: %v", i, err))
}
segs = append(segs, &seg.Meta{
Segment: s,
Type: seg.Type(rawType),
})
}
}
err = s.Registry.Register(ctx, hiddenpath.Registration{
Segments: segs,
GroupID: id,
Peer: p,
})
if err != nil {
logger.Debug("Error during registration", "err", err)
return nil, status.Error(codes.Internal, err.Error())
}
return &hspb.HiddenSegmentRegistrationResponse{}, nil
}
func getPeer(ctx context.Context) (*snet.SVCAddr, addr.IA, error) {
p, ok := peer.FromContext(ctx)
if !ok {
return nil, 0, serrors.New("not present", "ctx", ctx)
}
a, ok := p.Addr.(*snet.UDPAddr)
if !ok {
return nil, 0, serrors.New("invalid type, expected snet.UDPAddr",
"type", fmt.Sprintf("%T", p.Addr))
}
// XXX(lukedirtwalker): because the remote might send from the client QUIC
// stack we can't simply use the peer address. So for now we just use the
// SVC_CS address in the peer.
return &snet.SVCAddr{
IA: a.IA,
Path: a.Path,
NextHop: a.NextHop,
SVC: addr.SvcCS,
}, a.IA, nil
}