Skip to content

Commit 68f040a

Browse files
committed
remove "stripEmailDomain" argument
This commit makes a wrapper function round the normalisation requiring "stripEmailDomain" which has to be passed in almost all functions of headscale by loading it from Viper instead. Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
1 parent de64af1 commit 68f040a

16 files changed

+127
-220
lines changed

hscontrol/app.go

-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) {
169169
database, err := db.NewHeadscaleDatabase(
170170
cfg.DBtype,
171171
dbString,
172-
cfg.OIDC.StripEmaildomain,
173172
app.dbDebug,
174173
app.stateUpdateChan,
175174
cfg.IPPrefixes,

hscontrol/db/acls_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestValidExpandTagOwnersInSources(t *testing.T) {
5353
},
5454
}
5555

56-
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{}, false)
56+
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{})
5757
assert.NoError(t, err)
5858

5959
want := []tailcfg.FilterRule{
@@ -107,7 +107,7 @@ func TestInvalidTagValidUser(t *testing.T) {
107107
},
108108
}
109109

110-
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{}, false)
110+
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{})
111111
assert.NoError(t, err)
112112

113113
want := []tailcfg.FilterRule{
@@ -169,7 +169,7 @@ func TestPortGroup(t *testing.T) {
169169
pol, err := policy.LoadACLPolicyFromBytes(acl, "hujson")
170170
assert.NoError(t, err)
171171

172-
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{}, false)
172+
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{})
173173
assert.NoError(t, err)
174174

175175
want := []tailcfg.FilterRule{
@@ -224,7 +224,7 @@ func TestPortUser(t *testing.T) {
224224
pol, err := policy.LoadACLPolicyFromBytes(acl, "hujson")
225225
assert.NoError(t, err)
226226

227-
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{}, false)
227+
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{})
228228
assert.NoError(t, err)
229229

230230
want := []tailcfg.FilterRule{
@@ -285,7 +285,7 @@ func TestValidExpandTagOwnersInDestinations(t *testing.T) {
285285
// c.Assert(rules[0].DstPorts, check.HasLen, 1)
286286
// c.Assert(rules[0].DstPorts[0].IP, check.Equals, "100.64.0.1/32")
287287

288-
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{}, false)
288+
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{})
289289
assert.NoError(t, err)
290290

291291
want := []tailcfg.FilterRule{
@@ -361,7 +361,7 @@ func TestValidTagInvalidUser(t *testing.T) {
361361
},
362362
}
363363

364-
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{machine2}, false)
364+
got, _, err := policy.GenerateFilterRules(pol, &machine, types.Machines{machine2})
365365
assert.NoError(t, err)
366366

