Skip to content

Create different templates for CSPM and CNVM #1125

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

Merged
merged 11 commits into from
Jul 19, 2023

Conversation

jeniawhite
Copy link
Contributor

Summary of your changes

In order to have different permissions and policies, we've resorted to creating different templates.

Related Issues

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary README/documentation (if appropriate)

@jeniawhite jeniawhite requested review from a team as code owners July 10, 2023 19:57
@jeniawhite jeniawhite requested a review from olegsu July 10, 2023 19:57
@mergify
Copy link

mergify bot commented Jul 10, 2023

This pull request does not have a backport label. Could you fix it @jeniawhite? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 8./d branch. /d is the digit
    NOTE: backport-skip has been added to this pull request.

@github-actions
Copy link

github-actions bot commented Jul 10, 2023

📊 Allure Report - 💚 No failures were reported.

Result Count
🟥 Failed 0
🟩 Passed 33
⬜ Skipped 1

Copy link
Collaborator

@oren-zohar oren-zohar left a comment

Choose a reason for hiding this comment

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

We could use another set of eyes here, I focused mainly on style and linting

FILENAME="cloudformation-$VERSION-$DATE.yml"
echo "Uploading $FILENAME to S3"
aws s3 cp deploy/cloudformation/elastic-agent-ec2.yml s3://elastic-cspm-cft/$FILENAME
CNVMFILENAME="cloudformation-cnvm-$VERSION-$DATE.yml"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

Suggested change
CNVMFILENAME="cloudformation-cnvm-$VERSION-$DATE.yml"
CNVM_FILENAME="cloudformation-cnvm-$VERSION-$DATE.yml"

echo "Uploading $CNVMFILENAME to S3"
aws s3 cp deploy/cloudformation/elastic-agent-ec2-cnvm.yml s3://elastic-cnvm-cft/$CNVMFILENAME

CSPMFILENAME="cloudformation-cspm-$VERSION-$DATE.yml"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

Suggested change
CSPMFILENAME="cloudformation-cspm-$VERSION-$DATE.yml"
CSPM_FILENAME="cloudformation-cspm-$VERSION-$DATE.yml"

@@ -7,7 +7,7 @@ repos:
- id: check-added-large-files
- id: check-yaml
args: [--allow-multiple-documents, --unsafe]
exclude: (^tests/deploy/k8s-cloudbeat-tests/templates/.*)$|(^deploy/cloudformation/elastic-agent-ec2.yml)$
exclude: (^tests/deploy/k8s-cloudbeat-tests/templates/.*)$|(^deploy/cloudformation/elastic-agent-ec2-*.yml)$
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we still want to ignore them now that we won't use templating?

Comment on lines +46 to +48
"-rn", # Only display messages
"--rcfile=tests/pylintrc", # Link to your config file
]
Copy link
Collaborator

Choose a reason for hiding this comment

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

please revert the auto IDE formatting changes (unless you think they are needed)

--check,
--diff,
]
args: [--line-length=120, --check, --diff]
Copy link
Collaborator

Choose a reason for hiding this comment

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

please revert the auto IDE formatting changes (unless you think they are needed)


- repo: local
hooks:
- id: yq
name: format with yq
entry: sh -c 'for file in "$@"; do yq -i "$file"; done'
language: system
args: [ -i ]
args: [-i]
Copy link
Collaborator

Choose a reason for hiding this comment

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

please revert the auto IDE formatting changes (unless you think they are needed)

files: deploy/cloudformation/.*yml

- repo: https://github.com/awslabs/cfn-python-lint
rev: v0.77.5
hooks:
- id: cfn-python-lint
- id: cfn-python-lint
Copy link
Collaborator

Choose a reason for hiding this comment

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

please revert the auto IDE formatting changes (unless you think they are needed)

Comment on lines 1 to 2
elastic-agent-ec2-dev-cspm.yml
elastic-agent-ec2-dev-cnvm.yml
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can use dev-* here instead, but don't you think it would be useful to commit these files and share them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've kept the pre-existing logic of ignoring the dev generated yml.
I'm not against pushing the dev templates, but they are auto-generated anyways.

@@ -3,8 +3,8 @@ AWSTemplateFormatVersion: "2010-09-09"
Description: Creates an EC2 instance with permissions to run elastic-agent
Parameters:
LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-arm64'
Type: "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>"
Copy link
Collaborator

@oren-zohar oren-zohar Jul 10, 2023

