Skip to content

Commit

Permalink
Merge branch 'main' into health-status-agent-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
olegsu committed Dec 18, 2022
2 parents 4fa50cf + ec517ec commit 2ba924e
Show file tree
Hide file tree
Showing 23 changed files with 364 additions and 62 deletions.
1 change: 1 addition & 0 deletions bin/.aws-iam-authenticator-0.5.12.pkg
1 change: 1 addition & 0 deletions bin/aws-iam-authenticator
20 changes: 20 additions & 0 deletions config/benchmark.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// Config is put into a different package to prevent cyclic imports in case
// it is needed in several locations

package config

// https://github.com/elastic/integrations/tree/main/packages/cloud_security_posture/data_stream/findings/agent/stream
Expand Down
21 changes: 5 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package config
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"time"
Expand All @@ -48,15 +47,14 @@ type Fetcher struct {
}

type Config struct {
Type string `config:"type"`
AWSConfig aws.ConfigAWS `config:",inline"`
RuntimeCfg *RuntimeConfig `config:"runtime_cfg"`
Fetchers []*config.C `config:"fetchers"`
KubeConfig string `config:"kube_config"`
Period time.Duration `config:"period"`
Processors processors.PluginConfig `config:"processors"`
BundlePath string `config:"bundle_path"`
Benchmark *string `config:"config.v1.benchmark"`
Benchmark string `config:"config.v1.benchmark"`
}

type RuntimeConfig struct {
Expand All @@ -79,23 +77,18 @@ func New(cfg *config.C) (*Config, error) {
return nil, err
}

if c.Benchmark != nil {
if !isSupportedBenchmark(*c.Benchmark) {
if c.Benchmark != "" {
if !isSupportedBenchmark(c.Benchmark) {
return c, ErrBenchmarkNotSupported
}
c.Type = buildConfigType(*c.Benchmark)
} else {
if c.RuntimeCfg != nil && c.RuntimeCfg.ActivatedRules != nil && len(c.RuntimeCfg.ActivatedRules.CisEks) > 0 {
c.Type = buildConfigType(CIS_EKS)
}
}
return c, nil
}

func defaultConfig() (*Config, error) {
ret := &Config{
Period: 4 * time.Hour,
Type: buildConfigType(CIS_K8S),
Period: 4 * time.Hour,
Benchmark: CIS_K8S,
}

bundle, err := getBundlePath()
Expand Down Expand Up @@ -136,7 +129,3 @@ func isSupportedBenchmark(benchmark string) bool {
}
return false
}

func buildConfigType(benchmark string) string {
return fmt.Sprintf("cloudbeat/%s", benchmark)
}
17 changes: 10 additions & 7 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fetchers:
directory: b
`,
&Benchmarks{CisK8s: []string{"a", "b", "c", "d", "e"}},
"cloudbeat/cis_k8s",
"cis_k8s",
aws.ConfigAWS{},
2,
},
Expand Down Expand Up @@ -101,7 +101,7 @@ fetchers:
directory: c
`,
&Benchmarks{CisEks: []string{"a", "b", "c", "d", "e"}},
"cloudbeat/cis_eks",
"cis_eks",
aws.ConfigAWS{
AccessKeyID: "key",
SecretAccessKey: "secret",
Expand All @@ -122,7 +122,7 @@ fetchers:
c, err := New(cfg)
s.NoError(err)

s.Equal(test.expectedType, c.Type)
s.Equal(test.expectedType, c.Benchmark)
s.EqualValues(test.expectedActivatedRules, c.RuntimeCfg.ActivatedRules)
s.Equal(test.expectedAWSConfig, c.AWSConfig)
s.Equal(test.expectedFetchers, len(c.Fetchers))
Expand Down Expand Up @@ -207,7 +207,7 @@ config:
return
}
s.NoError(err)
s.Equal(test.expected, *c.Benchmark)
s.Equal(test.expected, c.Benchmark)
})
}
}
Expand Down Expand Up @@ -298,7 +298,7 @@ runtime_cfg:
`,
[]string{"a", "b"},
nil,
"cloudbeat/cis_k8s",
"cis_k8s",
},
{
`
Expand All @@ -307,10 +307,13 @@ runtime_cfg:
cis_eks:
- a
- b
config:
v1:
benchmark: cis_eks
`,
nil,
[]string{"a", "b"},
"cloudbeat/cis_eks",
"cis_eks",
},
}

Expand All @@ -322,7 +325,7 @@ runtime_cfg:
c, err := New(cfg)
s.NoError(err)

s.Equal(test.expectedType, c.Type)
s.Equal(test.expectedType, c.Benchmark)
s.Equal(test.expectedActivatedRules, c.RuntimeCfg.ActivatedRules.CisK8s)
s.Equal(test.expectedEksActivatedRules, c.RuntimeCfg.ActivatedRules.CisEks)
})
Expand Down
27 changes: 27 additions & 0 deletions deploy/cloud/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*
*.tfplan

# Crash log files
crash.log

# Exclude all .tfvars files, which are likely to contain sentitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Ignore CLI configuration files
.terraformrc
terraform.rc
58 changes: 46 additions & 12 deletions deploy/cloud/README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,74 @@
# Cloud Deployment

**Motivation**
Provide an easy and deterministic way to setup latest cloud environment so it can be monitored and used properly.
**Motivation**
Provide an easy and deterministic way to set up latest cloud environment, so it can be monitored and used properly.

This guide deploys both an Elastic cloud environment, and an AWS EKS cluster. To only deploy specific resources, check out the examples section.

**Prerequisite**
* [terraform](https://www.terraform.io/)
* [Terraform](https://developer.hashicorp.com/terraform/downloads)
* the AWS CLI, [installed](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and [configured](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
* [AWS IAM Authenticator](https://docs.aws.amazon.com/eks/latest/userguide/install-aws-iam-authenticator.html)
* the [Kubernetes CLI](https://kubernetes.io/docs/tasks/tools/install-kubectl/), also known as `kubectl`


**How To**
Create environment
1. Create an [API token](https://cloud.elastic.co/deployment-features/keys) from your cloud console account.

1.1 use the token `export EC_API_KEY={TOKEN}`

2. run `cd deploy/cloud`
3. run `terraform init`
4. run `terraform apply --auto-approve` to create the environment from the latest version (the latest version is vary in cloud/regions combinations).
To connect to the environment use the console ui or see the details how to connect to the environment, use `terraform output -json`
2. to create the environment from the latest version (the latest version is varying in cloud/regions combinations).
```bash
cd deploy/cloud
terraform init
terraform apply --auto-approve
3. Run the following command to retrieve the access credentials for your EKS cluster and configure kubectl.
```bash
aws eks --region $(terraform output -raw eks_region) update-kubeconfig \
--name $(terraform output -raw eks_cluster_name)
Delete environment
1. `terraform destroy --auto-approve`
To connect to the environment use the console UI or see the details how to connect to the environment, using:
```bash
terraform output -json
**Delete environment:**
```bash
terraform destroy --auto-approve
**Next Steps**
* [Setup](https://github.com/elastic/security-team/blob/main/docs/cloud-security-posture-team/onboarding/deploy-agent-cloudbeat-on-eks.mdx) EKS cluster
* Setup Vanila cluster
* Enable rules add slack webhook to connctor
* Setup Self-Managed cluster
* Enable rules add slack webhook to connector
# Examples
## Specific version
To create an environment with specific version use
To create an environment with specific version use
`terraform apply --auto-approve -var="stack_version=8.5.1"`
When working with non production versions it is required to also update the deployment regions.
For example, to deploy `8.6.0-SNAPSHOT` use
For example, to deploy `8.6.0-SNAPSHOT` use
`terraform apply --auto-approve -var="stack_version=8.6.0-SNAPSHOT" -var="ess_region=gcp-us-west2"`
## Named environment
To give your environment a different prefix in the name use
To give your environment a different prefix in the name use
`terraform apply --auto-approve -var="deployment_name_prefix=elastic-deployment"`
## Deploy specific resources
To deploy specific resources use the `-target` flag.
### Deploy only Elastic Cloud with no EKS cluster or Dashboard
`terraform apply --auto-approve -target "module.ec_deployment"`
### Deploy only Dashboard on an existing Elastic Cloud deployment
`terraform apply --auto-approve -target "null_resource.rules" -target "null_resource.store_local_dashboard"`
### Deploy only EKS cluster
`terraform apply --auto-approve -target "module.eks"`
36 changes: 19 additions & 17 deletions deploy/cloud/main.tf
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
terraform {
required_version = ">= 1.1.8, < 2.0.0"
required_providers {
ec = {
source = "elastic/ec"
version = ">=0.5.0"
}
}
}

provider "ec" {}


module "ec_deployment" {
module "ec_deployment" {
source = "github.com/elastic/apm-server/testing/infra/terraform/modules/ec_deployment"

region = var.ess_region
stack_version = var.stack_version

deployment_template = var.deployment_template
deployment_name_prefix = var.deployment_name_prefix
deployment_name_prefix = "${var.deployment_name_prefix}-${random_string.suffix.result}"

integrations_server = true

Expand All @@ -33,31 +23,43 @@ module "ec_deployment" {
}
}

module "eks" {
source = "./modules/provision-eks-cluster"

region = var.eks_region
cluster_name_prefix = "${var.deployment_name_prefix}-${random_string.suffix.result}"
}

data "local_file" "dashboard" {
filename = "data/dashboard.ndjson"
filename = "data/dashboard.ndjson"
}

resource "null_resource" "store_local_dashboard" {
provisioner "local-exec" {
command = "curl -X POST -u ${module.ec_deployment.elasticsearch_username}:${module.ec_deployment.elasticsearch_password} ${module.ec_deployment.kibana_url}/api/saved_objects/_import?overwrite=true -H \"kbn-xsrf: true\" --form file=@data/dashboard.ndjson"
}
depends_on = [module.ec_deployment]
triggers = {
triggers = {
dashboard_sha1 = sha1(file("data/dashboard.ndjson"))
}
}


data "local_file" "rules" {
filename = "data/rules.ndjson"
filename = "data/rules.ndjson"
}

resource "null_resource" "rules" {
provisioner "local-exec" {
command = "curl -X POST -u ${module.ec_deployment.elasticsearch_username}:${module.ec_deployment.elasticsearch_password} ${module.ec_deployment.kibana_url}/api/saved_objects/_import?overwrite=true -H \"kbn-xsrf: true\" --form file=@data/rules.ndjson"
}
depends_on = [module.ec_deployment]
triggers = {
dashboard_sha1 = "${sha1(file("data/rules.ndjson"))}"
triggers = {
dashboard_sha1 = sha1(file("data/rules.ndjson"))
}
}

resource "random_string" "suffix" {
length = 3
special = false
}
1 change: 1 addition & 0 deletions deploy/cloud/modules/provision-eks-cluster/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Based on the [Provision an EKS Cluster tutorial](https://developer.hashicorp.com/terraform/tutorials/kubernetes/eks), and [repo](https://github.com/hashicorp/learn-terraform-provision-eks-cluster).
Loading

0 comments on commit 2ba924e

Please sign in to comment.