Skip to content

Commit

Permalink
test policy set validation
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Aug 30, 2024
1 parent cb12055 commit 7860d37
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1676,3 +1676,77 @@ func TestPolicyCommand(t *testing.T) {
assert.Len(t, output.ACLs, 1)
assert.Equal(t, output.TagOwners["tag:exists"], []string{"policy-user"})
}

func TestPolicyBrokenConfigCommand(t *testing.T) {
IntegrationSkip(t)
t.Parallel()

scenario, err := NewScenario(dockertestMaxWait())
assertNoErr(t, err)
// defer scenario.Shutdown()

spec := map[string]int{
"policy-user": 1,
}

err = scenario.CreateHeadscaleEnv(
spec,
[]tsic.Option{},
hsic.WithTestName("clins"),
hsic.WithConfigEnv(map[string]string{
"HEADSCALE_POLICY_MODE": "database",
}),
)
assertNoErr(t, err)

headscale, err := scenario.Headscale()
assertNoErr(t, err)

p := policy.ACLPolicy{
ACLs: []policy.ACL{
{
// This is an unknown action, so it will return an error
// and the config will not be applied.
Action: "acccept",
Sources: []string{"*"},
Destinations: []string{"*:*"},
},
},
TagOwners: map[string][]string{
"tag:exists": {"policy-user"},
},
}

pBytes, _ := json.Marshal(p)

policyFilePath := "/etc/headscale/policy.json"

err = headscale.WriteFile(policyFilePath, pBytes)
assertNoErr(t, err)

// No policy is present at this time.
// Add a new policy from a file.
_, err = headscale.Execute(
[]string{
"headscale",
"policy",
"set",
"-f",
policyFilePath,
},
)
assert.ErrorContains(t, err, "verifying policy rules: invalid action")

// The new policy was invalid, the old one should still be in place, which
// is none.
_, err = headscale.Execute(
[]string{
"headscale",
"policy",
"get",
"--output",
"json",
},
)
assert.ErrorContains(t, err, "acl policy not found")
}

0 comments on commit 7860d37

Please sign in to comment.