Skip to content

Commit acc2ff8

Browse files
committed
fix(BRE2-804): various polish: name sanitization, interface cleanliness, etc
1 parent ce7deb8 commit acc2ff8

10 files changed

Lines changed: 251 additions & 59 deletions

File tree

pkg/cmd/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func NewCmdCreate(t *terminal.Terminal, createStore CreateStore) *cobra.Command
5959
Detached: detached,
6060
InstanceType: gpu,
6161
}, createStore)
62-
if err != nil {
62+
if err != x nil {
6363
if strings.Contains(err.Error(), "duplicate instance with name") {
6464
t.Vprint(t.Yellow("try running:"))
6565
t.Vprint(t.Yellow("\tbrev start --name [different name] [repo] # or"))

pkg/cmd/deregister/deregister_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ type mockNetBirdManager struct {
9191
err error
9292
}
9393

94-
func (m *mockNetBirdManager) Install() error { return m.err }
95-
func (m *mockNetBirdManager) Uninstall() error { m.called = true; return m.err }
94+
func (m *mockNetBirdManager) Install() error { return m.err }
95+
func (m *mockNetBirdManager) Uninstall() error { m.called = true; return m.err }
96+
func (m *mockNetBirdManager) EnsureRunning() error { return m.err }
9697

9798
type mockNodeClientFactory struct {
9899
serverURL string

pkg/cmd/gpucreate/gpucreate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/brevdev/brev-cli/pkg/entity"
1919
breverrors "github.com/brevdev/brev-cli/pkg/errors"
2020
"github.com/brevdev/brev-cli/pkg/featureflag"
21+
"github.com/brevdev/brev-cli/pkg/names"
2122
"github.com/brevdev/brev-cli/pkg/store"
2223
"github.com/brevdev/brev-cli/pkg/terminal"
2324
"github.com/spf13/cobra"
@@ -194,8 +195,8 @@ func NewCmdGPUCreate(t *terminal.Terminal, gpuCreateStore GPUCreateStore) *cobra
194195
}
195196
}
196197

197-
if name == "" {
198-
return breverrors.NewValidationError("name is required (as argument or --name flag)")
198+
if err := names.ValidateNodeName(name); err != nil {
199+
return breverrors.WrapAndTrace(err)
199200
}
200201

201202
if count < 1 {

pkg/cmd/register/device_registration_store.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func (s *FileRegistrationStore) Load() (*DeviceRegistration, error) {
8585
if err := files.ReadJSON(files.AppFs, path, &reg); err != nil {
8686
return nil, breverrors.WrapAndTrace(err)
8787
}
88+
if reg.ExternalNodeID == "" || reg.OrgID == "" {
89+
return nil, breverrors.New("corrupt registration file: missing external_node_id or org_id")
90+
}
8891
return &reg, nil
8992
}
9093

@@ -125,6 +128,9 @@ func sudoWriteFile(path string, data []byte) error {
125128
if err := cmd.Run(); err != nil {
126129
return fmt.Errorf("sudo tee %s failed: %w", path, err)
127130
}
131+
if err := exec.Command("sudo", "chmod", "644", path).Run(); err != nil { //nolint:gosec // fixed base path
132+
return fmt.Errorf("sudo chmod %s failed: %w", path, err)
133+
}
128134
return nil
129135
}
130136

pkg/cmd/register/device_registration_store_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,48 @@ func Test_LoadRegistration_FailsWhenMissing(t *testing.T) {
144144
}
145145
}
146146

147+
func Test_LoadRegistration_RejectsMissingExternalNodeID(t *testing.T) {
148+
cleanup := setupTestFs(t)
149+
defer cleanup()
150+
151+
store := NewFileRegistrationStore()
152+
153+
reg := &DeviceRegistration{
154+
ExternalNodeID: "",
155+
DisplayName: "Test",
156+
OrgID: "org_xyz",
157+
}
158+
if err := store.Save(reg); err != nil {
159+
t.Fatalf("Save failed: %v", err)
160+
}
161+
162+
_, err := store.Load()
163+
if err == nil {
164+
t.Fatal("expected error loading registration with empty ExternalNodeID")
165+
}
166+
}
167+
168+
func Test_LoadRegistration_RejectsMissingOrgID(t *testing.T) {
169+
cleanup := setupTestFs(t)
170+
defer cleanup()
171+
172+
store := NewFileRegistrationStore()
173+
174+
reg := &DeviceRegistration{
175+
ExternalNodeID: "unode_abc",
176+
DisplayName: "Test",
177+
OrgID: "",
178+
}
179+
if err := store.Save(reg); err != nil {
180+
t.Fatalf("Save failed: %v", err)
181+
}
182+
183+
_, err := store.Load()
184+
if err == nil {
185+
t.Fatal("expected error loading registration with empty OrgID")
186+
}
187+
}
188+
147189
func Test_DeleteRegistration_FailsWhenMissing(t *testing.T) {
148190
cleanup := setupTestFs(t)
149191
defer cleanup()

pkg/cmd/register/providers.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package register
22

33
import (
4+
"fmt"
5+
"os/exec"
46
"runtime"
7+
"strings"
58

69
nodev1connect "buf.build/gen/go/brevdev/devplane/connectrpc/go/devplaneapi/v1/devplaneapiv1connect"
710

@@ -38,6 +41,33 @@ type Netbird struct{}
3841
func (Netbird) Install() error { return InstallNetbird() }
3942
func (Netbird) Uninstall() error { return UninstallNetbird() }
4043

44+
// EnsureRunning checks if the netbird systemd service is active and attempts
45+
// to start it if it is not. It also checks the netbird peer connection status
46+
// and runs "netbird up" if the peer is disconnected.
47+
func (Netbird) EnsureRunning() error {
48+
out, err := exec.Command("systemctl", "is-active", "netbird").Output() //nolint:gosec // fixed service name
49+
if err != nil || strings.TrimSpace(string(out)) != "active" {
50+
if startErr := exec.Command("sudo", "systemctl", "start", "netbird").Run(); startErr != nil { //nolint:gosec // fixed service name
51+
return fmt.Errorf("failed to start Brev tunnel service: %w", startErr)
52+
}
53+
}
54+
55+
statusOut, err := exec.Command("netbird", "status").Output() //nolint:gosec // fixed command
56+
if err != nil {
57+
// Service is running, just can't confirm peer status.
58+
return nil
59+
}
60+
61+
if netbirdManagementConnected(string(statusOut)) {
62+
return nil
63+
}
64+
65+
if upErr := exec.Command("sudo", "netbird", "up").Run(); upErr != nil { //nolint:gosec // fixed command
66+
return fmt.Errorf("failed to reconnect Brev tunnel: %w", upErr)
67+
}
68+
return nil
69+
}
70+
4171
// ShellSetupRunner runs setup scripts via shell.
4272
type ShellSetupRunner struct{}
4373

pkg/cmd/register/register.go

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"os"
8-
"os/exec"
98
"os/user"
109
"strings"
1110
"time"
@@ -18,6 +17,7 @@ import (
1817
"github.com/brevdev/brev-cli/pkg/entity"
1918
breverrors "github.com/brevdev/brev-cli/pkg/errors"
2019
"github.com/brevdev/brev-cli/pkg/externalnode"
20+
"github.com/brevdev/brev-cli/pkg/names"
2121
"github.com/brevdev/brev-cli/pkg/terminal"
2222

2323
"github.com/spf13/cobra"
@@ -41,10 +41,14 @@ func (r OSFileReader) ReadFile(path string) ([]byte, error) {
4141
return data, nil
4242
}
4343

44-
// NetBirdManager installs and uninstalls the NetBird network agent.
44+
// NetBirdManager installs, uninstalls, and monitors the NetBird network agent.
4545
type NetBirdManager interface {
4646
Install() error
4747
Uninstall() error
48+
// EnsureRunning checks whether the NetBird service is active and
49+
// connected, starting or reconnecting it if needed. Returns nil when
50+
// the tunnel is healthy.
51+
EnsureRunning() error
4852
}
4953

5054
// SetupRunner runs a setup script on the local machine.
@@ -120,8 +124,8 @@ func runRegister(ctx context.Context, t *terminal.Terminal, s RegisterStore, nam
120124
return checkExistingRegistration(ctx, t, s, name, deps)
121125
}
122126

123-
if name == "" {
124-
return fmt.Errorf("please provide a name for this device\n\nUsage: brev register <name>\nExample: brev register \"my-DGX-Spark\"")
127+
if err := names.ValidateNodeName(name); err != nil {
128+
return breverrors.WrapAndTrace(err)
125129
}
126130

127131
brevUser, err := s.GetCurrentUser()
@@ -273,7 +277,9 @@ func checkExistingRegistration(ctx context.Context, t *terminal.Terminal, s Regi
273277

274278
// Check local netbird service and start it if down.
275279
t.Vprint(" Checking local Brev tunnel...")
276-
if ensureNetbirdRunning(t) {
280+
if err := deps.netbird.EnsureRunning(); err != nil {
281+
t.Vprintf(" %s\n", t.Yellow(fmt.Sprintf("Warning: %v", err)))
282+
} else {
277283
t.Vprint(t.Green(" Brev tunnel is running."))
278284
}
279285

@@ -282,41 +288,6 @@ func checkExistingRegistration(ctx context.Context, t *terminal.Terminal, s Regi
282288
return nil
283289
}
284290

285-
// ensureNetbirdRunning checks if the netbird systemd service is active and
286-
// attempts to start it if it is not. It also checks the netbird peer
287-
// connection status and runs "netbird up" if the peer is disconnected.
288-
// Returns true if the service is running and connected after the check.
289-
func ensureNetbirdRunning(t *terminal.Terminal) bool {
290-
out, err := exec.Command("systemctl", "is-active", "netbird").Output() //nolint:gosec // fixed service name
291-
if err != nil || strings.TrimSpace(string(out)) != "active" {
292-
t.Vprintf(" %s\n", t.Yellow("Brev tunnel service is not running. Attempting to start..."))
293-
if startErr := exec.Command("sudo", "systemctl", "start", "netbird").Run(); startErr != nil { //nolint:gosec // fixed service name
294-
t.Vprintf(" %s\n", t.Yellow(fmt.Sprintf("Warning: failed to start Brev tunnel service: %v", startErr)))
295-
return false
296-
}
297-
t.Vprint(t.Green(" Brev tunnel service started."))
298-
}
299-
300-
// Service is running — now check peer connection status.
301-
statusOut, err := exec.Command("netbird", "status").Output() //nolint:gosec // fixed command
302-
if err != nil {
303-
t.Vprintf(" %s\n", t.Yellow(fmt.Sprintf("Warning: could not check Brev tunnel status: %v", err)))
304-
return true // service is running, just can't confirm peer status
305-
}
306-
307-
if netbirdManagementConnected(string(statusOut)) {
308-
return true
309-
}
310-
311-
t.Vprintf(" %s\n", t.Yellow("Brev tunnel peer is disconnected. Reconnecting..."))
312-
if upErr := exec.Command("sudo", "netbird", "up").Run(); upErr != nil { //nolint:gosec // fixed command
313-
t.Vprintf(" %s\n", t.Yellow(fmt.Sprintf("Warning: failed to reconnect Brev tunnel: %v", upErr)))
314-
return false
315-
}
316-
t.Vprint(t.Green(" Brev tunnel reconnected."))
317-
return true
318-
}
319-
320291
// netbirdManagementConnected parses "netbird status" output and returns true
321292
// when the Management line reports "Connected".
322293
func netbirdManagementConnected(statusOutput string) bool {

0 commit comments

Comments
 (0)