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

apmpackage: add more elasticsearch privileges #6139

Merged
merged 11 commits into from
Oct 12, 2021
9 changes: 9 additions & 0 deletions apmpackage/apm/agent/input/template.yml.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ apm-server:
{{/each}}
{{/if}}
write_timeout: {{write_timeout}}
{{#if tail_sampling_policies}}
sampling:
tail:
enabled: true
{{#if tail_sampling_interval}}
interval: {{tail_sampling_interval}}
{{/if}}
policies: {{tail_sampling_policies}}
{{/if}}
6 changes: 6 additions & 0 deletions apmpackage/apm/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
- description: remove warm phase from ILM policies
type: enhancement
link: https://github.com/elastic/apm-server/pull/6229
- description: added privileges to tail-sampled traces data stream
type: enhancement
link: https://github.com/elastic/apm-server/pull/6139
- description: added tail-sampling config vars
type: enhancement
link: https://github.com/elastic/apm-server/pull/6139
- version: "0.4.0"
changes:
- description: add anonymous auth config, replace some RUM config
Expand Down
5 changes: 5 additions & 0 deletions apmpackage/apm/data_stream/sampled_traces/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ type: traces
dataset: apm.sampled
ilm_policy: traces-apm.sampled-default_policy
elasticsearch:
privileges:
# We need additional privileges for the sampled traces data stream,
# for refreshing indices, querying index stats, and reading documents.
# This data stream holds only non-sensitive information.
indices: [auto_configure, create_doc, maintenance, monitor, read]
index_template:
settings:
# Create a single shard per index, so we can use
Expand Down
4 changes: 4 additions & 0 deletions apmpackage/apm/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ policy_templates:
- name: tls_curve_types
type: text
multi: true
- name: tail_sampling_policies
type: yaml
- name: tail_sampling_interval
type: text
template_path: template.yml.hbs
owner:
github: elastic/apm-server
4 changes: 3 additions & 1 deletion systemtest/agentconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
)

func TestAgentConfig(t *testing.T) {
systemtest.CleanupElasticsearch(t)

serviceName := "systemtest_service"
serviceEnvironment := "testing"
systemtest.DeleteAgentConfig(t, serviceName, "")
Expand All @@ -44,7 +46,7 @@ func TestAgentConfig(t *testing.T) {
require.NoError(t, err)

// Run apm-server under Fleet, exercising the Fleet agent config implementation.
apmIntegration := initAPMIntegration(t, map[string]interface{}{})
apmIntegration := newAPMIntegration(t, map[string]interface{}{})
serverURLs := []string{srv.URL, apmIntegration.URL}

expectChange := func(serverURL string, etag string) (map[string]string, *http.Response) {
Expand Down
48 changes: 45 additions & 3 deletions systemtest/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
Expand Down Expand Up @@ -301,9 +302,10 @@ func NewUnstartedElasticAgentContainer() (*ElasticAgentContainer, error) {
return nil, err
}
stackVersion := agentImageDetails.Config.Labels["org.label-schema.version"]
vcsRef := agentImageDetails.Config.Labels["org.label-schema.vcs-ref"]

// Build a custom elastic-agent image with a locally built apm-server binary injected.
agentImage, err = buildElasticAgentImage(context.Background(), docker, stackVersion, agentImageVersion)
agentImage, err = buildElasticAgentImage(context.Background(), docker, stackVersion, agentImageVersion, vcsRef)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -415,6 +417,45 @@ func (c *ElasticAgentContainer) Logs(ctx context.Context) (io.ReadCloser, error)
return c.container.Logs(ctx)
}

// Exec executes a command in the container, and returns its stdout and stderr.
func (c *ElasticAgentContainer) Exec(ctx context.Context, cmd ...string) (stdout, stderr []byte, _ error) {
docker, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, nil, err
}
defer docker.Close()

response, err := docker.ContainerExecCreate(ctx, c.container.GetContainerID(), types.ExecConfig{
AttachStderr: true,
AttachStdout: true,
Cmd: cmd,
})
if err != nil {
return nil, nil, err
}

// Consume all the exec output.
resp, err := docker.ContainerExecAttach(ctx, response.ID, types.ExecStartCheck{})
if err != nil {
return nil, nil, err
}
defer resp.Close()
var stdoutBuf, stderrBuf bytes.Buffer
if _, err := stdcopy.StdCopy(&stdoutBuf, &stderrBuf, resp.Reader); err != nil {
return nil, nil, err
}

// Return an error if the command exited non-zero.
execResp, err := docker.ContainerExecInspect(ctx, response.ID)
if err != nil {
return nil, nil, err
}
if execResp.ExitCode != 0 {
return nil, nil, fmt.Errorf("process exited with code %d", execResp.ExitCode)
}
return stdoutBuf.Bytes(), stderrBuf.Bytes(), nil
}

func pullDockerImage(ctx context.Context, docker *client.Client, imageRef string) error {
rc, err := docker.ImagePull(context.Background(), imageRef, types.ImagePullOptions{})
if err != nil {
Expand All @@ -438,7 +479,7 @@ func matchFleetServerAPIStatusHealthy(r io.Reader) bool {
}

// buildElasticAgentImage builds a Docker image from the published image with a locally built apm-server injected.
func buildElasticAgentImage(ctx context.Context, docker *client.Client, stackVersion, imageVersion string) (string, error) {
func buildElasticAgentImage(ctx context.Context, docker *client.Client, stackVersion, imageVersion, vcsRef string) (string, error) {
imageName := fmt.Sprintf("elastic-agent-systemtest:%s", imageVersion)
log.Printf("Building image %s...", imageName)

Expand All @@ -448,7 +489,8 @@ func buildElasticAgentImage(ctx context.Context, docker *client.Client, stackVer
if arch == "amd64" {
arch = "x86_64"
}
apmServerInstallDir := fmt.Sprintf("./state/data/install/apm-server-%s-linux-%s", stackVersion, arch)
vcsRefShort := vcsRef[:6]
apmServerInstallDir := fmt.Sprintf("./data/elastic-agent-%s/install/apm-server-%s-linux-%s", vcsRefShort, stackVersion, arch)
Comment on lines +492 to +493
Copy link
Member Author

Choose a reason for hiding this comment

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

there were some changes to elastic-agent which meant we haven't been running with the custom, injected apm-server binary.

apmServerBinary, err := apmservertest.BuildServerBinary("linux")
if err != nil {
return "", err
Expand Down
10 changes: 1 addition & 9 deletions systemtest/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,7 @@ func CleanupElasticsearch(t testing.TB) {
)

// Delete index templates after deleting data streams.
doParallel(
esapi.IndicesDeleteTemplateRequest{Name: legacyPrefix},
esapi.IndicesDeleteIndexTemplateRequest{Name: apmTracesPrefix},
esapi.IndicesDeleteIndexTemplateRequest{Name: apmMetricsPrefix},
esapi.IndicesDeleteIndexTemplateRequest{Name: apmLogsPrefix},
)

// Delete index templates after deleting data streams.
if err := doReq(esapi.IndicesDeleteIndexTemplateRequest{Name: legacyPrefix}); err != nil {
if err := doReq(esapi.IndicesDeleteTemplateRequest{Name: legacyPrefix}); err != nil {
t.Fatal(err)
}

Expand Down
Loading