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

[Cloud Security] Create Serverless QA Environment #1982

Closed
wants to merge 91 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
0ffb8d1
Adding Option to override environment URL
opauloh Jan 19, 2024
514da18
fix compatibility
opauloh Jan 19, 2024
e10a0a6
create tf var def for input with ec url
Omolola-Akinleye Jan 24, 2024
922db97
add ec_url var to ec-deployment module
Omolola-Akinleye Jan 24, 2024
0166ccb
populate TF_VAR_ec_url env var
Omolola-Akinleye Jan 24, 2024
11afebe
fix typo
Omolola-Akinleye Jan 24, 2024
d45a191
remove ec_url definitions
Omolola-Akinleye Jan 31, 2024
e8c8281
fix ec url options
Omolola-Akinleye Feb 8, 2024
8aa7e71
remove ec url options
Omolola-Akinleye Feb 8, 2024
3c132dc
Merge branch 'main' of github.com:elastic/cloudbeat into qa-dev-env
Omolola-Akinleye Feb 8, 2024
acba93d
fix region
Omolola-Akinleye Feb 9, 2024
a46cf8d
fix EOF file error
Omolola-Akinleye Feb 9, 2024
a6682d6
add QA env api key to provisioning process
Omolola-Akinleye Feb 12, 2024
a9095b7
fix bash script syntax
Omolola-Akinleye Feb 12, 2024
192f7c8
fix condition spacing
Omolola-Akinleye Feb 12, 2024
b03f135
fix bash script condition
Omolola-Akinleye Feb 13, 2024
f75f659
use secrets for TF_VAR_ec_api_key
Omolola-Akinleye Feb 13, 2024
cbd89da
fix syntax
Omolola-Akinleye Feb 13, 2024
228aaca
update condition
Omolola-Akinleye Feb 13, 2024
d681aad
store secret in TF_VAR_ec_api_key
Omolola-Akinleye Feb 13, 2024
f32657e
fix syntax for if condition
Omolola-Akinleye Feb 14, 2024
15b8521
fix syntax
Omolola-Akinleye Feb 14, 2024
dda0bcb
fix bash script condition
Omolola-Akinleye Feb 14, 2024
a330eb7
fix variable values
Omolola-Akinleye Feb 14, 2024
76cfb5b
fix variable syntax
Omolola-Akinleye Feb 14, 2024
adf9e0f
fix ec_api_key in logs
Omolola-Akinleye Feb 14, 2024
16c45ec
fix yaml file
Omolola-Akinleye Feb 14, 2024
e98f15e
add semi colon
Omolola-Akinleye Feb 14, 2024
5e19766
add semi colon
Omolola-Akinleye Feb 14, 2024
317d3ad
remove semi from then
Omolola-Akinleye Feb 14, 2024
1b2652b
remove TF_VAR_EC_URL environment
Omolola-Akinleye Feb 15, 2024
16a478d
update script to work with production env
Omolola-Akinleye Feb 15, 2024
64a1f6d
update script with env variables
Omolola-Akinleye Feb 15, 2024
6efde08
remove ec url setting
Omolola-Akinleye Feb 15, 2024
ad9ed6f
add back ec url with input
Omolola-Akinleye Feb 15, 2024
d9fa950
fix syntax
Omolola-Akinleye Feb 15, 2024
d74931b
fix inputs syntax
Omolola-Akinleye Feb 16, 2024
f97e613
set EC CLOUD API KEY
Omolola-Akinleye Feb 16, 2024
6d55b31
add ability to destroy environments in qa
Omolola-Akinleye Feb 17, 2024
be2a53b
set env variables for for url
Omolola-Akinleye Feb 17, 2024
3c44959
remove the slash
Omolola-Akinleye Feb 17, 2024
27d7496
add TF_VAR_ec_url
Omolola-Akinleye Feb 21, 2024
68aa959
fix yaml file
Omolola-Akinleye Feb 21, 2024
34118a6
move inputs env down
Omolola-Akinleye Feb 21, 2024
d5016da
add env TF_VAR_ec_url
Omolola-Akinleye Feb 21, 2024
b2b7fbe
add quotes to input var
Omolola-Akinleye Feb 21, 2024
f326b43
move inputs globally
Omolola-Akinleye Feb 21, 2024
7f3248e
fix syntax
Omolola-Akinleye Feb 21, 2024
4075592
revert untouched files to main
Omolola-Akinleye Feb 22, 2024
8005b3a
revert more untouched files to main
Omolola-Akinleye Feb 22, 2024
ab120c3
update the TF script logic
Omolola-Akinleye Feb 22, 2024
d733f36
fix yaml syntax issue
Omolola-Akinleye Feb 22, 2024
2b7d3dd
add closing fi
Omolola-Akinleye Feb 22, 2024
1a61325
use quotes syntax
Omolola-Akinleye Feb 22, 2024
9edc4d0
fix yaml syntax
Omolola-Akinleye Feb 22, 2024
28a0b99
fix yaml syntax
Omolola-Akinleye Feb 22, 2024
751ce6c
revert TF script logic
Omolola-Akinleye Feb 22, 2024
b7958f3
remove comma
Omolola-Akinleye Feb 22, 2024
20f51aa
update condition
Omolola-Akinleye Feb 22, 2024
2dd5578
test tf logic
Omolola-Akinleye Feb 22, 2024
8a03c71
address pr comments
Omolola-Akinleye Mar 7, 2024
9952eda
remove ec_url var
Omolola-Akinleye Mar 7, 2024
6e12489
update destroy script
Omolola-Akinleye Mar 7, 2024
f79f676
add TF_VAR_qa_ec_api_key env var
Omolola-Akinleye Mar 7, 2024
b678356
fix build
Omolola-Akinleye Mar 11, 2024
fcf6025
fix exit strategy
Omolola-Akinleye Mar 12, 2024
8315173
fix env var syntax
Omolola-Akinleye Mar 14, 2024
f121cab
update script with local secrets
Omolola-Akinleye Mar 14, 2024
952bb4b
try another syntax pattern to update api key
Omolola-Akinleye Mar 14, 2024
43cbb5d
update script condition
Omolola-Akinleye Mar 14, 2024
74127c7
fix spacing for syntax
Omolola-Akinleye Mar 14, 2024
0797e76
add another condition for qa env
Omolola-Akinleye Mar 20, 2024
193b9ce
remove condition
Omolola-Akinleye Mar 20, 2024
4a95fcc
update ec url to terraform apply
Omolola-Akinleye Mar 21, 2024
34f434d
update original build passing logic
Omolola-Akinleye Mar 21, 2024
5e70086
add region to cloud env
Omolola-Akinleye Mar 25, 2024
22a8be4
enable support for keep_
Omolola-Akinleye Mar 26, 2024
d567e3d
remove keep prefix and update api key logic
Omolola-Akinleye Mar 27, 2024
0b709ab
rever logic and updated required fields docs
Omolola-Akinleye Mar 27, 2024
68fecc1
Merge branch 'main' of github.com:elastic/cloudbeat into qa-dev-env
Omolola-Akinleye Mar 27, 2024
3d8feff
updates docs with manual notes for qa
Omolola-Akinleye Apr 1, 2024
90499eb
Update Cloud-Env-Testing.md with images
Omolola-Akinleye Apr 1, 2024
028a2a3
update required parameters docs
Omolola-Akinleye Apr 1, 2024
186b4f6
remove ec url var from weekly
Omolola-Akinleye Apr 1, 2024
bf7b752
Merge branch 'qa-dev-env' of github.com:elastic/cloudbeat into qa-dev…
Omolola-Akinleye Apr 1, 2024
b2fa1dc
test delete script without TF_VARupdate
Omolola-Akinleye Apr 4, 2024
73b73a8
use echo instead of export
Omolola-Akinleye Apr 4, 2024
0afb9d9
revert regex condition and update logging
Omolola-Akinleye Apr 4, 2024
83792df
update kibana url
Omolola-Akinleye Apr 4, 2024
6f0b37c
add logging
Omolola-Akinleye Apr 4, 2024
0cd3736
Merge branch 'main' into qa-dev-env
gurevichdmitry May 8, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/destroy-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ env:
ENV_PREFIX: ${{ inputs.prefix }}
ENV_IGNORE_PREFIX: ${{ inputs.ignore-prefix }}
TF_VAR_ec_api_key: ${{ secrets.EC_API_KEY }}
TF_VAR_qa_ec_api_key: ${{ secrets.QA_EC_API_KEY }}

