Skip to content
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

Enroll proxy settings #26514

Merged
merged 49 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
d955f54
Introducde httpcommon package in libbeat
Apr 16, 2021
9a5cf78
Update heartbeat
Apr 16, 2021
a768d97
Update metricbeat
Apr 16, 2021
3902e66
Update cloudfoundry
Apr 16, 2021
6d44309
No more nil errors
Apr 16, 2021
dbb6088
update esleg
Apr 16, 2021
ba2cbe7
update kibana
Apr 16, 2021
48c4692
update metricbeat http helper
Apr 16, 2021
e87950d
Update httpjson
Apr 16, 2021
a1c1ebd
Update httpjson/v2
Apr 16, 2021
d5af1e3
Add json encoding to httpcommon
Apr 16, 2021
9eb720e
Update agent
Apr 16, 2021
511a656
Add proxy_headers setting
Apr 21, 2021
041e725
init proxy headers
Apr 21, 2021
d5cec18
Add missing license headers
Apr 22, 2021
b3d9855
fix CM
Apr 22, 2021
090e8ec
fix unit test
Apr 22, 2021
f7e5dfd
fix fileset test setup
Apr 24, 2021
feecc41
Merge branch 'master' into http_common
Apr 24, 2021
f4aeca2
fix heartbeat after cleanups
Apr 24, 2021
f445fe5
import order
Apr 24, 2021
1893c01
import order... thanks goimports :/
Apr 24, 2021
3ec53dd
udpate dev-tools
Apr 24, 2021
2344081
FB integration test
Apr 24, 2021
2cd6abc
Fix libbeat licenser integration test
Apr 24, 2021
1a9c368
fix typo
Apr 24, 2021
197a5f7
more updates
Apr 25, 2021
87e45aa
Load tls config more lazily... agent does not like preloading
Apr 26, 2021
7d194b7
Merge branch 'master' into http_common
Apr 28, 2021
796489b
fix metricbeat integration test after merge master
Apr 28, 2021
7af762d
Merge branch 'master' into http_common
Apr 29, 2021
85e367f
remove obsolete todo commen
Apr 29, 2021
c47a3df
remove unused test code
Apr 29, 2021
43138fa
Merge branch 'master' into http_common
Apr 29, 2021
5d2be4c
Merge branch 'master' into http_common
Jun 1, 2021
1bb2121
Merge branch 'master' into http_common
Jun 22, 2021
7675353
fix lint
Jun 22, 2021
1d4de2a
Update hearbeat docs
Jun 22, 2021
5417800
Merge branch 'master' into http_common
Jun 27, 2021
c62be8b
Add changelog entry
Jun 27, 2021
c8bae54
Add elastic agent changelog entries
Jun 27, 2021
55550a3
fix PR number
Jun 27, 2021
4d01864
Add proxy support to enrollment command
Jun 27, 2021
ee83fa0
add changelog entry
Jun 28, 2021
5648f77
Merge branch 'master' into enroll-proxy-settings
Jun 29, 2021
09c34e2
fix merge error
Jun 29, 2021
7f8d666
Disable proxy in agent enroll when fleet server is bootstrapped
Jun 29, 2021
2bc9dae
disable proxy in serialized fleet.yml config if fleet server is local
Jun 29, 2021
77c3981
update
Jun 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@
- Enable agent to send custom headers to kibana/ES {pull}26275[26275]
- Set `agent.id` to the Fleet Agent ID in events published from inputs backed by Beats. {issue}21121[21121] {pull}26394[26394]
- Add proxy support to artifact downloader and communication with fleet server. {pull}25219[25219]
- Add proxy support to enroll command. {pull}26514[26514]
- Enable configuring monitoring namespace {issue}26439[26439]
- Communicate with Fleet Server over HTTP2. {pull}26474[26474]
26 changes: 26 additions & 0 deletions x-pack/elastic-agent/pkg/agent/cmd/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func addEnrollFlags(cmd *cobra.Command) {
cmd.Flags().StringP("ca-sha256", "p", "", "Comma separated list of certificate authorities hash pins used for certificate verifications")
cmd.Flags().BoolP("insecure", "i", false, "Allow insecure connection to fleet-server")
cmd.Flags().StringP("staging", "", "", "Configures agent to download artifacts from a staging build")
cmd.Flags().StringP("proxy-url", "", "", "Configures the proxy url")
cmd.Flags().BoolP("proxy-disabled", "", false, "Disable proxy support including environment variables")
cmd.Flags().StringSliceP("proxy-header", "", []string{}, "Proxy headers used with CONNECT request")
}

