Ecs secrets

Ecs secrets DEFAULT

ECS Secrets - Managing Runtime Secrets for Containers in ECS

Containerized applications frequently need access to sensitive information at runtime such as API keys, passwords, certificates etc (aka secrets). Handling such secrets is a challenging and recurring problem for Docker containers. ECS customers also come up against this issue and there's a need to provide a mechanism for delivering secrets securely to such containerized applications.

A write-up of various approaches to this problem is summarized in the "Secrets: write-up best practices, do's and don'ts, roadmap" docker issue. This issue also outlines the risks with the following commonly used work-arounds for secrets management:

  • Environment Variables: Secrets are easily leaked and unencrypted as secrets are visible in docker inspect
  • Volumes: Secrets are easily leaked via and when creating images from existing containers
  • Building into the container image: Secrets are unencrypted and can be leaked easily via build cache or image sharing

The How to Manage Secrets for Amazon EC2 Container Service–Based Applications by Using Amazon S3 and Docker blog documents how you could store secrets in an Amazon S3 bucket and use AWS Identity and Management (IAM) roles to grant access to those stored secrets. The Managing Secrets for Amazon ECS Applications Using Parameter Store and IAM Roles for Tasks blog illustrates how the EC2 SSM Parameter Store can be used to do the same. The tool takes an alternative approach of using the AWS Key Management Service (KMS) to encrypt and decrypt secrets stored in Amazon DynamoDB service and use IAM roles for ECS Tasks to control access to these secrets.

What is ?

provides an out-of-the-box solution for managing and accessing runtime secrets for containers on ECS. It provides a simple command line interface and RESTful APIs to create, rotate, fetch and revoke runtime secrets. It uses DynamoDB to store and retrieve application secret key-value pairs. The secret payload is encrypted and decrypted using KMS Data Keys. Permissions and policies to access these secrets can then be set using IAM Users and Roles.

This design helps in addressing the following security goals:

  • Privilege Separation: Different entities can be authorized to perform secret management and secret query. This ensures that applications that read secrets can be authorized to just secrets while a separate IAM user/role is used to create, rotate and revoke secrets
  • Encryption: Secrets are encrypted at rest in DynamoDB using KMS data keys and also in transit using AWS SDK (AWS SDK uses HTTPS by default to ensure secrets are encrypted in transit). Even if access is gained to view entries in the DynamoDB table, IAM role/user permissions to decrypt using KMS Customer Master Key are still needed to view secrets
  • Access Control: Access to secrets from containers and tasks can be controlled using IAM roles
  • Version Control: Secrets are version controlled. New versions of secrets can be registered and existing versions can be revoked

You can interact with using either the CLI or the RESTful API endpoint. To ensure easy access to IAM role credentials for different entities managing secrets, and to ensure between the same, it's recommended that you incorporate the packaged container into an existing Task Definition as a sidecar and make use of the RESTful APIs by running it in the daemon mode.

Getting Started

To use , you can download the container from dockerhub and execute the command. You can run this from either your local host or an EC2 Instance as long as you have access to credentials that let you create a CloudFormation stack with a KMS Master Key and a Dynamo DB table.

In this example, credentials for an IAM user with administrative privileges for an account have been saved in a profile named to setup :

$ cat <<EOF > setup-env.txtAWS_REGION=us-west-2AWS_PROFILE=defaultAWS_SHARED_CREDENTIALS_FILE=/root/.aws/credentialsEOF

The following command deploys up a Cloudformation stack named , which creates:

  • A DynamoDB table named
  • A KMS Master Key with alias with policies to ensure that:
    • Only the IAM role is allowed to create, rotate and revoke secrets
    • Only the IAM role can be used to fetch secrets

Note that you can use Task IAM roles to grant permissions to Tasks. You can read more about creating an IAM Role for your Task here.

$ docker run --env-file setup-env.txt -v ~/.aws:/root/.aws \ amazon/amazon-ecs-secrets setup \ --application-name cryptex \ --create-principal arn:aws:iamrole/SecretsAdmin \ --fetch-role arn:aws:iamrole/MyApplicationRole TZ [INFO] Unable to describe stack: ECS-Secrets-cryptex, creating a new one TZ [INFO] Secrets are stored in the table: arn:aws:dynamodb:us-westtable/ECS-Secrets-cryptex-Secrets TZ [INFO] Update 'arn:aws:iamrole/MyApplicationRole' to provide read access for this table by updating the policy statement with: { "Effect": "Allow", "Action": [ "dynamodb:Query", "dynamodb:GetItem" ], "Resource": [ "arn:aws:dynamodb:us-westtable/ECS-Secrets-cryptex-Secrets" ] } TZ [INFO] Update 'arn:aws:iamrole/SecretsAdmin' to provide write access for this table by updating the policy statement with: { "Effect": "Allow", "Action": [ "dynamodb:PutItem", "dynamodb:Query", "dynamodb:UpdateItem" ], "Resource": [ "arn:aws:dynamodb:us-westtable/ECS-Secrets-cryptex-Secrets" ] } TZ [INFO] Setup complete

