Skip to content

Commit

Permalink
inline old acl hujson tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby authored and juanfont committed May 10, 2023
1 parent f2c1d1b commit 2d365c8
Show file tree
Hide file tree
Showing 16 changed files with 359 additions and 474 deletions.
35 changes: 20 additions & 15 deletions hscontrol/acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ const (

var featureEnableSSH = envknob.RegisterBool("HEADSCALE_EXPERIMENTAL_FEATURE_SSH")

// LoadACLPolicy loads the ACL policy from the specify path, and generates the ACL rules.
func (h *Headscale) LoadACLPolicy(path string) error {
// LoadACLPolicyFromPath loads the ACL policy from the specify path, and generates the ACL rules.
func (h *Headscale) LoadACLPolicyFromPath(path string) error {
log.Debug().
Str("func", "LoadACLPolicy").
Str("path", path).
Expand All @@ -72,37 +72,42 @@ func (h *Headscale) LoadACLPolicy(path string) error {
}
defer policyFile.Close()

var policy ACLPolicy
policyBytes, err := io.ReadAll(policyFile)
if err != nil {
return err
}

log.Debug().
Str("path", path).
Bytes("file", policyBytes).
Msg("Loading ACLs")

switch filepath.Ext(path) {
case ".yml", ".yaml":
log.Debug().
Str("path", path).
Bytes("file", policyBytes).
Msg("Loading ACLs from YAML")
return h.LoadACLPolicyFromBytes(policyBytes, "yaml")
}

err := yaml.Unmarshal(policyBytes, &policy)
return h.LoadACLPolicyFromBytes(policyBytes, "hujson")
}

func (h *Headscale) LoadACLPolicyFromBytes(acl []byte, format string) error {
var policy ACLPolicy
switch format {
case "yaml":
err := yaml.Unmarshal(acl, &policy)
if err != nil {
return err
}

log.Trace().
Interface("policy", policy).
Msg("Loaded policy from YAML")

default:
ast, err := hujson.Parse(policyBytes)
ast, err := hujson.Parse(acl)
if err != nil {
return err
}

ast.Standardize()
policyBytes = ast.Pack()
err = json.Unmarshal(policyBytes, &policy)
acl = ast.Pack()
err = json.Unmarshal(acl, &policy)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 2d365c8

Please sign in to comment.