jobs:
Destroy:
Expand Down
59 changes: 49 additions & 10 deletions .github/workflows/test-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ on:
type: boolean
required: true
default: false
ec-url:
required: true
default: "https://cloud.elastic.co"
type: choice
description: Select the Environment URL
options:
- https://cloud.elastic.co
- https://console.qa.cld.elstc.co
elk-stack-version:
required: true
description: "Stack version: For released version use 8.x.y, for BC use version with hash 8.x.y-hash, for SNAPSHOT use 8.x.y-SNAPSHOT"
Expand All @@ -30,9 +38,9 @@ on:
description: "Provide the full Docker image path to override the default image (e.g. for testing BC/SNAPSHOT)"
type: string
run-sanity-tests:
description: "Run sanity tests after provision"
default: false
type: boolean
description: "Run sanity tests after provision"
default: false
type: boolean
cleanup-env:
description: "Cleanup resources after provision"
default: false
Expand All @@ -52,6 +60,11 @@ on:
type: boolean
required: true
default: false
ec-url:
required: true
default: "https://cloud.elastic.co"
type: string
description: Environment URL
elk-stack-version:
required: true
description: "Stack version: For released version use 8.x.y, for BC use version with hash 8.x.y-hash, for SNAPSHOT use 8.x.y-SNAPSHOT"
Expand Down Expand Up @@ -95,6 +108,9 @@ env:
AWS_DEFAULT_TAGS: "Key=division,Value=engineering Key=org,Value=security Key=team,Value=cloud-security-posture Key=project,Value=test-environments"
GCP_DEFAULT_TAGS: "division=engineering,org=security,team=cloud-security-posture,project=test-environments"
TF_VAR_ec_api_key: ${{ secrets.EC_API_KEY }}
TF_VAR_qa_ec_api_key: ${{ secrets.QA_EC_API_KEY }}
TF_VAR_ec_url: ${{ inputs.ec-url }}


