30 ms·
Don't write keys into code, even "test" code. Discipline yourself (and your team) to use dedicated key storage even on test systems, (and of course that key sto
by tialaramex 6y ago
Don't write keys into code, even "test" code. Discipline yourself (and your team) to use dedicated key storage even on test systems, (and of course that key storage isn't "Duh, a github repo" but an appropriate technology for your platform)
If you instinctively want to write
shiny_api_key = 'SOME.KEY.BOSS.EMAILED.ME'
Force yourself to instead take the extra few minutes to do
# my-cloud-vendor key-store create shiny_api_key
SOME.KEY.BOSS.EMAILED.ME
and then write
shiny_api_key = key_store_access('shiny_api_key')
This way, when six hours later you check that code in, to GitHub, and go home for the weekend, you won't get a call from the boss ruining your Sunday lie-in, "WTF Did you upload our $5000 shiny API key to GitHub? You are fired".
This habit also allows for correct separation as an organisation grows, when you're big enough you want it to be possible for somebody to write code against test systems with test API keys, and that same code to work as written in production, even though production API keys are not accessible to the coders. Using key stores to mediate makes this much more likely to actually work.
- 0xbadcafebee 6y agoDon't know why you were downvoted, this is 100% what everyone should be doing. Don't even write shiny_api_key = "A-CLEARLY-FAKE-KEY". Don't write ANY secret AT ALL in your code, no matter what. Even for test frameworks. Just don't do it. Build in the secret retriving function from day one. Have it grab from an environment variable and it will be 100% seamless from development to production. Add more complexity/security if and when you need it. This can later be leveraged to allow for zero-downtime credential rotations for running services, which allows for seamless use of ephemeral credentials, which is what we should all be moving towards.
- yowlingcat 6y agoThis is the wrong answer. At a company I worked at, we had a well known saying: "Favor mechanisms over good intentions." As the saying went, if good intentions worked, we "wouldn't need mechanisms." The right answer here (as stated in sibling comments) is to install a mechanism into the build. A push that accidentally commits credentials should break the build. How it goes about doing that is up to you, but the specific way I've done that in the past is a git hook [1] specifically the pre-commit hook. It looks like there's a reasonably well-updated package by Yelp to help do this kind of thing [2]. [1] https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks [2] https://github.com/Yelp/detect-secrets https://github.com/Yelp/detect-secrets
- tialaramex 6y agoI agree with trying to build this, and with what you say about mechanism - but I don't agree it's step one. This is a prod, a reminder, it reminds people "Do the thing we trained you to do" but it's a mistake to rely on it without the accompanying policy about what they ought to do. These types of hook are brittle. If you already knew you were supposed to do it the way I wrote, you get a hook failure, you say "D'oh" and you do it properly. But if you aren't trained to do anything in particular, this type of hook is a behavioural branch. Maybe it's 0845, your mentor just arrived, the perfect time to ask what the diagnostic means. "You need to use a dedicated key store, let me show you" she says and you just learned an important thing. But maybe it's 1845, you're the last one in the office. You promised to raise a PR for tomorrow, but the hook fails. You try some things, and eventually discover that it hides well enough if split into four strings. You commit, click to raise the PR and go home. And the person who reads that PR will see immediately tomorrow it's unacceptable because it has an obfuscated API key, but alas if it's a real working key that's too late.
- yowlingcat 6y ago> But maybe it's 1845, you're the last one in the office. You promised to raise a PR for tomorrow, but the hook fails. You try some things, and eventually discover that it hides well enough if split into four strings. You commit, click to raise the PR and go home. You raise a great point which is that these kinds of mechanisms are only as strong as the cultures that support them. If the team doesn't wholeheartedly digest and commit to the new mechanism, it will at best achieve only surface level improvements. Getting your team's culture to that point is a step zero of sorts -- completely necessary precondition and you can't do much without it. And that can end up with people paying lip service to the letter but not spirit of the mechanism.