-
Notifications
You must be signed in to change notification settings - Fork 156
/
lookup.go
126 lines (113 loc) · 4.03 KB
/
lookup.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
// 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"
"google.golang.org/grpc/codes"
"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"
hspb "github.com/scionproto/scion/pkg/proto/hidden_segment"
seg "github.com/scionproto/scion/pkg/segment"
infra "github.com/scionproto/scion/private/segment/verifier"
)
// SegmentServer serves segments from a lookuper.
type SegmentServer struct {
Lookup hiddenpath.Lookuper
}
// HiddenSegments serves hidden segments requests using the provided lookup.
func (s *SegmentServer) HiddenSegments(ctx context.Context,
pbReq *hspb.HiddenSegmentsRequest) (*hspb.HiddenSegmentsResponse, error) {
logger := log.FromCtx(ctx)
if pbReq == nil {
logger.Debug("invalid request")
return nil, status.Error(codes.Internal, "invalid request")
}
req := fromHSPB(pbReq)
reply, err := s.Lookup.Segments(ctx, req)
if err != nil {
// TODO(lukedirtwalker): determine the proper error code here.
logger.Debug("Failed to look up segments", "err", err)
return nil, status.Error(codes.Internal, err.Error())
}
return &hspb.HiddenSegmentsResponse{
Segments: toHSPB(reply),
}, nil
}
// AuthoritativeSegmentServer serves hidden segments from a lookuper and
// verifies that requests are correctly signed from the peer.
type AuthoritativeSegmentServer struct {
Lookup hiddenpath.Lookuper
Verifier infra.Verifier
}
// AuthoritativeHiddenSegments serves the given hidden segments request.
func (s AuthoritativeSegmentServer) AuthoritativeHiddenSegments(ctx context.Context,
pbReq *hspb.AuthoritativeHiddenSegmentsRequest,
) (*hspb.AuthoritativeHiddenSegmentsResponse, error) {
logger := log.FromCtx(ctx)
if pbReq == nil {
logger.Debug("invalid request")
return nil, status.Error(codes.Internal, "invalid request")
}
p, peerIA, err := getPeer(ctx)
if err != nil {
logger.Debug("Extracting peer", "err", err)
return nil, status.Error(codes.Internal, "extracting peer")
}
msg, err := s.Verifier.WithIA(peerIA).WithServer(p).Verify(ctx, pbReq.SignedRequest)
if err != nil {
logger.Debug("Verifying request", "err", err)
return nil, status.Error(codes.Unauthenticated, "verifying signature")
}
var r hspb.HiddenSegmentsRequest
if err := proto.Unmarshal(msg.Body, &r); err != nil {
logger.Debug("Parsing body", "err", err)
return nil, status.Error(codes.InvalidArgument, "parsing body")
}
req := fromHSPB(&r)
req.Peer = peerIA
reply, err := s.Lookup.Segments(ctx, req)
if err != nil {
// TODO(lukedirtwalker): determine the proper error code here.
logger.Debug("Failed to look up segments", "err", err)
return nil, status.Error(codes.Internal, err.Error())
}
return &hspb.AuthoritativeHiddenSegmentsResponse{
Segments: toHSPB(reply),
}, nil
}
func fromHSPB(pbReq *hspb.HiddenSegmentsRequest) hiddenpath.SegmentRequest {
groups := make([]hiddenpath.GroupID, 0, len(pbReq.GroupIds))
for _, id := range pbReq.GroupIds {
groups = append(groups, hiddenpath.GroupIDFromUint64(id))
}
return hiddenpath.SegmentRequest{
GroupIDs: groups,
DstIA: addr.IA(pbReq.DstIsdAs),
}
}
func toHSPB(input []*seg.Meta) map[int32]*hspb.Segments {
segments := make(map[int32]*hspb.Segments)
for _, meta := range input {
s, ok := segments[int32(meta.Type)]
if !ok {
s = &hspb.Segments{}
segments[int32(meta.Type)] = s
}
s.Segments = append(s.Segments, seg.PathSegmentToPB(meta.Segment))
}
return segments
}