From c054e2766e78e4676055566a7b5fec3591a74295 Mon Sep 17 00:00:00 2001 From: Samuel Lock Date: Thu, 12 May 2022 20:18:52 +1000 Subject: [PATCH] added cli options to output ACLs --- acls_types.go | 10 +++---- app.go | 4 +++ cmd/headscale/cli/acls.go | 60 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 cmd/headscale/cli/acls.go diff --git a/acls_types.go b/acls_types.go index fb869821..cdfab720 100644 --- a/acls_types.go +++ b/acls_types.go @@ -11,11 +11,11 @@ import ( // ACLPolicy represents a Tailscale ACL Policy. type ACLPolicy struct { - Groups Groups `json:"Groups" yaml:"Groups"` - Hosts Hosts `json:"Hosts" yaml:"Hosts"` - TagOwners TagOwners `json:"TagOwners" yaml:"TagOwners"` - ACLs []ACL `json:"ACLs" yaml:"ACLs"` - Tests []ACLTest `json:"Tests" yaml:"Tests"` + Groups Groups `json:"Groups,omitempty" yaml:"Groups,omitempty"` + Hosts Hosts `json:"Hosts,omitempty" yaml:"Hosts,omitempty"` + TagOwners TagOwners `json:"TagOwners,omitempty" yaml:"TagOwners,omitempty"` + ACLs []ACL `json:"ACLs,omitempty" yaml:"ACLs,omitempty"` + Tests []ACLTest `json:"Tests,omitempty" yaml:"Tests,omitempty"` } // ACL is a basic rule for the ACL Policy. diff --git a/app.go b/app.go index a96ab83a..a853d307 100644 --- a/app.go +++ b/app.go @@ -874,3 +874,7 @@ func readOrCreatePrivateKey(path string) (*key.MachinePrivate, error) { return &machineKey, nil } + +func (h *Headscale) GetACLPolicy() (*ACLPolicy) { + return h.aclPolicy +} \ No newline at end of file diff --git a/cmd/headscale/cli/acls.go b/cmd/headscale/cli/acls.go new file mode 100644 index 00000000..ef814613 --- /dev/null +++ b/cmd/headscale/cli/acls.go @@ -0,0 +1,60 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(aclsCmd) + aclsCmd.AddCommand(listAclsCmd) + +} + +var aclsCmd = &cobra.Command{ + Use: "acls", + Short: "Manage Access Control Lists (ACLs)", + Aliases: []string{"access-lists","acl"}, +} + +var listAclsCmd = &cobra.Command{ + Use: "list", + Short: "List ACLs", + Aliases: []string{"ls","show"}, + Run: func(cmd *cobra.Command, args []string) { + output, _ := cmd.Flags().GetString("output") + if output == `` { + output = `json` + } + h, err := getHeadscaleApp() + if err != nil { + ErrorOutput( + err, + fmt.Sprintf("Error getting headscale app: %s", err), + output, + ) + + return + } + policy := h.GetACLPolicy() + if policy == nil { + SuccessOutput( + ``, + `No policy defined.`, + ``, + ) + + return + } + + SuccessOutput( + policy, + ``, + output, + ) + + return + }, + +} \ No newline at end of file