jobs:
Deploy:
Expand All @@ -114,8 +130,8 @@ jobs:
CNVM_STACK_NAME: "${{ inputs.deployment_name }}-cnvm-sanity-test-stack"
# Add "id-token" with the intended permissions.
permissions:
contents: 'read'
id-token: 'write'
contents: "read"
id-token: "write"
outputs:
deploy-s3-bucket: ${{ steps.upload-state.outputs.s3-bucket-folder }}
aws-cnvm-stack-name: ${{ steps.upload-state.outputs.aws-cnvm-stack }}
Expand Down Expand Up @@ -144,11 +160,33 @@ jobs:
fi

- name: Mask Sensitive Data
if: inputs.ec-api-key != ''
env:
QA_EC_URL: "https://console.qa.cld.elstc.co"
EC_URL: "https://cloud.elastic.co"
run: |
# Extract the EC API key and selected EC URL
ec_api_key=$(jq -r '.inputs["ec-api-key"]' $GITHUB_EVENT_PATH)
echo "::add-mask::$ec_api_key"
echo "TF_VAR_ec_api_key=$ec_api_key" >> $GITHUB_ENV

input_ec_url="${{ env.TF_VAR_ec_url }}"

qa_ec_url="${{ env.QA_EC_URL }}"
ec_url="${{ env.EC_URL }}"

