-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[Elastic-Agent] Use internal port for local fleet server #28993
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 == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the use case where we would not use localhost? Should we even support something else? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fleet-server and its own Elastic Agent are always gonna be on the same host aren't they? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It must be on the same host. Elastic Agent starts fleet-server as a process. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then it will always be a localhost communication. Gotcha. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should always be on localhost, however making it overridable could be useful for testing. |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works on all os's? Seems risky to me. Assuming that the OS will allow a rebind in a short period of time and/or won't decide to be aggressive and reuse port elsewhere. Can we just default to 8221 or something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was thinking also about reversal flow, and if we agree that having this configurable is ok. |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, thanks |
||
- 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?
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.