Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registration simplification #366

Merged
merged 31 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c58ce6f
Generalise the registration method to DRY stuff up
kradalby Feb 27, 2022
acb9458
Generalise registration for pre auth keys
kradalby Feb 27, 2022
fd1e4a1
Generalise registration for openid
kradalby Feb 27, 2022
caffbd8
Update cli registration with new method
kradalby Feb 27, 2022
ecc2643
Fix excessive replace
kradalby Feb 27, 2022
1caa6f5
Add todo for JSON datatype
kradalby Feb 27, 2022
469551b
Register new machines needing callback in memory
kradalby Feb 28, 2022
402a760
Reuse machine structure for parameters, named parameters
kradalby Feb 28, 2022
54cc3c0
Implement new machine register parameter
kradalby Feb 28, 2022
50053e6
Ignore complexity linter
kradalby Feb 28, 2022
c6b87de
Remove poorly aged test
kradalby Feb 28, 2022
e7bef56
Remove reference to registered in integration test
kradalby Feb 28, 2022
35616eb
Fix oidc error were namespace isnt created #365
kradalby Feb 28, 2022
16b21e8
Remove all references to Machine.Registered
kradalby Feb 28, 2022
a8649d8
Remove all references to Machine.Registered from tests
kradalby Feb 28, 2022
78251ce
Remove registrated field
kradalby Feb 28, 2022
eea8e7b
Update changelog
kradalby Feb 28, 2022
5e1b129
Remove registered field from proto
kradalby Feb 28, 2022
e64bee7
Regenerate proto
kradalby Feb 28, 2022
5e92dda
Remove redundant caches
kradalby Feb 28, 2022
8bef04d
Remove sorted todo
kradalby Feb 28, 2022
3790176
Reformat and add db backup note
kradalby Feb 28, 2022
82cb6b9
Cleanup some unreachable code
kradalby Feb 28, 2022
7c63412
Remove todo
kradalby Feb 28, 2022
d34d617
Merge branch 'main' into registration-simplification
kradalby Mar 1, 2022
a9d4fa8
Merge branch 'main' into registration-simplification
kradalby Mar 1, 2022
86ade72
Remove err check
kradalby Mar 1, 2022
ec4dc68
Use correct machinekey format for oidc reg
kradalby Mar 2, 2022
ef422e6
Protect against expiry nil
kradalby Mar 2, 2022
1f8c7f4
Add comment
kradalby Mar 2, 2022
e4d81bb
Merge branch 'main' into registration-simplification
kradalby Mar 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func (s *Suite) TestRegisterMachine(c *check.C) {
machineAfterRegistering, err := app.RegisterMachine(
machine.MachineKey,
namespace.Name,
RegisterMethodCLI,
nil, nil, nil, nil,
)
c.Assert(err, check.IsNil)
c.Assert(machineAfterRegistering.Registered, check.Equals, true)
Expand Down
75 changes: 39 additions & 36 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ type Machine struct {

Registered bool // temp
RegisterMethod string
AuthKeyID uint
AuthKey *PreAuthKey

// TODO(kradalby): This seems like irrelevant information?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see which machine has been registered with what key

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but is it really relevant? I delete the keys quite often so not sure

AuthKeyID uint
AuthKey *PreAuthKey

LastSeen *time.Time
LastSuccessfulUpdate *time.Time
Expand Down Expand Up @@ -686,6 +688,13 @@ func (machine *Machine) toProto() *v1.Machine {
func (h *Headscale) RegisterMachine(
machineKeyStr string,
namespaceName string,
registrationMethod string,

// Optionals
expiry *time.Time,
authKey *PreAuthKey,
nodePublicKey *string,
lastSeen *time.Time,
) (*Machine, error) {
namespace, err := h.GetNamespace(namespaceName)
if err != nil {
Expand All @@ -709,27 +718,13 @@ func (h *Headscale) RegisterMachine(
return nil, err
}

// TODO(kradalby): Currently, if it fails to find a requested expiry, non will be set
// This means that if a user is to slow with register a machine, it will possibly not
// have the correct expiry.
requestedTime := time.Time{}
if requestedTimeIf, found := h.requestedExpiryCache.Get(machineKey.String()); found {
log.Trace().
Caller().
Str("machine", machine.Name).
Msg("Expiry time found in cache, assigning to node")
if reqTime, ok := requestedTimeIf.(time.Time); ok {
requestedTime = reqTime
}
}

if machine.isRegistered() {
log.Trace().
Caller().
Str("machine", machine.Name).
Msg("machine already registered, reauthenticating")

h.RefreshMachine(machine, requestedTime)
h.RefreshMachine(machine, *expiry)

return machine, nil
}
Expand All @@ -739,17 +734,6 @@ func (h *Headscale) RegisterMachine(
Str("machine", machine.Name).
Msg("Attempting to register machine")

if machine.isRegistered() {
err := errMachineAlreadyRegistered
log.Error().
Caller().
Err(err).
Str("machine", machine.Name).
Msg("Attempting to register machine")

return nil, err
}

h.ipAllocationMutex.Lock()
defer h.ipAllocationMutex.Unlock()

Expand All @@ -764,17 +748,36 @@ func (h *Headscale) RegisterMachine(
return nil, err
}

log.Trace().
Caller().
Str("machine", machine.Name).
Str("ip", strings.Join(ips.ToStringSlice(), ",")).
Msg("Found IP for host")

machine.IPAddresses = ips

if expiry != nil {
machine.Expiry = expiry
}

if authKey != nil {
machine.AuthKeyID = uint(authKey.ID)
}

if nodePublicKey != nil {
machine.NodeKey = *nodePublicKey
}

if lastSeen != nil {
machine.LastSeen = lastSeen
}

machine.NamespaceID = namespace.ID

// TODO(kradalby): This field is uneccessary metadata,
// move it to tags instead of having a column.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tags of the node? to be used in acls?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the idea, but not sure if it makes sense. Might just keep it around for now.

machine.RegisterMethod = registrationMethod

// TODO(kradalby): Registered is a very frustrating value
// to keep up to date, and it makes is have to care if a
// machine is registered, authenticated and expired.
// Let us simplify the model, a machine is _only_ saved if
// it is registered.
machine.Registered = true
machine.RegisterMethod = RegisterMethodCLI
machine.Expiry = &requestedTime
h.db.Save(&machine)

log.Trace().
Expand Down