@@ -16,8 +16,11 @@ package servers
16
16
17
17
import (
18
18
"context"
19
+ "encoding/json"
19
20
"fmt"
21
+ "io"
20
22
"net"
23
+ "net/http"
21
24
"time"
22
25
23
26
durationpb "github.com/golang/protobuf/ptypes/duration"
@@ -661,20 +664,78 @@ func requestToHostHostMeta(req *sdpb.DRKeyHostHostRequest) (drkey.HostHostMeta,
661
664
func (s * DaemonServer ) PolicyDescription (ctx context.Context ,
662
665
request * sdpb.PolicyDescriptionRequest ) (
663
666
* 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 )
665
710
if err != nil {
666
- log . FromCtx ( ctx ). Debug ( "Dialing CS failed" , "err " , err )
711
+ return nil , serrors . WrapStr ( "failed to fetch global policy " , err )
667
712
}
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 )
675
721
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 )
677
729
}
678
730
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
680
741
}
0 commit comments