367367
want := []tailcfg.FilterRule{

hscontrol/db/db.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,15 @@ type HSDatabase struct {
4141

4242
ipAllocationMutex sync.Mutex
4343

44-
ipPrefixes []netip.Prefix
45-
baseDomain string
46-
stripEmailDomain bool
44+
ipPrefixes []netip.Prefix
45+
baseDomain string
4746
}
4847

4948
// TODO(kradalby): assemble this struct from toptions or something typed
5049
// rather than arguments.
5150
func NewHeadscaleDatabase(
5251
dbType, connectionAddr string,
53-
stripEmailDomain, debug bool,
52+
debug bool,
5453
notifyStateChan chan<- struct{},
5554
ipPrefixes []netip.Prefix,
5655
baseDomain string,
@@ -64,9 +63,8 @@ func NewHeadscaleDatabase(
6463
db: dbConn,
6564
notifyStateChan: notifyStateChan,
6665

67-
ipPrefixes: ipPrefixes,
68-
baseDomain: baseDomain,
69-
stripEmailDomain: stripEmailDomain,
66+
ipPrefixes: ipPrefixes,
67+
baseDomain: baseDomain,
7068
}
7169

7270
log.Debug().Msgf("database %#v", dbConn)
@@ -202,9 +200,8 @@ func NewHeadscaleDatabase(
202200

203201
for item, machine := range machines {
204202
if machine.GivenName == "" {
205-
normalizedHostname, err := util.NormalizeToFQDNRules(
203+
normalizedHostname, err := util.NormalizeToFQDNRulesConfigFromViper(
206204
machine.Hostname,
207-
stripEmailDomain,
208205
)
209206
if err != nil {
210207
log.Error().

hscontrol/db/machine.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,8 @@ func (hsdb *HSDatabase) enableRoutes(machine *types.Machine, routeStrs ...string
632632
}
633633

634634
func (hsdb *HSDatabase) generateGivenName(suppliedName string, randomSuffix bool) (string, error) {
635-
normalizedHostname, err := util.NormalizeToFQDNRules(
635+
normalizedHostname, err := util.NormalizeToFQDNRulesConfigFromViper(
636636
suppliedName,
637-
hsdb.stripEmailDomain,
638637
)
639638
if err != nil {
640639
return "", err

hscontrol/db/machine_test.go

+9-23
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ func (s *Suite) TestGetACLFilteredPeers(c *check.C) {
293293
testPeers, err := db.ListPeers(testMachine)
294294
c.Assert(err, check.IsNil)
295295

296-
adminRules, _, err := policy.GenerateFilterRules(aclPolicy, adminMachine, adminPeers, false)
296+
adminRules, _, err := policy.GenerateFilterRules(aclPolicy, adminMachine, adminPeers)
297297
c.Assert(err, check.IsNil)
298298

299-
testRules, _, err := policy.GenerateFilterRules(aclPolicy, testMachine, testPeers, false)
299+
testRules, _, err := policy.GenerateFilterRules(aclPolicy, testMachine, testPeers)
300300
c.Assert(err, check.IsNil)
301301

302302
peersOfAdminMachine := policy.FilterMachinesByACL(adminMachine, adminPeers, adminRules)
@@ -482,9 +482,7 @@ func TestHeadscale_generateGivenName(t *testing.T) {
482482
}{
483483
{
484484
name: "simple machine name generation",
485-
db: &HSDatabase{
486-
stripEmailDomain: true,
487-
},
485+
db: &HSDatabase{},
488486
args: args{
489487
suppliedName: "testmachine",
490488
randomSuffix: false,
@@ -494,9 +492,7 @@ func TestHeadscale_generateGivenName(t *testing.T) {
494492
},
495493
{
496494
name: "machine name with 53 chars",
497-
db: &HSDatabase{
498-
stripEmailDomain: true,
499-
},
495+
db: &HSDatabase{},
500496
args: args{
501497
suppliedName: "testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine",
502498
randomSuffix: false,
@@ -506,9 +502,7 @@ func TestHeadscale_generateGivenName(t *testing.T) {
506502
},
507503
{
508504
name: "machine name with 63 chars",
509-
db: &HSDatabase{
510-
stripEmailDomain: true,
511-
},
505+
db: &HSDatabase{},
512506
args: args{
513507
suppliedName: "machineeee12345678901234567890123456789012345678901234567890123",
514508
randomSuffix: false,
@@ -518,9 +512,7 @@ func TestHeadscale_generateGivenName(t *testing.T) {
518512
},
519513
{
520514
name: "machine name with 64 chars",
521-
db: &HSDatabase{
522-
stripEmailDomain: true,
523-
},
515+
db: &HSDatabase{},
524516
args: args{
525517
suppliedName: "machineeee123456789012345678901234567890123456789012345678901234",
526518
randomSuffix: false,
@@ -530,9 +522,7 @@ func TestHeadscale_generateGivenName(t *testing.T) {
530522
},
531523
{
532524
name: "machine name with 73 chars",
533-
db: &HSDatabase{
534-
stripEmailDomain: true,
535-
},
525+
db: &HSDatabase{},
536526
args: args{
537527
suppliedName: "machineeee123456789012345678901234567890123456789012345678901234567890123",
538528
randomSuffix: false,
@@ -542,9 +532,7 @@ func TestHeadscale_generateGivenName(t *testing.T) {
542532
},
543533
{
544534
name: "machine name with random suffix",
545-
db: &HSDatabase{
546-
stripEmailDomain: true,
547-
},
535+
db: &HSDatabase{},
548536
args: args{
549537
suppliedName: "test",
550538
randomSuffix: true,
@@ -554,9 +542,7 @@ func TestHeadscale_generateGivenName(t *testing.T) {
554542
},
555543
{
556544
name: "machine name with 63 chars with random suffix",
557-
db: &HSDatabase{
558-
stripEmailDomain: true,
559-
},
545+
db: &HSDatabase{},
560546
args: args{
561547
suppliedName: "machineeee12345678901234567890123456789012345678901234567890123",
562548
randomSuffix: true,

hscontrol/db/routes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func (hsdb *HSDatabase) EnableAutoApprovedRoutes(
424424
approvedRoutes = append(approvedRoutes, advertisedRoute)
425425
} else {
426426
// TODO(kradalby): figure out how to get this to depend on less stuff
427-
approvedIps, err := aclPolicy.ExpandAlias(types.Machines{*machine}, approvedAlias, hsdb.stripEmailDomain)
427+
approvedIps, err := aclPolicy.ExpandAlias(types.Machines{*machine}, approvedAlias)
428428
if err != nil {
429429
log.Err(err).
430430
Str("alias", approvedAlias).

hscontrol/db/suite_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func (s *Suite) ResetDB(c *check.C) {
6060
"sqlite3",
6161
tmpDir+"/headscale_test.db",
6262
false,
63-
false,
6463
sink,
6564
[]netip.Prefix{
6665
netip.MustParsePrefix("10.27.0.0/23"),

hscontrol/grpcv1.go

-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ func (api headscaleV1APIServer) ListMachines(
340340
m := machine.Proto()
341341
validTags, invalidTags := api.h.ACLPolicy.GetTagsOfMachine(
342342
machine,
343-
api.h.cfg.OIDC.StripEmaildomain,
344343
)
345344
m.InvalidTags = invalidTags
346345
m.ValidTags = validTags

hscontrol/mapper/mapper.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ type Mapper struct {
4141
dnsCfg *tailcfg.DNSConfig
4242
logtail bool
4343
randomClientPort bool
44-
stripEmailDomain bool
4544
}
4645

4746
func NewMapper(
@@ -53,7 +52,6 @@ func NewMapper(
5352
dnsCfg *tailcfg.DNSConfig,
5453
logtail bool,
5554
randomClientPort bool,
56-
stripEmailDomain bool,
5755
) *Mapper {
5856
return &Mapper{
5957
db: db,
@@ -66,7 +64,6 @@ func NewMapper(
6664
dnsCfg: dnsCfg,
6765
logtail: logtail,
6866
randomClientPort: randomClientPort,
69-
stripEmailDomain: stripEmailDomain,
7067
}
7168
}
7269

@@ -87,14 +84,13 @@ func fullMapResponse(
8784
machine *types.Machine,
8885
peers types.Machines,
8986

90-
stripEmailDomain bool,
9187
baseDomain string,
9288
dnsCfg *tailcfg.DNSConfig,
9389
derpMap *tailcfg.DERPMap,
9490
logtail bool,
9591
randomClientPort bool,
9692
) (*tailcfg.MapResponse, error) {
97-
tailnode, err := tailNode(*machine, pol, dnsCfg, baseDomain, stripEmailDomain)
93+
tailnode, err := tailNode(*machine, pol, dnsCfg, baseDomain)
9894
if err != nil {
9995
return nil, err
10096
}
@@ -103,7 +99,6 @@ func fullMapResponse(
10399
pol,
104100
machine,
105101
peers,
106-
stripEmailDomain,
107102
)
108103
if err != nil {
109104
return nil, err
@@ -129,7 +124,7 @@ func fullMapResponse(
129124
peers,
130125
)
131126

132-
tailPeers, err := tailNodes(peers, pol, dnsCfg, baseDomain, stripEmailDomain)
127+
tailPeers, err := tailNodes(peers, pol, dnsCfg, baseDomain)
133128
if err != nil {
134129
return nil, err
135130
}
@@ -296,7 +291,6 @@ func (m Mapper) CreateMapResponse(
296291
pol,
297292
machine,
298293
peers,
299-
m.stripEmailDomain,
300294
m.baseDomain,
301295
m.dnsCfg,
302296
m.derpMap,

hscontrol/mapper/mapper_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ func Test_fullMapResponse(t *testing.T) {
320320
machine *types.Machine
321321
peers types.Machines
322322

323-
stripEmailDomain bool
324323
baseDomain string
325324
dnsConfig *tailcfg.DNSConfig
326325
derpMap *tailcfg.DERPMap
@@ -335,7 +334,6 @@ func Test_fullMapResponse(t *testing.T) {
335334
// pol: &policy.ACLPolicy{},
336335
// dnsConfig: &tailcfg.DNSConfig{},
337336
// baseDomain: "",
338-
// stripEmailDomain: false,
339337
// want: nil,
340338
// wantErr: true,
341339
// },
@@ -344,7 +342,6 @@ func Test_fullMapResponse(t *testing.T) {
344342
pol: &policy.ACLPolicy{},
345343
machine: mini,
346344
peers: []types.Machine{},
347-
stripEmailDomain: false,
348345
baseDomain: "",
349346
dnsConfig: &tailcfg.DNSConfig{},
350347
derpMap: &tailcfg.DERPMap{},
@@ -375,7 +372,6 @@ func Test_fullMapResponse(t *testing.T) {
375372
peers: []types.Machine{
376373
peer1,
377374
},
378-
stripEmailDomain: false,
379375
baseDomain: "",
380376
dnsConfig: &tailcfg.DNSConfig{},
381377
derpMap: &tailcfg.DERPMap{},
@@ -417,7 +413,6 @@ func Test_fullMapResponse(t *testing.T) {
417413
peer1,
418414
peer2,
419415
},
420-
stripEmailDomain: false,
421416
baseDomain: "",
422417
dnsConfig: &tailcfg.DNSConfig{},
423418
derpMap: &tailcfg.DERPMap{},
@@ -458,7 +453,6 @@ func Test_fullMapResponse(t *testing.T) {
458453
tt.pol,
459454
tt.machine,
460455
tt.peers,
461-
tt.stripEmailDomain,
462456
tt.baseDomain,
463457
tt.dnsConfig,
464458
tt.derpMap,

hscontrol/mapper/tail.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ func tailNodes(
1818
pol *policy.ACLPolicy,
1919
dnsConfig *tailcfg.DNSConfig,
2020
baseDomain string,
21-
stripEmailDomain bool,
2221
) ([]*tailcfg.Node, error) {
2322
nodes := make([]*tailcfg.Node, len(machines))
2423

@@ -28,7 +27,6 @@ func tailNodes(
2827
pol,
2928
dnsConfig,
3029
baseDomain,
31-
stripEmailDomain,
3230
)
3331
if err != nil {
3432
return nil, err
@@ -47,7 +45,6 @@ func tailNode(
4745
pol *policy.ACLPolicy,
4846
dnsConfig *tailcfg.DNSConfig,
4947
baseDomain string,
50-
stripEmailDomain bool,
5148
) (*tailcfg.Node, error) {
5249
nodeKey, err := machine.NodePublicKey()
5350
if err != nil {
@@ -107,7 +104,7 @@ func tailNode(
107104

108105
online := machine.IsOnline()
109106

110-
tags, _ := pol.GetTagsOfMachine(machine, stripEmailDomain)
107+
tags, _ := pol.GetTagsOfMachine(machine)
111108
tags = lo.Uniq(append(tags, machine.ForcedTags...))
112109

113110
node := tailcfg.Node{

0 commit comments

Comments
 (0)