Update the write and read policies as per the output of the command. This is critical to provide the necessary permissions to the and the roles to write and read secrets from the DynamoDB table.

An example of doing the same from the CLI is provided next:

Example: Updating the role:

$ cat <<EOF > write-policy.json{ "Version": "", "Statement" : [{ "Effect": "Allow", "Action": [ "dynamodb:PutItem", "dynamodb:Query", "dynamodb:UpdateItem" ], "Resource": [ "arn:aws:dynamodb:us-westtable/ECS-Secrets-cryptex-Secrets" ] }]}EOF $ aws --region us-west-2 iam create-policy --policy-name ECS-Secrets-cryptex-Secrets-write --policy-document file://write-policy.json { "Policy": { "PolicyName": "ECS-Secrets-cryptex-Secrets-write", "CreateDate": "", "Path": "/", "Arn": "arn:aws:iampolicy/ECS-Secrets-cryptex-Secrets-write", "UpdateDate": "" } } $ aws --region us-west-2 iam attach-role-policy --role-name SecretsAdmin --policy-arn arn:aws:iampolicy/ECS-Secrets-cryptex-Secrets-write

Example: Updating the role:

$ cat <<EOF > read-policy.json{ "Version": "", "Statement" : [{ "Effect": "Allow", "Action": [ "dynamodb:Query", "dynamodb:GetItem" ], "Resource": [ "arn:aws:dynamodb:us-westtable/ECS-Secrets-cryptex-Secrets" ]}]}EOF $ aws --region us-west-2 iam create-policy --policy-name ECS-Secrets-cryptex-Secrets-read --policy-document file://read-policy.json { "Policy": { "PolicyName": "ECS-Secrets-cryptex-Secrets-read", "CreateDate": "", "Path": "/", "Arn": "arn:aws:iampolicy/ECS-Secrets-cryptex-Secrets-read", "UpdateDate": "" } } $ aws --region us-west-2 iam attach-role-policy --role-name MyApplicationRole --policy-arn arn:aws:iampolicy/ECS-Secrets-cryptex-Secrets-read

Creating Secrets

The following diagram illustrates the workflow for creating a secret. The first step is sending a HTTP POST, containing the secret in the body of the request to the RESTful endpoint exposed by the container. The container then invokes the KMS API using IAM Role credentials for the Task [2]. This causes the AWS IAM service to validate if the IAM Role associated with the task has the relevant permission to use the KMS API [3]. Next, a KMS Data Encryption Key is returned to the container [4]. This key is used to encrypt the secret. The encrypted secret and the encrypted data key are then saved in a DynamoDB table [5].

The following task defintion provides an example of creating a secret using the container running in daemon mode and an application container posting a request to create the secret. The task is registered with the the IAM role. This ensures that the container is authorized to create KMS data encryption keys. The container posts the request to create a secret named with contents of the file named :

On the instance, the password file has the following contents:

$ cat /tmp/secrets/password.txt {"payload":""}

If you specified an IAM user as argument to instead of an IAM role (Example: ), you can also run the command using the CLI. In the example listed next, the profile credentials from profile are used to create secrets.

$ cat <<EOF > setup-env.txtAWS_REGION=us-west-2AWS_DEFAULT_PROFILE=cryptex-adminAWS_SHARED_CREDENTIALS_FILE=/root/.aws/credentialsEOF $ echo"mydbpassword"> secrets.txt $ docker run --env-file setup-env.txt -v ~/.aws:/root/.aws \ amazon/amazon-ecs-secrets create \ --application-name cryptex \ --name dbpassword \ --payload `cat secrets.txt`

You can register a new version of the secret key by running the command again. Note that you can also specify the location of the file that containers secrets using . Example:

$ docker run --env-file setup-env.txt -v ~/.aws:/root/.aws \ amazon/amazon-ecs-secrets create \ --application-name cryptex \ --name dbpassword \ --payload-location secret.txt

Retrieving Secrets

