-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config option to deploy custom elastic-agents as test services (#786
) * Add config option to deploy custom elastic-agents as test services * Tidy go.mod * Simplify runner changes * Clean go.mod * Move logic to custom_agent deployer * Use DockerComposeDeployedService code * Connect to network before healthcheck * Add test, docs, and initialize the same as a composer deployer * Change Makefile * Format test package * Add test to CI * Bump stack versions * Ignore errors in test pipeline * Use apache as custom-agent test and revert stack version changes * Fail if docker-compose.yml is not found * Change custom agent deployer to use a base agent config * Replace apache custom agent example with auditd_manager * Wrap compose service deployer * Format * Overwrite setup and teardown to avoid changing compose deployer * Use service variant to avoid overriding teardown entirely * Update docs * Revert Makefile unrelated change * Use installed resources instead of a temp file * add version to custom-agent.yml * quote version field
- Loading branch information
Showing
23 changed files
with
2,441 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,15 @@ | ||
version: "2.3" | ||
services: | ||
docker-custom-agent: | ||
image: "${ELASTIC_AGENT_IMAGE_REF}" | ||
healthcheck: | ||
test: "elastic-agent status" | ||
retries: 180 | ||
interval: 1s | ||
hostname: docker-custom-agent | ||
environment: | ||
- FLEET_ENROLL=1 | ||
- FLEET_INSECURE=1 | ||
- FLEET_URL=http://fleet-server:8220 | ||
volumes: | ||
- ${SERVICE_LOGS_DIR}:/tmp/service_logs/ |
This file contains 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 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
151 changes: 151 additions & 0 deletions
151
internal/testrunner/runners/system/servicedeployer/custom_agent.go
This file contains 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,151 @@ | ||
// 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 servicedeployer | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/elastic-package/internal/compose" | ||
"github.com/elastic/elastic-package/internal/configuration/locations" | ||
"github.com/elastic/elastic-package/internal/docker" | ||
"github.com/elastic/elastic-package/internal/files" | ||
"github.com/elastic/elastic-package/internal/install" | ||
"github.com/elastic/elastic-package/internal/kibana" | ||
"github.com/elastic/elastic-package/internal/logger" | ||
"github.com/elastic/elastic-package/internal/stack" | ||
) | ||
|
||
const dockerCustomAgentName = "docker-custom-agent" | ||
|
||
// CustomAgentDeployer knows how to deploy a custom elastic-agent defined via | ||
// a Docker Compose file. | ||
type CustomAgentDeployer struct { | ||
cfg string | ||
} | ||
|
||
// NewCustomAgentDeployer returns a new instance of a deployedCustomAgent. | ||
func NewCustomAgentDeployer(cfgPath string) (*CustomAgentDeployer, error) { | ||
return &CustomAgentDeployer{ | ||
cfg: cfgPath, | ||
}, nil | ||
} | ||
|
||
// SetUp sets up the service and returns any relevant information. | ||
func (d *CustomAgentDeployer) SetUp(inCtxt ServiceContext) (DeployedService, error) { | ||
logger.Debug("setting up service using Docker Compose service deployer") | ||
|
||
appConfig, err := install.Configuration() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "can't read application configuration") | ||
} | ||
|
||
kibanaClient, err := kibana.NewClient() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "can't create Kibana client") | ||
} | ||
|
||
stackVersion, err := kibanaClient.Version() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "can't read Kibana injected metadata") | ||
} | ||
|
||
env := append( | ||
appConfig.StackImageRefs(stackVersion).AsEnv(), | ||
fmt.Sprintf("%s=%s", serviceLogsDirEnv, inCtxt.Logs.Folder.Local), | ||
) | ||
|
||
ymlPaths, err := d.loadComposeDefinitions() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
service := dockerComposeDeployedService{ | ||
ymlPaths: ymlPaths, | ||
project: "elastic-package-service", | ||
sv: ServiceVariant{ | ||
Name: dockerCustomAgentName, | ||
Env: env, | ||
}, | ||
} | ||
|
||
outCtxt := inCtxt | ||
|
||
p, err := compose.NewProject(service.project, service.ymlPaths...) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not create Docker Compose project for service") | ||
} | ||
|
||
// Verify the Elastic stack network | ||
err = stack.EnsureStackNetworkUp() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Elastic stack network is not ready") | ||
} | ||
|
||
// Clean service logs | ||
err = files.RemoveContent(outCtxt.Logs.Folder.Local) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "removing service logs failed") | ||
} | ||
|
||
inCtxt.Name = dockerCustomAgentName | ||
serviceName := inCtxt.Name | ||
opts := compose.CommandOptions{ | ||
Env: env, | ||
ExtraArgs: []string{"--build", "-d"}, | ||
} | ||
err = p.Up(opts) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not boot up service using Docker Compose") | ||
} | ||
|
||
// Connect service network with stack network (for the purpose of metrics collection) | ||
err = docker.ConnectToNetwork(p.ContainerName(serviceName), stack.Network()) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "can't attach service container to the stack network") | ||
} | ||
|
||
err = p.WaitForHealthy(opts) | ||
if err != nil { | ||
processServiceContainerLogs(p, compose.CommandOptions{ | ||
Env: opts.Env, | ||
}, outCtxt.Name) | ||
return nil, errors.Wrap(err, "service is unhealthy") | ||
} | ||
|
||
// Build service container name | ||
outCtxt.Hostname = p.ContainerName(serviceName) | ||
|
||
logger.Debugf("adding service container %s internal ports to context", p.ContainerName(serviceName)) | ||
serviceComposeConfig, err := p.Config(compose.CommandOptions{Env: env}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not get Docker Compose configuration for service") | ||
} | ||
|
||
s := serviceComposeConfig.Services[serviceName] | ||
outCtxt.Ports = make([]int, len(s.Ports)) | ||
for idx, port := range s.Ports { | ||
outCtxt.Ports[idx] = port.InternalPort | ||
} | ||
|
||
// Shortcut to first port for convenience | ||
if len(outCtxt.Ports) > 0 { | ||
outCtxt.Port = outCtxt.Ports[0] | ||
} | ||
|
||
outCtxt.Agent.Host.NamePrefix = inCtxt.Name | ||
service.ctxt = outCtxt | ||
return &service, nil | ||
} | ||
|
||
func (d *CustomAgentDeployer) loadComposeDefinitions() ([]string, error) { | ||
locationManager, err := locations.NewLocationManager() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "can't locate Docker Compose file for Custom Agent deployer") | ||
} | ||
return []string{locationManager.DockerCustomAgentDeployerYml(), d.cfg}, nil | ||
} |
This file contains 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
3 changes: 3 additions & 0 deletions
3
test/packages/with-custom-agent/auditd_manager/_dev/build/build.yml
This file contains 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,3 @@ | ||
dependencies: | ||
ecs: | ||
reference: [email protected] |
This file contains 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,6 @@ | ||
# newer versions go on top | ||
- version: "999.999.999" | ||
changes: | ||
- description: Initial draft of the package | ||
type: enhancement | ||
link: https://github.com/elastic/integrations/pull/1 |
8 changes: 8 additions & 0 deletions
8
...es/with-custom-agent/auditd_manager/data_stream/auditd/_dev/deploy/agent/custom-agent.yml
This file contains 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,8 @@ | ||
version: "2.3" | ||
services: | ||
docker-custom-agent: | ||
pid: host | ||
cap_add: | ||
- AUDIT_CONTROL | ||
- AUDIT_READ | ||
user: root |
5 changes: 5 additions & 0 deletions
5
...h-custom-agent/auditd_manager/data_stream/auditd/_dev/test/system/test-default-config.yml
This file contains 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,5 @@ | ||
data_stream: | ||
vars: | ||
audit_rules: | ||
- "-a always,exit -F arch=b64 -S execve,execveat -k exec" | ||
preserve_original_event: true |
Oops, something went wrong.