func buildEnrollmentFlags(cmd *cobra.Command, url string, token string) []string {
Expand All @@ -89,6 +92,9 @@ func buildEnrollmentFlags(cmd *cobra.Command, url string, token string) []string
sha256, _ := cmd.Flags().GetString("ca-sha256")
insecure, _ := cmd.Flags().GetBool("insecure")
staging, _ := cmd.Flags().GetString("staging")
fProxyURL, _ := cmd.Flags().GetString("proxy-url")
fProxyDisabled, _ := cmd.Flags().GetBool("proxy-disabled")
fProxyHeaders, _ := cmd.Flags().GetStringSlice("proxy-header")

args := []string{}
if url != "" {
Expand Down Expand Up @@ -155,6 +161,20 @@ func buildEnrollmentFlags(cmd *cobra.Command, url string, token string) []string
args = append(args, "--staging")
args = append(args, staging)
}

if fProxyURL != "" {
args = append(args, "--proxy-url")
args = append(args, fProxyURL)
}
if fProxyDisabled {
args = append(args, "--proxy-disabled")
args = append(args, "true")
}
for k, v := range mapFromEnvList(fProxyHeaders) {
args = append(args, "--proxy-header")
args = append(args, k+"="+v)
}

return args
}

Expand Down Expand Up @@ -228,6 +248,9 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, args []string) error {
fCert, _ := cmd.Flags().GetString("fleet-server-cert")
fCertKey, _ := cmd.Flags().GetString("fleet-server-cert-key")
fInsecure, _ := cmd.Flags().GetBool("fleet-server-insecure-http")
fProxyURL, _ := cmd.Flags().GetString("proxy-url")
fProxyDisabled, _ := cmd.Flags().GetBool("proxy-disabled")
fProxyHeaders, _ := cmd.Flags().GetStringSlice("proxy-header")

caStr, _ := cmd.Flags().GetString("certificate-authorities")
CAs := cli.StringToSlice(caStr)
Expand Down Expand Up @@ -257,6 +280,9 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, args []string) error {
Insecure: fInsecure,
SpawnAgent: !fromInstall,
Headers: mapFromEnvList(fHeaders),
ProxyURL: fProxyURL,
ProxyDisabled: fProxyDisabled,
ProxyHeaders: mapFromEnvList(fProxyHeaders),
},
}

Expand Down
82 changes: 70 additions & 12 deletions x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import (
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"time"

"gopkg.in/yaml.v2"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/backoff"

"github.com/elastic/beats/v7/libbeat/common/transport/httpcommon"
"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/filelock"
Expand Down Expand Up @@ -80,6 +84,9 @@ type enrollCmdFleetServerOption struct {
Insecure bool
SpawnAgent bool
Headers map[string]string
ProxyURL string
ProxyDisabled bool
ProxyHeaders map[string]string
}

// enrollCmdOption define all the supported enrollment option.
Expand Down Expand Up @@ -116,6 +123,29 @@ func (e *enrollCmdOption) remoteConfig() (remote.Config, error) {
}

cfg.Transport.TLS = &tlsCfg

var proxyURL *url.URL
if e.FleetServer.ProxyURL != "" {
proxyURL, err = common.ParseURL(e.FleetServer.ProxyURL)
if err != nil {
return remote.Config{}, err
}
}

var headers http.Header
if len(e.FleetServer.ProxyHeaders) > 0 {
headers = http.Header{}
for k, v := range e.FleetServer.ProxyHeaders {
headers.Add(k, v)
}
}

cfg.Transport.Proxy = httpcommon.HTTPClientProxySettings{
URL: proxyURL,
Disable: e.FleetServer.ProxyDisabled,
Headers: headers,
}

return cfg, nil
}

Expand Down Expand Up @@ -166,15 +196,10 @@ func (c *enrollCmd) Execute(ctx context.Context) error {
return err
}

if c.options.FleetServer.ConnStr != "" {
token, err := c.fleetServerBootstrap(ctx)
if err != nil {
return err
}
if c.options.EnrollAPIKey == "" && token != "" {
c.options.EnrollAPIKey = token
}
}
// localFleetServer indicates that we start our internal fleet server. Agent
// will communicate to the internal fleet server on localhost only.
// Connection setup should disable proxies in that case.
localFleetServer := c.options.FleetServer.ConnStr != ""

c.remoteConfig, err = c.options.remoteConfig()
if err != nil {
Expand All @@ -184,6 +209,20 @@ func (c *enrollCmd) Execute(ctx context.Context) error {
errors.M(errors.MetaKeyURI, c.options.URL))
}

if localFleetServer {
// Ensure that the agent does not use a proxy configuration
// when connecting to the local fleet server.
c.remoteConfig.Transport.Proxy.Disable = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this persist after enrollment? This looks like it will work for enrollment, but how does the restarted Elastic Agent after enrollment ensure that it talks to its local Fleet Server not over the proxy? Does this Disable = true get persisted into the fleet.yml so that Elastic Agent knows not to use the proxy?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated PR. The config for agent is serialized in fleetServerBootstrap.


token, err := c.fleetServerBootstrap(ctx)
if err != nil {
return err
}
if c.options.EnrollAPIKey == "" && token != "" {
c.options.EnrollAPIKey = token
}
}

