1
0
mirror of https://github.com/juanfont/headscale.git synced 2025-09-20 17:53:11 +02:00
This commit is contained in:
Samuel Lock 2022-08-06 17:17:43 +10:00
parent c308e21c70
commit 85e4dd684d
15 changed files with 110 additions and 62 deletions

31
acls.go
View File

@ -129,20 +129,19 @@ func (h *Headscale) ListACLPolicy() (*ACLPolicy, error) {
}
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 {
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 {
for n, i := range vhosts {
addr, err := netaddr.ParseIPPrefix(i)
if err != nil {
return nil, err
@ -153,18 +152,18 @@ func ACLProtoToStruct(v *v1.ACLPolicy) (*ACLPolicy, error) {
// tag owners parsing
vtagowners := v.GetTagOwners()
tagowners := make(map[string][]string, len(vtagowners))
for n,i := range 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 := make([]ACL, len(vacls))
for n, i := range vacls {
acls[n] = ACL{
Action: i.GetAction(),
Protocol: i.GetProtocol(),
Sources: i.GetSources(),
Action: i.GetAction(),
Protocol: i.GetProtocol(),
Sources: i.GetSources(),
Destinations: i.GetDestinations(),
}
}
@ -172,20 +171,20 @@ func ACLProtoToStruct(v *v1.ACLPolicy) (*ACLPolicy, error) {
// ACL Tests parsing
vtests := v.GetAclTest()
tests := make([]ACLTest, len(vtests))
for n,i := range vtests {
for n, i := range vtests {
tests[n] = ACLTest{
Source: i.GetSource(),
Accept: i.GetAccept(),
Deny: i.GetDeny(),
Deny: i.GetDeny(),
}
}
return &ACLPolicy{
Groups: groups,
Hosts: hosts,
TagOwners: tagowners,
ACLs: acls,
Tests: tests,
Groups: groups,
Hosts: hosts,
TagOwners: tagowners,
ACLs: acls,
Tests: tests,
}, nil
}

View File

@ -62,7 +62,11 @@ func (s *Suite) TestBasicRule(c *check.C) {
func (s *Suite) TestInvalidAction(c *check.C) {
app.aclPolicy = &ACLPolicy{
ACLs: []ACL{
{Action: "invalidAction", Sources: []string{"*"}, Destinations: []string{"*:*"}},
{
Action: "invalidAction",
Sources: []string{"*"},
Destinations: []string{"*:*"},
},
},
}
err := app.UpdateACLRules()
@ -77,7 +81,11 @@ func (s *Suite) TestInvalidGroupInGroup(c *check.C) {
"group:error": []string{"foo", "group:test"},
},
ACLs: []ACL{
{Action: "accept", Sources: []string{"group:error"}, Destinations: []string{"*:*"}},
{
Action: "accept",
Sources: []string{"group:error"},
Destinations: []string{"*:*"},
},
},
}
err := app.UpdateACLRules()
@ -88,7 +96,11 @@ func (s *Suite) TestInvalidTagOwners(c *check.C) {
// this ACL is wrong because no tagOwners own the requested tag for the server
app.aclPolicy = &ACLPolicy{
ACLs: []ACL{
{Action: "accept", Sources: []string{"tag:foo"}, Destinations: []string{"*:*"}},
{
Action: "accept",
Sources: []string{"tag:foo"},
Destinations: []string{"*:*"},
},
},
}
err := app.UpdateACLRules()
@ -131,7 +143,11 @@ func (s *Suite) TestValidExpandTagOwnersInSources(c *check.C) {
Groups: Groups{"group:test": []string{"user1", "user2"}},
TagOwners: TagOwners{"tag:test": []string{"user3", "group:test"}},
ACLs: []ACL{
{Action: "accept", Sources: []string{"tag:test"}, Destinations: []string{"*:*"}},
{
Action: "accept",
Sources: []string{"tag:test"},
Destinations: []string{"*:*"},
},
},
}
err = app.UpdateACLRules()
@ -177,7 +193,11 @@ func (s *Suite) TestValidExpandTagOwnersInDestinations(c *check.C) {
Groups: Groups{"group:test": []string{"user1", "user2"}},
TagOwners: TagOwners{"tag:test": []string{"user3", "group:test"}},
ACLs: []ACL{
{Action: "accept", Sources: []string{"*"}, Destinations: []string{"tag:test:*"}},
{
Action: "accept",
Sources: []string{"*"},
Destinations: []string{"tag:test:*"},
},
},
}
err = app.UpdateACLRules()
@ -222,7 +242,11 @@ func (s *Suite) TestInvalidTagValidNamespace(c *check.C) {
app.aclPolicy = &ACLPolicy{
TagOwners: TagOwners{"tag:test": []string{"user1"}},
ACLs: []ACL{
{Action: "accept", Sources: []string{"user1"}, Destinations: []string{"*:*"}},
{
Action: "accept",
Sources: []string{"user1"},
Destinations: []string{"*:*"},
},
},
}
err = app.UpdateACLRules()

View File

@ -20,10 +20,10 @@ type ACLPolicy struct {
// ACL is a basic rule for the ACL Policy.
type ACL struct {
Action string `json:"action,omitempty" yaml:"action,omitempty"`
Protocol string `json:"proto,omitempty" yaml:"proto,omitempty"`
Sources []string `json:"src,omitempty" yaml:"src,omitempty"`
Destinations []string `json:"dst,omitempty" yaml:"dst,omitempty"`
Action string `json:"action,omitempty" yaml:"action,omitempty"`
Protocol string `json:"proto,omitempty" yaml:"proto,omitempty"`
Sources []string `json:"src,omitempty" yaml:"src,omitempty"`
Destinations []string `json:"dst,omitempty" yaml:"dst,omitempty"`
}
// Groups references a series of alias in the ACL rules.
@ -37,9 +37,9 @@ type TagOwners map[string][]string
// ACLTest is not implemented, but should be use to check if a certain rule is allowed.
type ACLTest struct {
Source string `json:"src,omitempty" yaml:"src,omitempty"`
Accept []string `json:"accept,omitempty" yaml:"accept,omitempty"`
Deny []string `json:"deny,omitempty" yaml:"deny,omitempty"`
Source string `json:"src,omitempty" yaml:"src,omitempty"`
Accept []string `json:"accept,omitempty" yaml:"accept,omitempty"`
Deny []string `json:"deny,omitempty" yaml:"deny,omitempty"`
}
// UnmarshalJSON allows to parse the Hosts directly into netaddr objects.

5
app.go
View File

@ -766,7 +766,10 @@ func (h *Headscale) setLastStateChangeToNow(namespaces ...string) {
if len(namespaces) == 0 {
namespaces, err = h.ListNamespacesStr()
if err != nil {
log.Error().Caller().Err(err).Msg("failed to fetch all namespaces, failing to update last changed state.")
log.Error().
Caller().
Err(err).
Msg("failed to fetch all namespaces, failing to update last changed state.")
}
}

View File

@ -134,7 +134,9 @@ If you loose a key, create a new one and revoke (expire) the old one.`,
expiration := time.Now().UTC().Add(time.Duration(duration))
log.Trace().Dur("expiration", time.Duration(duration)).Msg("expiration has been set")
log.Trace().
Dur("expiration", time.Duration(duration)).
Msg("expiration has been set")
request.Expiration = timestamppb.New(expiration)

View File

@ -164,7 +164,9 @@ var createPreAuthKeyCmd = &cobra.Command{
expiration := time.Now().UTC().Add(time.Duration(duration))
log.Trace().Dur("expiration", time.Duration(duration)).Msg("expiration has been set")
log.Trace().
Dur("expiration", time.Duration(duration)).
Msg("expiration has been set")
request.Expiration = timestamppb.New(expiration)

View File

@ -24,7 +24,10 @@ const (
func getHeadscaleApp() (*headscale.Headscale, error) {
cfg, err := headscale.GetHeadscaleConfig()
if err != nil {
return nil, fmt.Errorf("failed to load configuration while creating headscale instance: %w", err)
return nil, fmt.Errorf(
"failed to load configuration while creating headscale instance: %w",
err,
)
}
app, err := headscale.NewHeadscale(cfg)

1
db.go
View File

@ -111,7 +111,6 @@ func (h *Headscale) initDB() error {
Err(err).
Msg("Failed to save normalized machine name in DB migration")
}
}
}
}

View File

@ -219,7 +219,6 @@ func getIPs(
func getDNSNames(
headscale *dockertest.Resource,
) ([]string, error) {
listAllResult, err := ExecuteCommand(
headscale,
[]string{
@ -253,7 +252,6 @@ func getDNSNames(
func getMagicFQDN(
headscale *dockertest.Resource,
) ([]string, error) {
listAllResult, err := ExecuteCommand(
headscale,
[]string{
@ -278,7 +276,11 @@ func getMagicFQDN(
hostnames := make([]string, len(listAll))
for index := range listAll {
hostnames[index] = fmt.Sprintf("%s.%s.headscale.net", listAll[index].GetGivenName(), listAll[index].GetNamespace().GetName())
hostnames[index] = fmt.Sprintf(
"%s.%s.headscale.net",
listAll[index].GetGivenName(),
listAll[index].GetNamespace().GetName(),
)
}
return hostnames, nil

View File

@ -188,8 +188,16 @@ func (s *Suite) TestGetACLFilteredPeers(c *check.C) {
Hosts: map[string]netaddr.IPPrefix{},
TagOwners: map[string][]string{},
ACLs: []ACL{
{Action: "accept", Sources: []string{"admin"}, Destinations: []string{"*:*"}},
{Action: "accept", Sources: []string{"test"}, Destinations: []string{"test:*"}},
{
Action: "accept",
Sources: []string{"admin"},
Destinations: []string{"*:*"},
},
{
Action: "accept",
Sources: []string{"test"},
Destinations: []string{"test:*"},
},
},
Tests: []ACLTest{},
}

View File

@ -278,7 +278,11 @@ func (h *Headscale) PollNetMapStream(
return
}
ctx := context.WithValue(ctx.Request.Context(), machineNameContextKey, machine.Hostname)
ctx := context.WithValue(
ctx.Request.Context(),
machineNameContextKey,
machine.Hostname,
)
ctx, cancel := context.WithCancel(ctx)
defer cancel()

View File

@ -3,11 +3,11 @@ package headscale.v1;
option go_package = "github.com/juanfont/headscale/gen/go/v1";
message ACLPolicy {
map<string,Group> groups = 1;
map<string,string> hosts = 2;
map<string,TagOwners> tag_owners = 3;
repeated ACL acls = 4;
repeated ACLTest acl_test = 5;
map<string, Group> groups = 1;
map<string, string> hosts = 2;
map<string, TagOwners> tag_owners = 3;
repeated ACL acls = 4;
repeated ACLTest acl_test = 5;
}
message Group {
@ -19,16 +19,16 @@ message TagOwners {
}
message ACL {
string action = 1;
string protocol = 2;
repeated string sources = 3;
string action = 1;
string protocol = 2;
repeated string sources = 3;
repeated string destinations = 4;
}
message ACLTest {
string source = 1;
string source = 1;
repeated string accept = 2;
repeated string deny = 3;
repeated string deny = 3;
}
message ListACLPolicyRequest {

View File

@ -28,7 +28,7 @@ func (s *Suite) TestGetRoutes(c *check.C) {
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "test_get_route_machine",
Hostname: "test_get_route_machine",
NamespaceID: namespace.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
@ -79,7 +79,7 @@ func (s *Suite) TestGetEnableRoutes(c *check.C) {
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "test_enable_route_machine",
Hostname: "test_enable_route_machine",
NamespaceID: namespace.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),

View File

@ -332,7 +332,9 @@ func GenerateRandomStringDNSSafe(n int) (string, error) {
if err != nil {
return "", err
}
str = strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(str, "_", ""), "-", ""))
str = strings.ToLower(
strings.ReplaceAll(strings.ReplaceAll(str, "_", ""), "-", ""),
)
}
return str[:n], nil