-
Notifications
You must be signed in to change notification settings - Fork 5k
[Elastic-Agent] Use internal port for local fleet server #28993
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
Changes from 2 commits
f223462
cfcc3f0
f9a3e4b
fa9f592
3607c83
27400e8
778c564
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ package cmd | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net" | ||
| "os" | ||
| "os/signal" | ||
| "path/filepath" | ||
|
|
@@ -291,6 +292,8 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, args []string) error { | |
| fPolicy, _ := cmd.Flags().GetString("fleet-server-policy") | ||
| fHost, _ := cmd.Flags().GetString("fleet-server-host") | ||
| fPort, _ := cmd.Flags().GetUint16("fleet-server-port") | ||
| fInternalHost, _ := cmd.Flags().GetString("fleet-server-internal-host") | ||
| fInternalPort, _ := cmd.Flags().GetUint16("fleet-server-internal-port") | ||
| fCert, _ := cmd.Flags().GetString("fleet-server-cert") | ||
| fCertKey, _ := cmd.Flags().GetString("fleet-server-cert-key") | ||
| fInsecure, _ := cmd.Flags().GetBool("fleet-server-insecure-http") | ||
|
|
@@ -308,6 +311,24 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, args []string) error { | |
|
|
||
| ctx := handleSignal(context.Background()) | ||
|
|
||
| if localFleetServer := fServer != ""; localFleetServer { | ||
| if fInternalHost == "" { | ||
|
||
| fInternalHost = "localhost" | ||
| } | ||
|
|
||
| if fInternalPort <= 0 { | ||
| randomPort, err := getRandomPort() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fInternalPort = randomPort | ||
| } | ||
| } else { | ||
| // don't use without local fleet server | ||
| fInternalHost = "" | ||
| fInternalPort = 0 | ||
| } | ||
|
|
||
| options := enrollCmdOption{ | ||
| EnrollAPIKey: enrollmentToken, | ||
| URL: url, | ||
|
|
@@ -336,6 +357,8 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, args []string) error { | |
| SpawnAgent: !fromInstall, | ||
| Headers: mapFromEnvList(fHeaders), | ||
| Timeout: fTimeout, | ||
| InternalHost: fInternalHost, | ||
| InternalPort: fInternalPort, | ||
| }, | ||
| } | ||
|
|
||
|
|
@@ -384,3 +407,19 @@ func mapFromEnvList(envList []string) map[string]string { | |
| } | ||
| return m | ||
| } | ||
|
|
||
| func getRandomPort() (uint16, error) { | ||
|
||
| addr, err := net.ResolveTCPAddr("tcp", "localhost:0") | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| l, err := net.ListenTCP("tcp", addr) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| port := l.Addr().(*net.TCPAddr).Port | ||
| l.Close() | ||
|
|
||
| return uint16(port), nil | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,8 @@ rules: | |
| selectors: | ||
| - fleet.server.host | ||
| - fleet.server.port | ||
| - fleet.server.internal_host | ||
|
||
| - fleet.server.internal_port | ||
| - fleet.server.ssl | ||
| path: inputs.0.server | ||
|
|
||
|
|
||
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.
Could you move this to a separate function and then have some unit tests on it to validate the behaviour?
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.
I think that port should be configurable. What if the user is already using that port for any other process running?
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.
Further down a random port is selected that is free. So this conflict should never happen.
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 line is really hard to understand. Can you break it up somehow?
Uh oh!
There was an error while loading. Please reload this page.
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.
i made it configurable to make the option available for configuring port. if user wants to have it on some managed port. i can take it out if that's something not important atm.