generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 25
feat(BRE2-736): add ability to register device to Brev #295
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8f67b8c
feat(BRE2-736): registration of user owned linux device to Brev. Begi…
patelspratik 677f49e
claude review feedback
patelspratik 622a898
review feedback and more: some small interface changes, added an in m…
patelspratik 8239775
Merge branch 'main' into BRE2-736-register
patelspratik b627ee5
review feedback
patelspratik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ dist/ | |
| # binary | ||
| brev-cli | ||
| brev | ||
| brev-local | ||
|
|
||
| # golang executable | ||
| go1.* | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| // Package deregister provides the brev deregister command for device deregistration | ||
| package deregister | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/user" | ||
|
|
||
| nodev1 "buf.build/gen/go/brevdev/devplane/protocolbuffers/go/devplaneapi/v1" | ||
| "connectrpc.com/connect" | ||
|
|
||
| "github.com/brevdev/brev-cli/pkg/cmd/register" | ||
| "github.com/brevdev/brev-cli/pkg/config" | ||
| "github.com/brevdev/brev-cli/pkg/entity" | ||
| breverrors "github.com/brevdev/brev-cli/pkg/errors" | ||
| "github.com/brevdev/brev-cli/pkg/externalnode" | ||
| "github.com/brevdev/brev-cli/pkg/terminal" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // DeregisterStore defines the store methods needed by the deregister command. | ||
| type DeregisterStore interface { | ||
| GetCurrentUser() (*entity.User, error) | ||
| GetBrevHomePath() (string, error) | ||
| GetAccessToken() (string, error) | ||
| } | ||
|
|
||
| // SSHKeyRemover removes Brev-managed SSH keys and returns the lines removed. | ||
| type SSHKeyRemover interface { | ||
| RemoveBrevKeys(u *user.User) ([]string, error) | ||
| } | ||
|
|
||
| // brevSSHKeyRemover delegates to register.RemoveBrevAuthorizedKeys. | ||
| type brevSSHKeyRemover struct{} | ||
|
|
||
| func (brevSSHKeyRemover) RemoveBrevKeys(u *user.User) ([]string, error) { | ||
| removed, err := register.RemoveBrevAuthorizedKeys(u) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("removing brev authorized keys: %w", err) | ||
| } | ||
| return removed, nil | ||
| } | ||
|
|
||
| // deregisterDeps bundles the side-effecting dependencies of runDeregister so | ||
| // they can be replaced in tests. | ||
| type deregisterDeps struct { | ||
| platform externalnode.PlatformChecker | ||
| prompter terminal.Selector | ||
| netbird register.NetBirdManager | ||
| nodeClients externalnode.NodeClientFactory | ||
| registrationStore register.RegistrationStore | ||
| sshKeys SSHKeyRemover | ||
| } | ||
|
|
||
| func defaultDeregisterDeps(brevHome string) deregisterDeps { | ||
| return deregisterDeps{ | ||
| platform: register.LinuxPlatform{}, | ||
| prompter: register.TerminalPrompter{}, | ||
| netbird: register.Netbird{}, | ||
| nodeClients: register.DefaultNodeClientFactory{}, | ||
| registrationStore: register.NewFileRegistrationStore(brevHome), | ||
| sshKeys: brevSSHKeyRemover{}, | ||
| } | ||
| } | ||
|
|
||
| var ( | ||
| deregisterLong = `Deregister your device from NVIDIA Brev | ||
|
|
||
| This command removes the local registration data and uninstalls | ||
| the Brev tunnel (network agent).` | ||
|
|
||
| deregisterExample = ` brev deregister` | ||
| ) | ||
|
|
||
| func NewCmdDeregister(t *terminal.Terminal, store DeregisterStore) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Annotations: map[string]string{"configuration": ""}, | ||
| Use: "deregister", | ||
| DisableFlagsInUseLine: true, | ||
| Short: "Deregister your device from Brev", | ||
| Long: deregisterLong, | ||
| Example: deregisterExample, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| brevHome, err := store.GetBrevHomePath() | ||
| if err != nil { | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
| return runDeregister(cmd.Context(), t, store, defaultDeregisterDeps(brevHome)) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runDeregister(ctx context.Context, t *terminal.Terminal, s DeregisterStore, deps deregisterDeps) error { //nolint:funlen // deregistration flow | ||
| if !deps.platform.IsCompatible() { | ||
| return fmt.Errorf("brev deregister is only supported on Linux") | ||
| } | ||
|
|
||
| registered, err := deps.registrationStore.Exists() | ||
| if err != nil { | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
| if !registered { | ||
| return fmt.Errorf("no registration found; this machine does not appear to be registered\nRun 'brev register' to register your device") | ||
| } | ||
|
|
||
| reg, err := deps.registrationStore.Load() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read registration file: %w", err) | ||
| } | ||
|
|
||
| t.Vprint("") | ||
| t.Vprint(t.Green("Deregistering device")) | ||
| t.Vprint("") | ||
| t.Vprintf(" Node ID: %s\n", reg.ExternalNodeID) | ||
| t.Vprintf(" Name: %s\n", reg.DisplayName) | ||
| t.Vprint("") | ||
|
|
||
| confirm := deps.prompter.Select( | ||
| "Proceed with deregistration?", | ||
| []string{"Yes, proceed", "No, cancel"}, | ||
| ) | ||
| if confirm != "Yes, proceed" { | ||
| t.Vprint("Deregistration canceled.") | ||
| return nil | ||
| } | ||
|
|
||
| t.Vprint("") | ||
| t.Vprint(t.Yellow("Removing node from Brev...")) | ||
| client := deps.nodeClients.NewNodeClient(s, config.GlobalConfig.GetBrevPublicAPIURL()) | ||
| if _, err := client.RemoveNode(ctx, connect.NewRequest(&nodev1.RemoveNodeRequest{ | ||
| ExternalNodeId: reg.ExternalNodeID, | ||
| })); err != nil { | ||
| return fmt.Errorf("failed to deregister node: %w", err) | ||
| } | ||
| t.Vprint(t.Green(" Node removed from Brev.")) | ||
| t.Vprint("") | ||
|
|
||
| // Remove Brev SSH keys from authorized_keys. | ||
| osUser, err := user.Current() | ||
| if err != nil { | ||
| t.Vprintf(" Warning: could not determine current user for SSH key cleanup: %v\n", err) | ||
| } else { | ||
| removed, kerr := deps.sshKeys.RemoveBrevKeys(osUser) | ||
| switch { | ||
| case kerr != nil: | ||
| t.Vprintf(" Warning: failed to remove Brev SSH keys: %v\n", kerr) | ||
| case len(removed) > 0: | ||
| t.Vprint(t.Green(" Brev SSH keys removed from authorized_keys:")) | ||
| for _, key := range removed { | ||
| t.Vprintf(" - %s\n", key) | ||
| } | ||
| default: | ||
| t.Vprint(" No Brev SSH keys found in authorized_keys.") | ||
| } | ||
| } | ||
| t.Vprint("") | ||
|
|
||
| t.Vprint("Removing Brev tunnel...") | ||
| if err := deps.netbird.Uninstall(); err != nil { | ||
| t.Vprintf(" Warning: failed to remove Brev tunnel: %v\n", err) | ||
| } else { | ||
| t.Vprint(t.Green(" Brev tunnel removed.")) | ||
| } | ||
| t.Vprint("") | ||
|
|
||
| t.Vprint("Removing registration data...") | ||
| if err := deps.registrationStore.Delete(); err != nil { | ||
| t.Vprintf(" Warning: failed to remove local registration file: %v\n", err) | ||
| t.Vprint(" You can manually remove it with: rm ~/.brev/device_registration.json") | ||
| } | ||
|
|
||
| t.Vprint(t.Green("Deregistration complete.")) | ||
| t.Vprint("") | ||
|
|
||
| return nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is nice