c.client, err = fleetclient.NewWithConfig(c.log, c.remoteConfig)
if err != nil {
return errors.New(
Expand Down Expand Up @@ -234,7 +273,11 @@ func (c *enrollCmd) fleetServerBootstrap(ctx context.Context) (string, error) {
c.options.FleetServer.PolicyID,
c.options.FleetServer.Host, c.options.FleetServer.Port,
c.options.FleetServer.Cert, c.options.FleetServer.CertKey, c.options.FleetServer.ElasticsearchCA,
c.options.FleetServer.Headers)
c.options.FleetServer.Headers,
c.options.FleetServer.ProxyURL,
c.options.FleetServer.ProxyDisabled,
c.options.FleetServer.ProxyHeaders,
)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -419,13 +462,15 @@ func (c *enrollCmd) enroll(ctx context.Context, persistentConfig map[string]inte
return err
}

if c.options.FleetServer.ConnStr != "" {
localFleetServer := c.options.FleetServer.ConnStr != ""
if localFleetServer {
serverConfig, err := createFleetServerBootstrapConfig(
c.options.FleetServer.ConnStr, c.options.FleetServer.ServiceToken,
c.options.FleetServer.PolicyID,
c.options.FleetServer.Host, c.options.FleetServer.Port,
c.options.FleetServer.Cert, c.options.FleetServer.CertKey, c.options.FleetServer.ElasticsearchCA,
c.options.FleetServer.Headers)
c.options.FleetServer.Headers,
c.options.FleetServer.ProxyURL, c.options.FleetServer.ProxyDisabled, c.options.FleetServer.ProxyHeaders)
if err != nil {
return err
}
Expand Down Expand Up @@ -725,7 +770,12 @@ func createFleetServerBootstrapConfig(
port uint16,
cert, key, esCA string,
headers map[string]string,
proxyURL string,
proxyDisabled bool,
proxyHeaders map[string]string,
) (*configuration.FleetAgentConfig, error) {
localFleetServer := connStr != ""

es, err := configuration.ElasticsearchFromConnStr(connStr, serviceToken)
if err != nil {
return nil, err
Expand All @@ -750,6 +800,10 @@ func createFleetServerBootstrapConfig(
es.Headers[k] = v
}
}
es.ProxyURL = proxyURL
es.ProxyDisabled = proxyDisabled
es.ProxyHeaders = proxyHeaders

cfg := configuration.DefaultFleetAgentConfig()
cfg.Enabled = true
cfg.Server = &configuration.FleetServerConfig{
Expand All @@ -772,6 +826,10 @@ func createFleetServerBootstrapConfig(
}
}

if localFleetServer {
cfg.Client.Transport.Proxy.Disable = true
}

if err := cfg.Valid(); err != nil {
return nil, errors.New(err, "invalid enrollment options", errors.TypeConfig)
}
Expand Down
19 changes: 11 additions & 8 deletions x-pack/elastic-agent/pkg/agent/configuration/fleet_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ type FleetServerOutputConfig struct {

// Elasticsearch is the configuration for elasticsearch.
type Elasticsearch struct {
Protocol string `config:"protocol" yaml:"protocol"`
Hosts []string `config:"hosts" yaml:"hosts"`
Path string `config:"path" yaml:"path,omitempty"`
Username string `config:"username" yaml:"username,omitempty"`
Password string `config:"password" yaml:"password,omitempty"`
ServiceToken string `config:"service_token" yaml:"service_token,omitempty"`
TLS *tlscommon.Config `config:"ssl" yaml:"ssl,omitempty"`
Headers map[string]string `config:"headers" yaml:"headers,omitempty"`
Protocol string `config:"protocol" yaml:"protocol"`
Hosts []string `config:"hosts" yaml:"hosts"`
Path string `config:"path" yaml:"path,omitempty"`
Username string `config:"username" yaml:"username,omitempty"`
Password string `config:"password" yaml:"password,omitempty"`
ServiceToken string `config:"service_token" yaml:"service_token,omitempty"`
TLS *tlscommon.Config `config:"ssl" yaml:"ssl,omitempty"`
Headers map[string]string `config:"headers" yaml:"headers,omitempty"`
ProxyURL string `config:"proxy_url" yaml:"proxy_url,omitempty"`
ProxyDisabled bool `config:"proxy_disabled" yaml:"proxy_disabled"`
ProxyHeaders map[string]string `config:"proxy_headers" yaml:"proxy_headers"`
}

// ElasticsearchFromConnStr returns an Elasticsearch configuration from the connection string.
Expand Down