The following diagram illustrates the workflow for retrieving secrets from the secret store. The application container sends a HTTP GET request with the name of the secret [1] to the container's RESTful endpoint. The container retrieves encrypted data keys and encrypted secrets from the DynamoDB table [2]. It then invokes the KMS API to decrypt the encrypted data key [3]. This causes the AWS IAM service to validate if the IAM Role associated with the task has the relevant permission to use the KMS API [4]. The decrypted data key is then used to decrypt the encrypted secret [6].

The following example illustrates a Task Definition for fetching secrets from the secret store:

On the instance, the output of the container shows the following:

$ docker logs 9b8aba99 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 64 64 0 0 0 {"name":"password","serial":1,"payload":"","active":true}

Revoking Secrets

also supports versioning of secrets. You can use the command to revoke specific versions of secrets. Example:

$ docker run --env-file setup-env.txt -v ~/.aws:/root/.aws \ amazon/amazon-ecs-secrets revoke \ --application-name cryptex \ --name dbpassword \ --serial 1 $ docker run --env-file setup-env.txt -v ~/.aws:/root/.aws \ amazon/amazon-ecs-secrets fetch \ --application-name cryptex \ --name dbpassword \ --serial 1 {"name":"dbpassword","serial":1,"payload":"","active":false}

In the above example, the version of secret named has been revoked, because of which, retrieving that version of the secret using the command would not work. Where as, fetching the version of the secret returns the appropriate value of the secret.

$ docker run --env-file setup-env.txt -v ~/.aws:/root/.aws \ amazon/amazon-ecs-secrets fetch \ --application-name cryptex \ --name dbpassword {"name":"dbpassword","serial":2,"payload":"mydbpassword","active":true}

You can also run this within a Task Definition as with all other commands. A HTTP POST request to the endpoint revokes a secret. A sample task definition for the same is listed next:

Sours: https://github.com/awslabs/ecs-secrets

How can I pass secrets or sensitive information securely to containers in an Amazon ECS task?

I want to pass secrets or sensitive information securely to containers in a task for Amazon Elastic Container Service (Amazon ECS).

Short description

Passing sensitive data in plaintext can cause security issues, as it's discoverable in the AWS Management Console or through AWS APIs such as DescribeTaskDefinition or DescribeTasks.

As a security best practice, pass sensitive information to containers as environment variables. You can securely inject data into containers by referencing values stored in AWS Systems Manager Parameter Store or AWS Secrets Manager in the container definition of an Amazon ECS task definition. Then, you can expose your sensitive information as environment variables or in the log configuration of a container.

AWS supports data injection only for the following:

Resolution

Complete prerequisites

1.    Store your sensitive information in either AWS Systems Manager Parameter Store or Secrets Manager.

For AWS Systems Manager Parameter Store, run the following command:

For Secrets Manager, run the following command:

3.    To create an inline policy for your role in the IAM console, choose Roles, select the role that you created in step 2, and then choose Add inline policy on the Permissions tab. Choose the JSON tab, and then create a policy with the following code:

Note: Replace us-east-1 and awsExampleAccountID with the AWS Region and account where your parameters are stored. Replace awsExampleParameter with the name of the parameters that you created in step 1.

Note: If you use a customer managed KMS key for encrypting data in AWS Systems Manager Parameter Store or Secrets Manager, then you must get permissions for kms:Decrypt.

4.    (Optional) Attach the managed policy AmazonECSTaskExecutionRolePolicy to the role that you created in step 2.

Important: A managed policy is required for tasks that use images stored in Amazon Elastic Container Registry (Amazon ECR) or send logs to Amazon CloudWatch.

Reference sensitive information in the ECS task definition

From the AWS Management Console:

1.    Open the Amazon ECS console.

2.    From the navigation pane, choose Task Definitions, and then choose Create new Task Definition.

3.    Choose your launch type, and then choose Next step.

4.    For Task execution role, choose the task execution IAM role that you created earlier.

5.    In the Container Definitions section, choose Add container.

6.    In the Environment variables section under ENVIRONMENT, for Key, enter a key for your environment variable.

7.    On the Value dropdown list, choose ValueFrom.

8.    In the text box for the key, enter the Amazon Resource Name (ARN) of your Parameter Store or Secrets Manager resource.

Note: You can also specify secrets in the log driver configuration.

From the AWS Command Line Interface (AWS CLI):

Note: If you receive errors when running AWS CLI commands, be sure that you’re using the most recent version of the AWS CLI.

1.    Reference AWS Systems Manager Parameter Store or Secrets Manager resources in the task definition as environment variables using the secrets section or as log configuration options using the secretOptions section. For example:

