-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
container.go
953 lines (866 loc) · 30.9 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package cmd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/elastic/beats/v7/libbeat/common/transport/httpcommon"
"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
"github.com/elastic/beats/v7/libbeat/kibana"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact/install/tar"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/cli"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/process"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release"
)
const (
requestRetrySleepEnv = "KIBANA_REQUEST_RETRY_SLEEP"
maxRequestRetriesEnv = "KIBANA_REQUEST_RETRY_COUNT"
defaultRequestRetrySleep = "1s" // sleep 1 sec between retries for HTTP requests
defaultMaxRequestRetries = "30" // maximum number of retries for HTTP requests
defaultStateDirectory = "/usr/share/elastic-agent/state" // directory that will hold the state data
)
var (
// Used to strip the appended ({uuid}) from the name of an enrollment token. This makes much easier for
// a container to reference a token by name, without having to know what the generated UUID is for that name.
tokenNameStrip = regexp.MustCompile(`\s\([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)$`)
)
func newContainerCommand(_ []string, streams *cli.IOStreams) *cobra.Command {
cmd := cobra.Command{
Hidden: true, // not exposed over help; used by container entrypoint only
Use: "container",
Short: "Bootstrap Elastic Agent to run inside of a container",
Long: `This should only be used as an entrypoint for a container. This will prepare the Elastic Agent using
environment variables to run inside of the container.
The following actions are possible and grouped based on the actions.
* Elastic Agent Fleet Enrollment
This enrolls the Elastic Agent into a Fleet Server. It is also possible to have this create a new enrollment token
for this specific Elastic Agent.
FLEET_ENROLL - set to 1 for enrollment into fleet-server. If not set, Elastic Agent is run in standalone mode.
FLEET_URL - URL of the Fleet Server to enroll into
FLEET_ENROLLMENT_TOKEN - token to use for enrollment. This is not needed in case FLEET_SERVER_ENABLED and FLEET_ENROLL is set. Then the token is fetched from Kibana.
FLEET_CA - path to certificate authority to use with communicate with Fleet Server [$KIBANA_CA]
FLEET_INSECURE - communicate with Fleet with either insecure HTTP or unverified HTTPS
The following vars are need in the scenario that Elastic Agent should automatically fetch its own token.
KIBANA_FLEET_HOST - kibana host to enable create enrollment token on [$KIBANA_HOST]
FLEET_TOKEN_NAME - token name to use for fetching token from Kibana. This requires Kibana configs to be set.
FLEET_TOKEN_POLICY_NAME - token policy name to use for fetching token from Kibana. This requires Kibana configs to be set.
* Bootstrapping Fleet Server
This bootstraps the Fleet Server to be run by this Elastic Agent. At least one Fleet Server is required in a Fleet
deployment for other Elastic Agent to bootstrap. In case the Elastic Agent is run without fleet-server. These variables
are not needed.
If FLEET_SERVER_ENABLE and FLEET_ENROLL is set but no FLEET_ENROLLMENT_TOKEN, the token is automatically fetched from Kibana.
FLEET_SERVER_ENABLE - set to 1 enables bootstrapping of Fleet Server inside Elastic Agent (forces FLEET_ENROLL enabled)
FLEET_SERVER_ELASTICSEARCH_HOST - elasticsearch host for Fleet Server to communicate with [$ELASTICSEARCH_HOST]
FLEET_SERVER_ELASTICSEARCH_CA - path to certificate authority to use with communicate with elasticsearch [$ELASTICSEARCH_CA]
FLEET_SERVER_ELASTICSEARCH_CA_TRUSTED_FINGERPRINT - The sha-256 fingerprint value of the certificate authority to trust
FLEET_SERVER_ELASTICSEARCH_INSECURE - disables cert validation for communication with Elasticsearch
FLEET_SERVER_SERVICE_TOKEN - service token to use for communication with elasticsearch
FLEET_SERVER_POLICY_ID - policy ID for Fleet Server to use for itself ("Default Fleet Server policy" used when undefined)
FLEET_SERVER_HOST - binding host for Fleet Server HTTP (overrides the policy). By default this is 0.0.0.0.
FLEET_SERVER_PORT - binding port for Fleet Server HTTP (overrides the policy)
FLEET_SERVER_CERT - path to certificate to use for HTTPS endpoint
FLEET_SERVER_CERT_KEY - path to private key for certificate to use for HTTPS endpoint
FLEET_SERVER_INSECURE_HTTP - expose Fleet Server over HTTP (not recommended; insecure)
* Preparing Kibana for Fleet
This prepares the Fleet plugin that exists inside of Kibana. This must either be enabled here or done externally
before Fleet Server will actually successfully start. All the Kibana variables are not needed in case Elastic Agent
should not setup Fleet. To manually trigger KIBANA_FLEET_SETUP navigate to Kibana -> Fleet -> Agents and enabled it.
KIBANA_FLEET_SETUP - set to 1 enables the setup of Fleet in Kibana by Elastic Agent. This was previously FLEET_SETUP.
KIBANA_FLEET_HOST - Kibana host accessible from fleet-server. [$KIBANA_HOST]
KIBANA_FLEET_USERNAME - kibana username to service token [$KIBANA_USERNAME]
KIBANA_FLEET_PASSWORD - kibana password to service token [$KIBANA_PASSWORD]
KIBANA_FLEET_CA - path to certificate authority to use with communicate with Kibana [$KIBANA_CA]
KIBANA_REQUEST_RETRY_SLEEP - specifies sleep duration taken when agent performs a request to kibana [default 1s]
KIBANA_REQUEST_RETRY_COUNT - specifies number of retries agent performs when executing a request to kibana [default 30]
The following environment variables are provided as a convenience to prevent a large number of environment variable to
be used when the same credentials will be used across all the possible actions above.
ELASTICSEARCH_HOST - elasticsearch host [http://elasticsearch:9200]
ELASTICSEARCH_USERNAME - elasticsearch username [elastic]
ELASTICSEARCH_PASSWORD - elasticsearch password [changeme]
ELASTICSEARCH_CA - path to certificate authority to use with communicate with elasticsearch
KIBANA_HOST - kibana host [http://kibana:5601]
KIBANA_FLEET_USERNAME - kibana username to enable Fleet [$ELASTICSEARCH_USERNAME]
KIBANA_FLEET_PASSWORD - kibana password to enable Fleet [$ELASTICSEARCH_PASSWORD]
KIBANA_CA - path to certificate authority to use with communicate with Kibana [$ELASTICSEARCH_CA]
By default when this command starts it will check for an existing fleet.yml. If that file already exists then
all the above actions will be skipped, because the Elastic Agent has already been enrolled. To ensure that enrollment
occurs on every start of the container set FLEET_FORCE to 1.
`,
Run: func(c *cobra.Command, args []string) {
if err := logContainerCmd(streams, c); err != nil {
logError(streams, err)
os.Exit(1)
}
},
}
return &cmd
}
func logError(streams *cli.IOStreams, err error) {
fmt.Fprintf(streams.Err, "Error: %v\n%s\n", err, troubleshootMessage())
}
func logInfo(streams *cli.IOStreams, a ...interface{}) {
fmt.Fprintln(streams.Out, a...)
}
func logContainerCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
logsPath := envWithDefault("", "LOGS_PATH")
if logsPath != "" {
// log this entire command to a file as well as to the passed streams
if err := os.MkdirAll(logsPath, 0755); err != nil {
return fmt.Errorf("preparing LOGS_PATH(%s) failed: %s", logsPath, err)
}
logPath := filepath.Join(logsPath, "elastic-agent-startup.log")
w, err := os.Create(logPath)
if err != nil {
return fmt.Errorf("opening startup log(%s) failed: %s", logPath, err)
}
defer w.Close()
streams.Out = io.MultiWriter(streams.Out, w)
streams.Err = io.MultiWriter(streams.Out, w)
}
return containerCmd(streams, cmd)
}
func containerCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
// set paths early so all action below use the defined paths
if err := setPaths("", "", "", true); err != nil {
return err
}
elasticCloud := envBool("ELASTIC_AGENT_CLOUD")
// if not in cloud mode, always run the agent
runAgent := !elasticCloud
// create access configuration from ENV and config files
cfg, err := defaultAccessConfig()
if err != nil {
return err
}
for _, f := range []string{"fleet-setup.yml", "credentials.yml"} {
c, err := config.LoadFile(filepath.Join(paths.Config(), f))
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("parsing config file(%s): %s", f, err)
}
if c != nil {
err = c.Unpack(&cfg)
if err != nil {
return fmt.Errorf("unpacking config file(%s): %s", f, err)
}
// if in elastic cloud mode, only run the agent when configured
runAgent = true
}
}
// start apm-server legacy process when in cloud mode
var wg sync.WaitGroup
var apmProc *process.Info
apmPath := os.Getenv("APM_SERVER_PATH")
if elasticCloud {
logInfo(streams, "Starting in elastic cloud mode")
if elasticCloud && apmPath != "" {
// run legacy APM Server as a daemon; send termination signal
// to the main process if the daemon is stopped
mainProc, err := os.FindProcess(os.Getpid())
if err != nil {
return errors.New(err, "finding current process")
}
if apmProc, err = runLegacyAPMServer(streams, apmPath); err != nil {
return errors.New(err, "starting legacy apm-server")
}
wg.Add(1) // apm-server legacy process
logInfo(streams, "Legacy apm-server daemon started.")
go func() {
if err := func() error {
apmProcState, err := apmProc.Process.Wait()
if err != nil {
return err
}
if apmProcState.ExitCode() != 0 {
return fmt.Errorf("apm-server process exited with %d", apmProcState.ExitCode())
}
return nil
}(); err != nil {
logError(streams, err)
}
wg.Done()
// sending kill signal to current process (elastic-agent)
logInfo(streams, "Initiate shutdown elastic-agent.")
mainProc.Signal(syscall.SIGTERM)
}()
defer func() {
if apmProc != nil {
apmProc.Stop()
logInfo(streams, "Initiate shutdown legacy apm-server.")
}
}()
}
}
if runAgent {
// run the main elastic-agent container command
err = runContainerCmd(streams, cmd, cfg)
}
// wait until APM Server shut down
wg.Wait()
return err
}
func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig) error {
var err error
var client *kibana.Client
executable, err := os.Executable()
if err != nil {
return err
}
_, err = os.Stat(paths.AgentConfigFile())
if !os.IsNotExist(err) && !cfg.Fleet.Force {
// already enrolled, just run the standard run
return run(streams, logToStderr)
}
if cfg.Kibana.Fleet.Setup || cfg.FleetServer.Enable {
err = ensureServiceToken(streams, &cfg)
if err != nil {
return err
}
}
if cfg.Kibana.Fleet.Setup {
client, err = kibanaClient(cfg.Kibana, cfg.Kibana.Headers)
if err != nil {
return err
}
logInfo(streams, "Performing setup of Fleet in Kibana\n")
err = kibanaSetup(cfg, client, streams)
if err != nil {
return err
}
}
if cfg.Fleet.Enroll {
var policy *kibanaPolicy
token := cfg.Fleet.EnrollmentToken
if token == "" && !cfg.FleetServer.Enable {
if client == nil {
client, err = kibanaClient(cfg.Kibana, cfg.Kibana.Headers)
if err != nil {
return err
}
}
policy, err = kibanaFetchPolicy(cfg, client, streams)
if err != nil {
return err
}
token, err = kibanaFetchToken(cfg, client, policy, streams, cfg.Fleet.TokenName)
if err != nil {
return err
}
}
policyID := cfg.FleetServer.PolicyID
if policy != nil {
policyID = policy.ID
}
if policyID != "" {
logInfo(streams, "Policy selected for enrollment: ", policyID)
}
cmdArgs, err := buildEnrollArgs(cfg, token, policyID)
if err != nil {
return err
}
enroll := exec.Command(executable, cmdArgs...)
enroll.Stdout = streams.Out
enroll.Stderr = streams.Err
err = enroll.Start()
if err != nil {
return errors.New("failed to execute enrollment command", err)
}
err = enroll.Wait()
if err != nil {
return errors.New("enrollment failed", err)
}
}
return run(streams, logToStderr)
}
// TokenResp is used to decode a response for generating a service token
type TokenResp struct {
Name string `json:"name"`
Value string `json:"value"`
}
// ensureServiceToken will ensure that the cfg specified has the service_token attributes filled.
//
// If no token is specified it will use the elasticsearch username/password to request a new token from Kibana
func ensureServiceToken(streams *cli.IOStreams, cfg *setupConfig) error {
// There's already a service token
if cfg.Kibana.Fleet.ServiceToken != "" || cfg.FleetServer.Elasticsearch.ServiceToken != "" {
return nil
}
if cfg.Kibana.Fleet.Username == "" || cfg.Kibana.Fleet.Password == "" {
return fmt.Errorf("username/password must be provided to retrieve service token")
}
logInfo(streams, "Requesting service_token from Kibana.")
// Client is not passed in to this function because this function will use username/password and then
// all the following clients will use the created service token.
client, err := kibanaClient(cfg.Kibana, cfg.Kibana.Headers)
if err != nil {
return err
}
code, r, err := client.Connection.Request("POST", "/api/fleet/service-tokens", nil, nil, nil)
if err != nil {
return fmt.Errorf("request to get security token from Kibana failed: %w", err)
}
if code >= 400 {
return fmt.Errorf("request to get security token from Kibana failed with status %d, body: %s", code, string(r))
}
t := TokenResp{}
err = json.Unmarshal(r, &t)
if err != nil {
return fmt.Errorf("unable to decode response: %w", err)
}
logInfo(streams, "Created service_token named:", t.Name)
cfg.Kibana.Fleet.ServiceToken = t.Value
cfg.FleetServer.Elasticsearch.ServiceToken = t.Value
return nil
}
func buildEnrollArgs(cfg setupConfig, token string, policyID string) ([]string, error) {
args := []string{
"enroll", "-f",
"-c", paths.ConfigFile(),
"--path.home", paths.Top(), // --path.home actually maps to paths.Top()
"--path.config", paths.Config(),
"--path.logs", paths.Logs(),
}
if paths.Downloads() != "" {
args = append(args, "--path.downloads", paths.Downloads())
}
if paths.Install() != "" {
args = append(args, "--path.install", paths.Install())
}
if !paths.IsVersionHome() {
args = append(args, "--path.home.unversioned")
}
if cfg.FleetServer.Enable {
connStr, err := buildFleetServerConnStr(cfg.FleetServer)
if err != nil {
return nil, err
}
args = append(args, "--fleet-server-es", connStr)
if cfg.FleetServer.Elasticsearch.ServiceToken != "" {
args = append(args, "--fleet-server-service-token", cfg.FleetServer.Elasticsearch.ServiceToken)
}
if policyID != "" {
args = append(args, "--fleet-server-policy", policyID)
}
if cfg.FleetServer.Elasticsearch.CA != "" {
args = append(args, "--fleet-server-es-ca", cfg.FleetServer.Elasticsearch.CA)
}
if cfg.FleetServer.Elasticsearch.CATrustedFingerprint != "" {
args = append(args, "--fleet-server-es-ca-trusted-fingerprint", cfg.FleetServer.Elasticsearch.CATrustedFingerprint)
}
if cfg.FleetServer.Host != "" {
args = append(args, "--fleet-server-host", cfg.FleetServer.Host)
}
if cfg.FleetServer.Port != "" {
args = append(args, "--fleet-server-port", cfg.FleetServer.Port)
}
if cfg.FleetServer.Cert != "" {
args = append(args, "--fleet-server-cert", cfg.FleetServer.Cert)
}
if cfg.FleetServer.CertKey != "" {
args = append(args, "--fleet-server-cert-key", cfg.FleetServer.CertKey)
}
for k, v := range cfg.FleetServer.Headers {
args = append(args, "--header", k+"="+v)
}
if cfg.Fleet.URL != "" {
args = append(args, "--url", cfg.Fleet.URL)
}
if cfg.FleetServer.InsecureHTTP {
args = append(args, "--fleet-server-insecure-http")
}
if cfg.FleetServer.InsecureHTTP || cfg.Fleet.Insecure {
args = append(args, "--insecure")
}
if cfg.FleetServer.Elasticsearch.Insecure {
args = append(args, "--fleet-server-es-insecure")
}
if cfg.FleetServer.Timeout != 0 {
args = append(args, "--fleet-server-timeout")
args = append(args, cfg.FleetServer.Timeout.String())
}
} else {
if cfg.Fleet.URL == "" {
return nil, errors.New("FLEET_URL is required when FLEET_ENROLL is true without FLEET_SERVER_ENABLE")
}
args = append(args, "--url", cfg.Fleet.URL)
if cfg.Fleet.Insecure {
args = append(args, "--insecure")
}
}
if cfg.Fleet.CA != "" {
args = append(args, "--certificate-authorities", cfg.Fleet.CA)
}
if token != "" {
args = append(args, "--enrollment-token", token)
}
if cfg.Fleet.DaemonTimeout != 0 {
args = append(args, "--daemon-timeout")
args = append(args, cfg.Fleet.DaemonTimeout.String())
}
return args, nil
}
func buildFleetServerConnStr(cfg fleetServerConfig) (string, error) {
u, err := url.Parse(cfg.Elasticsearch.Host)
if err != nil {
return "", err
}
path := ""
if u.Path != "" {
path += "/" + strings.TrimLeft(u.Path, "/")
}
return fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, path), nil
}
func kibanaSetup(cfg setupConfig, client *kibana.Client, streams *cli.IOStreams) error {
err := performPOST(cfg, client, "/api/fleet/setup", streams.Err, "Kibana Fleet setup")
if err != nil {
return err
}
err = performPOST(cfg, client, "/api/fleet/agents/setup", streams.Err, "Kibana Fleet Agents setup")
if err != nil {
return err
}
return nil
}
func kibanaFetchPolicy(cfg setupConfig, client *kibana.Client, streams *cli.IOStreams) (*kibanaPolicy, error) {
var policies kibanaPolicies
err := performGET(cfg, client, "/api/fleet/agent_policies", &policies, streams.Err, "Kibana fetch policy")
if err != nil {
return nil, err
}
return findPolicy(cfg, policies.Items)
}
func kibanaFetchToken(cfg setupConfig, client *kibana.Client, policy *kibanaPolicy, streams *cli.IOStreams, tokenName string) (string, error) {
var keys kibanaAPIKeys
err := performGET(cfg, client, "/api/fleet/enrollment-api-keys", &keys, streams.Err, "Kibana fetch token")
if err != nil {
return "", err
}
key, err := findKey(keys.List, policy, tokenName)
if err != nil {
return "", err
}
var keyDetail kibanaAPIKeyDetail
err = performGET(cfg, client, fmt.Sprintf("/api/fleet/enrollment-api-keys/%s", key.ID), &keyDetail, streams.Err, "Kibana fetch token detail")
if err != nil {
return "", err
}
return keyDetail.Item.APIKey, nil
}
func kibanaClient(cfg kibanaConfig, headers map[string]string) (*kibana.Client, error) {
var tls *tlscommon.Config
if cfg.Fleet.CA != "" {
tls = &tlscommon.Config{
CAs: []string{cfg.Fleet.CA},
}
}
transport := httpcommon.DefaultHTTPTransportSettings()
transport.TLS = tls
return kibana.NewClientWithConfigDefault(&kibana.ClientConfig{
Host: cfg.Fleet.Host,
Username: cfg.Fleet.Username,
Password: cfg.Fleet.Password,
ServiceToken: cfg.Fleet.ServiceToken,
IgnoreVersion: true,
Transport: transport,
Headers: headers,
}, 0, "Elastic-Agent")
}
func findPolicy(cfg setupConfig, policies []kibanaPolicy) (*kibanaPolicy, error) {
policyID := ""
policyName := cfg.Fleet.TokenPolicyName
if cfg.FleetServer.Enable {
policyID = cfg.FleetServer.PolicyID
}
for _, policy := range policies {
if policyID != "" {
if policyID == policy.ID {
return &policy, nil
}
} else if policyName != "" {
if policyName == policy.Name {
return &policy, nil
}
} else if cfg.FleetServer.Enable {
if policy.IsDefaultFleetServer {
return &policy, nil
}
} else {
if policy.IsDefault {
return &policy, nil
}
}
}
return nil, fmt.Errorf(`unable to find policy named "%s"`, policyName)
}
func findKey(keys []kibanaAPIKey, policy *kibanaPolicy, tokenName string) (*kibanaAPIKey, error) {
for _, key := range keys {
name := strings.TrimSpace(tokenNameStrip.ReplaceAllString(key.Name, ""))
if name == tokenName && key.PolicyID == policy.ID {
return &key, nil
}
}
return nil, fmt.Errorf(`unable to find enrollment token named "%s" in policy "%s"`, tokenName, policy.Name)
}
func envWithDefault(def string, keys ...string) string {
for _, key := range keys {
val, ok := os.LookupEnv(key)
if ok {
return val
}
}
return def
}
func envBool(keys ...string) bool {
for _, key := range keys {
val, ok := os.LookupEnv(key)
if ok && isTrue(val) {
return true
}
}
return false
}
func envTimeout(keys ...string) time.Duration {
for _, key := range keys {
val, ok := os.LookupEnv(key)
if ok {
dur, err := time.ParseDuration(val)
if err == nil {
return dur
}
}
}
return 0
}
func envMap(key string) map[string]string {
m := make(map[string]string)
prefix := key + "="
for _, env := range os.Environ() {
if !strings.HasPrefix(env, prefix) {
continue
}
envVal := strings.TrimPrefix(env, prefix)
keyValue := strings.SplitN(envVal, "=", 2)
if len(keyValue) != 2 {
continue
}
m[keyValue[0]] = keyValue[1]
}
return m
}
func isTrue(val string) bool {
trueVals := []string{"1", "true", "yes", "y"}
val = strings.ToLower(val)
for _, v := range trueVals {
if val == v {
return true
}
}
return false
}
func performGET(cfg setupConfig, client *kibana.Client, path string, response interface{}, writer io.Writer, msg string) error {
var lastErr error
for i := 0; i < cfg.Kibana.RetryMaxCount; i++ {
code, result, err := client.Connection.Request("GET", path, nil, nil, nil)
if err != nil || code != 200 {
err = fmt.Errorf("http GET request to %s%s fails: %v. Response: %s",
client.Connection.URL, path, err, truncateString(result))
fmt.Fprintf(writer, "%s failed: %s\n", msg, err)
<-time.After(cfg.Kibana.RetrySleepDuration)
continue
}
if response == nil {
return nil
}
return json.Unmarshal(result, response)
}
return lastErr
}
func performPOST(cfg setupConfig, client *kibana.Client, path string, writer io.Writer, msg string) error {
var lastErr error
for i := 0; i < cfg.Kibana.RetryMaxCount; i++ {
code, result, err := client.Connection.Request("POST", path, nil, nil, nil)
if err != nil || code >= 400 {
err = fmt.Errorf("http POST request to %s%s fails: %v. Response: %s",
client.Connection.URL, path, err, truncateString(result))
lastErr = err
fmt.Fprintf(writer, "%s failed: %s\n", msg, err)
<-time.After(cfg.Kibana.RetrySleepDuration)
continue
}
return nil
}
return lastErr
}
func truncateString(b []byte) string {
const maxLength = 250
runes := bytes.Runes(b)
if len(runes) > maxLength {
runes = append(runes[:maxLength], []rune("... (truncated)")...)
}
return strings.Replace(string(runes), "\n", " ", -1)
}
// runLegacyAPMServer extracts the bundled apm-server from elastic-agent
// to path and runs it with args.
func runLegacyAPMServer(streams *cli.IOStreams, path string) (*process.Info, error) {
name := "apm-server"
logInfo(streams, "Preparing apm-server for legacy mode.")
cfg := artifact.DefaultConfig()
logInfo(streams, fmt.Sprintf("Extracting apm-server into install directory %s.", path))
installer, err := tar.NewInstaller(cfg)
if err != nil {
return nil, errors.New(err, "creating installer")
}
spec := program.Spec{Name: name, Cmd: name, Artifact: name}
version := release.Version()
if release.Snapshot() {
version = fmt.Sprintf("%s-SNAPSHOT", version)
}
// Extract the bundled apm-server into the APM_SERVER_PATH
if err := installer.Install(context.Background(), spec, version, path); err != nil {
return nil, errors.New(err,
fmt.Sprintf("installing %s (%s) from %s to %s", spec.Name, version, cfg.TargetDirectory, path))
}
// Get the apm-server directory
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, errors.New(err, fmt.Sprintf("reading directory %s", path))
}
if len(files) != 1 || !files[0].IsDir() {
return nil, errors.New("expected one directory")
}
apmDir := filepath.Join(path, files[0].Name())
// Start apm-server process respecting path ENVs
apmBinary := filepath.Join(apmDir, spec.Cmd)
log, err := logger.New("apm-server", false)
if err != nil {
return nil, err
}
// add APM Server specific configuration
var args []string
addEnv := func(arg, env string) {
if v := os.Getenv(env); v != "" {
args = append(args, arg, v)
}
}
addSettingEnv := func(arg, env string) {
if v := os.Getenv(env); v != "" {
args = append(args, "-E", fmt.Sprintf("%v=%v", arg, v))
}
}
addEnv("--path.home", "HOME_PATH")
addEnv("--path.config", "CONFIG_PATH")
addEnv("--path.data", "DATA_PATH")
addEnv("--path.logs", "LOGS_PATH")
addEnv("--httpprof", "HTTPPROF")
addSettingEnv("gc_percent", "APMSERVER_GOGC")
logInfo(streams, "Starting legacy apm-server daemon as a subprocess.")
return process.Start(log, apmBinary, nil, os.Geteuid(), os.Getegid(), args)
}
func logToStderr(cfg *configuration.Configuration) {
logsPath := envWithDefault("", "LOGS_PATH")
if logsPath == "" {
// when no LOGS_PATH defined the container should log to stderr
cfg.Settings.LoggingConfig.ToStderr = true
cfg.Settings.LoggingConfig.ToFiles = false
}
}
func setPaths(statePath, configPath, logsPath string, writePaths bool) error {
statePath = envWithDefault(statePath, "STATE_PATH")
if statePath == "" {
statePath = defaultStateDirectory
}
topPath := filepath.Join(statePath, "data")
configPath = envWithDefault(configPath, "CONFIG_PATH")
if configPath == "" {
configPath = statePath
}
// ensure that the directory and sub-directory data exists
if err := os.MkdirAll(topPath, 0755); err != nil {
return fmt.Errorf("preparing STATE_PATH(%s) failed: %s", statePath, err)
}
// ensure that the elastic-agent.yml exists in the state directory or if given in the config directory
baseConfig := filepath.Join(configPath, paths.DefaultConfigName)
if _, err := os.Stat(baseConfig); os.IsNotExist(err) {
if err := copyFile(baseConfig, paths.ConfigFile(), 0); err != nil {
return err
}
}
// sync the downloads to the data directory
destDownloads := filepath.Join(statePath, "data", "downloads")
if err := syncDir(paths.Downloads(), destDownloads); err != nil {
return fmt.Errorf("syncing download directory to STATE_PATH(%s) failed: %s", statePath, err)
}
originalInstall := paths.Install()
originalTop := paths.Top()
paths.SetTop(topPath)
paths.SetConfig(configPath)
// when custom top path is provided the home directory is not versioned
paths.SetVersionHome(false)
// install path stays on container default mount (otherwise a bind mounted directory could have noexec set)
paths.SetInstall(originalInstall)
// set LOGS_PATH is given
logsPath = envWithDefault(logsPath, "LOGS_PATH")
if logsPath != "" {
paths.SetLogs(logsPath)
// ensure that the logs directory exists
if err := os.MkdirAll(filepath.Join(logsPath), 0755); err != nil {
return fmt.Errorf("preparing LOGS_PATH(%s) failed: %s", logsPath, err)
}
}
// persist the paths so other commands in the container will use the correct paths
if writePaths {
if err := writeContainerPaths(originalTop, statePath, configPath, logsPath); err != nil {
return err
}
}
return nil
}
type containerPaths struct {
StatePath string `config:"state_path" yaml:"state_path"`
ConfigPath string `config:"state_path" yaml:"config_path,omitempty"`
LogsPath string `config:"state_path" yaml:"logs_path,omitempty"`
}
func writeContainerPaths(original, statePath, configPath, logsPath string) error {
pathFile := filepath.Join(original, "container-paths.yml")
fp, err := os.Create(pathFile)
if err != nil {
return fmt.Errorf("failed creating %s: %s", pathFile, err)
}
b, err := yaml.Marshal(containerPaths{
StatePath: statePath,
ConfigPath: configPath,
LogsPath: logsPath,
})
if err != nil {
return fmt.Errorf("failed to marshal for %s: %s", pathFile, err)
}
_, err = fp.Write(b)
if err != nil {
return fmt.Errorf("failed to write %s: %s", pathFile, err)
}
return nil
}
func tryContainerLoadPaths() error {
pathFile := filepath.Join(paths.Top(), "container-paths.yml")
_, err := os.Stat(pathFile)
if os.IsNotExist(err) {
// no container-paths.yml file exists, so nothing to do
return nil
}
cfg, err := config.LoadFile(pathFile)
if err != nil {
return fmt.Errorf("failed to load %s: %s", pathFile, err)
}
var paths containerPaths
err = cfg.Unpack(&paths)
if err != nil {
return fmt.Errorf("failed to unpack %s: %s", pathFile, err)
}
return setPaths(paths.StatePath, paths.ConfigPath, paths.LogsPath, false)
}
func syncDir(src string, dest string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relativePath := strings.TrimPrefix(path, src)
if info.IsDir() {
err = os.MkdirAll(filepath.Join(dest, relativePath), info.Mode())
if err != nil {
return err
}
return nil
}
return copyFile(filepath.Join(dest, relativePath), path, info.Mode())
})
}
func copyFile(destPath string, srcPath string, mode os.FileMode) error {
// if mode is unset; set to the same as the source file
if mode == 0 {
info, err := os.Stat(srcPath)
if err == nil {
// ignoring error because; os.Open will also error if the file cannot be stat'd
mode = info.Mode()
}
}
src, err := os.Open(srcPath)
if err != nil {
return err
}
defer src.Close()
dest, err := os.OpenFile(destPath, os.O_CREATE|os.O_WRONLY, mode)
if err != nil {
return err
}
defer dest.Close()
_, err = io.Copy(dest, src)
return err
}
type kibanaPolicy struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
IsDefault bool `json:"is_default"`
IsDefaultFleetServer bool `json:"is_default_fleet_server"`
}
type kibanaPolicies struct {
Items []kibanaPolicy `json:"items"`
}
type kibanaAPIKey struct {
ID string `json:"id"`
Name string `json:"name"`
Active bool `json:"active"`
PolicyID string `json:"policy_id"`
APIKey string `json:"api_key"`
}
type kibanaAPIKeys struct {
List []kibanaAPIKey `json:"list"`
}
type kibanaAPIKeyDetail struct {
Item kibanaAPIKey `json:"item"`
}
func envDurationWithDefault(defVal string, keys ...string) (time.Duration, error) {
valStr := defVal
for _, key := range keys {
val, ok := os.LookupEnv(key)
if ok {
valStr = val
break
}
}
return time.ParseDuration(valStr)
}
func envIntWithDefault(defVal string, keys ...string) (int, error) {
valStr := defVal
for _, key := range keys {
val, ok := os.LookupEnv(key)
if ok {
valStr = val
break
}
}
return strconv.Atoi(valStr)
}