How To Prevent Secrets From Ending Up On Developers' Machines
Tags: devops, security, programming, cloud
Published: 2022-06-21 00:28:05

---
title: How To Prevent Secrets From Ending Up On Developers' Machines
published: true
description: All you need is dynamic secrets injection and ephemeral secrets files.
tags: devops,security,programming,cloud
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7kowscg64aw8ufeqrin.jpg
canonical_url: https://blog.doppler.com/how-to-prevent-secrets-from-ending-up-on-developers-machines
---
Even with environment variable storage offered by modern hosting platforms and secrets managers provided by every cloud, developers' machines are still littered with secrets in unencrypted text files because local development was left out of the picture.

But we can remedy this situation using dynamic secrets injection and ephemeral secrets files, and in this post, we'll be using the [Doppler CLI](https://docs.doppler.com/docs/accessing-secrets) to demonstrate how this is possible.

But before diving in, we need to solve the problem of secure and encrypted storage for secrets used in local development.

## SecretOps and Local Development

![Doppler SecretOps Platform](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekavxmtuelypomp0wp7u.jpeg)

Development scoped secrets simply don't exist in traditional solutions because [secrets are siloed](https://blog.doppler.com/siloed-secrets-and-security-theatre-why-we-need-secretops) within the confines of their respective cloud or platform.

While multi-cloud capable secret managers such as [HashiCorp Vault](https://blog.doppler.com/doppler-vs-hashicorp-vault) showed great promise, the prohibitively steep learning curve and unavoidable complexity involved with fetching secrets create a significant barrier to adoption as teams are left with no incentive to switch.

A SecretOps Platform builds upon the idea of centralized secrets storage but differs from existing solutions by providing a CLI and integrations for syncing secrets to any environment, machine, platform, or cloud secrets manager. It's the best of both worlds, providing a single source of truth for management while development teams choose the best secrets access method on a per-application basis.

How does this help local development? In a SecretOps world, each application has a **Development** environment specifically for use on developers' machines, solving the storage problem.

![doppler-project.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1654745217584/uRYxiX7It.jpg align="left")

Using the [Doppler CLI](https://docs.doppler.com/docs/install-cli) to demonstrate, let's dive in to explore the mechanics of dynamic secrets injection and ephemeral secrets files.

## Dynamic Secrets Injection

The [Doppler CLI](https://docs.doppler.com/docs/install-cli) uses the same secrets injection model as platforms such as Heroku and Cloudflare Workers by injecting the secrets as environment variables into the application process.

You can use a command:

```sh
doppler run -- npm start
```

Use a script:

```sh
doppler run -- ./launch-app.sh
```

Create a long-running background process in a virtual machine:

```sh
nohup doppler run -- npm start >/dev/null 2>&1 &
```

Use the Doppler CLI inside a Docker container:

```Dockerfile
…
FROM ubuntu

# Install Doppler CLI
RUN curl -Ls --tlsv1.2 --proto "=https" --retry 3 https://cli.doppler.com/install.sh | sh

CMD ["doppler", "run", "--", "npm", "start"]
```

And inject environment variables consumed by Docker Compose:

```sh
doppler run -- docker-compose up
``` 

It's also possible to pipe secrets in .env file format to the Docker CLI, where it reads the output as a file using [process substitution](https://tldp.org/LDP/abs/html/process-sub.html):
```sh
docker run \
  --env-file <(doppler secrets download --no-file --format docker) \
  my-awesome-app
```

The same technique applies to creating a Kubernetes secret:

```sh
kubectl create secret generic my-app-secret \
  --from-env-file <(doppler secrets download --no-file --format docker)
```

But if a secrets file is what your application needs, we've got you covered.

## Ephemeral Secrets Files

The Doppler CLI enables the mounting of ephemeral secrets files in .env, JSON, or a custom file format using a secrets template that is automatically cleaned up when the application process exits. Imagine how happy your Security team will be when they learn that secrets will never live on any developer's machines again!

To mount an .env file:

```sh
# Node.js
doppler run --mount .env -- npm start

# PHP
doppler run --mount .env -- php artisan serve
```

To mount a JSON file:

```sh
doppler run --mount env.json -- npm start
```

You can specify the format using `--mount-format` if the file extension doesn't map to a known format:

```sh
doppler run --mount app.config --mount-format json -- npm start
```

Or you can use a custom template, e.g. configure Firebase functions emulator using a `.runtimeconfig.json` file:

```sh
# 1. Create the template
echo '{ "doppler": {{tojson .}} }' > .runtimeconfig.json.tmpl

# 2. Mount the .runtimeconfig.json and run the emulator
doppler run \
  --mount .runtimeconfig.json \
  --mount-template .runtimeconfig.json.tmpl -- firebase emulators:start --only functions
```

You can even make things more secure by restricting the number of read requests using the --mount-max-reads option, e.g. caching PHP configuration which only requires the .env file to be read once:

```sh
doppler run --mount .env --mount-max-reads 1 --command="php artisan config:cache && php artisan serve"
```

If you're wondering what happens to the mounted file if the Doppler process is force killed, its file contents will appear to vanish instantly. The mounted file isn't a regular file at all, but a [Unix named-pipe](https://en.wikipedia.org/wiki/Named_pipe#In_Unix). If you've ever heard the phrase "[*everything is a file in Unix*](https://en.wikipedia.org/wiki/Everything_is_a_file)", you now have a better understanding of what that means.

Named pipes are designed for inter-process communication while still using the file system as the access point. In this case, it's a client-server model, where your application is effectively sending a read request to the Doppler CLI. If the Doppler CLI is force killed, the .env file (named pipe) will still exist, but because no process is attached to it, requests to read the file will simply hang. Just delete the file from your terminal, and you're good to go.

## Summary

Thanks to SecretOps, we now have the workflows required to prevent secrets from ever living on a developer's machine again. All you need is dynamic secrets injection, ephemeral secrets files, and a single source of truth to pull it all together.

---

[![Create your free Doppler account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lcu1dmvld1wzmz3b0wl0.png)](https://dashboard.doppler.com/register?utm_source=dev&utm_medium=blog&utm_content=how-to-prevent-secrets-from-ending-up-on-developers-machines-40cb)