Important: Replace us-east-1 and awsExampleAccountID with your AWS Region and account ID. Replace awsExampleParameter with the parameter that you created earlier. Replace awsExampleRoleName with the role that you created earlier.

2.    To register the task definition, run the following command:

When a task is launched using the task definition that you create, the Amazon ECS container agent automatically resolves the secrets and injects the values as environment variables to the container.

Important: Sensitive data is injected into your container when the container is initially started. If the secret or Parameter Store parameter is updated or rotated, the container doesn't receive the updated value automatically. You must launch a new task. If your task is part of a service, update the service and use the Force new deployment option to force the service to launch a fresh task.

To force a new deployment:

1.    Open the Amazon ECS console.

2.    Choose Clusters, and then select the cluster with your service.

3.    Select the Force New Deployment check box, and then choose Update Service.

Note: To force a new deployment from the AWS CLI, run the update-service command with the --force-new-deployment flag.


Sours: https://aws.amazon.com/premiumsupport/knowledge-center/ecs-data-security-container-task/
  1. Sleep sound effect
  2. Thunderbolt 2 dock
  3. Iglesia cristiana avivamiento
  4. Mill drill machine

AWS Secrets Manager Overview

AWS Secrets Manager helps you protect secrets needed to access your applications, services, and IT resources. The service enables you to easily rotate, manage, and retrieve database credentials, API keys, and other secrets throughout their lifecycle. Users and applications retrieve secrets with a call to Secrets Manager APIs, eliminating the need to hard-code sensitive information in plain text. Secrets Manager offers secret rotation with built-in integration for Amazon RDS, Amazon Redshift, and Amazon DocumentDB. Also, the service is extensible to other types of secrets, including API keys and OAuth tokens. In addition, Secrets Manager enables you to control access to secrets using fine-grained permissions and audit secret rotation centrally for resources in the AWS Cloud, third-party services, and on-premises.

Secrets Manager stores, retrieves, rotates, encrypts and monitors the use of secrets within your application. Secrets Manager uses AWS KMS for encryption with IAM roles to restrict access to the services and CloudTrial for recording the API calls made for secrets. You can also use your own customer-managed key (CMK) with AWS Secrets Manager.

This tutorial will demonstrate using AWS Secrets Manager with ECS Fargate.

Here is a diagram of the infrastructure we are going to build:

Secrets Diagram

When incoming web traffic passes through the load balancer to our ECS Cluster, the application running in the container reads environment variables that contain the sensitive content. In this example the sensitive content is credentials for connecting the container app to the RDS instance. The environment variables are populated by Secrets Manager when the service is started. Secrets are managed with both security in transit and at rest. Credential rotation can be configured within Secrets Manager, eliminating the need for custom code within your application.

Sours: https://ecsworkshop.com/secrets/overview/
Amazon Elastic Container Service (ECS) Tutorial - 02 How to Create A Task Definition

Secrets Management for AWS ECS

Running tasks in the isolated environment of a container can make your life a lot easier. However, letting your containerized application get access to secrets is not straightforward. Especially when running these containers in AWS Elastic Container Service.

Using the SecretHub AWS Identity Provider can help you out. This guide will show you how to use the AWS Identity Provider to access secrets in an ECS Task using SecretHub.

To make life easy, you can use demo app from the Getting Started guide to have something to deploy to ECS.

“Just show me the code”

If you prefer to just go straight to the end result, have a look at the Terraformed example code on GitHub.

View Example on GitHub

Overview of using SecretHub in an ECS Task

Before you begin

Before you start, make sure you have completed the following steps:

  1. Set up SecretHub on your workstation.
  2. Configure your AWS credentials.
  3. If you are using Terraform, installed the SecretHub Terraform Provider.

Step 1: Create an IAM Role for ECS

The first thing we need for the AWS Integration to work, is an IAM role.

  1. Go to the Create role page on the AWS Console.
  2. Select AWS Service and Elastic Container Service as trusted entity.
  3. Select Elastic Container Service Task as use case and continue by clicking Next: Permissions.

First step in creating IAM Role

  1. Select any Policies your ECS Task needs and then click Next: Tags. For using SecretHub no specific policy is needed.
  2. Add any tags you like and click Next: Review.
  3. Set a descriptive Role name (e.g. ) and description and click Create role.

Run the following command to create an IAM role with the required policy attached:


Step 2: Create a KMS key

Next, we have to setup a KMS Key to use for encryption and decryption.

