From f4758c91b75cb366aaa5d73d8c2a98c8d40c52de Mon Sep 17 00:00:00 2001 From: Pallab Pain Date: Sat, 22 Jun 2024 17:35:17 +0530 Subject: [PATCH] fix: reads policy file in the get policy api when mode is file --- hscontrol/grpcv1.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/hscontrol/grpcv1.go b/hscontrol/grpcv1.go index 9fa5fbc6af2..97e89c68b03 100644 --- a/hscontrol/grpcv1.go +++ b/hscontrol/grpcv1.go @@ -3,8 +3,9 @@ package hscontrol import ( "context" - "encoding/json" "errors" + "io" + "os" "sort" "strings" "time" @@ -685,17 +686,20 @@ func (api headscaleV1APIServer) GetPolicy( return nil, err } - // We retain the HuJSON format of the policy while storing - // it in the database. But should we return the same in the - // get policy response? Or should we consider returning the - // policy in JSON format? - return &v1.GetPolicyResponse{ Policy: p.Data, UpdatedAt: timestamppb.New(p.UpdatedAt), }, nil case types.PolicyModeFile: - b, err := json.Marshal(api.h.ACLPolicy) + // Read the file and return the contents as-is. + f, err := os.Open(api.h.cfg.Policy.Path) + if err != nil { + return nil, err + } + + defer f.Close() + + b, err := io.ReadAll(f) if err != nil { return nil, err }