fix: handle empty aclPolicy for integration tests

This commit is contained in:
Adrien Raffin-Caboisse 2022-05-04 22:56:55 +02:00
parent 68417cc888
commit dc8c20e002
No known key found for this signature in database
GPG key ID: 7FB60532DEBEAD6A
4 changed files with 31 additions and 9 deletions

View file

@ -68,7 +68,8 @@ func init() {
if err != nil { if err != nil {
log.Fatalf(err.Error()) log.Fatalf(err.Error())
} }
tagCmd.Flags().StringSliceP("tags", "t", []string{}, "List of tags to add to the node") tagCmd.Flags().
StringSliceP("tags", "t", []string{}, "List of tags to add to the node")
nodeCmd.AddCommand(tagCmd) nodeCmd.AddCommand(tagCmd)
} }

View file

@ -268,7 +268,7 @@ func (api headscaleV1APIServer) ListMachines(
for index, machine := range machines { for index, machine := range machines {
m := machine.toProto() m := machine.toProto()
validTags, invalidTags := getTags( validTags, invalidTags := getTags(
*api.h.aclPolicy, api.h.aclPolicy,
machine, machine,
api.h.cfg.OIDC.StripEmaildomain, api.h.cfg.OIDC.StripEmaildomain,
) )

View file

@ -659,14 +659,18 @@ func (machine *Machine) toProto() *v1.Machine {
// getTags will return the tags of the current machine. // getTags will return the tags of the current machine.
func getTags( func getTags(
aclPolicy ACLPolicy, aclPolicy *ACLPolicy,
machine Machine, machine Machine,
stripEmailDomain bool, stripEmailDomain bool,
) (validTags []string, invalidTags []string) { ) (validTags []string, invalidTags []string) {
if aclPolicy == nil {
return
}
fmt.Println(aclPolicy)
validTagMap := make(map[string]bool) validTagMap := make(map[string]bool)
invalidTagMap := make(map[string]bool) invalidTagMap := make(map[string]bool)
for _, tag := range machine.HostInfo.RequestTags { for _, tag := range machine.HostInfo.RequestTags {
owners, err := expandTagOwners(aclPolicy, tag, stripEmailDomain) owners, err := expandTagOwners(*aclPolicy, tag, stripEmailDomain)
if errors.Is(err, errInvalidTag) { if errors.Is(err, errInvalidTag) {
invalidTagMap[tag] = true invalidTagMap[tag] = true

View file

@ -279,7 +279,7 @@ func (s *Suite) TestSerdeAddressStrignSlice(c *check.C) {
func Test_getTags(t *testing.T) { func Test_getTags(t *testing.T) {
type args struct { type args struct {
aclPolicy ACLPolicy aclPolicy *ACLPolicy
machine Machine machine Machine
stripEmailDomain bool stripEmailDomain bool
} }
@ -292,7 +292,7 @@ func Test_getTags(t *testing.T) {
{ {
name: "valid tag one machine", name: "valid tag one machine",
args: args{ args: args{
aclPolicy: ACLPolicy{ aclPolicy: &ACLPolicy{
TagOwners: TagOwners{ TagOwners: TagOwners{
"tag:valid": []string{"joe"}, "tag:valid": []string{"joe"},
}, },
@ -313,7 +313,7 @@ func Test_getTags(t *testing.T) {
{ {
name: "invalid tag and valid tag one machine", name: "invalid tag and valid tag one machine",
args: args{ args: args{
aclPolicy: ACLPolicy{ aclPolicy: &ACLPolicy{
TagOwners: TagOwners{ TagOwners: TagOwners{
"tag:valid": []string{"joe"}, "tag:valid": []string{"joe"},
}, },
@ -334,7 +334,7 @@ func Test_getTags(t *testing.T) {
{ {
name: "multiple invalid and identical tags, should return only one invalid tag", name: "multiple invalid and identical tags, should return only one invalid tag",
args: args{ args: args{
aclPolicy: ACLPolicy{ aclPolicy: &ACLPolicy{
TagOwners: TagOwners{ TagOwners: TagOwners{
"tag:valid": []string{"joe"}, "tag:valid": []string{"joe"},
}, },
@ -359,7 +359,7 @@ func Test_getTags(t *testing.T) {
{ {
name: "only invalid tags", name: "only invalid tags",
args: args{ args: args{
aclPolicy: ACLPolicy{ aclPolicy: &ACLPolicy{
TagOwners: TagOwners{ TagOwners: TagOwners{
"tag:valid": []string{"joe"}, "tag:valid": []string{"joe"},
}, },
@ -377,6 +377,23 @@ func Test_getTags(t *testing.T) {
wantValid: nil, wantValid: nil,
wantInvalid: []string{"tag:invalid", "very-invalid"}, wantInvalid: []string{"tag:invalid", "very-invalid"},
}, },
{
name: "empty ACLPolicy should return empty tags and should not panic",
args: args{
aclPolicy: nil,
machine: Machine{
Namespace: Namespace{
Name: "joe",
},
HostInfo: HostInfo{
RequestTags: []string{"tag:invalid", "very-invalid"},
},
},
stripEmailDomain: false,
},
wantValid: nil,
wantInvalid: nil,
},
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {