-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[client,management] Feature/client service expose #5411
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
36 commits
Select commit
Hold shift + click to select a range
799c039
add draft of implementation
mlsmaycon 57a6d2a
fix mocks
mlsmaycon 96023fc
Merge branch 'main' into feature/client-service-expose
mlsmaycon fbcb81e
replace stream protocol
mlsmaycon 96086d0
update reverseproxy service with source type changes and last renewed…
mlsmaycon b50909b
add permission validation, expiration handling, and peer context to r…
mlsmaycon c9515c7
add auth metadata
mlsmaycon 9ed8bb5
rename commands
mlsmaycon 47119ec
enhance reverseproxy service with prefix validation, mutex for concur…
mlsmaycon 692ee3f
add additional tests for reverseproxy and grpc modules, improve error…
mlsmaycon 72b201e
fix mock behavior
mlsmaycon 829fc8a
fix comments
mlsmaycon e5571aa
fix last seem timestamp
mlsmaycon 0d98f22
fix mock
mlsmaycon 492a716
use pointer type for ServiceMeta.CertificateIssuedAt to avoid MySQL z…
mlsmaycon 40eaf6d
enhance reverseproxy module with dynamic protocol mapping, improve er…
mlsmaycon d30e25c
refactor expose service flow: introduce manager to simplify lifecycle…
mlsmaycon 1175f1e
add pin validation to expose service and command, introduce comprehen…
mlsmaycon 01725f9
improve expose command flag descriptions for clarity and add usage me…
mlsmaycon 3c568d7
add validation for peer expose group settings
mlsmaycon 1714c35
add support for tracking account peer expose settings changes
mlsmaycon fb61d2d
refactor expose command: extract flag validation into a dedicated fun…
mlsmaycon 38feca6
refactor context handling in expose logic and remove redundant peer r…
mlsmaycon 9621c10
update expose command description
mlsmaycon 96e6dc2
introduce mgm client types
mlsmaycon e6ebc87
lock before return GetExposeManager
mlsmaycon dd93d98
use expose types and use int for pin and port
mlsmaycon 12d6626
use string for pin and adjust code with new types
mlsmaycon c3ad534
move keepalive to expose manager
mlsmaycon 0494f39
allow https exposure
mlsmaycon 59f4fab
use account settings instead
mlsmaycon 4206574
remove unused messages from proto
mlsmaycon b1488f4
Merge branch 'main' into feature/client-service-expose
mlsmaycon 3d99476
refactor reverseproxy manager to use sendServiceUpdate function
mlsmaycon b0b4d52
use settings mock manager in reverseproxy manager tests
mlsmaycon c5c46bf
use status errors instead of fmt errors in reverseproxy manager
mlsmaycon 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 |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/signal" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/netbirdio/netbird/client/proto" | ||
| "github.com/netbirdio/netbird/util" | ||
| ) | ||
|
|
||
| var pinRegexp = regexp.MustCompile(`^\d{6}$`) | ||
|
|
||
| var ( | ||
| exposePin string | ||
| exposePassword string | ||
| exposeUserGroups []string | ||
| exposeDomain string | ||
| exposeNamePrefix string | ||
| exposeProtocol string | ||
| ) | ||
|
|
||
| var exposeCmd = &cobra.Command{ | ||
| Use: "expose <port>", | ||
| Short: "Expose a local port via the NetBird reverse proxy", | ||
| Args: cobra.ExactArgs(1), | ||
| Example: "netbird expose --with-password safe-pass 8080", | ||
| RunE: exposeFn, | ||
| } | ||
|
|
||
| func init() { | ||
| exposeCmd.Flags().StringVar(&exposePin, "with-pin", "", "Protect the exposed service with a 6-digit PIN (e.g. --with-pin 123456)") | ||
| exposeCmd.Flags().StringVar(&exposePassword, "with-password", "", "Protect the exposed service with a password (e.g. --with-password my-secret)") | ||
| exposeCmd.Flags().StringSliceVar(&exposeUserGroups, "with-user-groups", nil, "Restrict access to specific user groups with SSO (e.g. --with-user-groups devops,Backend)") | ||
| exposeCmd.Flags().StringVar(&exposeDomain, "with-custom-domain", "", "Custom domain for the exposed service, must be configured to your account (e.g. --with-custom-domain myapp.example.com)") | ||
| exposeCmd.Flags().StringVar(&exposeNamePrefix, "with-name-prefix", "", "Prefix for the generated service name (e.g. --with-name-prefix my-app)") | ||
| exposeCmd.Flags().StringVar(&exposeProtocol, "protocol", "http", "Protocol to use, http/https is supported (e.g. --protocol http)") | ||
| } | ||
|
|
||
| func validateExposeFlags(cmd *cobra.Command, portStr string) (uint64, error) { | ||
| port, err := strconv.ParseUint(portStr, 10, 32) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("invalid port number: %s", portStr) | ||
| } | ||
| if port == 0 || port > 65535 { | ||
| return 0, fmt.Errorf("invalid port number: must be between 1 and 65535") | ||
| } | ||
|
|
||
| if !isProtocolValid(exposeProtocol) { | ||
| return 0, fmt.Errorf("unsupported protocol %q: only 'http' or 'https' are supported", exposeProtocol) | ||
| } | ||
|
|
||
| if exposePin != "" && !pinRegexp.MatchString(exposePin) { | ||
| return 0, fmt.Errorf("invalid pin: must be exactly 6 digits") | ||
| } | ||
|
|
||
| if cmd.Flags().Changed("with-password") && exposePassword == "" { | ||
| return 0, fmt.Errorf("password cannot be empty") | ||
| } | ||
|
|
||
| if cmd.Flags().Changed("with-user-groups") && len(exposeUserGroups) == 0 { | ||
| return 0, fmt.Errorf("user groups cannot be empty") | ||
| } | ||
|
|
||
| return port, nil | ||
| } | ||
|
|
||
| func isProtocolValid(exposeProtocol string) bool { | ||
| return strings.ToLower(exposeProtocol) == "http" || strings.ToLower(exposeProtocol) == "https" | ||
| } | ||
|
|
||
| func exposeFn(cmd *cobra.Command, args []string) error { | ||
| SetFlagsFromEnvVars(rootCmd) | ||
|
|
||
| if err := util.InitLog(logLevel, util.LogConsole); err != nil { | ||
| log.Errorf("failed initializing log %v", err) | ||
| return err | ||
| } | ||
|
|
||
| cmd.Root().SilenceUsage = false | ||
|
|
||
| port, err := validateExposeFlags(cmd, args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cmd.Root().SilenceUsage = true | ||
|
|
||
| ctx, cancel := context.WithCancel(cmd.Context()) | ||
| defer cancel() | ||
|
|
||
| sigCh := make(chan os.Signal, 1) | ||
| signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) | ||
| go func() { | ||
| <-sigCh | ||
| cancel() | ||
| }() | ||
|
|
||
| conn, err := DialClientGRPCServer(ctx, daemonAddr) | ||
| if err != nil { | ||
| return fmt.Errorf("connect to daemon: %w", err) | ||
| } | ||
| defer func() { | ||
| if err := conn.Close(); err != nil { | ||
| log.Debugf("failed to close daemon connection: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| client := proto.NewDaemonServiceClient(conn) | ||
|
|
||
| protocol, err := toExposeProtocol(exposeProtocol) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| stream, err := client.ExposeService(ctx, &proto.ExposeServiceRequest{ | ||
| Port: uint32(port), | ||
| Protocol: protocol, | ||
| Pin: exposePin, | ||
| Password: exposePassword, | ||
| UserGroups: exposeUserGroups, | ||
| Domain: exposeDomain, | ||
| NamePrefix: exposeNamePrefix, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("expose service: %w", err) | ||
| } | ||
|
|
||
| if err := handleExposeReady(cmd, stream, port); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return waitForExposeEvents(cmd, ctx, stream) | ||
| } | ||
|
|
||
| func toExposeProtocol(exposeProtocol string) (proto.ExposeProtocol, error) { | ||
| switch strings.ToLower(exposeProtocol) { | ||
| case "http": | ||
| return proto.ExposeProtocol_EXPOSE_HTTP, nil | ||
| case "https": | ||
| return proto.ExposeProtocol_EXPOSE_HTTPS, nil | ||
| default: | ||
| return 0, fmt.Errorf("unsupported protocol %q: only 'http' or 'https' are supported", exposeProtocol) | ||
| } | ||
| } | ||
|
|
||
| func handleExposeReady(cmd *cobra.Command, stream proto.DaemonService_ExposeServiceClient, port uint64) error { | ||
| event, err := stream.Recv() | ||
| if err != nil { | ||
| return fmt.Errorf("receive expose event: %w", err) | ||
| } | ||
|
|
||
| switch e := event.Event.(type) { | ||
| case *proto.ExposeServiceEvent_Ready: | ||
| cmd.Println("Service exposed successfully!") | ||
| cmd.Printf(" Name: %s\n", e.Ready.ServiceName) | ||
| cmd.Printf(" URL: %s\n", e.Ready.ServiceUrl) | ||
| cmd.Printf(" Domain: %s\n", e.Ready.Domain) | ||
| cmd.Printf(" Protocol: %s\n", exposeProtocol) | ||
| cmd.Printf(" Port: %d\n", port) | ||
| cmd.Println() | ||
| cmd.Println("Press Ctrl+C to stop exposing.") | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("unexpected expose event: %T", event.Event) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func waitForExposeEvents(cmd *cobra.Command, ctx context.Context, stream proto.DaemonService_ExposeServiceClient) error { | ||
| for { | ||
| _, err := stream.Recv() | ||
| if err != nil { | ||
| if ctx.Err() != nil { | ||
| cmd.Println("\nService stopped.") | ||
| //nolint:nilerr | ||
| return nil | ||
| } | ||
| if errors.Is(err, io.EOF) { | ||
| return fmt.Errorf("connection to daemon closed unexpectedly") | ||
| } | ||
| return fmt.Errorf("stream error: %w", err) | ||
| } | ||
| } | ||
| } | ||
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,95 @@ | ||
| package expose | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| mgm "github.com/netbirdio/netbird/shared/management/client" | ||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| const renewTimeout = 10 * time.Second | ||
|
|
||
| // Response holds the response from exposing a service. | ||
| type Response struct { | ||
| ServiceName string | ||
| ServiceURL string | ||
| Domain string | ||
| } | ||
|
|
||
| type Request struct { | ||
| NamePrefix string | ||
| Domain string | ||
| Port uint16 | ||
| Protocol int | ||
| Pin string | ||
| Password string | ||
| UserGroups []string | ||
| } | ||
|
|
||
| type ManagementClient interface { | ||
| CreateExpose(ctx context.Context, req mgm.ExposeRequest) (*mgm.ExposeResponse, error) | ||
| RenewExpose(ctx context.Context, domain string) error | ||
| StopExpose(ctx context.Context, domain string) error | ||
| } | ||
|
|
||
| // Manager handles expose session lifecycle via the management client. | ||
| type Manager struct { | ||
|
mlsmaycon marked this conversation as resolved.
|
||
| mgmClient ManagementClient | ||
| ctx context.Context | ||
| } | ||
|
|
||
| // NewManager creates a new expose Manager using the given management client. | ||
| func NewManager(ctx context.Context, mgmClient ManagementClient) *Manager { | ||
| return &Manager{mgmClient: mgmClient, ctx: ctx} | ||
| } | ||
|
|
||
| // Expose creates a new expose session via the management server. | ||
| func (m *Manager) Expose(ctx context.Context, req Request) (*Response, error) { | ||
| log.Infof("exposing service on port %d", req.Port) | ||
| resp, err := m.mgmClient.CreateExpose(ctx, toClientExposeRequest(req)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| log.Infof("expose session created for %s", resp.Domain) | ||
|
|
||
| return fromClientExposeResponse(resp), nil | ||
| } | ||
|
|
||
| func (m *Manager) KeepAlive(ctx context.Context, domain string) error { | ||
| ticker := time.NewTicker(10 * time.Second) | ||
| defer ticker.Stop() | ||
| defer m.stop(domain) | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| log.Infof("context canceled, stopping keep alive for %s", domain) | ||
|
|
||
| return nil | ||
| case <-ticker.C: | ||
| if err := m.renew(ctx, domain); err != nil { | ||
| log.Errorf("renewing expose session for %s: %v", domain, err) | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // renew extends the TTL of an active expose session. | ||
| func (m *Manager) renew(ctx context.Context, domain string) error { | ||
| renewCtx, cancel := context.WithTimeout(ctx, renewTimeout) | ||
| defer cancel() | ||
| return m.mgmClient.RenewExpose(renewCtx, domain) | ||
| } | ||
|
|
||
| // stop terminates an active expose session. | ||
| func (m *Manager) stop(domain string) { | ||
| stopCtx, cancel := context.WithTimeout(m.ctx, renewTimeout) | ||
| defer cancel() | ||
| err := m.mgmClient.StopExpose(stopCtx, domain) | ||
| if err != nil { | ||
| log.Warnf("Failed stopping expose session for %s: %v", domain, err) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.