KMS keys are region bound. Make sure you create the key in the region you want the ECS task to run.

  1. Go to the Create Customer Managed Key page on the AWS Console.
  2. Enter an alias (e.g. ) and optionally a description for the key and click Next.
  3. Add any tags you like and click Next.
  4. Select any users or roles you would like as Key administrators and click Next. Make sure your own IAM user or a role you have access is selected or that you select it on the next page as a Key user.
  5. Select the role you previously created as a Key user and set any other preferred key users and then click Next. A role or user you have access to should either be a Key user or a Key Administrator.

Select the correct Key user for the KMS key

  1. Create the KMS key by clicking Finish.
  2. Take note of the id of the newly created key (e.g. ), you’ll need it in the next step.

To create a KMS key, run:

Then, to allow the IAM role to use this KMS key, create a policy by running:

Attach this policy to the previously create IAM role, by running:


Step 3: Setup SecretHub Service Account

With the IAM role and KMS key in place, we can go ahead and create a SecretHub service account for the app.

Run the following command and you’ll be prompted for the name of the role, the id or ARN of the KMS key and the region the KMS key is in:

Setting automatically creates an access rule to give the newly created service account read access on the repo.

requires access to AWS KMS for encrypting the account key of the created service account. So make sure you’ve configured the AWS CLI or have correctly set the and environment variables. To verify whether it has been setup successfully, you can run . It should return details about the currently authenticated user/role.

As you may have noticed, – in contrary to the generic command – does not output a credential.

That’s because applications on AWS do not need it anymore: as long they take on the specified role, they can automatically get their secrets from SecretHub.


Step 4: Create ECS Cluster

Before you can run a task on ECS, you have to create an ECS cluster:

  1. Go to the Create Cluster wizard.
  2. Select Networking only and click Next step.
  3. Enter a name for the cluster (e.g. ) and click Create.

Create a cluster by running the following command:


Step 5: Create Task Definition

The next step is to create a task definition that describes the task you’re going to run.

To run the task you’ll need the application in a Docker image. You can use our image or create and publish your own.

  1. Go to the Create new Task Definition wizard.
  2. Select FARGATE and click Next step.
  3. Enter a Task Definition Name (e.g. ).
  4. Select the previously created IAM role as Task Role.
  5. Set Task memory to GB and Task CPU to vCPU.
  6. Click Add container.
  7. Enter as Container name.
  8. Enter the location of the demo app image in the Image field: .
  9. Scroll down to ENVIRONMENT section and add an Environment variable with the key SECRETHUB_IDENTITY_PROVIDER and aws as value.
  10. Provision the demo app with secrets by populating environment variables and with secret references: Environment variables for ECS Task Definition
  1. Click Add.
  2. Click Create.

First, get the ARN of the by running:

Run the following command to register the task definition:

As you can see, the task definition is completely free of secrets.

is set to so that IAM is used to authenticate as the previously created AWS service account.

As you can see, the task definition is completely free of secrets.

is set to so that IAM is used to authenticate as the previously created AWS service account.


Step 6: Launch Task

The only thing left to do is launch the task in the ECS Cluster:

  1. Go to the ECS Cluster Overview and select the cluster you just created.
  2. Select the Tasks tab and click Run new Task.
  3. Set Launch type to FARGATE.
  4. Select the Task Definition you just created.
  5. Select the desired Cluster VPC and Subnets (default will suffice for testing purposes).
  6. Under Security Groups make sure that there is a rule that allows TCP traffic from your IP (or anywhere) on port and click Save.
  7. Set Auto-assign public IP to ENABLED.
  8. Click Run Task.
  9. Click on the Task id in the list of tasks.
  10. When the Status of the container is RUNNING, look for the public IP can be found in the Network section of the Task.

To launch the ECS cluster, you first need to create a security group for it.

Pick a VPC with one or more public subnets and run the following:

Then you can create a security group for the ECS service:

To allow connections on port , run:

Finally, create and deploy the service by running:

To get the public IP of the service go to the ECS page of the AWS console, click on your cluster’s name and switch to the tab. When the newly created task has the status, click on its name. You can find the public IP of the task under the section.

Pick a VPC with one or more public subnets and configure the and variables. All that’s left now is to run .

Note that the resource doesn’t offer the assigned public IP as an output, so you’ll have to dig that up in the AWS console or using the AWS CLI to see the application in action.

Visit in your browser, and if all went well you’ve got an application that automatically provisioned itself with end-to-end encrypted secrets, yet doesn’t need to be provisioned with a key!


See also

Sours: https://secrethub.io/docs/guides/aws-ecs/

Secrets ecs

.

AWS Systems Manager Parameter Store - Use Credentials with ECS Environment Variables (Hands-On)

.

You will also like:

.



1080 1081 1082 1083 1084