Skip to content

Commit b36002c

Browse files
henrybarretogustavosbarreto
authored andcommitted
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.
1 parent 289ff1e commit b36002c

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

pkg/agent/agent.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,34 @@ func (a *Agent) probeServerInfo() error {
343343
return err
344344
}
345345

346+
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")
347+
346348
// authorize send auth request to the server with device information in order to register it in the namespace.
347349
func (a *Agent) authorize() error {
348-
data, err := a.cli.AuthDevice(&models.DeviceAuthRequest{
350+
req := &models.DeviceAuthRequest{
349351
Info: a.Info,
350352
DeviceAuth: &models.DeviceAuth{
351353
Hostname: a.config.PreferredHostname,
352354
Identity: a.Identity,
353355
TenantID: a.config.TenantID,
354356
PublicKey: string(keygen.EncodePublicKeyToPem(a.pubKey)),
355357
},
356-
})
358+
}
359+
360+
// NOTE: A MAC address can be empty when the network interface used to communicate with the external world isn't a
361+
// physical one. In this case, we should be able to define a custom value for MAC's field using the
362+
// [PREFERRED_IDENTITY] variable. If the hostname is also empty, [PREFERRED_HOSTNAME] could be defined to provide a
363+
// fallback identifier for the device. This ensures that even if both the MAC address and hostname are missing, we
364+
// have a way to identify the device uniquely. When it occurs, and no variable was defined, the agent should fail to
365+
// initialize.
366+
if req.DeviceAuth.Hostname == "" && (req.DeviceAuth.Identity == nil || req.DeviceAuth.Identity.MAC == "") {
367+
return ErrNoIdentityAndHostname
368+
}
369+
370+
data, err := a.cli.AuthDevice(req)
371+
if err != nil {
372+
return err
373+
}
357374

358375
a.authData = data
359376

0 commit comments

Comments
 (0)