Skip to content

Commit d8504b7

Browse files
author
Marc Odermatt
committed
move global lookup to SD
1 parent 95ffaac commit d8504b7

File tree

7 files changed

+441
-422
lines changed

7 files changed

+441
-422
lines changed

daemon/internal/servers/grpc.go

+72-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ package servers
1616

1717
import (
1818
"context"
19+
"encoding/json"
1920
"fmt"
21+
"io"
2022
"net"
23+
"net/http"
2124
"time"
2225

2326
durationpb "github.com/golang/protobuf/ptypes/duration"
@@ -661,20 +664,78 @@ func requestToHostHostMeta(req *sdpb.DRKeyHostHostRequest) (drkey.HostHostMeta,
661664
func (s *DaemonServer) PolicyDescription(ctx context.Context,
662665
request *sdpb.PolicyDescriptionRequest) (
663666
*sdpb.PolicyDescriptionResponse, error) {
664-
conn, err := s.Dialer.Dial(ctx, &snet.SVCAddr{SVC: addr.SvcCS})
667+
668+
var description string
669+
if request.IsLocal {
670+
conn, err := s.Dialer.Dial(ctx, &snet.SVCAddr{SVC: addr.SvcCS})
671+
if err != nil {
672+
log.FromCtx(ctx).Debug("Dialing CS failed", "err", err)
673+
}
674+
defer conn.Close()
675+
client := experimental.NewFABRIDIntraServiceClient(conn)
676+
response, err := client.RemotePolicyDescription(ctx,
677+
&experimental.RemotePolicyDescriptionRequest{
678+
PolicyIdentifier: request.PolicyIdentifier,
679+
IsdAs: request.IsdAs,
680+
})
681+
if err != nil {
682+
return &sdpb.PolicyDescriptionResponse{}, err
683+
}
684+
description = response.Description
685+
} else {
686+
globalPolicyURL := "https://raw.githubusercontent.com/marcodermatt/fabrid-global-policies/main/policy-descriptions.json"
687+
688+
// Fetch the global policy from the URL
689+
policy, err := FetchGlobalPolicy(globalPolicyURL)
690+
if err != nil {
691+
return nil, serrors.WrapStr("fetching global policy", err)
692+
}
693+
694+
// Retrieve the description for the given identifier
695+
description, err = GetPolicyDescription(policy, request.PolicyIdentifier)
696+
if err != nil {
697+
return nil, serrors.WrapStr("getting global policy description", err)
698+
}
699+
700+
}
701+
return &sdpb.PolicyDescriptionResponse{Description: description}, nil
702+
}
703+
704+
// GlobalPolicy holds the mapping of uint32 identifiers to their string descriptions
705+
type GlobalPolicy map[uint32]string
706+
707+
// FetchGlobalPolicy fetches and parses the global policy from the given URL
708+
func FetchGlobalPolicy(url string) (GlobalPolicy, error) {
709+
resp, err := http.Get(url)
665710
if err != nil {
666-
log.FromCtx(ctx).Debug("Dialing CS failed", "err", err)
711+
return nil, serrors.WrapStr("failed to fetch global policy", err)
667712
}
668-
defer conn.Close()
669-
client := experimental.NewFABRIDIntraServiceClient(conn)
670-
response, err := client.RemotePolicyDescription(ctx,
671-
&experimental.RemotePolicyDescriptionRequest{
672-
PolicyIdentifier: request.PolicyIdentifier,
673-
IsdAs: request.IsdAs,
674-
})
713+
defer resp.Body.Close()
714+
715+
if resp.StatusCode != http.StatusOK {
716+
return nil, serrors.New("failed to fetch global policy", "StatusCode", resp.StatusCode)
717+
}
718+
719+
// Read the response body
720+
body, err := io.ReadAll(resp.Body)
675721
if err != nil {
676-
return &sdpb.PolicyDescriptionResponse{}, err
722+
return nil, serrors.WrapStr("failed to read response body", err)
723+
}
724+
725+
// Unmarshal the JSON data into a map
726+
var policy GlobalPolicy
727+
if err = json.Unmarshal(body, &policy); err != nil {
728+
return nil, serrors.WrapStr("failed to unmarshal policy JSON", err)
677729
}
678730

679-
return &sdpb.PolicyDescriptionResponse{Description: response.Description}, nil
731+
return policy, nil
732+
}
733+
734+
// GetPolicyDescription retrieves the description for the given identifier
735+
func GetPolicyDescription(policy GlobalPolicy, identifier uint32) (string, error) {
736+
description, exists := policy[identifier]
737+
if !exists {
738+
return "", serrors.New("no policy found", "identifier", identifier)
739+
}
740+
return description, nil
680741
}

pkg/daemon/daemon.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ type Connector interface {
9090
DRKeyGetHostHostKey(ctx context.Context, meta drkey.HostHostMeta) (drkey.HostHostKey, error)
9191
// FabridKeys requests FABRID DRKeys for all provided ASes and the path validation key
9292
FabridKeys(ctx context.Context, meta drkey.FabridKeysMeta) (drkey.FabridKeysResponse, error)
93-
RemotePolicyDescription(ctx context.Context, identifier uint32, ia addr.IA) (string, error)
93+
// PolicyDescription reqests the string description for a FABRID policy
94+
PolicyDescription(ctx context.Context, isLocal bool, identifier uint32,
95+
ia *addr.IA) (string, error)
9496
// Close shuts down the connection to the daemon.
9597
Close() error
9698
}

pkg/daemon/grpc.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,22 @@ func (c grpcConn) FabridKeys(ctx context.Context, meta drkey.FabridKeysMeta,
277277
}, nil
278278
}
279279

280-
func (c grpcConn) RemotePolicyDescription(ctx context.Context,
281-
identifier uint32, ia addr.IA) (string, error) {
280+
func (c grpcConn) PolicyDescription(ctx context.Context,
281+
isLocal bool, identifier uint32, ia *addr.IA) (string, error) {
282282

283283
client := sdpb.NewDaemonServiceClient(c.conn)
284-
response, err := client.PolicyDescription(ctx, &sdpb.PolicyDescriptionRequest{
284+
request := &sdpb.PolicyDescriptionRequest{
285+
IsLocal: isLocal,
285286
PolicyIdentifier: identifier,
286-
IsdAs: uint64(ia),
287-
})
287+
}
288+
if isLocal {
289+
request.IsdAs = uint64(*ia)
290+
}
291+
response, err := client.PolicyDescription(ctx, request)
288292
if err != nil {
289-
return "", nil
293+
return "", err
290294
}
291-
return response.Description, err
295+
return response.Description, nil
292296
}
293297

294298
func (c grpcConn) Close() error {

0 commit comments

Comments
 (0)