I have created a program in Go which receives to arguments, project_id
and private_token
. Basically that is how I do it:
project_id := flag.String("project", "", "The id of the project")
private_token := flag.String("pat", "", "The personal access token with api and read user permissions")
flag.Parse()
I have created the following docker image:
FROM golang:1.16-alpine
WORKDIR /app
COPY . /app
RUN go build
ENV PROJECT=""
ENV PRIVATE_TOKEN=""
ENTRYPOINT "./my-program" "-project" $PROJECT "-pat" $PRIVATE_TOKEN
I run the image by running:
docker run -e PROJECT=29065042 -e PRIVATE_TOKEN="glpat-1CHf9T8Nz98W8ZzyT7V4" --rm -it my-image-name
As you can see, I’m passing a private token, which is a sensitive data. I wanted to know if this is the best approach of passing sensitive data from docker to my go program or if there is a better pattern?
2
Answers
ENTRYPOINT "./my-program" "-project" $PROJECT "-pat" $PRIVATE_TOKEN
is an independent shell and cannot share variables from previous Dokerfile lines, this is a tradeoff of using Dockerfiles.I would like to suggest to modify
./my-program
to read aconfig-file
likekubectl
does. There is a good Viper package that helps to achieve this approach.ie:
ENTRYPOINT "./my-program" "--config-file" "config.yaml"
You should save all the secrets in the config.yaml before ENTRYPOINT
References: https://github.com/spf13/viper
There are different ways to store and use sensitive data inside your Go application with docker. Every method has its pros and cons.
1. Hard-code the secrets inside the code.(never do this)
2. Put secrets in environment variables, loaded from a .env file.
There is two package to use .env file easily , godotenv and viper , I prefer godotenv beacuse it’s much easier .
3. Put secrets in a dedicated secret manager, like Vault by HashiCorp , Secret Manager by Google Cloud. Parameter Store by AWS , Azure Key Vault from Azure
So the choice is really between items 2 and 3 above. Which one you pick will depend on how sensitive your secrets are and how much extra work it would be to use a dedicated secret manager. For example, if your project is running on Google Cloud Platform, the Secret Manager is just one API call away. It may be just as easy on the other major cloud platforms, but I don’t have first-hand experience with them.