# When user stores input ec api key then update Elastic Cloud env API KEY
if [[ $ec_api_key != '' && $input_ec_url == $ec_url ]]; then
echo "Set TF_VAR_ec_api_key from the input ec api key"
echo "::add-mask::$ec_api_key"
echo "TF_VAR_ec_api_key=$ec_api_key" >> $GITHUB_ENV
elif [[ $input_ec_url == $ec_url ]]; then
echo "Set TF_VAR_ec_api_key from the PROD github secret api key"
echo "TF_VAR_ec_api_key=$TF_VAR_ec_api_key" >> $GITHUB_ENV
elif [[ $input_ec_url == $qa_ec_url ]]; then
echo "Set TF_VAR_ec_api_key from the QA github secret api key"
echo "TF_VAR_ec_api_key=$TF_VAR_qa_ec_api_key" >> $GITHUB_ENV
else
echo "error: Elastic Cloud API key is required from a valid ec url"
exit 1
fi

- name: Process Stack Version
id: remove-commit-hash
Expand Down Expand Up @@ -192,7 +230,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.9'
python-version: "3.9"

- name: Install Poetry
run: |
Expand Down Expand Up @@ -230,7 +268,8 @@ jobs:
-var="deployment_name=${{ env.DEPLOYMENT_NAME }}" \
-var="region=${{ env.AWS_REGION }}" \
-var="project=${{ github.actor }}" \
-var="owner=${{ github.actor }}"
-var="owner=${{ github.actor }}" \


- name: Set Environment Output
id: env-output
Expand Down
5 changes: 5 additions & 0 deletions deploy/cloud/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ variable "ec_api_key" {
type = string
}

variable "ec_url" {
description = "Elastic cloud Environment URL"
type = string
}

