-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added cli options to output ACLs #581
Changes from all commits
c054e27
6b13558
4cf9dc5
93b19ae
8e6401b
244c2f1
8725cf0
f95a4ff
c308e21
c462529
85e4dd6
12eb258
cdbe183
d655f6e
a84c630
fd68115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |||||||
"strconv" | ||||||||
"strings" | ||||||||
|
||||||||
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | ||||||||
"github.com/rs/zerolog/log" | ||||||||
"github.com/tailscale/hujson" | ||||||||
"gopkg.in/yaml.v3" | ||||||||
|
@@ -123,6 +124,144 @@ func (h *Headscale) UpdateACLRules() error { | |||||||
return nil | ||||||||
} | ||||||||
|
||||||||
func (h *Headscale) ListACLPolicy() (*ACLPolicy, error) { | ||||||||
return h.aclPolicy, nil | ||||||||
} | ||||||||
|
||||||||
func ACLProtoToStruct(v *v1.ACLPolicy) (*ACLPolicy, error) { | ||||||||
// v := req.GetPolicy() | ||||||||
|
||||||||
// groups parsing | ||||||||
vgroups := v.GetGroups() | ||||||||
groups := make(map[string][]string, len(vgroups)) | ||||||||
for n, i := range vgroups { | ||||||||
groups[n] = i.GetGroup() | ||||||||
} | ||||||||
|
||||||||
// hosts parsing | ||||||||
vhosts := v.GetHosts() | ||||||||
hosts := make(map[string]netaddr.IPPrefix, len(vhosts)) | ||||||||
for n, i := range vhosts { | ||||||||
addr, err := netaddr.ParseIPPrefix(i) | ||||||||
if err != nil { | ||||||||
return nil, err | ||||||||
} | ||||||||
hosts[n] = addr | ||||||||
} | ||||||||
|
||||||||
// tag owners parsing | ||||||||
vtagowners := v.GetTagOwners() | ||||||||
tagowners := make(map[string][]string, len(vtagowners)) | ||||||||
for n, i := range vtagowners { | ||||||||
tagowners[n] = i.GetTagOwners() | ||||||||
} | ||||||||
|
||||||||
// ACLs parsing | ||||||||
vacls := (*v).GetAcls() | ||||||||
acls := make([]ACL, len(vacls)) | ||||||||
for n, i := range vacls { | ||||||||
acls[n] = ACL{ | ||||||||
Action: i.GetAction(), | ||||||||
Protocol: i.GetProtocol(), | ||||||||
Sources: i.GetSources(), | ||||||||
Destinations: i.GetDestinations(), | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// ACL Tests parsing | ||||||||
vtests := v.GetAclTest() | ||||||||
tests := make([]ACLTest, len(vtests)) | ||||||||
for n, i := range vtests { | ||||||||
tests[n] = ACLTest{ | ||||||||
Source: i.GetSource(), | ||||||||
Accept: i.GetAccept(), | ||||||||
Deny: i.GetDeny(), | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return &ACLPolicy{ | ||||||||
Groups: groups, | ||||||||
Hosts: hosts, | ||||||||
TagOwners: tagowners, | ||||||||
ACLs: acls, | ||||||||
Tests: tests, | ||||||||
}, nil | ||||||||
} | ||||||||
|
||||||||
func (policy *ACLPolicy) toProto() *v1.ACLPolicy { | ||||||||
protoACLPolicy := v1.ACLPolicy{ | ||||||||
Groups: policy.Groups.toProto(), | ||||||||
Hosts: policy.Hosts.toProto(), | ||||||||
TagOwners: policy.TagOwners.toProto(), | ||||||||
} | ||||||||
|
||||||||
// proto acls | ||||||||
protoACLPolicy.Acls = make([]*v1.ACL, len(policy.ACLs)) | ||||||||
for k, v := range policy.ACLs { | ||||||||
protoACLPolicy.Acls[k] = v.toProto() | ||||||||
} | ||||||||
|
||||||||
// proto acl tests | ||||||||
protoACLPolicy.AclTest = make([]*v1.ACLTest, len(policy.Tests)) | ||||||||
for k, v := range policy.Tests { | ||||||||
protoACLPolicy.AclTest[k] = v.toProto() | ||||||||
} | ||||||||
|
||||||||
return &protoACLPolicy | ||||||||
} | ||||||||
|
||||||||
func (a *ACL) toProto() *v1.ACL { | ||||||||
protoACL := v1.ACL{ | ||||||||
Action: a.Action, | ||||||||
Protocol: a.Protocol, | ||||||||
Sources: a.Sources, | ||||||||
Destinations: a.Destinations, | ||||||||
} | ||||||||
|
||||||||
return &protoACL | ||||||||
} | ||||||||
|
||||||||
func (a *ACLTest) toProto() *v1.ACLTest { | ||||||||
protoACLTest := v1.ACLTest{ | ||||||||
Source: a.Source, | ||||||||
Accept: a.Accept, | ||||||||
Deny: a.Deny, | ||||||||
} | ||||||||
|
||||||||
return &protoACLTest | ||||||||
} | ||||||||
|
||||||||
func (g *Groups) toProto() map[string]*v1.Group { | ||||||||
protoGroups := make(map[string]*v1.Group, len(*g)) | ||||||||
for k, v := range *g { | ||||||||
protoGroupSingle := &v1.Group{ | ||||||||
Group: v, | ||||||||
} | ||||||||
protoGroups[k] = protoGroupSingle | ||||||||
} | ||||||||
|
||||||||
return protoGroups | ||||||||
} | ||||||||
|
||||||||
func (t *TagOwners) toProto() map[string]*v1.TagOwners { | ||||||||
protoTagOwners := make(map[string]*v1.TagOwners, len(*t)) | ||||||||
for k, v := range *t { | ||||||||
protoTagOwner := &v1.TagOwners{ | ||||||||
TagOwners: v, | ||||||||
} | ||||||||
protoTagOwners[k] = protoTagOwner | ||||||||
} | ||||||||
return protoTagOwners | ||||||||
} | ||||||||
|
||||||||
func (h *Hosts) toProto() map[string]string { | ||||||||
protoHosts := make(map[string]string, len(*h)) | ||||||||
for k, v := range *h { | ||||||||
protoHosts[k] = v.String() | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return protoHosts | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
func (h *Headscale) generateACLRules() ([]tailcfg.FilterRule, error) { | ||||||||
rules := []tailcfg.FilterRule{} | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,78 @@ | ||||
package cli | ||||
|
||||
import ( | ||||
"fmt" | ||||
|
||||
"github.com/juanfont/headscale" | ||||
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | ||||
"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` | ||||
} | ||||
|
||||
ctx, client, conn, cancel := getHeadscaleCLIClient() | ||||
defer cancel() | ||||
defer conn.Close() | ||||
|
||||
request := &v1.ListACLPolicyRequest{} | ||||
|
||||
response, err := client.ListACLPolicy(ctx, request) | ||||
if err != nil { | ||||
ErrorOutput( | ||||
err, | ||||
fmt.Sprintf("Error getting ACL from server: %s", err), | ||||
output, | ||||
) | ||||
|
||||
return | ||||
} | ||||
|
||||
if response == nil { | ||||
SuccessOutput( | ||||
``, | ||||
`No policy defined.`, | ||||
``, | ||||
) | ||||
|
||||
return | ||||
} | ||||
|
||||
policy, err := headscale.ACLProtoToStruct(response.Policy) | ||||
if err != nil { | ||||
ErrorOutput( | ||||
err, | ||||
fmt.Sprintf("Error parsing response from server: %s", err), | ||||
output, | ||||
) | ||||
|
||||
return | ||||
} | ||||
|
||||
SuccessOutput( | ||||
policy, | ||||
``, | ||||
output, | ||||
) | ||||
|
||||
return | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not required |
||||
}, | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here the linter is complaining
Replace
v
forpolicy
or something like this, for instance.