|
| 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 | +package stack |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/elastic/elastic-package/internal/kibana" |
| 16 | + "github.com/elastic/elastic-package/internal/logger" |
| 17 | + "github.com/elastic/elastic-package/internal/profile" |
| 18 | + "github.com/elastic/elastic-package/internal/registry" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + managedAgentPolicyID = "elastic-agent-managed-ep" |
| 23 | + fleetLogstashOutput = "fleet-logstash-output" |
| 24 | + fleetElasticsearchOutput = "fleet-elasticsearch-output" |
| 25 | +) |
| 26 | + |
| 27 | +// createAgentPolicy creates an agent policy with the initial configuration used for |
| 28 | +// agents managed by elastic-package. |
| 29 | +func createAgentPolicy(ctx context.Context, kibanaClient *kibana.Client, stackVersion string, outputId string, selfMonitor bool) (*kibana.Policy, error) { |
| 30 | + policy := kibana.Policy{ |
| 31 | + ID: managedAgentPolicyID, |
| 32 | + Name: "Elastic-Agent (elastic-package)", |
| 33 | + Description: "Policy created by elastic-package", |
| 34 | + Namespace: "default", |
| 35 | + MonitoringEnabled: []string{}, |
| 36 | + DataOutputID: outputId, |
| 37 | + } |
| 38 | + if selfMonitor { |
| 39 | + policy.MonitoringEnabled = []string{"logs", "metrics"} |
| 40 | + } |
| 41 | + |
| 42 | + newPolicy, err := kibanaClient.CreatePolicy(ctx, policy) |
| 43 | + if errors.Is(err, kibana.ErrConflict) { |
| 44 | + newPolicy, err = kibanaClient.GetPolicy(ctx, policy.ID) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("error while getting existing policy: %w", err) |
| 47 | + } |
| 48 | + return newPolicy, nil |
| 49 | + } |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("error while creating agent policy: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + if selfMonitor { |
| 55 | + err := createSystemPackagePolicy(ctx, kibanaClient, stackVersion, newPolicy.ID, newPolicy.Namespace) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return newPolicy, nil |
| 62 | +} |
| 63 | + |
| 64 | +func createSystemPackagePolicy(ctx context.Context, kibanaClient *kibana.Client, stackVersion, agentPolicyID, namespace string) error { |
| 65 | + systemPackages, err := registry.Production.Revisions("system", registry.SearchOptions{ |
| 66 | + KibanaVersion: strings.TrimSuffix(stackVersion, kibana.SNAPSHOT_SUFFIX), |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("could not get the system package version for Kibana %v: %w", stackVersion, err) |
| 70 | + } |
| 71 | + if len(systemPackages) != 1 { |
| 72 | + return fmt.Errorf("unexpected number of system package versions for Kibana %s - found %d expected 1", stackVersion, len(systemPackages)) |
| 73 | + } |
| 74 | + logger.Debugf("Found %s package - version %s", systemPackages[0].Name, systemPackages[0].Version) |
| 75 | + packagePolicy := kibana.PackagePolicy{ |
| 76 | + Name: "system-1", |
| 77 | + PolicyID: agentPolicyID, |
| 78 | + Namespace: namespace, |
| 79 | + } |
| 80 | + packagePolicy.Package.Name = "system" |
| 81 | + packagePolicy.Package.Version = systemPackages[0].Version |
| 82 | + |
| 83 | + _, err = kibanaClient.CreatePackagePolicy(ctx, packagePolicy) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("error while creating package policy: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func deleteAgentPolicy(ctx context.Context, kibanaClient *kibana.Client) error { |
| 92 | + err := kibanaClient.DeletePolicy(ctx, managedAgentPolicyID) |
| 93 | + var notFoundError *kibana.ErrPolicyNotFound |
| 94 | + if err != nil && !errors.As(err, ¬FoundError) { |
| 95 | + return fmt.Errorf("failed to delete policy: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func forceUnenrollAgentsWithPolicy(ctx context.Context, kibanaClient *kibana.Client) error { |
| 102 | + agents, err := kibanaClient.QueryAgents(ctx, fmt.Sprintf("policy_id: %s", managedAgentPolicyID)) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("error while querying agents with policy %s: %w", managedAgentPolicyID, err) |
| 105 | + } |
| 106 | + |
| 107 | + for _, agent := range agents { |
| 108 | + err := kibanaClient.RemoveAgent(ctx, agent) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("failed to remove agent %s: %w", agent.ID, err) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func addFleetOutput(ctx context.Context, client *kibana.Client, outputType, host, id string) error { |
| 118 | + output := kibana.FleetOutput{ |
| 119 | + Name: id, |
| 120 | + ID: id, |
| 121 | + Type: outputType, |
| 122 | + Hosts: []string{host}, |
| 123 | + } |
| 124 | + |
| 125 | + err := client.AddFleetOutput(ctx, output) |
| 126 | + if errors.Is(err, kibana.ErrConflict) { |
| 127 | + // Output already exists. |
| 128 | + return nil |
| 129 | + } |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("failed to add %s fleet output of type %s: %w", id, outputType, err) |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func addLogstashFleetOutput(ctx context.Context, client *kibana.Client) error { |
| 138 | + return addFleetOutput(ctx, client, "logstash", "logstash:5044", fleetLogstashOutput) |
| 139 | +} |
| 140 | + |
| 141 | +func addElasticsearchFleetOutput(ctx context.Context, client *kibana.Client, host string) error { |
| 142 | + return addFleetOutput(ctx, client, "elasticsearch", host, fleetElasticsearchOutput) |
| 143 | +} |
| 144 | + |
| 145 | +func updateLogstashFleetOutput(ctx context.Context, profile *profile.Profile, kibanaClient *kibana.Client) error { |
| 146 | + certsDir := filepath.Join(profile.ProfilePath, "certs", "elastic-agent") |
| 147 | + |
| 148 | + caFile, err := os.ReadFile(filepath.Join(certsDir, "ca-cert.pem")) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("failed to read ca certificate: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + certFile, err := os.ReadFile(filepath.Join(certsDir, "cert.pem")) |
| 154 | + if err != nil { |
| 155 | + return fmt.Errorf("failed to read client certificate: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + keyFile, err := os.ReadFile(filepath.Join(certsDir, "key.pem")) |
| 159 | + if err != nil { |
| 160 | + return fmt.Errorf("failed to read client certificate private key: %w", err) |
| 161 | + } |
| 162 | + |
| 163 | + logstashFleetOutput := kibana.FleetOutput{ |
| 164 | + SSL: &kibana.AgentSSL{ |
| 165 | + CertificateAuthorities: []string{string(caFile)}, |
| 166 | + Certificate: string(certFile), |
| 167 | + Key: string(keyFile), |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | + if err := kibanaClient.UpdateFleetOutput(ctx, logstashFleetOutput, fleetLogstashOutput); err != nil { |
| 172 | + return fmt.Errorf("failed to update logstash fleet output: %w", err) |
| 173 | + } |
| 174 | + |
| 175 | + return nil |
| 176 | +} |
0 commit comments