Skip to content

Commit e3521be

Browse files
authored
allow users to be defined with @ in v1 (#2495)
* allow users to be defined with @ in v1 Signed-off-by: Kristoffer Dalby <[email protected]> * remove integration test rewrite hack Signed-off-by: Kristoffer Dalby <[email protected]> * remove test rewrite hack Signed-off-by: Kristoffer Dalby <[email protected]> * add @ to integration tests Signed-off-by: Kristoffer Dalby <[email protected]> * a bit to agressive removeals Signed-off-by: Kristoffer Dalby <[email protected]> * fix last test Signed-off-by: Kristoffer Dalby <[email protected]> --------- Signed-off-by: Kristoffer Dalby <[email protected]>
1 parent f52f15f commit e3521be

File tree

12 files changed

+76
-150
lines changed

12 files changed

+76
-150
lines changed

hscontrol/db/node_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -440,16 +440,11 @@ func TestAutoApproveRoutes(t *testing.T) {
440440
adb, err := newSQLiteTestDB()
441441
require.NoError(t, err)
442442

443-
suffix := ""
444-
if version == 1 {
445-
suffix = "@"
446-
}
447-
448-
user, err := adb.CreateUser(types.User{Name: "test" + suffix})
443+
user, err := adb.CreateUser(types.User{Name: "test"})
449444
require.NoError(t, err)
450-
_, err = adb.CreateUser(types.User{Name: "test2" + suffix})
445+
_, err = adb.CreateUser(types.User{Name: "test2"})
451446
require.NoError(t, err)
452-
taggedUser, err := adb.CreateUser(types.User{Name: "tagged" + suffix})
447+
taggedUser, err := adb.CreateUser(types.User{Name: "tagged"})
453448
require.NoError(t, err)
454449

455450
node := types.Node{
@@ -572,7 +567,7 @@ func TestEphemeralGarbageCollectorLoads(t *testing.T) {
572567
})
573568
go e.Start()
574569

575-
for i := 0; i < want; i++ {
570+
for i := range want {
576571
go e.Schedule(types.NodeID(i), 1*time.Second)
577572
}
578573

hscontrol/policy/policy_test.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,6 @@ func TestTheInternet(t *testing.T) {
9797
}
9898
}
9999

100-
// addAtForFilterV1 returns a copy of the given userslice
101-
// and adds "@" character to the Name field.
102-
// This is a "compatibility" move to allow the old tests
103-
// to run against the "new" format which requires "@".
104-
func addAtForFilterV1(users types.Users) types.Users {
105-
ret := make(types.Users, len(users))
106-
for idx := range users {
107-
ret[idx] = users[idx]
108-
ret[idx].Name = ret[idx].Name + "@"
109-
}
110-
return ret
111-
}
112-
113100
func TestReduceFilterRules(t *testing.T) {
114101
users := types.Users{
115102
types.User{Model: gorm.Model{ID: 1}, Name: "mickael"},
@@ -780,11 +767,7 @@ func TestReduceFilterRules(t *testing.T) {
780767
t.Run(fmt.Sprintf("%s-v%d", tt.name, version), func(t *testing.T) {
781768
var pm PolicyManager
782769
var err error
783-
if version == 1 {
784-
pm, err = pmf(addAtForFilterV1(users), append(tt.peers, tt.node))
785-
} else {
786-
pm, err = pmf(users, append(tt.peers, tt.node))
787-
}
770+
pm, err = pmf(users, append(tt.peers, tt.node))
788771
require.NoError(t, err)
789772
got := pm.Filter()
790773
got = ReduceFilterRules(tt.node, got)

hscontrol/policy/v1/acls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,10 @@ var (
969969
func findUserFromToken(users []types.User, token string) (types.User, error) {
970970
var potentialUsers []types.User
971971

972+
// This adds the v2 support to looking up users with the new required
973+
// policyv2 format where usernames have @ at the end if they are not emails.
974+
token = strings.TrimSuffix(token, "@")
975+
972976
for _, user := range users {
973977
if user.ProviderIdentifier.Valid && user.ProviderIdentifier.String == token {
974978
// Prioritize ProviderIdentifier match and exit early

hscontrol/policy/v1/acls_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,6 +2964,16 @@ func TestFindUserByToken(t *testing.T) {
29642964
want: types.User{},
29652965
wantErr: true,
29662966
},
2967+
{
2968+
name: "test-v2-format-working",
2969+
users: []types.User{
2970+
{ProviderIdentifier: sql.NullString{Valid: false, String: ""}, Name: "user1", Email: "[email protected]"},
2971+
{ProviderIdentifier: sql.NullString{Valid: false, String: ""}, Name: "user2", Email: "[email protected]"},
2972+
},
2973+
token: "user2",
2974+
want: types.User{ProviderIdentifier: sql.NullString{Valid: false, String: ""}, Name: "user2", Email: "[email protected]"},
2975+
wantErr: false,
2976+
},
29672977
}
29682978

29692979
for _, tt := range tests {

hscontrol/util/dns.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func GenerateIPv6DNSRootDomain(ipPrefix netip.Prefix) []dnsname.FQDN {
196196
// and from what I can see, the generateMagicDNSRootDomains
197197
// function is called only once over the lifetime of a server process.
198198
prefixConstantParts := []string{}
199-
for i := 0; i < maskBits/nibbleLen; i++ {
199+
for i := range maskBits / nibbleLen {
200200
prefixConstantParts = append(
201201
[]string{string(nibbleStr[i])},
202202
prefixConstantParts...)
@@ -215,7 +215,7 @@ func GenerateIPv6DNSRootDomain(ipPrefix netip.Prefix) []dnsname.FQDN {
215215
} else {
216216
domCount := 1 << (maskBits % nibbleLen)
217217
fqdns = make([]dnsname.FQDN, 0, domCount)
218-
for i := 0; i < domCount; i++ {
218+
for i := range domCount {
219219
varNibble := fmt.Sprintf("%x", i)
220220
dom, err := makeDomain(varNibble)
221221
if err != nil {

hscontrol/util/string_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestGenerateRandomStringDNSSafe(t *testing.T) {
11-
for i := 0; i < 100000; i++ {
11+
for range 100000 {
1212
str, err := GenerateRandomStringDNSSafe(8)
1313
require.NoError(t, err)
1414
assert.Len(t, str, 8)

integration/acl_test.go

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ func TestACLHostsInNetMapTable(t *testing.T) {
137137
ACLs: []policyv1.ACL{
138138
{
139139
Action: "accept",
140-
Sources: []string{"user1"},
141-
Destinations: []string{"user1:*"},
140+
Sources: []string{"user1@"},
141+
Destinations: []string{"user1@:*"},
142142
},
143143
{
144144
Action: "accept",
145-
Sources: []string{"user2"},
146-
Destinations: []string{"user2:*"},
145+
Sources: []string{"user2@"},
146+
Destinations: []string{"user2@:*"},
147147
},
148148
},
149149
}, want: map[string]int{
@@ -160,23 +160,23 @@ func TestACLHostsInNetMapTable(t *testing.T) {
160160
ACLs: []policyv1.ACL{
161161
{
162162
Action: "accept",
163-
Sources: []string{"user1"},
164-
Destinations: []string{"user1:22"},
163+
Sources: []string{"user1@"},
164+
Destinations: []string{"user1@:22"},
165165
},
166166
{
167167
Action: "accept",
168-
Sources: []string{"user2"},
169-
Destinations: []string{"user2:22"},
168+
Sources: []string{"user2@"},
169+
Destinations: []string{"user2@:22"},
170170
},
171171
{
172172
Action: "accept",
173-
Sources: []string{"user1"},
174-
Destinations: []string{"user2:22"},
173+
Sources: []string{"user1@"},
174+
Destinations: []string{"user2@:22"},
175175
},
176176
{
177177
Action: "accept",
178-
Sources: []string{"user2"},
179-
Destinations: []string{"user1:22"},
178+
Sources: []string{"user2@"},
179+
Destinations: []string{"user1@:22"},
180180
},
181181
},
182182
}, want: map[string]int{
@@ -194,18 +194,18 @@ func TestACLHostsInNetMapTable(t *testing.T) {
194194
ACLs: []policyv1.ACL{
195195
{
196196
Action: "accept",
197-
Sources: []string{"user1"},
198-
Destinations: []string{"user1:*"},
197+
Sources: []string{"user1@"},
198+
Destinations: []string{"user1@:*"},
199199
},
200200
{
201201
Action: "accept",
202-
Sources: []string{"user2"},
203-
Destinations: []string{"user2:*"},
202+
Sources: []string{"user2@"},
203+
Destinations: []string{"user2@:*"},
204204
},
205205
{
206206
Action: "accept",
207-
Sources: []string{"user1"},
208-
Destinations: []string{"user2:*"},
207+
Sources: []string{"user1@"},
208+
Destinations: []string{"user2@:*"},
209209
},
210210
},
211211
}, want: map[string]int{
@@ -219,18 +219,18 @@ func TestACLHostsInNetMapTable(t *testing.T) {
219219
ACLs: []policyv1.ACL{
220220
{
221221
Action: "accept",
222-
Sources: []string{"user1"},
223-
Destinations: append([]string{"user1:*"}, veryLargeDestination...),
222+
Sources: []string{"user1@"},
223+
Destinations: append([]string{"user1@:*"}, veryLargeDestination...),
224224
},
225225
{
226226
Action: "accept",
227-
Sources: []string{"user2"},
228-
Destinations: append([]string{"user2:*"}, veryLargeDestination...),
227+
Sources: []string{"user2@"},
228+
Destinations: append([]string{"user2@:*"}, veryLargeDestination...),
229229
},
230230
{
231231
Action: "accept",
232-
Sources: []string{"user1"},
233-
Destinations: append([]string{"user2:*"}, veryLargeDestination...),
232+
Sources: []string{"user1@"},
233+
Destinations: append([]string{"user2@:*"}, veryLargeDestination...),
234234
},
235235
},
236236
}, want: map[string]int{
@@ -299,8 +299,8 @@ func TestACLAllowUser80Dst(t *testing.T) {
299299
ACLs: []policyv1.ACL{
300300
{
301301
Action: "accept",
302-
Sources: []string{"user1"},
303-
Destinations: []string{"user2:80"},
302+
Sources: []string{"user1@"},
303+
Destinations: []string{"user2@:80"},
304304
},
305305
},
306306
},
@@ -351,7 +351,7 @@ func TestACLDenyAllPort80(t *testing.T) {
351351
scenario := aclScenario(t,
352352
&policyv1.ACLPolicy{
353353
Groups: map[string][]string{
354-
"group:integration-acl-test": {"user1", "user2"},
354+
"group:integration-acl-test": {"user1@", "user2@"},
355355
},
356356
ACLs: []policyv1.ACL{
357357
{
@@ -400,8 +400,8 @@ func TestACLAllowUserDst(t *testing.T) {
400400
ACLs: []policyv1.ACL{
401401
{
402402
Action: "accept",
403-
Sources: []string{"user1"},
404-
Destinations: []string{"user2:*"},
403+
Sources: []string{"user1@"},
404+
Destinations: []string{"user2@:*"},
405405
},
406406
},
407407
},
@@ -456,7 +456,7 @@ func TestACLAllowStarDst(t *testing.T) {
456456
ACLs: []policyv1.ACL{
457457
{
458458
Action: "accept",
459-
Sources: []string{"user1"},
459+
Sources: []string{"user1@"},
460460
Destinations: []string{"*:*"},
461461
},
462462
},
@@ -912,8 +912,8 @@ func TestACLDevice1CanAccessDevice2(t *testing.T) {
912912
"group": {
913913
policy: policyv1.ACLPolicy{
914914
Groups: map[string][]string{
915-
"group:one": {"user1"},
916-
"group:two": {"user2"},
915+
"group:one": {"user1@"},
916+
"group:two": {"user2@"},
917917
},
918918
ACLs: []policyv1.ACL{
919919
{
@@ -1079,15 +1079,12 @@ func TestPolicyUpdateWhileRunningWithCLIInDatabase(t *testing.T) {
10791079
ACLs: []policyv1.ACL{
10801080
{
10811081
Action: "accept",
1082-
Sources: []string{"user1"},
1083-
Destinations: []string{"user2:*"},
1082+
Sources: []string{"user1@"},
1083+
Destinations: []string{"user2@:*"},
10841084
},
10851085
},
10861086
Hosts: policyv1.Hosts{},
10871087
}
1088-
if usePolicyV2ForTest {
1089-
hsic.RewritePolicyToV2(&p)
1090-
}
10911088

10921089
pBytes, _ := json.Marshal(p)
10931090

integration/cli_test.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func TestPreAuthKeyCommand(t *testing.T) {
263263
keys := make([]*v1.PreAuthKey, count)
264264
assertNoErr(t, err)
265265

266-
for index := 0; index < count; index++ {
266+
for index := range count {
267267
var preAuthKey v1.PreAuthKey
268268
err := executeAndUnmarshal(
269269
headscale,
@@ -639,7 +639,7 @@ func TestApiKeyCommand(t *testing.T) {
639639

640640
keys := make([]string, count)
641641

642-
for idx := 0; idx < count; idx++ {
642+
for idx := range count {
643643
apiResult, err := headscale.Execute(
644644
[]string{
645645
"headscale",
@@ -716,7 +716,7 @@ func TestApiKeyCommand(t *testing.T) {
716716
expiredPrefixes := make(map[string]bool)
717717

718718
// Expire three keys
719-
for idx := 0; idx < 3; idx++ {
719+
for idx := range 3 {
720720
_, err := headscale.Execute(
721721
[]string{
722722
"headscale",
@@ -951,7 +951,7 @@ func TestNodeAdvertiseTagCommand(t *testing.T) {
951951
},
952952
},
953953
TagOwners: map[string][]string{
954-
"tag:test": {"user1"},
954+
"tag:test": {"user1@"},
955955
},
956956
},
957957
wantTag: true,
@@ -960,7 +960,7 @@ func TestNodeAdvertiseTagCommand(t *testing.T) {
960960
name: "with-policy-groups",
961961
policy: &policyv1.ACLPolicy{
962962
Groups: policyv1.Groups{
963-
"group:admins": []string{"user1"},
963+
"group:admins": []string{"user1@"},
964964
},
965965
ACLs: []policyv1.ACL{
966966
{
@@ -1357,7 +1357,7 @@ func TestNodeExpireCommand(t *testing.T) {
13571357
assert.True(t, listAll[3].GetExpiry().AsTime().IsZero())
13581358
assert.True(t, listAll[4].GetExpiry().AsTime().IsZero())
13591359

1360-
for idx := 0; idx < 3; idx++ {
1360+
for idx := range 3 {
13611361
_, err := headscale.Execute(
13621362
[]string{
13631363
"headscale",
@@ -1484,7 +1484,7 @@ func TestNodeRenameCommand(t *testing.T) {
14841484
assert.Contains(t, listAll[3].GetGivenName(), "node-4")
14851485
assert.Contains(t, listAll[4].GetGivenName(), "node-5")
14861486

1487-
for idx := 0; idx < 3; idx++ {
1487+
for idx := range 3 {
14881488
res, err := headscale.Execute(
14891489
[]string{
14901490
"headscale",
@@ -1751,12 +1751,9 @@ func TestPolicyCommand(t *testing.T) {
17511751
},
17521752
},
17531753
TagOwners: map[string][]string{
1754-
"tag:exists": {"user1"},
1754+
"tag:exists": {"user1@"},
17551755
},
17561756
}
1757-
if usePolicyV2ForTest {
1758-
hsic.RewritePolicyToV2(&p)
1759-
}
17601757

17611758
pBytes, _ := json.Marshal(p)
17621759

@@ -1797,11 +1794,6 @@ func TestPolicyCommand(t *testing.T) {
17971794

17981795
assert.Len(t, output.TagOwners, 1)
17991796
assert.Len(t, output.ACLs, 1)
1800-
if usePolicyV2ForTest {
1801-
assert.Equal(t, output.TagOwners["tag:exists"], []string{"user1@"})
1802-
} else {
1803-
assert.Equal(t, output.TagOwners["tag:exists"], []string{"user1"})
1804-
}
18051797
}
18061798

18071799
func TestPolicyBrokenConfigCommand(t *testing.T) {
@@ -1840,12 +1832,9 @@ func TestPolicyBrokenConfigCommand(t *testing.T) {
18401832
},
18411833
},
18421834
TagOwners: map[string][]string{
1843-
"tag:exists": {"user1"},
1835+
"tag:exists": {"user1@"},
18441836
},
18451837
}
1846-
if usePolicyV2ForTest {
1847-
hsic.RewritePolicyToV2(&p)
1848-
}
18491838

18501839
pBytes, _ := json.Marshal(p)
18511840

integration/general_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func TestTaildrop(t *testing.T) {
345345

346346
retry := func(times int, sleepInterval time.Duration, doWork func() error) error {
347347
var err error
348-
for attempts := 0; attempts < times; attempts++ {
348+
for range times {
349349
err = doWork()
350350
if err == nil {
351351
return nil

0 commit comments

Comments
 (0)