|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +//go:build integration |
| 6 | + |
| 7 | +package integration |
| 8 | + |
| 9 | +import ( |
| 10 | + "bytes" |
| 11 | + "context" |
| 12 | + _ "embed" |
| 13 | + "encoding/json" |
| 14 | + "fmt" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | + "text/template" |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/google/uuid" |
| 21 | + |
| 22 | + "github.com/elastic/elastic-agent-libs/kibana" |
| 23 | + "github.com/elastic/elastic-agent/pkg/control/v2/client" |
| 24 | + "github.com/elastic/elastic-agent/pkg/testing/define" |
| 25 | +) |
| 26 | + |
| 27 | +//go:embed endpoint_security_package.json.tmpl |
| 28 | +var endpointPackagePolicyTemplate string |
| 29 | + |
| 30 | +type endpointPackageTemplateVars struct { |
| 31 | + ID string |
| 32 | + Name string |
| 33 | + PolicyID string |
| 34 | + Version string |
| 35 | +} |
| 36 | + |
| 37 | +// TODO: Setup a GitHub Action to update this for each release of https://github.com/elastic/endpoint-package |
| 38 | +const endpointPackageVersion = "8.11.0" |
| 39 | + |
| 40 | +func agentAndEndpointAreHealthy(t *testing.T, ctx context.Context, agentClient client.Client) bool { |
| 41 | + t.Helper() |
| 42 | + |
| 43 | + state, err := agentClient.State(ctx) |
| 44 | + if err != nil { |
| 45 | + t.Logf("Error getting agent state: %s", err) |
| 46 | + return false |
| 47 | + } |
| 48 | + |
| 49 | + if state.State != client.Healthy { |
| 50 | + t.Logf("local Agent is not Healthy: current state: %+v", state) |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + foundEndpointInputUnit := false |
| 55 | + foundEndpointOutputUnit := false |
| 56 | + for _, comp := range state.Components { |
| 57 | + isEndpointComponent := strings.Contains(comp.Name, "endpoint") |
| 58 | + if comp.State != client.Healthy { |
| 59 | + t.Logf("endpoint component is not Healthy: current state: %+v", comp) |
| 60 | + return false |
| 61 | + } |
| 62 | + |
| 63 | + for _, unit := range comp.Units { |
| 64 | + if isEndpointComponent { |
| 65 | + if unit.UnitType == client.UnitTypeInput { |
| 66 | + foundEndpointInputUnit = true |
| 67 | + } |
| 68 | + if unit.UnitType == client.UnitTypeOutput { |
| 69 | + foundEndpointOutputUnit = true |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if unit.State != client.Healthy { |
| 74 | + t.Logf("unit %q is not Healthy\n%+v", unit.UnitID, unit) |
| 75 | + return false |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Ensure both the endpoint input and output units were found and healthy. |
| 81 | + if !foundEndpointInputUnit || !foundEndpointOutputUnit { |
| 82 | + t.Logf("State did not contain endpoint units (input: %v/output: %v) state: %+v. ", foundEndpointInputUnit, foundEndpointOutputUnit, state) |
| 83 | + return false |
| 84 | + } |
| 85 | + |
| 86 | + return true |
| 87 | +} |
| 88 | + |
| 89 | +// Installs the Elastic Defend package to cause the agent to install the endpoint-security service. |
| 90 | +func installElasticDefendPackage(t *testing.T, info *define.Info, policyID string) (r kibana.PackagePolicyResponse, err error) { |
| 91 | + t.Helper() |
| 92 | + |
| 93 | + t.Log("Templating endpoint package policy request") |
| 94 | + tmpl, err := template.New("pkgpolicy").Parse(endpointPackagePolicyTemplate) |
| 95 | + if err != nil { |
| 96 | + return r, fmt.Errorf("error creating new template: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + packagePolicyID := uuid.New().String() |
| 100 | + var pkgPolicyBuf bytes.Buffer |
| 101 | + |
| 102 | + // Need unique name for Endpoint integration otherwise on multiple runs on the same instance you get |
| 103 | + // http error response with code 409: {StatusCode:409 Error:Conflict Message:An integration policy with the name Defend-cbomziz4uvn5fov9t1gsrcvdwn2p1s7tefnvgsye already exists. Please rename it or choose a different name.} |
| 104 | + err = tmpl.Execute(&pkgPolicyBuf, endpointPackageTemplateVars{ |
| 105 | + ID: packagePolicyID, |
| 106 | + Name: "Defend-" + packagePolicyID, |
| 107 | + PolicyID: policyID, |
| 108 | + Version: endpointPackageVersion, |
| 109 | + }) |
| 110 | + if err != nil { |
| 111 | + return r, fmt.Errorf("error executing template: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + // Make sure the templated value is actually valid JSON before making the API request. |
| 115 | + // Using json.Unmarshal will give us the actual syntax error, calling json.Valid() would not. |
| 116 | + packagePolicyReq := kibana.PackagePolicyRequest{} |
| 117 | + err = json.Unmarshal(pkgPolicyBuf.Bytes(), &packagePolicyReq) |
| 118 | + if err != nil { |
| 119 | + return r, fmt.Errorf("templated package policy is not valid JSON: %s, %w", pkgPolicyBuf.String(), err) |
| 120 | + } |
| 121 | + |
| 122 | + t.Log("POST /api/fleet/package_policies") |
| 123 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) |
| 124 | + defer cancel() |
| 125 | + |
| 126 | + pkgResp, err := info.KibanaClient.InstallFleetPackage(ctx, packagePolicyReq) |
| 127 | + if err != nil { |
| 128 | + t.Logf("Error installing fleet package: %v", err) |
| 129 | + return r, fmt.Errorf("error installing fleet package: %w", err) |
| 130 | + } |
| 131 | + t.Logf("Endpoint package Policy Response:\n%+v", pkgResp) |
| 132 | + return pkgResp, err |
| 133 | +} |
0 commit comments