variable "ess_region" {
default = "gcp-us-central1"
description = "Optional ESS region where the deployment will be created. Defaults to gcp-us-west2"
Expand Down
8 changes: 8 additions & 0 deletions deploy/test-environments/delete_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ AWS_REGION="eu-west-1" # Add your desired default AWS region here
DELETED_ENVS=()
FAILED_ENVS=()

: "${TF_VAR_qa_ec_api_key:?Please set TF_VAR_qa_ec_api_key with an Elastic QA Cloud API Key}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Omolola-Akinleye I'm unsure if this is the appropriate location to request the TF_VAR_qa_ec_api_key. Since I intend to delete a production environment, the TF_VAR_qa_ec_api_key isn't necessary. If I define it here, both keys would be required.


# Function to delete Terraform environment
function delete_environment() {
local ENV=$1
Expand All @@ -29,6 +31,12 @@ function delete_environment() {

# Check if the resource aws_auth exists in the local state file and remove it
terraform state rm -state "$tfstate" "$(terraform state list -state "$tfstate" | grep "kubernetes_config_map_v1_data.aws_auth")" || true
echo "KIBANA_URL=$(terraform output -raw kibana_url)" >> $GITHUB_ENV
echo "Value of KIBANA_URL: $KIBANA_URL"
if [[ "$KIBANA_URL" == *"qa.elastic"* ]]; then
echo "Set TF_VAR_ec_api_key with QA Elastic Cloud API Key"
export TF_VAR_ec_api_key="$TF_VAR_qa_ec_api_key"
fi
# Destroy environment and remove environment data from S3
if terraform destroy -var="region=$AWS_REGION" -state "$tfstate" --auto-approve &&
aws s3 rm "$BUCKET/$ENV" --recursive; then
Expand Down
7 changes: 3 additions & 4 deletions deploy/test-environments/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ locals {
owner = "${var.owner}"
deployment = "${var.deployment_name}"
}
ec_url = "https://cloud.elastic.co"
ec_headers = {
Content-type = "application/json"
Authorization = "ApiKey ${var.ec_api_key}"
Expand Down Expand Up @@ -49,7 +48,7 @@ provider "ec" {

provider "restapi" {
alias = "ec"
uri = local.ec_url
uri = var.ec_url
write_returns_object = true
headers = local.ec_headers
}
Expand Down Expand Up @@ -89,9 +88,9 @@ module "ec_project" {
count = var.serverless_mode ? 1 : 0
source = "../cloud/modules/serverless"
ec_apikey = var.ec_api_key
ec_url = local.ec_url
ec_url = var.ec_url
project_name = "${var.deployment_name}-${random_string.suffix.result}"
region_id = "aws-us-east-1" # TODO: replace with var.ess_region when more regions are supported
region_id = var.ess_region
}

module "eks" {
Expand Down
6 changes: 6 additions & 0 deletions deploy/test-environments/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ variable "ami_map" {

# Elastic Cloud variables
# ===========================================
variable "ec_url" {
default = "https://cloud.elastic.co"
description = "Optional Elastic Cloud Environment URL, use export TF_VAR_ec_url={URL}, it defaults to https://cloud.elastic.co"
type = string
}

variable "ec_api_key" {
description = "Provide Elastic Cloud API key or use export TF_VAR_ec_api_key={TOKEN}"
type = string
Expand Down
24 changes: 21 additions & 3 deletions dev-docs/Cloud-Env-Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ Follow these steps to run the workflow:
- **`deployment_name`**: Name your environment (Allowed characters: a-zA-Z0-9 and `-`). For
instance: `john-8-7-2-June01`.

- **`serverless_mode`**: Set to `true` to enable serverless mode. Default: `false`.

- **`elk-stack-version`**: Specify the version of Elastic Cloud stack, either a SNAPSHOT or a build candidate (BC)
version. Check the available versions [here](https://artifacts-staging.elastic.co/dra-info/index.html).
For BC, enter version with additions/commit sha, e.g. `8.12.0-61156bc6`.
For SNAPSHOT, enter the full version, e.g. `8.13.0-SNAPSHOT`.

- **`ec_url`**: Indicate the Elastic Cloud URL. The default value is `https://cloud.elastic.co` for production environments. For QA environments,
use `https://console.qa.cld.elstc.co/`.

- **`ess-region`**: Indicate the Elastic Cloud deployment region. The default value is `gcp-us-west2`, which
supports
snapshot and build candidate (BC) versions. Specify a different region only if necessary.
snapshot and build candidate (BC) versions. However, for QA environments, use `aws-eu-west-1`. Specify a different region only if necessary.
<img width="310" alt="Screenshot 2024-04-01 at 1 35 27 PM" src="https://github.com/elastic/cloudbeat/assets/17135495/7e32cafa-5a31-47b4-978b-bccb45771e24">

![Required Parameters](https://github.com/oren-zohar/cloudbeat/assets/85433724/6159129e-6d4d-46b1-97a1-f0d3859500fd)

4. Optionally, modify other parameters if required:

Expand Down Expand Up @@ -61,6 +66,19 @@ Follow these steps to run the workflow:

![Run Workflow](https://github.com/oren-zohar/cloudbeat/assets/85433724/7b05bf58-cc0b-4ec9-8e49-55d117673df8)

## Manual QA Environments Updates

For qa environments, to avoid [automatic deployment deletion](https://docs.elastic.dev/serverless/qa) be sure to update the deployment name with the prefix `keep_`.


1. Go to [`Projects > Project > "Project Deployment Name"`](https://console.qa.cld.elstc.co/)

<img width="1419" alt="Screenshot 2024-04-01 at 12 24 25 PM" src="https://github.com/elastic/cloudbeat/assets/17135495/4c23b11a-3bda-49ec-85f4-421f6b872b2e">

2. Update the *Project name* field value with the prefix `keep_`
<img width="1393" alt="Screenshot 2024-04-01 at 12 24 54 PM" src="https://github.com/elastic/cloudbeat/assets/17135495/7a394522-3d66-44ef-935a-5ab4ccbff81e">

Note that when you go to destroy the environment, be sure to remove `keep_` from the deployment name.

## Tracking Workflow Execution

Expand Down Expand Up @@ -131,7 +149,7 @@ Follow these steps to connect to your Amazon Elastic Kubernetes Service (EKS) cl

```kubectl get po -n kube-system```

This command should list the pods in the kube-system namespace, confirming that you have successfully connected to your EKS cluster.
This command should list the pods in the kube-system namespace, confirming that you have successfully connected to your EKS cluster.


## Cleanup Procedure
Expand Down
Loading