Skip to content
Draft
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0f98577
Add stack provider for Elastic Cloud
jsoriano Apr 21, 2023
8315709
Fix version
jsoriano Apr 21, 2023
8d7b1d7
Merge remote-tracking branch 'origin/main' into stack-cloud-provider
jsoriano May 22, 2023
ff03886
Upload GeoIP databases
jsoriano May 22, 2023
4cede7e
Replace GeoIP database in cloud deployment
jsoriano May 23, 2023
8298102
Start local agent connected to cloud
jsoriano May 23, 2023
1931b77
Create Agent policy
jsoriano May 24, 2023
0938e7a
Fix path to env file
jsoriano May 24, 2023
3a1643f
Retrieve Fleet Server url from Fleet API
jsoriano May 24, 2023
7f1450a
Merge remote-tracking branch 'origin/main' into stack-cloud-provider
jsoriano May 24, 2023
f71f8cb
Use facts directly in templates where possible
jsoriano May 24, 2023
ee3a31b
Some refactors
jsoriano May 24, 2023
6255827
Reduce size of deployment
jsoriano May 25, 2023
9b22f2c
Parameterize deployment
jsoriano May 25, 2023
4565592
Update deployments on stack up
jsoriano May 25, 2023
4864a3d
Use diferent compose project names per project
jsoriano May 25, 2023
42b95a7
Install zipped package
jsoriano May 25, 2023
a41b053
Merge remote-tracking branch 'origin/main' into stack-cloud-provider
jsoriano May 26, 2023
ed9e89d
Remove TODO
jsoriano May 26, 2023
c61f7db
Merge remote-tracking branch 'origin/main' into stack-cloud-provider
jsoriano Jul 12, 2023
74b5d1e
Merge remote-tracking branch 'origin/main' into stack-cloud-provider
jsoriano Jul 25, 2023
7a3f2f2
Merge remote-tracking branch 'origin/main' into stack-cloud-provider
jsoriano Jul 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create Agent policy
  • Loading branch information
jsoriano committed May 24, 2023
commit 1931b772c57df800faa5e5af303921d0f2e0d713
103 changes: 92 additions & 11 deletions internal/stack/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
Expand Down Expand Up @@ -123,7 +124,7 @@ func (cp *cloudProvider) BootUp(options Options) error {
plan.DeploymentTemplate.ID = &templateID
}

logger.Debugf("Creating deployment %q", name)
logger.Infof("Creating deployment %q", name)
res, err := deploymentapi.Create(deploymentapi.CreateParams{
API: cp.api,
Request: payload,
Expand Down Expand Up @@ -210,7 +211,21 @@ func (cp *cloudProvider) BootUp(options Options) error {
return fmt.Errorf("failed to store config: %w", err)
}

logger.Debugf("Replacing GeoIP databases")
logger.Infof("Creating agent policy")

err = cp.createAgentPolicy(config)
if err != nil {
return fmt.Errorf("failed to create agent policy: %w", err)
}

logger.Infof("Starting local agent")

err = cp.startLocalAgent(options, config)
if err != nil {
return fmt.Errorf("failed to start local agent: %w", err)
}

logger.Infof("Replacing GeoIP databases")

client, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{config.ElasticsearchHost},
Expand Down Expand Up @@ -292,15 +307,6 @@ func (cp *cloudProvider) BootUp(options Options) error {
return fmt.Errorf("failed to track cluster creation: %w", err)
}

// FIXME: Create initial agent policy.

logger.Debugf("Starting local agent")

err = cp.startLocalAgent(options, config)
if err != nil {
return fmt.Errorf("failed to start local agent: %w", err)
}

return nil
}

Expand Down Expand Up @@ -375,6 +381,81 @@ func zipGeoIPBundle() (*bytes.Buffer, error) {
return &bundle, nil
}

const cloudKibanaAgentPolicy = `{
"name": "Elastic-Agent (elastic-package)",
"id": "elastic-agent-managed-ep",
"description": "Policy created by elastic-package",
"namespace": "default",
"monitoring_enabled": [
"logs",
"metrics"
]
}`

// TODO: Avoid hard-coding the package version here.
const cloudKibanaPackagePolicy = `{
"name": "system-1",
"policy_id": "elastic-agent-managed-ep",
"package": {
"name": "system",
"version": "1.26.0"
}
}`

func doKibanaRequest(config Config, req *http.Request) error {
req.SetBasicAuth(config.ElasticsearchUsername, config.ElasticsearchPassword)
req.Header.Add("content-type", "application/json")
req.Header.Add("kbn-xsrf", "elastic-package")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("performing request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusConflict {
// Already created, go on.
// TODO: We could try to update the policy.
return nil
}
if resp.StatusCode >= 300 {
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("request failed with status %v and could not read body: %w", resp.StatusCode, err)
}
return fmt.Errorf("request failed with status %v and response %v", resp.StatusCode, string(body))
}
return nil
}

func (cp *cloudProvider) createAgentPolicy(config Config) error {
agentPoliciesURL, err := url.JoinPath(config.KibanaHost, "/api/fleet/agent_policies")
if err != nil {
return fmt.Errorf("failed to build url for agent policies: %w", err)
}
req, err := http.NewRequest(http.MethodPost, agentPoliciesURL, strings.NewReader(cloudKibanaAgentPolicy))
if err != nil {
return fmt.Errorf("failed to initialize request to create agent policy: %w", err)
}
err = doKibanaRequest(config, req)
if err != nil {
return fmt.Errorf("error while creating agent policy: %w", err)
}

packagePoliciesURL, err := url.JoinPath(config.KibanaHost, "/api/fleet/package_policies")
if err != nil {
return fmt.Errorf("failed to build url for package policies: %w", err)
}
req, err = http.NewRequest(http.MethodPost, packagePoliciesURL, strings.NewReader(cloudKibanaPackagePolicy))
if err != nil {
return fmt.Errorf("failed to initialize request to create package policy: %w", err)
}
err = doKibanaRequest(config, req)
if err != nil {
return fmt.Errorf("error while creating package policy: %w", err)
}

return nil
}

func (cp *cloudProvider) localAgentComposeProject() (*compose.Project, error) {
composeFile := cp.profile.Path(profileStackPath, CloudComposeFile)
return compose.NewProject(DockerComposeProjectName, composeFile)
Expand Down