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

View File

@ -129,7 +129,6 @@ func (h *Headscale) ListACLPolicy() (*ACLPolicy, error) {
} }
func ACLProtoToStruct(v *v1.ACLPolicy) (*ACLPolicy, error) { func ACLProtoToStruct(v *v1.ACLPolicy) (*ACLPolicy, error) {
// v := req.GetPolicy() // v := req.GetPolicy()
// groups parsing // groups parsing

View File

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

5
app.go
View File

@ -766,7 +766,10 @@ func (h *Headscale) setLastStateChangeToNow(namespaces ...string) {
if len(namespaces) == 0 { if len(namespaces) == 0 {
namespaces, err = h.ListNamespacesStr() namespaces, err = h.ListNamespacesStr()
if err != nil { 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)) 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) request.Expiration = timestamppb.New(expiration)

View File

@ -164,7 +164,9 @@ var createPreAuthKeyCmd = &cobra.Command{
expiration := time.Now().UTC().Add(time.Duration(duration)) 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) request.Expiration = timestamppb.New(expiration)

View File

@ -24,7 +24,10 @@ const (
func getHeadscaleApp() (*headscale.Headscale, error) { func getHeadscaleApp() (*headscale.Headscale, error) {
cfg, err := headscale.GetHeadscaleConfig() cfg, err := headscale.GetHeadscaleConfig()
if err != nil { 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) app, err := headscale.NewHeadscale(cfg)

1
db.go
View File

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

View File

@ -219,7 +219,6 @@ func getIPs(
func getDNSNames( func getDNSNames(
headscale *dockertest.Resource, headscale *dockertest.Resource,
) ([]string, error) { ) ([]string, error) {
listAllResult, err := ExecuteCommand( listAllResult, err := ExecuteCommand(
headscale, headscale,
[]string{ []string{
@ -253,7 +252,6 @@ func getDNSNames(
func getMagicFQDN( func getMagicFQDN(
headscale *dockertest.Resource, headscale *dockertest.Resource,
) ([]string, error) { ) ([]string, error) {
listAllResult, err := ExecuteCommand( listAllResult, err := ExecuteCommand(
headscale, headscale,
[]string{ []string{
@ -278,7 +276,11 @@ func getMagicFQDN(
hostnames := make([]string, len(listAll)) hostnames := make([]string, len(listAll))
for index := range 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 return hostnames, nil

View File

@ -188,8 +188,16 @@ func (s *Suite) TestGetACLFilteredPeers(c *check.C) {
Hosts: map[string]netaddr.IPPrefix{}, Hosts: map[string]netaddr.IPPrefix{},
TagOwners: map[string][]string{}, TagOwners: map[string][]string{},
ACLs: []ACL{ 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{}, Tests: []ACLTest{},
} }

View File

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

View File

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