Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions agent/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func registerInstallerCommands(rootCmd *cobra.Command) {
installCmd.Flags().String("preferred-identity", "", "Preferred device identity")
installCmd.Flags().Uint("keepalive-interval", 30, "Keepalive interval in seconds")
installCmd.MarkFlagRequired("server-address") //nolint:errcheck
installCmd.MarkFlagRequired("tenant-id") //nolint:errcheck
installCmd.MarkFlagRequired("tenant-id") //nolint:errcheck

rootCmd.AddCommand(installCmd)

Expand Down Expand Up @@ -169,7 +169,7 @@ func writeAgentEnvFile(cfg installerConfig) error {
fmt.Fprintf(&buf, "SHELLHUB_KEEPALIVE_INTERVAL=%d\n", cfg.KeepaliveInterval)
}

return os.WriteFile(agentEnvFile, buf.Bytes(), 0600)
return os.WriteFile(agentEnvFile, buf.Bytes(), 0o600)
}

func writeAgentServiceFile(binaryPath string) error {
Expand All @@ -183,7 +183,7 @@ func writeAgentServiceFile(binaryPath string) error {
return err
}

return os.WriteFile(agentServiceFile, buf.Bytes(), 0644)
return os.WriteFile(agentServiceFile, buf.Bytes(), 0o644)
}

func agentUninstall() error {
Expand Down
4 changes: 4 additions & 0 deletions api/routes/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func (h *Handler) UpdateDevice(c gateway.Context) error {
return err
}

if c.Tenant() != nil {
req.TenantID = c.Tenant().ID
}

if err := h.service.UpdateDevice(c.Ctx(), req); err != nil {
return err
}
Expand Down
53 changes: 47 additions & 6 deletions api/services/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,51 @@ func (s *service) UpdateDevice(ctx context.Context, req *requests.DeviceUpdate)
return NewErrDeviceNotFound(models.UID(req.UID), err)
}

