From b36002c8ddc43e2a6bf5f64d759ee70b3acdaec5 Mon Sep 17 00:00:00 2001 From: Henry Barreto Date: Wed, 19 Feb 2025 16:10:48 -0300 Subject: [PATCH] fix(agent): don't initialize agent when hostname and identity are empty A MAC address can be empty when the network interface used to communicate with the external world isn't a physical one. In this case, we should be able to define a custom value for MAC's field using the [PREFERRED_IDENTITY] variable. If the hostname is also empty, [PREFERRED_HOSTNAME] could be defined to provide a fallback identifier for the device. This ensures that even if both the MAC address and hostname is missing, we have a way to identify the device uniquely. When it occurs, and no variable was defined, the agent should fail to initialize. --- pkg/agent/agent.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index 709f0eda0de..8b71bfc790b 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -343,9 +343,11 @@ func (a *Agent) probeServerInfo() error { return err } +var ErrNoIdentityAndHostname = errors.New("the device doesn't have a valid hostname and identity. Set PREFERRED_IDENTITY or PREFERRED_HOSTNAME to specify the device's name and identity") + // authorize send auth request to the server with device information in order to register it in the namespace. func (a *Agent) authorize() error { - data, err := a.cli.AuthDevice(&models.DeviceAuthRequest{ + req := &models.DeviceAuthRequest{ Info: a.Info, DeviceAuth: &models.DeviceAuth{ Hostname: a.config.PreferredHostname, @@ -353,7 +355,22 @@ func (a *Agent) authorize() error { TenantID: a.config.TenantID, PublicKey: string(keygen.EncodePublicKeyToPem(a.pubKey)), }, - }) + } + + // NOTE: A MAC address can be empty when the network interface used to communicate with the external world isn't a + // physical one. In this case, we should be able to define a custom value for MAC's field using the + // [PREFERRED_IDENTITY] variable. If the hostname is also empty, [PREFERRED_HOSTNAME] could be defined to provide a + // fallback identifier for the device. This ensures that even if both the MAC address and hostname are missing, we + // have a way to identify the device uniquely. When it occurs, and no variable was defined, the agent should fail to + // initialize. + if req.DeviceAuth.Hostname == "" && (req.DeviceAuth.Identity == nil || req.DeviceAuth.Identity.MAC == "") { + return ErrNoIdentityAndHostname + } + + data, err := a.cli.AuthDevice(req) + if err != nil { + return err + } a.authData = data