Choose a reason for hiding this comment

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

I feel like there's an IDE formatting war between some of the CF devs (see here for example lol) Please let's solve this instead of committing multiple "->' lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Syncing with @amirbenun it looks like he manually edited the "->' changes (it looks like that he doesn't use any formatter for yml files).
I'm using the default configurations of VSCode.
If you think that I should change the default formatting, please provide me the configuration that you'd like me to use.

Copy link
Contributor

Choose a reason for hiding this comment

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

rain fmt seems to be the AWS recommendation.

@oren-zohar oren-zohar requested a review from orestisfl July 11, 2023 08:01
@@ -10,7 +10,7 @@ The EC2 instance has elastic-agent preinstalled in it using the fleet URL and en
2. You have AWS CLI installed on your laptop and configured to work with our dev account `elastic-security-cloud-security-dev` (in particular, `~/.aws/config` and `~/.aws/credentials` should be set, check https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html for more information)

*Steps:*
1. Install the Vulnerability Management integration on a new agent policy, you might have to check the "Display beta integrations" checkbox.
1. Install Vulnerability Management integration on a new agent policy, you might have to check the "Display beta integrations" checkbox.
Copy link
Contributor

Choose a reason for hiding this comment

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

"the" is grammatically correct there :)

Dev *devConfig `mapstructure:"DEV"`
DeploymentType string `mapstructure:"DEPLOYMENT_TYPE"`
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should be inside the devConfig because all the "regular" parameters being sent as CloudFormation parameters which will cause an unknown parameter.
Another option is to wrap the CloudFormation parameters under some new object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dev is also not a part of the template and yet it exists in the same scope.
I do think that the DeploymentType should be in the root because it is not part of the Dev subsection.
It actually decides what template we will apply eventually.
This is only relevant for use-case when we are running via the gomain.go.

@@ -19,7 +19,6 @@ FLEET_URL="<Elastic Agent Fleet URL>"
ENROLLMENT_TOKEN="<Elastic Agent Enrollment Token>"
ELASTIC_ARTIFACT_SERVER="https://artifacts.elastic.co/downloads/beats/elastic-agent" # Replace artifact URL with a pre-release version (BC or snapshot)
ELASTIC_AGENT_VERSION="<Elastic Agent Version>" # e.g: 8.8.0 | 8.8.0-SNAPSHOT
# INTEGRATION=CloudSecurityPostureManagement # Defaults to VulnerabilityManagement if not specified
Copy link
Contributor

Choose a reason for hiding this comment

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

Document the new parameter instead

Copy link
Contributor Author

@jeniawhite jeniawhite Jul 11, 2023

Choose a reason for hiding this comment

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

The DeploymentType is not a template parameter and is only being used by the config.go.

DEV: "elastic-agent-ec2-dev-cnvm.yml",
PROD: "elastic-agent-ec2-cnvm.yml",
},
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Create a struct

type flavor struct {
    Name string
    DevTemplate string
    ProdTemplate string
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed together and came to conclusion that we would like to keep the map access dynamic for now.
We can re-evaluate in future if needed.

@@ -171,3 +188,13 @@ func createStack(stackName string, templatePath string, params map[string]string
log.Printf("Created stack %s", *stackOutput.StackId)
return nil
}

func getTemplatePath(deploymentType string, env string) (string, error) {
if deploymentType == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

where possible, validations should be in config.go under the validation function.

Dev *devConfig `mapstructure:"DEV"`
DeploymentType string `mapstructure:"DEPLOYMENT_TYPE"`
Copy link
Contributor

Choose a reason for hiding this comment

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

The sanity flow needs to create a CNVM stack, you can add an env var here:

STACK_NAME: "${{ env.CNVM_STACK_NAME}}"

Copy link
Contributor Author

@jeniawhite jeniawhite Jul 11, 2023

Choose a reason for hiding this comment

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

It will do it by default.

func getTemplatePath(deploymentType string, env string) string {
	if deploymentType == "" {
		// Default is CNVM
		deploymentType = DeploymentTypeCNVM

@jeniawhite jeniawhite enabled auto-merge (squash) July 17, 2023 17:47
@oren-zohar oren-zohar disabled auto-merge July 19, 2023 10:35
@oren-zohar oren-zohar merged commit d63abaa into elastic:main Jul 19, 2023
orestisfl added a commit to orestisfl/cloudbeat that referenced this pull request Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Separate CloudFormation templates
4 participants