conflictsTarget := &models.DeviceConflicts{Name: req.Name}
conflictsTarget.Distinct(device)
if _, has, err := s.store.DeviceConflicts(ctx, conflictsTarget); err != nil || has {
return NewErrDeviceDuplicated(req.Name, err)
if req.Name != "" {
conflictsTarget := &models.DeviceConflicts{Name: req.Name}
conflictsTarget.Distinct(device)
if _, has, err := s.store.DeviceConflicts(ctx, conflictsTarget); err != nil || has {
return NewErrDeviceDuplicated(req.Name, err)
}

if !strings.EqualFold(req.Name, device.Name) {
device.Name = strings.ToLower(req.Name)
}
}

if req.Name != "" && !strings.EqualFold(req.Name, device.Name) {
device.Name = strings.ToLower(req.Name)
if req.SSH != nil {
// Only initialize SSH if device doesn't have one and we're updating it
if device.SSH == nil {
device.SSH = models.DefaultSSHSettings()
}

if req.SSH.AllowPassword != nil {
device.SSH.AllowPassword = *req.SSH.AllowPassword
}
if req.SSH.AllowPublicKey != nil {
device.SSH.AllowPublicKey = *req.SSH.AllowPublicKey
}
if req.SSH.AllowRoot != nil {
device.SSH.AllowRoot = *req.SSH.AllowRoot
}
if req.SSH.AllowEmptyPasswords != nil {
device.SSH.AllowEmptyPasswords = *req.SSH.AllowEmptyPasswords
}
if req.SSH.AllowTTY != nil {
device.SSH.AllowTTY = *req.SSH.AllowTTY
}
if req.SSH.AllowTCPForwarding != nil {
device.SSH.AllowTCPForwarding = *req.SSH.AllowTCPForwarding
}
if req.SSH.AllowWebEndpoints != nil {
device.SSH.AllowWebEndpoints = *req.SSH.AllowWebEndpoints
}
if req.SSH.AllowSFTP != nil {
device.SSH.AllowSFTP = *req.SSH.AllowSFTP
}
if req.SSH.AllowAgentForwarding != nil {
device.SSH.AllowAgentForwarding = *req.SSH.AllowAgentForwarding
}
}

if err := s.store.DeviceUpdate(ctx, device); err != nil { // nolint:revive
Expand Down Expand Up @@ -376,6 +413,10 @@ func (s *service) mergeDevice(ctx context.Context, tenantID string, oldDevice *m
}

log.WithFields(logFields).Debug("updating new device name to preserve old device identity")
if oldDevice.SSH != nil {
newDevice.SSH = oldDevice.SSH
}

newDevice.Name = oldDevice.Name
if err := s.store.DeviceUpdate(ctx, newDevice); err != nil {
log.WithError(err).WithFields(logFields).Error("failed to update new device name")
Expand Down
33 changes: 33 additions & 0 deletions api/services/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1699,13 +1699,35 @@ func TestUpdateDeviceStatus(t *testing.T) {
TenantID: "00000000-0000-0000-0000-000000000000",
Status: models.DeviceStatusAccepted,
Identity: &models.DeviceIdentity{MAC: "aa:bb:cc:dd:ee:ff"},
SSH: &models.SSHSettings{
AllowPassword: false,
AllowPublicKey: true,
AllowRoot: false,
AllowEmptyPasswords: true,
AllowTTY: false,
AllowTCPForwarding: true,
AllowWebEndpoints: false,
AllowSFTP: true,
AllowAgentForwarding: false,
},
}
mergedDevice := &models.Device{
UID: "new-device",
Name: "device-name",
TenantID: "00000000-0000-0000-0000-000000000000",
Status: models.DeviceStatusPending,
Identity: &models.DeviceIdentity{MAC: "aa:bb:cc:dd:ee:ff"},
SSH: &models.SSHSettings{
AllowPassword: false,
AllowPublicKey: true,
AllowRoot: false,
AllowEmptyPasswords: true,
AllowTTY: false,
AllowTCPForwarding: true,
AllowWebEndpoints: false,
AllowSFTP: true,
AllowAgentForwarding: false,
},
}
finalDevice := &models.Device{
UID: "new-device",
Expand All @@ -1714,6 +1736,17 @@ func TestUpdateDeviceStatus(t *testing.T) {
Status: models.DeviceStatusAccepted,
StatusUpdatedAt: now,
Identity: &models.DeviceIdentity{MAC: "aa:bb:cc:dd:ee:ff"},
SSH: &models.SSHSettings{
AllowPassword: false,
AllowPublicKey: true,
AllowRoot: false,
AllowEmptyPasswords: true,
AllowTTY: false,
AllowTCPForwarding: true,
AllowWebEndpoints: false,
AllowSFTP: true,
AllowAgentForwarding: false,
},
}

storeMock.
Expand Down
45 changes: 45 additions & 0 deletions api/services/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ func (s *service) CreateNamespace(ctx context.Context, req *requests.NamespaceCr
Settings: &models.NamespaceSettings{
SessionRecord: true,
ConnectionAnnouncement: "",
AllowPassword: true,
AllowPublicKey: true,
AllowRoot: true,
AllowEmptyPasswords: true,
AllowTTY: true,
AllowTCPForwarding: true,
AllowWebEndpoints: true,
AllowSFTP: true,
AllowAgentForwarding: true,
},
TenantID: req.TenantID,
Type: models.NewDefaultType(),
Expand Down Expand Up @@ -176,6 +185,42 @@ func (s *service) EditNamespace(ctx context.Context, req *requests.NamespaceEdit
namespace.Settings.ConnectionAnnouncement = *req.Settings.ConnectionAnnouncement
}

if req.Settings.AllowPassword != nil {
namespace.Settings.AllowPassword = *req.Settings.AllowPassword
}

if req.Settings.AllowPublicKey != nil {
namespace.Settings.AllowPublicKey = *req.Settings.AllowPublicKey
}

if req.Settings.AllowRoot != nil {
namespace.Settings.AllowRoot = *req.Settings.AllowRoot
}

if req.Settings.AllowEmptyPasswords != nil {
namespace.Settings.AllowEmptyPasswords = *req.Settings.AllowEmptyPasswords
}

if req.Settings.AllowTTY != nil {
namespace.Settings.AllowTTY = *req.Settings.AllowTTY
}

if req.Settings.AllowTCPForwarding != nil {
namespace.Settings.AllowTCPForwarding = *req.Settings.AllowTCPForwarding
}

if req.Settings.AllowWebEndpoints != nil {
namespace.Settings.AllowWebEndpoints = *req.Settings.AllowWebEndpoints
}

if req.Settings.AllowSFTP != nil {
namespace.Settings.AllowSFTP = *req.Settings.AllowSFTP
}

if req.Settings.AllowAgentForwarding != nil {
namespace.Settings.AllowAgentForwarding = *req.Settings.AllowAgentForwarding
}

if err := s.store.NamespaceUpdate(ctx, namespace); err != nil {
return nil, err
}
Expand Down
Loading
Loading