Latest / Talk Python To Me / #418: How To Keep A Secret in Python Apps
Transcript
- 0:00Think about the different APIs and databases your applications work with.
- 0:04Each one of them requires either an API key or a database connection string that itself contains a password.
- 0:10How do you let your application access this sensitive information without storing it in source code or putting it in other compromising locations?
- 0:19On this episode, we have Glyph on the show to share his security fable, as well as just good advice for keeping secrets out of Python code.
- 0:27This is Talk Python To Me, episode 418, recorded May 24th, 2023.
- 0:33Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:49This is your host, Michael Kennedy.
- 0:51Follow me on Mastodon, where I'm @mkennedy and follow the podcast using @talkpython.
- 0:56Both on Bostodon.org.
- 0:59Be careful with impersonating accounts on other instances.
- 1:01There are many.
- 1:02Keep up with the show and listen to over seven years of past episodes at talkpython.fm.
- 1:07We've started streaming most of our episodes live on YouTube.
- 1:11Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and be part of that episode.
- 1:20This episode is brought to you by JetBrains, who encourage you to get work done with PyCharm.
- 1:26Download your free trial of PyCharm Professional at talkpython.fm/done dash with dash PyCharm.
- 1:32And it's brought to you by the compiler podcast from Red Hat.
- 1:36Listen to an episode of their podcast to demystify the tech industry over at talkpython.fm/compiler.
- 1:44Cliff, welcome to Talk Python To Me.
- 1:46It's good to be here.
- 1:47Thank you very much for having me.
- 1:48It is good to have you here.
- 1:50And it's good to have you back.
- 1:51You've been on the show a couple of times.
- 1:54Way back when you talked about shipping software to users.
- 1:58And then also not so long ago, a little while ago, we talked about running Python in production.
- 2:03And those were a bunch of fun shows and really excited to now talk about secrets.
- 2:09Although it does pose a bit of a problem.
- 2:11If we talk about secrets, are they still secrets?
- 2:14That is an interesting epistemological question.
- 2:17But luckily, that's not the question that we need to answer today.
- 2:21Because the kinds of secrets we're going to talk about are the ones that are meaningless.
- 2:25They are, by definition, not very interesting to humans, but to computers.
- 2:29Yes, they are.
- 2:30They're both for you to access your database and cryptocurrency people to get cheaper crypto mining.
- 2:37Depending on which way it goes, if you do it right or wrong.
- 2:39So before we get into that, let's just do a quick catch up.
- 2:42It's been at least a year since you've been on the show a little bit more.
- 2:47So what have you been up to?
- 2:48What are you doing these days?
- 2:49I am nominally independent now.
- 2:51I am trying to make a career out of having good ideas and putting them on the internet.
- 2:57Work for a lot of different companies, doing a lot of different things.
- 3:00Now I am making my own app.
- 3:01I'm making an app called Pomodorobros, which is a terrible name that I'm sure you can't spell.
- 3:05But I'm sure we'll have some links in the show notes to get to that, which is an ADHD time blindness assistance app.
- 3:13Interesting.
- 3:14So on the one hand, I'm kind of leaning into the neurodiversity thing.
- 3:18I have ADHD and I'm kind of trying to help other people with it as well.
- 3:21But also it is written entirely in Python.
- 3:24I'm working on the Mac version first, but there'll be other versions later and it's all open source.
- 3:28And so I have a Patreon at patreon.com slash creator glyph, which has just terrible reward tiers.
- 3:34Hopefully I'll improve those soon.
- 3:36But the idea is if you want to support like various open source libraries that I'm working on maintaining,
- 3:41we'll talk about a couple of new ones that I've released recently in this podcast.
- 3:45If you want to support the random stuff that I do, now you can.
- 3:50And so we'll see.
- 3:51People have been telling me for years that they love the stuff that I do.
- 3:54So I guess we'll find out if they in fact do.
- 3:56Well, congratulations.
- 3:58That's really awesome.
- 3:59As somebody doing independent stuff myself, I know how rewarding it is, but also how stressful it is, especially early days.
- 4:08Yep.
- 4:08It is equal parts harrowing and amazing.
- 4:11Yes, that's well said.
- 4:13Well said.
- 4:14So yeah, certainly we will link to those in the show notes.
- 4:17You give me the links.
- 4:18I'll throw them in there for people.
- 4:19You said something interesting that I don't hear a lot of people say that I actually wish I heard more often was,
- 4:24I'm working on a macOS version and the word's Python.
- 4:29So this is actually kind of dovetails with what we're going to be talking about today in an interesting way,
- 4:34which is that one of the things that I want to be doing with my independent work is kind of letting people program their own computers.
- 4:42And part of that is like a lot of people use Macs.
- 4:45So I'm working on Mac desktop stuff.
- 4:48I hope to do some mobile stuff later.
- 4:50I also want to do Linux and Windows desktop versions of this app and then others.
- 4:54Because I think that it's, we've lost touch with that a little bit.
- 4:57You know, we all do stuff in the cloud these days, particularly in Python.
- 5:01But Python is so versatile and you can use it in so many places.
- 5:04And the way that that sort of dovetails with what we're going to be talking about today is that it's also a neglected part of the security story.
- 5:11Because no matter how great all of your cloud security is, you're using AWS Secrets Manager and HashiCorp Vault and, you know, all kinds of cloud security stuff.
- 5:21It all kind of terminates at your computer.
- 5:24If your computer is not secure, if the secrets that you have locally in your possession are not properly managed,
- 5:31then all of the cloud stuff, like you're doing it from your computer.
- 5:34So that computer had better be secure.
- 5:36And if it's not, that kind of flows outward towards everything else if you're a developer or a sysadmin.
- 5:43So, yeah, so the two libraries that we're going to be talking about today are also both,
- 5:47they're a little bit more traditional kind of console text things,
- 5:51but they are designed to be run on your computer and not on the cloud.
- 5:55Yeah, absolutely.
- 5:55It's a lot of interesting things for like, how do you secure secrets?
- 6:00Connection strings and whatnot on your machine, right?
- 6:03Yeah.
- 6:03So, and as far as the other sort of desktop programming stuff,
- 6:06we do not have time to get into that today,
- 6:08but you can definitely follow my blog for more interesting details about how PyObjective-C works.
- 6:13Right.
- 6:13Are you willing to share a quick what GUI framework you're using sort of thing
- 6:18and how are you packaging it up or not yet?
- 6:20I wrote a little tool called Encrust that does some of the code signing stuff.
- 6:24I'm packaging it with PyToApp right now.
- 6:26And as for GUI framework, I'm just using PyObjective-C and writing all of the GUI by hand as like a traditional Mac.
- 6:33I plan to redo the GUI for each platform because Pometer Obros is a thing that needs to be a local Mac app,
- 6:39like it can, or a local desktop app rather.
- 6:41And so I really want to get the UI very kind of finely tuned per platform.
- 6:47And so cross-platform GUI toolkits don't really do some of the stuff that I need to do.
- 6:51So, and that's kind of part of the point I want to make with it is,
- 6:54it's actually not that hard to just like write things multiple times.
- 6:57The Python lets you keep all the logic and a nice kind of core.
- 7:00And then if you need to call some native APIs, Python can do that pretty much everywhere.
- 7:04Okay.
- 7:05Well, very interesting approach.
- 7:06I think one of the things that's held me back from going,
- 7:09oh, I'm going to use TK Enter or GDK or whatever.
- 7:13It's just like, oh, but it just doesn't really look like it belongs here that much.
- 7:16It looks kind of like it belongs here, but not really.
- 7:19And that, I don't know, some people don't mind that.
- 7:21To me, it's just, I'm not a big fan.
- 7:23So I'm looking forward to this.
- 7:25It sounds great.
- 7:26Cool.
- 7:26Maybe you can have me on again to talk about it when it's closer to done.
- 7:29Yeah, absolutely.
- 7:29It'd be fun to talk about, you know, sort of going through and building,
- 7:32how you built it and some lessons that you've learned, right?
- 7:36Also, I'm working on some desktop apps now and it's not as easy as it used to be.
- 7:41It's like, oh, this one's not signed.
- 7:42And so it won't open because the macOS gatekeeper says it's probably dangerous.
- 7:47And you're just like, God, I usually will just hand out an app, all right?
- 7:51We can't have nice things because of people.
- 7:53And I guess that's pretty much a great lead into this entire talk,
- 7:56because if we could have nice things, we wouldn't even need this talk, would we?
- 8:00Exactly.
- 8:00I guess it's worth mentioning that I reached out to you and had you on the show here
- 8:05because you gave a talk at PyCon, How to Keep a Secret.
- 8:10And what is it today?
- 8:11It's May 24th.
- 8:12So a month and two days.
- 8:15And I was kind of looking forward to watching all these on YouTube and they're not out yet.
- 8:19So how about we have a podcast episode about it and share that with everyone?
- 8:24So I'm looking forward to kind of doing the audio conversational version of your talk.
- 8:29How did your talk go there?
- 8:30I thought it went great.
- 8:31There was pretty good response.
- 8:32One of the things I did, which I would recommend for other conference attendees and speakers in the future,
- 8:38is whenever anybody came up to me and said, great talk, which is very nice.
- 8:43I appreciate it when people do that.
- 8:44I didn't just say thanks.
- 8:46I said, thank you.
- 8:47And how is this going to affect your behavior going forward?
- 8:50What did you learn from the talk that's going to change what you do?
- 8:53And a lot of people had specific things that they were going to change about their workflows or their kind of personal security.
- 8:59And so that was telling me, like, I think this is an important message to get out there.
- 9:02And I think that it's something that every developer should know about.
- 9:05So the fact that people were, like, really learning from it, I thought, was the best metric for success.
- 9:10It's got a lot of small lessons for many people.
- 9:13And I suspect most people are not doing all of these things.
- 9:17So even if you're like, yes, of course, I keep, you know, I don't put secrets into my source code.
- 9:22Or do you?
- 9:23But there's something else.
- 9:24I'm sure there's something for everyone here.
- 9:26So let's kick things off by talking about what you mean by secrets.
- 9:31All right.
- 9:31What are we talking about here specifically?
- 9:33So the main kind of secret that I'm talking about is a token type of secret.
- 9:38Whenever you use an API on the web, they'll give you, like, a little hex key, usually a short string that lets you access it.
- 9:47Cryptographic secrets, like if you have some encrypted blob that's stored in block storage, you might have crypto key that's a similar kind of thing.
- 9:56For your HTTPS certificates, you've got, like, you know, private RSA key or something or ECTSA key that's a little bit longer, but still kind of text file that's no more than 20 lines.
- 10:07And so all of those secrets are kind of interesting.
- 10:10And I wanted to talk about them in particular because the way that security works just on computers generally is that all of your other security, all of the other bigger secrets you want to keep, the confidential documents, you know, media files or whatever it is that you're securing, your database, your user accounts, all of that stuff is secured by a smaller secret.
- 10:33And so these secrets generally represent some kind of authority, like a thing that you can do in the cloud.
- 10:40Even cryptographic ones are kind of like the authority to decrypt another secret.
- 10:44And so that's, in particular, the reason that restriction of the scope of this discussion to those smaller secrets is interesting is because those smaller secrets don't have, like, a lot of interesting storage requirements.
- 10:56Like, they just, they're very small, and it's really about how you secure them, not so much about how you manage, like, a huge scale of them.
- 11:03Yeah, also, if you're going to, say, encrypt huge blobs of data, usually what you actually need to get back to it is just the encryption key, right?
- 11:13Which is often pretty small, right?
- 11:14So even if the actual data is large, usually the thing that is the access to it is also small.
- 11:21Right.
- 11:21What you said you were not covering?
- 11:23Into encryption and that kind of stuff, right?
- 11:25There's a lot of other security stuff that you need to do.
- 11:28Like, I touched briefly in the talk on disk encryption.
- 11:31We can talk about that in a little bit, but it's mostly not about that.
- 11:35In particular, like, there's been a lot of discussion lately about end-to-end encrypted messaging between people, and I'm not really talking about that either.
- 11:43This is really about securing your applications.
- 11:45It's, it's, a lot of these techniques have to do with how you get your secrets, not just secured for you, but, like, into your Python code in a way which keeps them secure, kind of on the whole journey from typing them in to using them in a production application.
- 12:01For sure.
- 12:01For sure.
- 12:01I, I would just, as you're talking, I'm thinking, this problem is only getting worse, right?
- 12:05We're only consuming more APIs in our code, and we're only stitching together more back ends of cloud systems, right?
- 12:13Yeah.
- 12:14We've been writing code for a while.
- 12:15I have too.
- 12:16Like, it used to be, well, what's your database connection streak?
- 12:18All right, we're good.
- 12:18We're done.
- 12:19That's what you store, right?
- 12:20How do you store that?
- 12:21Well, and if you were doing something really esoteric, you would have, like, a Twilio API key, so you could send, like, SMS messages.
- 12:27And now you've got, like, your billing provider and your database provider and your cloud and your user, you know, your customer relationship management API and your email service.
- 12:38It's just, like, there's an API key for everything, and the scale of this stuff is, like, really, if you do this sort of simple thing that all the tutorials show you to do for each of these services, you end up in a complete mess with, like, basically no security on anything.
- 12:51So, yeah, and that's why I wanted to talk about this at this point, because we need that security.
- 12:57Yeah, we definitely do.
- 12:58So I want to start out a little bit like you did with your talk, although not the same.
- 13:04I want to start out with sort of something that, you know, maybe gets people's attention that might make them worry a little.
- 13:11And that's this project that has been gone unmaintained and has been shut down, I think, because it was a little too much on the edge of what really you should be doing.
- 13:21And it's called Shugit.
- 13:23Shugit is a CLI thing.
- 13:25I'm not exactly sure.
- 13:26Basically, a little app you can run, a little CLI that you can run that will show you secrets in Git code, in code and Git repositories.
- 13:34So you can look at your own, but for a while they were running this website called, like, Shugit.com or something, like, Shugit.
- 13:42And it was just a live stream of just all the secrets that were being committed to public repositories just screaming by.
- 13:50And it's going by so fast that you really can't read it.
- 13:54And this is just the public.
- 13:56Here's all the things being uploaded.
- 13:59These are all these API keys, database connection strings, etc. that we've been talking about that are just, you shouldn't be doing an, oh my gosh, look at this, right?
- 14:07Like, this has got to grab people's attention.
- 14:09Right.
- 14:09And this isn't, it's worth noting that this feed is not people accidentally committing their secrets to the improper kind of secure place in their organization where they, like, accidentally push to a private Git repo.
- 14:22This is only showing you public repos with API, like, valid API keys being committed to them.
- 14:28So, if, you know, you can imagine within organizations, there's probably 10 times as much traffic where stuff is being shared too broadly.
- 14:35Oh, yeah.
- 14:36People at least know they shouldn't do this and they still are.
- 14:38Like, they might think it's okay in their private repos, right?
- 14:42Right, exactly.
- 14:42It's just easier.
- 14:43Yeah, yeah, yeah.
- 14:44This portion of Talk Python To Me is brought to you by JetBrains, who encourage you to get work done with PyCharm.
- 14:53PyCharm Professional is the complete IDE that supports all major Python workflows, including full stack development.
- 15:00That's front-end JavaScript, Python backend, and data support, as well as data science workflows with Jupyter.
- 15:06PyCharm just works out of the box.
- 15:09Some editors provide their functionality through piecemeal add-ins that you put together from a variety of sources.
- 15:16PyCharm is ready to go from minute one.
- 15:18And PyCharm thrives on complexity.
- 15:21The biggest selling point for me personally is that PyCharm understands the code structure of my entire project,
- 15:28even across languages such as Python and SQL and HTML.
- 15:32If you see your editor completing statements just because the word appears elsewhere in the file,
- 15:37but it's not actually relevant to that code block, that should make you really nervous.
- 15:42I've been a happy paying customer of PyCharm for years.
- 15:45Hardly a workday passes that I'm not deep inside PyCharm working on projects here at Talk Python.
- 15:52What tool is more important to your productivity than your code editor?
- 15:56You deserve one that works the best.
- 15:58So download your free trial of PyCharm Professional today at talkpython.fm/done with PyCharm and get work done.
- 16:07That link is in your podcast player show notes.
- 16:09Thank you to PyCharm from JetBrains for sponsoring the show and keeping Talk Python going strong.
- 16:18You also pointed out right before we hit record that GitHub has a more constructive, official way of doing this as well.
- 16:26You want to tell people about the GitHub secret alerts?
- 16:29Yeah, I don't know a whole lot about this, except that it is a feed that also produces a lot of data.
- 16:36But there are secret scanning alerts.
- 16:39And I believe it's also only for public repositories.
- 16:42There might be some enterprise stuff that I have not used.
- 16:45As it says on the page, I guess, organizations using GitHub Enterprise Cloud with a license for GitHub Advanced Security can also enable secret scanning for users on their private and internal repositories.
- 16:57But yeah, this is something that if you're running an open source project or you have a corporate GitHub account,
- 17:02you should probably look into just to understand how to manage these alerts, how to get them, and what to do if people are accidentally checking in secrets to your repository.
- 17:12Because you may have to worry about open source PRs, including other people's API keys.
- 17:18If they were testing something out with your tool.
- 17:20Right.
- 17:20That's more likely even as you might have a placeholder.
- 17:23Enter API key here.
- 17:24Don't commit it.
- 17:26And someone commits it in a PR, right?
- 17:27Something like that.
- 17:28Yeah.
- 17:29Yeah.
- 17:29So don't do that.
- 17:30Don't put a place to paste something in a file that's actually checked into Git.
- 17:33But I think that it's also important not to shame people too much for doing this.
- 17:38One of the points of showcasing these tools is to say, hey, people are leaking secrets constantly.
- 17:45You should be aware of it.
- 17:46It's a serious problem.
- 17:47The other is people are doing this constantly.
- 17:49It's a really easy kind of mistake to make.
- 17:52And so if you do it, or if some junior engineer on your team pushes some secrets to GitHub, don't shame them.
- 17:58It's not a dumb thing to do.
- 17:59It's incredibly easy to screw up in this way.
- 18:01And we should help each other build better processes.
- 18:04So don't feel like it's a thing to mock or laugh at.
- 18:09So you're not suggesting that GitHub adopts a new kind of badge, like a secret teller badge or something, if you're caught doing this?
- 18:16I think let's not gamify that in either direction.
- 18:18It's a hard enough conversation already.
- 18:22It is.
- 18:23Well, and it can happen even if you know you shouldn't.
- 18:25Maybe you understand the security, but you're not good with Git, and you accidentally overcommit stuff.
- 18:32And you're like, oh, whoops.
- 18:34Yep, that's a very common way that it can happen.
- 18:36And even if you are very familiar with Git, and you know not to do this, and all of the projects that you've set up are set up properly,
- 18:44you might find yourself in an unfamiliar repository, assuming that something is Git ignored, and it's not.
- 18:49There's lots of different ways that it can happen.
- 18:50Yeah.
- 18:51So I guess if you're running an open source project, it's probably worth turning on, or at least looking into the secret scanning that GitHub has, if it's on GitHub, right?
- 19:00Yeah.
- 19:01And if you want to startle yourself, just check out the link, the animated GIF in the Shiget repository.
- 19:06All right.
- 19:07So as we start our conversation about the different secrets, the pitfalls, and how we might keep them, you told, this was a really creative presentation at PyCon, and you presented it through by way, mostly by way of storytelling.
- 19:23Well, thank you for saying so.
- 19:25Yes, of course.
- 19:25Was it Gerald who was your?
- 19:27Jethro.
- 19:28Jethro.
- 19:28Jethro is the protagonist.
- 19:30Yes, got it.
- 19:31So there's this practice in security engineering, which I'm sure a lot of your listeners would have heard of, which is called threat modeling.
- 19:38And threat modeling can be a bit dry, because it's really just repeating kind of these same scenarios over and over and over again, with slight things changed.
- 19:46And so I thought a good way to frame that would be like a time loop story, where our protagonist is kind of running this service.
- 19:52And every time through the time loop, his service gets pwned and taken down via some kind of compromise of secrets.
- 20:00And he wakes up and he wakes up and has to do it all again and try to secure things properly.
- 20:04Yes, indeed.
- 20:06That's where we get started.
- 20:08Yeah, for sure.
- 20:09Yeah.
- 20:09And one of the things I also kind of called out is like, I should mention this here, too.
- 20:12It could be a little bit stressful listening to all these different things.
- 20:16So this isn't exactly like a content warning, like a really serious, heavy stuff.
- 20:20But be aware that even if it sounds kind of light and fun and informative, if you find yourself ideating on these same ideas of like, oh, no, this could go wrong, this could go wrong, this could go wrong.
- 20:31That is kind of just what security engineering is like.
- 20:33But also, it can kind of drive you crazy.
- 20:36So like, don't let yourself get trapped in cycles of anxiety.
- 20:39Just be aware that, you know, you kind of have to do your best and like make a reasonable effort, but you're never going to get everything perfect.
- 20:46So and in fact, I'll spoil the story a little bit since we're not quite going through it in order here.
- 20:51That's really the twist at the end of the talk is that Jethro needs to like just put forward a good effort across the board for managing his secrets and not, you know, absolutely perfectly defend against every attack, but also not to defend against each attack piecemeal, kind of onesie twosie, just like, oh, I'll address this thing.
- 21:09And then I'll address that thing.
- 21:09And then I'll address that thing.
- 21:11It's like, think about your whole process.
- 21:13So, yeah.
- 21:15So I guess we should dive into what those actual specific attacks are.
- 21:19Before we do that, one thing that I thought was really nice that you pointed out is, at least for me, when I feel like I'm putting a website up there, I feel like, oh, my goodness.
- 21:29Have I what have I forgotten?
- 21:31Right.
- 21:32But there's you point out that there's a lot of people on your side.
- 21:36Right.
- 21:36There's operating system vendors, the open source maintainers.
- 21:40You want to riff on that a bit?
- 21:42This is also important to remember because so, for example, just to like some one attack that's been in the news a lot lately is typosquatting and software supply chain attacks.
- 21:54Right.
- 21:54Like somebody uploading something malicious to PyPI, somebody uploading, you know, pad NAS instead of pandas.
- 22:00And you fat finger the name in pip and then, oh, no, you're like running code from some attacker.
- 22:06And that is a threat.
- 22:08And, you know, you should be concerned about it.
- 22:10You should mitigate it in your code.
- 22:11All of the different ways to do that are like a little bit out of scope for this discussion.
- 22:15But also it's important to remember that a lot of these attacks are things that it's hard to demonstrate in like a quick, like circa 2007 type sniffing your Facebook password off the wire.
- 22:27Like, hey, look at this like immediate attack where I've got all of your stuff.
- 22:31And they're difficult to demonstrate because PyPI is actively scanning for these types of packages.
- 22:39They're actively responding to reports.
- 22:41There are lots of people.
- 22:42There are lots of security researchers developing tools, some of them kind of over aggressively running them against the index to taking up a little too much, too many resources.
- 22:51But there are lots of people doing things to defend you against each one of these attacks.
- 22:56And so the simplistic version of them will rarely work out of the box.
- 23:00The threat from typosquatting is a race condition.
- 23:04It's that somebody uploads a malicious package that doesn't look malicious enough to get automatically caught in these various ways.
- 23:11That doesn't get immediately flagged.
- 23:13It's not popular enough to like be installed on a lot of people's machines.
- 23:17And that you happen to do the typo in the window where it's still up before the admins have taken it down.
- 23:23That is a real threat, but it is not as serious as like, oh, it's just a free for all.
- 23:29Anybody can upload anything anywhere.
- 23:30Like there's a lot of security.
- 23:32There's the, not just the PyPI admins, but the distributors of all of the libraries on PyPI that you use are developing better processes on the teams that do uploading.
- 23:41PyPI now has orgs.
- 23:43And so we're doing like projects are starting to gather into organizations to apply more consistent security rules for uploading across the board.
- 23:50So there's lots of work that's going on.
- 23:52I happen to know a lot about PyPI, but your operating system vendor also is like, yeah, you could run some malware and that would really ruin your day.
- 24:01But Apple, Microsoft, Google, Red Hat, none of them want their operating system to be implicated in the attack.
- 24:08So they're constantly hardening their applications, hardening their APIs, developing secure APIs that developers can use to prevent certain attacks.
- 24:17Different platforms are kind of at different stages of maturity there and have kind of different security features, but they're all trying their best to prevent the worst case from happening.
- 24:26And so you're not like alone in this fight.
- 24:28You need to model your own threats and use the tools that you've been provided in ways that are kind of helpful, but you don't have to do everything yourself.
- 24:36You don't have to worry that you're kind of besieged with, and you're the only defender, right?
- 24:41You're actually in a pretty big crowd of defenders, and we all got to share knowledge with each other to try to help each other out.
- 24:47That's interesting.
- 24:47You know, we are, you know, there's a big concern about supply chain attacks and, you know, open source building blocks.
- 24:54But it is a bit of a herd immunity type of thing, right?
- 24:59Yeah.
- 24:59If something comes in pretty soon, if it at all starts to gain any popularity, people are going to notice, and it'll get beat down.
- 25:07So it's scary, but it's maybe not as likely to broadly affect the ecosystem.
- 25:13Right.
- 25:14And that doesn't mean that you can give up on this work or put it off as some kind of gold plating that you're going to do later.
- 25:21But it does mean that you can kind of moderate the amount of energy that you put in.
- 25:25And the way that you should think about it as an individual, as an app developer, is like, you should be trying to figure out what your responsibility is and handle your part of the responsibility.
- 25:35Don't try to, like, you know, think about every threat and become a vulnerability researcher in order to secure your application.
- 25:43Like, let those people, there's a lot of people doing that stuff.
- 25:45Let them do their job and just kind of figure out, okay, what do you need to do?
- 25:48Because obviously, no matter how great the defenses are, if you don't use them, if you're not properly secure in your own processes, then it doesn't matter.
- 25:57Like, you can easily, if you're pushing your, you know, root cloud keys to GitHub in a public repo, all of the security of the world is not going to help you.
- 26:05Yeah, it's not going to be great.
- 26:07You said there's even a PEP to help solve this, PEP 541.
- 26:11What is PEP 541?
- 26:12541 is actually a little bit broader than just, like, typo squatting.
- 26:16It's about how PyPI, like, retains names, how they deal with trademark dispute resolution.
- 26:22You know, it outlines some of the responsibilities of package maintainers.
- 26:27So, for example, like, one of the typo squatting or one of the attacks against supply chains is if you use a vanity domain name for your PyPI account, and then you let the domain name lapse, and then somebody, like, registers the domain name, now they have access to your account because they can do a password reset.
- 26:43And so there are various responsibilities that you have, like, you have to be reachable.
- 26:49If your project has been abandoned, the index might take it away, right?
- 26:52Like, they might shut it down.
- 26:53They might freeze it.
- 26:55And there are this PEP kind of lays out a bunch of processes for various things.
- 27:00And one of them is, you know, how things get taken down, how projects become invalid, what a typo squat is.
- 27:08And so it's an even broader project than just the security of typo squatting or even broader than supply chain security.
- 27:15It's, like, the whole supply chain shebang.
- 27:18And there will probably be more PEPs in this area in the future, I think.
- 27:21Yeah, I would imagine.
- 27:23Yeah, so, for example, if I had Michael.com and I registered, my account was Michael or me at Michael.com, and then I didn't, that went away.
- 27:34Someone else could buy Michael.com for who knows how much and then do a password reset, and they control all of my super important open source projects on there.
- 27:43Yeah, and then they can immediately do uploads, which is kind of a problem.
- 27:46Right.
- 27:46That might not be awesome.
- 27:48Okay, so that's PEP 541.
- 27:51Got it.
- 27:51So it was all about threat modeling, right?
- 27:53And you basically come through and you say, okay, well, first we're going to write some code, and then let's just push it to the cloud.
- 27:58And to the cloud, it says.
- 28:01Yeah.
- 28:02Uh-oh.
- 28:04You can't run your website, right?
- 28:06So the first thing you got to talk about when you talk about secrets is human-generated secrets.
- 28:11The ones, like your passwords that you actually have to remember and type in with your fingers, don't ever share those.
- 28:18If you're sharing passwords between accounts, if you've got like a LinkedIn account that has been around for a decade, that's probably been in a breach.
- 28:27Even if it hasn't been in a breach.
- 28:29Oh, for those who are not on the live stream, Michael is now typing, have I been pwned?
- 28:35Which is a great tool that you can use to see if your passwords have been in a previous breach.
- 28:41And spoiler alert, yes, they have.
- 28:43So the first lesson is like you should be using a password manager because that's the kind of absolute root of your security.
- 28:51You have your kind of vault password for your password manager.
- 28:56You need to be able to remember that.
- 28:58You need to generate that securely.
- 29:00And then you should let your password manager actually generate all of the passwords for all of the different sites and apps that you use.
- 29:07Because you don't want to be sharing those between them.
- 29:09Because inevitably, if you share a password between 10 sites, one of them will get breached, like for sure.
- 29:17And then all of the others can be breached later by anybody who can buy that data dump on the dark web or now access that information.
- 29:26So I actually wrote like one of the tools that I released during this talk at PyCon is called PinPal, which is kind of to help people adopt a password manager.
- 29:37One of the challenging things about adopting a password manager, especially if you haven't got one already, is you really need to generate like a long, secure password.
- 29:46And you have to memorize it.
- 29:48And you really can't forget that.
- 29:51And this is something that happened to Jethro.
- 29:53You have to think about not just confidentiality, but there's this thing called the CIA triad where you've got confidentiality, integrity, and availability of your service.
- 30:03And if you get hacked and people steal your secrets, then they can access your cloud and it's game over.
- 30:09But if you forget all of your passwords and you can't access your cloud, it's just as equally game over.
- 30:15So you have to like do that memorization.
- 30:17You should ideally also change your root password once every couple of years.
- 30:21You shouldn't change it too frequently, like policies that make you rotate every 90 days or whatever are just a kind of relic of a bygone time and not a very good idea.
- 30:29But you shouldn't keep it forever.
- 30:31And that process can be difficult because you need to memorize it.
- 30:35And because password managers try to be convenient, they're not prompting you for it all the time.
- 30:40So PinPal is this thing that kind of like sets up a spaced repetition process where it's like gradually generating the password over time.
- 30:48It just kind of prompts you over and over for like, here's the first word.
- 30:53It generates it.
- 30:54It asks you to kind of read it back to it.
- 30:55It's a little console tool that you like put in your prompt to see if it's time to do a memorization pass.
- 31:01But you can use PinPal, memorize the password before you get the password manager.
- 31:05Make sure you've got it thoroughly memorized.
- 31:08And then you don't have to worry so much about like, oh, where was that little piece of paper that I put in my wallet that has the password in it?
- 31:13Because like it's been a week and now I have to enter my master password again.
- 31:16PinPal can kind of get that part of the inconvenience of adopting a password manager in the first place out of the way.
- 31:23And so I've made it as easy as I possibly can for you.
- 31:26I know that that's the one thing that like gets people stuck.
- 31:28And so now you have no excuse.
- 31:30You got to get a password manager right now.
- 31:31I absolutely, totally recommend a password manager.
- 31:34And I personally use one password, but I'm also a big fan of Bitwarden.
- 31:39You know, props to the open source side of Bitwarden.
- 31:41That's pretty cool.
- 31:42And the pricing for the pro version is like a couple dollars a year or something ridiculously small.
- 31:48So it's, they're good options.
- 31:49Yeah, Dashlane is also pretty good.
- 31:52Full disclosure, that was actually started by my cousin in France, oddly enough.
- 31:56No kidding.
- 31:56Yeah, it's a little weird aside there.
- 31:59I don't believe he works there anymore.
- 32:01So those options are all good.
- 32:02I would avoid LastPass.
- 32:05If you've been using LastPass, that's fine.
- 32:07It's still way better that like, again, it can be easy to catastrophize security.
- 32:12Like LastPass had a bunch of breaches and the breaches were like worse than they said.
- 32:18And their security, like they used a lower iterations on their key derivation function than they should have.
- 32:23And so there's all these issues with LastPass.
- 32:25But like, if you had been using LastPass when they got breached, you're still in way better shape than if you had been trying to like memorize.
- 32:34Just reusing your passwords.
- 32:36Yeah.
- 32:36Yeah.
- 32:37One of the big, I think, big no-nos of LastPass, other than also the low iteration, the folding account,
- 32:45was that a lot of stuff is stored in plain text.
- 32:49So like the URL of the place for which the password goes to is stored in plain text.
- 32:54So that the hackers could go through the dump and go, okay, well, just show me all the Bank of America ones.
- 33:00Let's just work on that for a while.
- 33:01Right?
- 33:02Yeah.
- 33:03They definitely had an architecture that was like bad for no reason.
- 33:07Like there's no reason that they should, that stuff all should be.
- 33:09I totally agree.
- 33:10So yeah, people can choose LastPass if they want, but there's Bitward and 1Password.
- 33:15That's where my heart loves it.
- 33:19This portion of Talk Python To Me is sponsored by the Compiler podcast from Red Hat.
- 33:23Just like you, I'm a big fan of podcasts, and I'm happy to share a new one from a highly respected open source company.
- 33:29Compiler, an original podcast from Red Hat.
- 33:32Do you want to stay on top of tech without dedicating tons of time to it?
- 33:35Compiler presents perspectives, topics, and insights from the tech industry, free from jargon and judgment.
- 33:41They want to discover where technology is headed beyond the headlines and create a place for new IT professionals to learn, grow, and thrive.
- 33:48Compiler helps people break through the barriers and challenges turning code into community at all levels of the enterprise.
- 33:54One recent and interesting episode is there, The Great Stack Debate.
- 33:59I love, love, love talking to people about how they architect their code, the trade-offs and conventions they chose, and the costs, challenges, and smiles that result.
- 34:07This Great Stack Debate episode is like that.
- 34:10Check it out and see if software is more like an onion, or more like lasagna, or maybe even more complicated than that.
- 34:17It's the first episode in Compiler's series on software stacks.
- 34:20Learn more about Compiler at talkpython.fm/compiler.
- 34:24The link is in your podcast player show notes.
- 34:26And yes, you could just go search for Compiler and subscribe to it, but follow that link and click on your player's icon to add it.
- 34:34That way they know you came from us.
- 34:36Our thanks to the Compiler podcast for keeping this podcast going strong.
- 34:43I did have one of Have I Been Pwned up.
- 34:45And I would just like to point out, the question is, have I possibly, right?
- 34:48I can tell you, it has a list of most recent breaches.
- 34:52This Luxottica one here was 77 million.
- 34:56I just got a notification that I was in that one.
- 34:58That's like literally the most recent one.
- 35:01And here's what drives me nuts about these things, this bit of a sidebar.
- 35:05So nothing you can do about it, but it's maybe about the, you give it your best shot and you can just only do so much, Cliff.
- 35:11And I am in this, I'm in this breach because I went to my eye doctor and my health provider.
- 35:18They subcontracted to Luxottica to get my glasses created.
- 35:23Luxottica was hacked and therefore my information, I'd never even heard of this company or interacted with them.
- 35:30And yet here we are, right?
- 35:32And so, you know, do your best, but don't go crazy.
- 35:35Yep.
- 35:35Okay.
- 35:36So I guess you have more control over, right?
- 35:39But I guess one of your very first recommendations was use a pass manager, use something like PenPal to help not forget your password to your password.
- 35:48And sort of the lesson is the CIA triad, like availability is also important, not just the other two security bits.
- 35:54But if you can't get to it anymore, that's still a problem.
- 35:56Yep.
- 35:57Don't be your own worst attacker.
- 35:58Exactly.
- 36:00So, yeah.
- 36:01And I guess that one of the other major things I wanted to talk about here, part of the story is that the documentation.
- 36:07So Jethro follows the docs on a lot of different things that he's looking at.
- 36:13And a lot of those docs, and this is kind of the inspiration for the talk in the first place, was that many, many APIs are documented by saying, here's some code.
- 36:23And here's where you paste the string of your API key.
- 36:26And big companies do this.
- 36:28Stripe does this.
- 36:30DigitalOcean does this.
- 36:31It's not an unusual thing.
- 36:34It's not a thing that only kind of low security, uninteresting APIs do.
- 36:38The idea is they're writing the docs, assuming that you kind of have some secret management strategy to begin with, that you know your API token is really sensitive.
- 36:46So obviously you won't follow their example directly.
- 36:49But like, as seen from the GitHub stream of, you know, constant secrets being pushed to public repos, a lot of people don't know that.
- 36:59And there shouldn't be any shame in not knowing that.
- 37:01Like, if the docs say, put this string here, and you're like, okay, I'm going to put that string there.
- 37:05There's no way that you could have, like, somehow known in advance, if you're a novice coder, that, oh, if you put that string there, like they said, then all of your, you know, money will be stolen.
- 37:16And your servers, attackers boot thousands of servers to mine cryptocurrency.
- 37:20So one of the things that the calls to action here is if you're maintaining those docs, if you're leading people down that path, please at least mention that this token is extremely sensitive.
- 37:31At least explain that people should be looking somewhere else for documentation of what to do.
- 37:38And like in Python, it's actually quite easy to know what to do.
- 37:42It's use key ring, like exactly how you configure key ring and what you do with it.
- 37:46We can talk about in a minute or two.
- 37:48But from the perspective of like an application loading something, from the example, from the perspective of like a code example that you want to put on a website, just never tell users to put a string directly in the code.
- 38:00The string in your code is obviously never the user's like actual API key.
- 38:05They're going to have to edit it anyway.
- 38:06So in the example code, just import key ring into a key ring.get password.
- 38:10And they can replace that with their key as easily as they can replace a string.
- 38:15But at least you're calling out there, oh, hey, this is like a piece of data that has to be managed in a particular way.
- 38:21Here's how you manage it in Python is you call the key ring get password API.
- 38:26So the key ring module is like very simple.
- 38:29It's basically just a wrapper around a function that returns a string.
- 38:33But by centralizing that function, all of the myriad nuances of what exactly, like how exactly to get that string, how is it encrypted, where is it stored, whatever, can be kind of pushed out of the application.
- 38:46Yeah, so let's talk about key ring a little bit.
- 38:48It's a Python library that you can use.
- 38:51And you can say, basically, save a secret or get a secret.
- 38:55It calls it a password.
- 38:57I kind of would like it to not be password, because what if it's an API token for that?
- 39:02I don't know.
- 39:02It seems a little, what if it's a connection string?
- 39:04I don't know.
- 39:05Yeah, it's a little bit like, it was definitely written quickly and saw just a crazy amount of adoption.
- 39:11And, you know, we're kind of just stuck with that naming now.
- 39:13I agree that it's not the best, but it's, you know, it's fine.
- 39:17Any secret token that you treat this way is kind of functionally a password.
- 39:22So the terminology is a little confusing, but it's fine.
- 39:25Yeah, so regardless of whether it's an API key or a password or a connection string, you can say keyring.setpassword or keyring.getpassword.
- 39:34And that's pretty much the API, right?
- 39:36Yeah, and the nice thing about it is that there can be a lot of complexity in how it's configured.
- 39:40But the default, if you just like pip install keyring and you start using it on a desktop machine, Windows, Linux, macOS, doesn't matter.
- 39:48It, by default, will use your platform's default secret store, which is kind of the best that you can do without any additional configuration.
- 39:59And ironically enough, for the talk, I really wanted to talk about, like, how this works on every platform.
- 40:04I kind of figured that every platform was, like, equivalently secure.
- 40:08I kind of hate being a shill for a, you know, trillion dollar corporation.
- 40:13But, like, Apple does this so much better than either of the other major desktop platforms because on the Mac you've got, like, intra-user security boundaries.
- 40:24So, like, your, there's, apps are generally sandboxed on the Mac if they come from the App Store or if they've opted in.
- 40:30Even if they're not sandboxed, there's a lot of things that they can't do.
- 40:33Like, if you, you may have noticed if you use a Mac, if you try to, like, run a Python program on, like, a file on your desktop or in your documents folder, it'll, like, pop up a dialogue and say, like, hey, this program's trying to access this thing.
- 40:45Secrets are something like that where when an app tries to access a secret, you have to not just say yes, but you have to, like, enter your account, your login password again.
- 40:56And you can choose to either allow it temporarily or permanently.
- 40:59And you can then go in and configure it to say, like, oh, this, like, I know I said that I, this app could access its own secrets, but, like, actually, I want to be prompted every time.
- 41:08So, like, for example, my PyPI keys, if I use Twine or Flit to upload a package to PyPI, I always get a password prompt because my PyPI token is not stored anywhere on my file system.
- 41:19The key that's stored in my Mac keyring is, like, locked.
- 41:23It's not allowed to be automatically accessed by anything.
- 41:26Right.
- 41:26And that adds a little bit of friction, but for something like PyPI package uploads, like, you want to have that friction there.
- 41:31You don't, it's not very frequently used.
- 41:33Now, on Windows and Linux, you still get, like, some protection against offline attacks.
- 41:39You get protection against other users.
- 41:40Like, if you share a computer or you're on a Windows, like, you know, a corporate laptop where things are executed under, like, administrative tasks are executed by your company under different users.
- 41:52It'll be protected from that.
- 41:54It'll be protected when you're not locked in.
- 41:55And on Linux, oddly enough, you can lock your credential locker, but, like, only the whole thing.
- 42:00And then anytime any app needs to do anything, it's like, hey, do you want to unlock this again?
- 42:05And so, like, your mail client will be checking your mail, and it'll constantly be saying, like, hey, do you want to unlock this?
- 42:10But nevertheless, it is still locked in a way that, like, if you step away from your computer, if the screensaver is on, I think it will generally kind of lock it for you.
- 42:19So it provides as much protection as it can, given the way the platform works, without any additional inconvenience.
- 42:27Like, if you use Keyring and you just kind of use it in the default mode, you get a bunch of extra security, and it's all for free.
- 42:33Like, you don't have to think about it.
- 42:34It'll just work.
- 42:36I'm pretty comfortable just putting true secrets into macOS Keychain, for example.
- 42:42I feel like that's a pretty safe, probably better than a source file, a text file.
- 42:47Yes, it's definitely better than a text file.
- 42:49And it's better than a text.
- 42:50I mean, it's way better than a text file in your Git repo.
- 42:52It's still much better than a text file, like, in your home directory.
- 42:57Like, a lot of the less irresponsible code documentation tends to tell you to, like, put your keys into, like, dot this app dot CFG in your home directory or something.
- 43:09And that's, like, kind of okay.
- 43:12Like, any software that can steal stuff out of your home directory has probably already, you know, got a lot of control over your computer.
- 43:21But it's still better to put it in Keyring if you can.
- 43:25Right.
- 43:25Well, my assumption about most malware type of things is it's going to be 100% automated.
- 43:32I imagine there's very few.
- 43:34I'm just going to remotely log into each computer and go hunting for secrets.
- 43:37More like it's going to look for well-known things.
- 43:40Much like if you tail the log of your website, you're going to see some things trying to access slash WP admin dot PHP.
- 43:47Even if it's not a PHP site, like, they just got the things they look for and they're going to go through their processes to gather secrets and access.
- 43:54And I guess the less standard, the less obvious it is to get a hold of it, probably the better.
- 43:59It's easy to kind of trick yourself into thinking, like, oh, I should just put everything in, like, really weird locations and, like, the automation won't find it.
- 44:07Quite often the automation, like, does kind of scrape through every file it can get access to because it doesn't cost them anything.
- 44:13They can just, like, read every file.
- 44:14It's your processor.
- 44:15It's your processing.
- 44:16Maybe just run shigit on every git repo I can find on your computer.
- 44:19Right.
- 44:21So it's better to kind of, like, put all your eggs in one basket and really watch that basket.
- 44:25And that's what the keyring approach is.
- 44:27But it also, like, beyond just the, you know, protection against malware, there's also a kind of level of management that you can do.
- 44:35It puts, by using your platform's default keyring or encrypted store, there are platform-specific APIs for, like,
- 44:44enumerating secrets.
- 44:45And so if you're trying to do an inventory of, like, what secrets are on my computer, having them in there is nice because you don't have to go hunting around for them.
- 44:52It gives you, as a defender, a little bit of an advantage because you know what you're defending against.
- 44:56You know, oh, hey, this secret shouldn't be on this computer.
- 44:59This should really be, like, only, you know, on this one server over here.
- 45:03So using keyring also gets you that.
- 45:05It also gets you this configurability because, like, once you actually put your code into production in the cloud, it's not going to have access to your local, you know, keyring on your Mac.
- 45:14It's going to be a certain deadlock of your app if it pops up.
- 45:18Can this website access the keychain?
- 45:20You know, use Touch ID to allow.
- 45:22It's probably not going to work in production.
- 45:23Right.
- 45:24So you have to use an alternate keyring backend on your server.
- 45:29And that is a little bit awkward.
- 45:30And that sometimes turns people off and they're just like, ah, I'll just use a config file.
- 45:33But there are ways to configure, like, and they're actually documented, like, right on the keyring webpage.
- 45:39Like, you can, it talks about keyrings.crypt file, which is this, like, extra backend that you can get that stores stuff in an encrypted file.
- 45:47Keyrings.alt includes, like, just a plain text file.
- 45:51If you know that on your server you have, like, a secure place to put a text file because the server is going to need to be accessing the secret over and over again, you can just say, okay, I'm going to configure the backend as part of this Docker container.
- 46:03Like, it's just going to read this file from this volume and that's the keyring backend I'm using.
- 46:08But again, that's all configuration.
- 46:10You write your app to just use keyring.
- 46:13And then after the fact, when you're setting up your deployments, you can select what the most appropriate backend is.
- 46:20And in fact, one of the things I talked about during the talk was as I was trying to find, like, the super secure way to, like, disable access to secrets on Windows and Linux, and I didn't find it, I actually wrote my own keyring backend to demonstrate how you would do this.
- 46:33So, when you're doing this, like, you're doing this, like, you're doing this, like, you're doing this.
- 46:47It works on macOS, it works on Windows, and it requires you to be seated at your computer with your YubiKey plugged in in order to use this, like, keyring backend.
- 46:58So, by configuring...
- 47:00When your Python code hits keyring.getpassword, the next thing that has to happen before that returns, you have to attach the YubiKey.
- 47:07And this is also a good example of how, like, this was a cool project.
- 47:10It was fun to play around with that API.
- 47:13I had some smarter security people than me look at it to make sure I wasn't releasing something super unsafe, just for a laugh.
- 47:19So, I think it is definitely usable.
- 47:21But it's also an example of a keyring backend that you would not always want to use, right?
- 47:26Like, this is for your super high security secrets.
- 47:29It doesn't even have support for multiple YubiKeys.
- 47:32So, again, you've got to think about availability.
- 47:34Like, anything you put into this, you probably want to back up somewhere else.
- 47:37But it is a relatively convenient thing.
- 47:40You just have to kind of touch it with your finger to unlock it.
- 47:43But it's also really, really secure.
- 47:45So, like, some of your secrets you might want to put into this backend to make sure that there's no way for software to access them automatically.
- 47:52There's no way that malware running on your computer is going to be able to just, like, hoover them up.
- 47:57Anytime you see a prompt for an authenticator touch that you weren't expecting, that's like a real eyebrow raise moment.
- 48:05And so, but, you know, Token Ring is just kind of my example of one of these things.
- 48:11There are dozens of other keyring backends that you can use for either different levels of security on your own machine.
- 48:17Although, honestly, the platform encrypted stores are generally pretty good, like, for a default.
- 48:23But you can also use alternate backends for different cloud secret providers.
- 48:27Like, I believe HVAC, which is a HashiCorp vault backend.
- 48:32I haven't actually used HVAC, so I'm not totally sure how it works.
- 48:35But I believe it actually has a keyring backend, which is only one, like, tiny part of what it does.
- 48:41But it can turn a vault into a keyring for you, I think.
- 48:46If it can't, then the adapter to do so would be very, you know, trivial to write.
- 48:50But that's the idea, is that you write your code to use keyring, and then wherever you deploy it, you can figure it to use the keyring that's appropriate to that context.
- 48:57All right, right.
- 48:57One of the interesting ones is the Bitwarden backend, which is kind of nice.
- 49:02Right.
- 49:02I was actually a little disappointed because I'm a big 1Password fan.
- 49:04I know.
- 49:05I don't use Bitwarden personally.
- 49:06And I was like, oh, but there's a CLI.
- 49:07Like, they could have made one.
- 49:09And I'm sure somebody eventually will.
- 49:11Yeah, I think it's also worth just a quick pointing out that both 1Password and Bitwarden come with CLIs.
- 49:19So that is an option, right?
- 49:21And 1Password even can be an SSH daemon for some of that as well.
- 49:26So those are both options for ways that your app can directly interact with your password manager.
- 49:32And it doesn't have to necessarily be the one that you use, right?
- 49:35For example, you and I are 1Password people, but maybe our app is a Bitwarden person.
- 49:40It's all about separating that software authorship thing from the user experience, right?
- 49:47Like, the user should be able to select their own keyring.
- 49:49It almost sounds like a little bit trivial to say, just use keyring and it'll all work.
- 49:54But like, it's that separation that you're trying to achieve is that you want code that can be configured to be as secure as it needs to be and has like good defaults for most people.
- 50:03Okay.
- 50:03All right.
- 50:04So there's a couple more lessons in here.
- 50:06If you're a doc writer, don't encourage people to put their API token straight in the source code.
- 50:13And you shame a few well-known companies saying it's not just that, but...
- 50:17Yeah.
- 50:18So that was like the biggest lesson.
- 50:20But some other things here is that...
- 50:22Or sorry, not another call to action here is that you really want to think about, you know, think about that time loop that you're in.
- 50:29What are all of the different attacks could impact you?
- 50:32Write them down.
- 50:34Think about how...
- 50:36What you want to be secure against the talk when it's out on YouTube kind of goes through them in order and can maybe when it comes out, you can have a listen.
- 50:44But once you've written that down, stick to it and make sure that your processes are such that it's really easy to do all of the defenses that you have selected that are like relevant to you and to your application.
- 50:57Like doing threat modeling is a great way to draw a line around that anxiety about like how secure are my secrets?
- 51:04Like if you kind of...
- 51:05If it's all just vibes and like, oh, you think about this one news story one day and then you write some security that like defends against that one particular thing.
- 51:12Or you like do something on your laptop one day that's in a more secure way, but you don't like encode that as a process or something that you do repeatedly for each new secret.
- 51:23Like as you said close to the top of the show here, like we're all using a zillion APIs now.
- 51:30So if you think of a good way to access your Stripe API key and then next week you have to add Braintree support and then the week after that you have to add, you know, OpenAI, right?
- 51:41Like as you add each one, you need to be doing things the same way, both so that you can correctly implement your security controls for each new thing, but also so that once you've implemented them, when you want to enhance them, when you grow to a certain size, you start getting more attacks, you start getting, you know, facing new, more difficult threats as you're a bigger target.
- 52:05You have to be able to upgrade and you want to be able to upgrade all of your secrets management at once as easily as you can.
- 52:11And there, there are other things outside the scope of what we're talking about today.
- 52:14Like you also need to be able to like rotate those secrets, which is a whole other mess.
- 52:19I mean, that's what tools like HashiCorp's vault can like help manage that part of the process.
- 52:25But yeah, so the call to action for like doc writers and framework authors is like, don't document things the wrong way.
- 52:31Don't provide bad defaults.
- 52:32You use key ring if you're doing Python stuff, but the call to action for like app maintainers and people operating services is do some threat modeling, write it down and do everything the same way.
- 52:44Like the more that it's all funneled through that choke point of key ring and of your documented security processes, the more that when you have to make it better, it will not be a giant pain of like trawling through all of your code and all of your libraries and trying to figure out where things even are.
- 53:01You'll just be able to look at your, you know, the code that's accessing the secret and say like, oh, well, we're, we're going to switch from like, you know, our sort of homegrown thing to some vault or to Amazon secret store or Google cloud equivalent, or, you know, digital ocean has a thing.
- 53:17Everybody's got their own secret storage thing.
- 53:19There's a bunch of products in this area.
- 53:20And when you adopt one, you want that process to be as smooth as possible.
- 53:24It's also probably a bit of a friction.
- 53:26I always think about, you know, how do you encourage people to do the right thing?
- 53:30In a way you discourage them is you add friction and you make it harder to do the right thing.
- 53:36Right.
- 53:36So if the right thing is to, you figure out a way to manage one of your secrets, well, make it easy to bring on another API, make it easy to, you know, basically get in there and change that and work with that system.
- 53:47Yeah.
- 53:47All right.
- 53:48Let's see.
- 53:48We're getting short on time, but what other lessons do we have?
- 53:51You know, talk a bit about where do we put some of these things.
- 53:53You did also point out that there's beyond the keyring stuff is you do have stuff for like AWS secrets in particular.
- 54:03And I suppose there's probably other ones as well, right?
- 54:06Like what we talked about so far mostly is security on your computer or security on the one server that you go and run your app or the cluster of servers.
- 54:16But, you know, what about GitHub automation?
- 54:18You know, things like that, right?
- 54:20There's various tools.
- 54:22The two that I've talked about in my talk are there's a tool called AWS Vault.
- 54:26There's another one called Gimme AWS Creds, which make it more secure to store your AWS credentials in particular.
- 54:33Because those are pretty in the same way that like your computer is the root of security for like everything.
- 54:38And that's where all your secrets are ultimately stored.
- 54:40Your AWS account is like kind of the root of your security for AWS for all of your cloud resources if you're using them.
- 54:47So it's that it's good to focus more on like the specific things that have a lot of power.
- 54:53Those also can reduce the power, like have roles that have like less permission that isn't just like your cloud root API key for everything.
- 55:01There's also like GitHub has a secrets system, which is effectively just injecting environment variables.
- 55:07But it stores them in a way that like keeps them out of your repo.
- 55:11It puts them in a place that, again, you can enumerate.
- 55:15You can see all of the secrets that are provisioned against your particular repository.
- 55:19It manages access control for like, you know, public open source PRs can't like immediately access all those secrets because they're running like they have to be run in a way that indicates that like an admin has kind of.
- 55:31Seen that something is going to happen.
- 55:33So they only run on like your main branch or things like that.
- 55:37And it's a little difficult to talk about these.
- 55:39And this is one of the reasons that I focus so heavily on your local computer, both because it's kind of the base of this pyramid, but also because you really have to do that research on your own.
- 55:48Because you have to know what services you're using and what tools are available to manage certain secrets within them.
- 55:56And keyring is so important because it's the thing that gives you the joint of flexibility to say like, oh, well, when it's running in GitHub, it's going to be in an N var.
- 56:04And it's going to be in this N var.
- 56:06And I'm going to write a keyring back in that knows that.
- 56:07Interesting.
- 56:08Yeah.
- 56:08Keyring sounds like a really important piece of this story.
- 56:11Yeah.
- 56:12I mean, it is in the Python world.
- 56:14It really is like the beginning of every story about improving your security is, okay, now that you're using keyring, you can.
- 56:20And then there's like the next thing.
- 56:21And there's a bunch of directions you can go in depending on if it's your local computer, if it's the cloud, if it's like a dedicated server, if it's an embedded device.
- 56:29There's all different ways that you could go, but it kind of all starts with like acknowledging that you're going to need that flexibility.
- 56:35And honestly, you could write your own version of keyring.
- 56:38You know, all it is a function that like looks at a config file and figures out what other function to call.
- 56:43But because it's the standard one, like it is a social convention that is very powerful because you really don't want to write all of your own backends for everything.
- 56:52Right. It's about the integration with the other systems, about integrating with the Bitwarden CLI and with macOS keychain and doing all those things right.
- 57:00Yeah.
- 57:00Right. Yeah. And those good defaults are really important because like integrating with the macOS keychain is not easy and keyring just does it all for you.
- 57:08So yeah.
- 57:08One thing I also wanted to, this is kind of a minor point, but I just, I really want to toss it in there because I feel like it's rarely covered, which is environment variables have kind of taken over in the cloud.
- 57:18Like they're the standard way that a lot of services expect you to inject stuff.
- 57:22So I can't say like, don't use env vars, like it's just, that's not feasible, but when you can avoid them, like if you can get your credentials stored some other way, like in a text file or whatever, try to stick to that because env vars are really easy to accidentally dump in a debugging session.
- 57:39Like you can just type env and it'll dump out everything.
- 57:41And a lot of like CI builds or debug log uploaders will just like kind of dump out the environment because it's useful to know.
- 57:49And filtering secrets out of that is a whole like discipline that you have to develop.
- 57:54And like a lot of tools that do debugging like Sentry will have like a lot of code to do that filtering, but it's best if you could just put the secrets where the secrets go.
- 58:02So like one idiom that you could use is like, you can put a crypto key in your environment variable, which can't do anything but like decrypt the text file.
- 58:11And so that key is kind of useless outside the CI context or whatever.
- 58:15And so you just like indirect it so that the actual secret is stored in a place that is not just going to be dumped in every debug log.
- 58:22The danger may be given away too much.
- 58:23That's what I do.
- 58:24I have one encryption key that's pretty long and a really high level of encryption of the actual secret somewhere else.
- 58:31And for me to add another secret, another API endpoint as well, to add it to this file and then re-encrypt it.
- 58:37You know, it doesn't, it's sort of the same process.
- 58:39So it's not like, well, these APIs are encrypted that way and the database connection is encrypted that way.
- 58:45It's like, here's the file that's got all the info in it and then it's highly encrypted.
- 58:50Yeah, and that's exactly what I want to encourage people to do.
- 58:53Put all your eggs in one basket and then watch the basket.
- 58:55Passwords go in the password manager.
- 58:58Crypto stuff goes in the file.
- 59:00Please, please, please don't let it get exposed.
- 59:02Okay.
- 59:03Let's wrap up this conversation with a bit of a summary from what you take away from your talk here.
- 59:10Yeah, so I think we've hammered home, you know, you should use a password manager.
- 59:13You should make sure that your master passwords for that password manager are backed up.
- 59:19One thing we didn't cover today, but it's pretty big in the talk is like, you should trust the autofill on your password manager.
- 59:24Because like, a lot of phishing attacks will make a very convincing website that will trick even the most seasoned security people.
- 59:32So like, just if it doesn't autofill, close the window, type the URL by hand, make sure it autofills.
- 59:38Yeah, I think people often see that.
- 59:40Well, this is just the password manager is incomplete.
- 59:43So it's not helping me with this website.
- 59:45Like, that should be, you know, a super big red flag if the password manager is not suggesting to autofill.
- 59:50Yeah, and one of my favorite things about 1Password is that they have been getting progressively more aggressive about making it super annoying to like disable that.
- 59:59Like now when you do, if you try to autofill with 1Password in the wrong app or the wrong website, it now pops up a dialogue that's like, do you want to like add this URL?
- 1:00:08Do you want to fill this once?
- 1:00:09Or was this a mistake?
- 1:00:10Like it will ask you every time if you try to force it to.
- 1:00:15But obviously like copy paste kind of circumvents all those protections.
- 1:00:18So like never, ever use copy paste.
- 1:00:20Always try to get it to autofill.
- 1:00:22You should use full disk encryption.
- 1:00:23Just turn on FileVault or what's the Microsoft?
- 1:00:27BitLocker.
- 1:00:27BitLocker, yeah.
- 1:00:28Or DMCrypt, I think is the Linux one.
- 1:00:31Like you should, the technology now is like pretty much free.
- 1:00:34It doesn't really cost much in the way of performance.
- 1:00:36Like, and it gives you peace of mind if your laptop gets stolen, which is sadly a thing that speaking of like, you know, GitHub credentials leaking constantly.
- 1:00:44Laptops are stolen every day.
- 1:00:45So you should make encrypted backups.
- 1:00:48You should make sure you have like a restore plan because availability is really important.
- 1:00:51Obviously, use keyring, use keyring, use keyring.
- 1:00:54And pick an appropriate backend for the level of security that you need in each place where your code's deployed.
- 1:00:59Yeah, absolutely.
- 1:01:01All right.
- 1:01:01Well, Cliff, thanks for thinking about all this for everyone and giving us a list.
- 1:01:06And I think people will really enjoy your storytelling when the PyCon video eventually comes out.
- 1:01:13Yeah, I can't wait to start sending people over to the YouTube video.
- 1:01:15I was pretty proud of how it went.
- 1:01:17It's my first talk in quite a while because I don't know if you know this, but the last few years have been a little stressful for everybody.
- 1:01:24A lot of stuff going on.
- 1:01:27It scrambles up the whole conference track, conference circuit a little bit.
- 1:01:32Yes.
- 1:01:32All right.
- 1:01:32Before we get out of here, let's give you a chance to give a shout out to a PyPI package.
- 1:01:36I mean, feel free to pick keyring if you want one more time or something else that you think is kind of awesome people should check out.
- 1:01:41That's always tough.
- 1:01:42I remember, I know that this question is coming every time I'm on this podcast.
- 1:01:46Well, let's see.
- 1:01:46The choices are getting harder because we're up to 456,000 packages on PyPI.
- 1:01:52How nuts is that?
- 1:01:53There's a lot out there.
- 1:01:54I guess I should take this as an opportunity to like, oh, you know what?
- 1:01:58Actually, I do have a self-promotion thing that I should put in here that is not, we did not talk about at all today, which is date type.
- 1:02:05Date type is another little package that I wrote.
- 1:02:09And it's a workaround for like one specific bug in the standard library, which is that dates are assignable to like dates and date times are considered equivalent because they are one subclass is the other.
- 1:02:24And this will make it so that you can't assign a date time to a date because they don't actually, like they're not compatible.
- 1:02:31They're not Liskov substitutable.
- 1:02:33And it will also give you separate types for naive and aware date times.
- 1:02:37So you can't mix those up.
- 1:02:39Oh, nice.
- 1:02:39It is not actually a library.
- 1:02:41It looks like a library.
- 1:02:43And I guess it does have one function in it.
- 1:02:44But what it actually is, is a bunch of abstract protocol like type stubs.
- 1:02:50And so the actual, at runtime, there's still date times.
- 1:02:53Everything works exactly the same.
- 1:02:54It doesn't change how your code behaves at all.
- 1:02:55But by importing from date type instead of date time, you get this extra mypy type safety.
- 1:03:00Excellent.
- 1:03:00All right.
- 1:03:01Very cool.
- 1:03:01That and also Rich, because like that text UI stuff is amazing.
- 1:03:06But I feel like everybody talks about that one.
- 1:03:08Rich is awesome.
- 1:03:09Will's doing a good job over there with that for sure.
- 1:03:11All right.
- 1:03:12Final question.
- 1:03:13If you're going to work on date type or any of these other projects, what editor are you using these days?
- 1:03:18I use Emacs as I have for nigh on 30 years now.
- 1:03:22You prefer to have an operating system for your editor?
- 1:03:25I don't treat it as an OS.
- 1:03:26I feel like a lot of people, you know, kind of live inside Emacs all the time.
- 1:03:30And I use Emacs for a lot.
- 1:03:31I think I love the text editing facilities.
- 1:03:33Every time I kind of try to drop into like VS Code or one of the, you know, new cool text editors, I always like the IDE stuff is often nicer than Emacs.
- 1:03:43But the text editing itself, like the number of motions, the kinds of things you can do to like select a word, select a function, select a line, you know, select a sentence, rewrap a paragraph.
- 1:03:55There's all these things that you do as you're just working with text in Emacs.
- 1:04:00So it's like a, it's a text editor in a way that few other IDEs are really text.
- 1:04:06Yeah.
- 1:04:07I feel like Vim is a little more interchangeable.
- 1:04:09Yeah.
- 1:04:09A lot of the editors have Vim bindings, right?
- 1:04:11I've used Vim.
- 1:04:12I actually kind of took a break from Emacs for about two years at one point and switched entirely to Vim.
- 1:04:17But my Vim config has been slowly breaking over the years.
- 1:04:21So I actually deleted the whole thing last year and I've been trying to get into NeoVim, but there's a little bit of like, it's a different world.
- 1:04:28So I haven't managed to get into it.
- 1:04:30Very cool.
- 1:04:31All right.
- 1:04:32Well, final call to action for people who are now maybe a little bit nervous and thinking about how they should treat their secrets differently.
- 1:04:38Yeah.
- 1:04:39I mean, I think that that's a good point.
- 1:04:41They might be a little nervous.
- 1:04:42Relax.
- 1:04:42It's going to be okay.
- 1:04:44That's my call to action.
- 1:04:45And use keyring and don't like write docs that tell people to paste stuff into their source code.
- 1:04:50But like mostly just like relax and take this methodically.
- 1:04:54Nobody's attacking you right now.
- 1:04:55And it's very easy to get worked up about security and think like, oh no, you know, all of these attacks, like all this horrible stuff could happen.
- 1:05:02But really, like today's the same as yesterday.
- 1:05:05You just need to start adding those defenses a little bit at a time.
- 1:05:08Absolutely.
- 1:05:08Well, thanks again for being on the show.
- 1:05:10It's great to catch up with you.
- 1:05:11Thanks so much for having me.
- 1:05:12This was great.
- 1:05:12Yeah, you bet.
- 1:05:13Bye.
- 1:05:13Bye.
- 1:05:13This has been another episode of Talk Python To Me.
- 1:05:17Thank you to our sponsors.
- 1:05:19Be sure to check out what they're offering.
- 1:05:20It really helps support the show.
- 1:05:22The folks over at JetBrains encourage you to get work done with PyCharm.
- 1:05:27PyCharm Professional understands complex projects across multiple languages and technologies, so you can stay productive while you're writing Python code and other code like HTML or SQL.
- 1:05:38Download your free trial at talkpython.fm/done with PyCharm.
- 1:05:44Listen to an episode of Compiler, an original podcast from Red Hat.
- 1:05:48Compiler unravels industry topics, trends, and things you've always wanted to know about tech through interviews with the people who know it best.
- 1:05:55Subscribe today by following talkpython.fm/compiler.
- 1:06:00Want to level up your Python?
- 1:06:01We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:06:05Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:06:10And best of all, there's not a subscription in sight.
- 1:06:13Check it out for yourself at training.talkpython.fm.
- 1:06:16Be sure to subscribe to the show, open your favorite podcast app, and search for Python.
- 1:06:20We should be right at the top.
- 1:06:22You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:06:31We're live streaming most of our recordings these days.
- 1:06:34If you want to be part of the show and have your comments featured on the air, be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:06:43This is your host, Michael Kennedy.
- 1:06:44Thanks so much for listening.
- 1:06:45I really appreciate it.
- 1:06:47Now get out there and write some Python code.
- 1:06:48I'll see you next time.
- 1:07:09Thank you.