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

Exec instead of formatting environment variables to eval #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ $ wget https://github.com/Droplr/aws-env/raw/master/bin/aws-env-linux-amd64 -O a
```

3. Start your application with aws-env
* `AWS_ENV_PATH` - path of parameters. If it won't be provided, aws-env will exit immediately. That way, you can run your Dockerfiles locally.
* `AWS_ENV_PATH` - path of parameters. If it isn't be provided, aws-env will not make requests to SSM. That way, you can run your Dockerfiles locally.
* `AWS_REGION` and AWS Credentials - [configuring credentials](https://github.com/aws/aws-sdk-go#configuring-credentials)
```
$ eval $(AWS_ENV_PATH=/prod/my-app/ AWS_REGION=us-west-2 ./aws-env) && node -e "console.log(process.env)"
$ AWS_ENV_PATH=/prod/my-app/ AWS_REGION=us-west-2 ./aws-env node -e "console.log(process.env)"
```


Expand All @@ -45,7 +45,7 @@ RUN apk update && apk upgrade && \
RUN wget https://github.com/Droplr/aws-env/raw/master/bin/aws-env-linux-amd64 -O /bin/aws-env && \
chmod +x /bin/aws-env

CMD eval $(aws-env) && node -e "console.log(process.env)"
CMD aws-env node -e "console.log(process.env)"
```

```
Expand All @@ -69,13 +69,6 @@ $ docker run -t my-app
$ wget https://github.com/Droplr/aws-env/raw/befe6fa44ea508508e0bcd2c3f4ac9fc7963d542/bin/aws-env-linux-amd64
```

* Many Docker images (e.g. ruby) are using /bin/sh as a default shell. It crashes `$'string'`
notation that enables multi-line variables export. For this reason, to use aws-env, it's
required to switch shell to /bin/bash:
```
CMD ["/bin/bash", "-c", "eval $(aws-env) && rails s Puma"]
```

* You should never pass AWS credentials inside the containers, instead use IAM Roles for that -
[Managing Secrets for Amazon ECS Applications Using Parameter Store and IAM Roles for Tasks](
https://aws.amazon.com/blogs/compute/managing-secrets-for-amazon-ecs-applications-using-parameter-store-and-iam-roles-for-tasks/)
Expand Down
35 changes: 23 additions & 12 deletions aws-env.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"path"
"syscall"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"log"
"os"
"strings"
)

func main() {
if os.Getenv("AWS_ENV_PATH") == "" {
log.Println("aws-env running locally, without AWS_ENV_PATH")
return
} else {
ExportVariables(os.Getenv("AWS_ENV_PATH"), "")
}

ExportVariables(os.Getenv("AWS_ENV_PATH"), "")
binary, lookErr := exec.LookPath(os.Args[1])
if lookErr != nil {
panic(lookErr)
}

env := os.Environ()
args := os.Args[1:]
execErr := syscall.Exec(binary, args, env)
if execErr != nil {
panic(execErr)
}
}

func CreateClient() *ssm.SSM {
Expand All @@ -43,20 +56,18 @@ func ExportVariables(path string, nextToken string) {
}

for _, element := range output.Parameters {
PrintExportParameter(path, element)
SetExportParameter(element)
}

if output.NextToken != nil {
ExportVariables(path, *output.NextToken)
}
}

func PrintExportParameter(path string, parameter *ssm.Parameter) {
func SetExportParameter(parameter *ssm.Parameter) {
name := *parameter.Name
value := *parameter.Value

env := strings.Trim(name[len(path):], "/")
value = strings.Replace(value, "\n", "\\n", -1)

fmt.Printf("export %s=$'%s'\n", env, value)
_, envName := path.Split(name)
os.Setenv(envName, value)
}
Binary file modified bin/aws-env-darwin-386
Binary file not shown.
Binary file modified bin/aws-env-darwin-amd64
Binary file not shown.
Binary file modified bin/aws-env-linux-386
Binary file not shown.
Binary file modified bin/aws-env-linux-amd64
Binary file not shown.
Binary file modified bin/aws-env-windows-386
Binary file not shown.
Binary file modified bin/aws-env-windows-amd64
Binary file not shown.