# Efe Öge - Keeping secrets secret in Django

Source: https://djangotv.com/videos/django-day-copenhagen/2026/efe-oge-keeping-secrets-secret-in-django-django-day-2026/
Date: 2026-10-13
Speakers: Speaker 1, Efe Öge, Speaker 3, Speaker 4, Speaker 5, Speaker 6, Speaker 7
Text: lightly edited for readability (verbatim on the page)

## Transcript

**Speaker 1:** [00:00] Welcome on stage, Efe. And thank you for traveling, and thank you to everyone else for that matter who's traveled here today. Great to have you on stage. Looking forward to your talk, and let's give a big round of applause.

**Efe Öge:** [00:15] Thank you, Benjamin. Thank you very much, everyone. Today we would like to talk about keeping secrets secret in Django. This talk started from a curiosity, actually. It was my secret because I didn't know what the secret key in settings.py is for. And I ignored it for quite a long time in my career, and I said, okay, after some time maybe I can learn this. And I am also using some other frameworks like FastAPI and others, and they don't have this secret key in their settings.py. So what's the deal? Why do we have it in Django, and why don't we have it in the other frameworks? What is this coming from?

[01:01] And I hope that we're going to learn why we have it in Django today. Let me introduce myself. My name is Efe. I'm originally from Turkey. I'm based in Amsterdam right now, and I'm working as a software engineer at Nebius, which is a neo-cloud company. And besides that, I try to also pivot my open-source projects to some kind of startups, and ESCLOT is one of them. It started as a Django package called Django Translate Messages, and you can use it for your open-source projects for free. And I occasionally write in my blog, which is efe.me, and you can find these slides there. And lastly, I also try to be active in the Django NL community in the Netherlands.

[01:49] And we're also going to have Django Girls Amsterdam on the 10th of October this month. And besides that, I like playing backgammon, because it comes with age. I'm in my 30s, and I love computers. So this is the agenda for today. What is the secret? I always like starting with definitions. What is the secret key in Django? And the third one is where should secrets live? And the fourth one is who can read this secret? Because we don't think that much about where it can go. And what is the secret? Donald Trump? No.

[02:34] No, it's not political. A secret is something that, when we disclose its value, might grant some capabilities to other people, and it might also weaken our security guarantees. For example, a database password, because most of our applications need to access a database, and to convince the database that, yes, we are coming from the right server, we need to have some kind of common, decided thing between us. Therefore, they can let us run queries and change data. Or we might like to charge someone in our application, and it could be a Stripe

[03:22] or any payment provider's secret key. Or we would like to maybe automatically commit something in our application, and we might have GitHub access tokens. Or if you have OAuth authentication, we might also have our client secret. So these are kind of third-party or external secrets. And, as you also know, we have Django's secret key, which is for our internal back-end application. And we also have an AWS secret key if you are using AWS. And what is not a secret? Because sometimes I'm also having a hard time understanding, okay, should I think of my database table name as a secret or not?

[04:09] Because if it is known by someone else, maybe it's going to be easier for them to penetrate my database. And the first thing that's not secret is configurations. Configurations just decide how our application works. For example, if you have an API base URL, or if you would like to say, okay, this application only runs on DjangoDate.dk, or set the time zone of our application. The second category is sensitive information that we shouldn't disclose. So this actually conflicts a lot with secrets. Why? Both of them shouldn't be leaked, but they are two different things.

[04:56] So what is an example of sensitive information? Customer data in our database, because we don't want to lose this data. Maybe it might be about healthcare; it might be about payments. We don't want to leak this data. Or our internal logs. They might show performance problems that we might have in our systems, and an attacker might see the pattern and try to target us. Or financial data. And there are some public values that kind of sound like secret things, and we should be careful about them. But they are actually public values. They are designed to be shared with others. For example, our OAuth client ID or

[05:41] SSH public key. Because it's an asymmetric way of encryption, we need to have a public key and a private key, and the public key should always be public. And a Stripe public key. So, in summary, if we leak a secret, it discloses a grant of a capability for one action. Okay, this is just a general summary of secrets. Let's go a bit more to the Django side. The first thing you notice when you start a new project in Django is that it creates a few boilerplate files for you, and one of them is settings.py. And in settings.py

[06:27] it's going to create a secret key for you, and it starts with "django-insecure". Why? You need to pay attention to how to manage this data in your codebase. But why do we have this? In FastAPI we don't have it. In some other microframeworks we don't have this secret key. Why do we have it in Django? Where is this secret key used? I read a lot of Django's codebase when I was preparing for this talk, and it's mainly used for cryptographic signing. Most people say, okay, it's going to be used for passwords, it's going to be used for sessions. Yes, it's true, but the main core is actually one thing, and everything is using this functionality.

[07:17] And cryptographic signing sounds like a bit of a magical, fancy term, and I wanted to create a scenario analogy to understand it better. Let's say that we receive a message from a stranger. We don't trust that person that much, and we would like to know, okay, is this message a real message? Is it an authentic message? Is it tampered with or not? But we cannot call the sender and ask, okay, this person says this to me. Can I rely on this information? No, I cannot ask this question to the sender. And what could we do to overcome this problem? Smart

[08:02] people thought, okay, what if the sender and the receiver share something between them which is going to enable them to understand if the message is authenticated or not? And I thought a lot about analogies that I could explain in this case. There were lots of Alice and Bob keys, signing, sealing, and I didn't like any of them. And I came up with another example, which is sunglasses. So basically, I always think that the sender and receiver share the same sunglasses. And if they look through the same sunglasses, they're going to see something similar.

[08:49] And I tried to visualize it, and I came here by plane. I also thought, why not? So actually, this is very real. I came here with the 30D middle seat. It's not very comfy, and I'm not very happy about it. I would actually like to change it to 15A or something like this, so there would be more leg room. I would like to tamper with the data for my own sake. So how does it work, basically, if we have cryptographic signing in this process? At the desk, she's going to give me the boarding pass with the 30D. I didn't like it. Before that, she's going to put the glasses on this

[09:34] boarding pass and show some characters. She doesn't need to know what these characters mean, but there is something deterministic about these sunglasses that is always going to show the same result. Then she's going to put these numbers or signs under the boarding pass, and she's going to give it to me. But I would like to change my seat number. Then I would like to be in 15A. Okay, I just change this first part in a super nice and unnoticeable way, but I didn't touch these bottom characters. Then, in front of the gate, I handed over my boarding pass

[10:20] and the same person just put the glasses on it. Not the same person—a different person put the glasses on it. And they see that the patterns are not matching. And they say, okay, there is something sneaky in here. And if you would like to match this analogy to technical terms, my boarding pass is actually a value or a message. Should I trust this message or not? And the special glasses are basically an algorithm that's fed with my secret key. So therefore, when we have different Django applications, our signed values are not going to be the same, because our keys are going to be different. Therefore, the signed values are going to be different.

[11:06] And the figure seen on the sunglasses is called a signature. You might notice the aesthetics next to it, because I think this is not the right term, especially for me, because for human signatures, we mostly have the same signature for everything, and we are just repeating it. Maybe we have small differences, but it is mainly the same thing. But in the cryptographic world, a signature is actually unique to what you signed for. Therefore, it is different from the human signature. It took me a while to understand why it's called a signature and why it's a bit different. So please think that it is a bit different from the human signature and an electronic cryptographic signature.

[11:54] And looking again with the same glasses is just verification, and changing the seat number is called a tampered value. Okay, this is enough with the analogy. Let's come back to code, and I would like to show some Django code to you. Django cryptographic signing is in Django core, and you can just import it like this. On the third line, you are initiating a signer, and you just pass the value you would like to sign with the sign method. Django signs this value and shows the output in the print statement. And as you notice,

[12:39] when we sign the value, we actually didn't hide the value we had. It's already visible to anyone. It's not encryption, because most people, or sometimes, might tend to confuse encryption with cryptographic signing. When we have encryption, we don't see the value that we encrypted. But when we sign a value, we see the value, and we would like to be sure about its authenticity. And you can also call the unsign function to check if it is really authentic or not. And let's try to mimic the tampering that I did in the cartoons.

[13:27] I sign the value, and this is the signed value. It starts with 30D, and I just wanted to replace my seat, 30D, with 15A. Then I kept it in the tampered variable. And I would like to unsign this. Django is automatically going to raise an exception, which is a bad signature, because it is not the same value that I passed. Okay, like you mentioned, the secret key. Why do we have it? And here we don't see any secret key. Where is it coming from? To do that, we need to go to Django's source code. In Django's signing code, when we initiate a signer, we pass a key. And if you don't pass

[14:13] a specific key, Django falls back to its secret key. And when we are signing the value, we're appending the signature, and the signature is actually created with self.key. I know it's also kind of a bit like a matryoshka, but actually, we are using a secret key inside of signing. And in Django, we have different types of signers. In the examples, you saw mostly the most primitive one, which gets only the string and doesn't know about

[14:59] When do we sign this value? In the second one, we use the same class, but we are using a different method, which is sign_object. Basically, if we have a JSON-serializable Python object, Django, under the hood, serializes this object to a JSON string, then it calls the same sign method. But if we would like to do a more time-sensitive thing, like if you would like to send an email and in this email we have a forget password link, and it should be only valid for 24 hours, then it becomes more time-sensitive information. In that case, we can use TimestampSigner. And it also gets all

[15:44] input and adds a timestamp. In the last one, you can pass Python objects to signing.dumps, and it also adds a timestamp. Okay, up to so far, I mostly talked about cryptographic signing, and I said the secret key is only used in Django for this. But where do we use this cryptographic signing in Django? These are the applications of cryptographic signing in Django. There are different types of session backends in Django. If you use the signed cookie session, this is the default one, so all the data of the session are held

[16:32] on the browser. And as Django, we just need to verify that, okay, is it tampered with or not, by our secret key, and we are using the signing functions of Django under the hood. Same as in the second one, if you use Django's default class-based views for password reset, they are also using TimestampSigners. Or the session authentication hash is also binding the user's validity. This is a very nice feature of Django, which is called the messages framework. In Django admin, it is also very handy to use to notify or show a notification pop-up to the user, and it also uses this signing to be sure that these messages are not tampered with.

[17:22] So actually, cryptographic signing is a method to store less and verify more. If you come back to the forget password example, we could actually just generate a random string and keep this value in our database. When someone calls the endpoint, we can just check that, okay, does this value match our database value? If it doesn't, we can just raise an exception or return a 400 response. But we need to create more database fields for everything, like forget password or some other cases. So there's gonna be maybe overhead in your database, and you would like to limit the overhead in your database. Like in the example, you might remember that

[18:08] I don't wanna call the sender to verify that it's really that message or not. So in the real world of Django, it corresponds to the database queries which we make. Okay, I mentioned enough about cryptographic signing, why do we need a secret key? But I never mentioned how we need to generate a secure secret key, because the default one is insecure when you create your first Django application. And I wanted to have a small quiz in here. So we have three options. I have to just close my eyes and press buttons, and I hope that it's gonna be okay.

[18:55] Or I'm gonna do a bit of fancy stuff: I'm gonna hash something. Why not a timestamp? There is enough entropy there. Or I'm gonna use an online Django secret key generator. How do you feel? Like, A? Who says A? B? This is the fun. I like this one also. And C? Please don't do that. There are lots of websites. I checked before preparing this presentation. Maybe there are 10 different web pages who try to tell us that, okay, this is only on your browser, we don't get your secret. We don't need it, because Django already has the solution built in. So there is a specific method for this.

[19:40] You can just call this method in your Django shell, and it's gonna return you a long enough secret key. And this is another quiz. Now you know how to create a secure Django secret key. Maybe you had a bit weak ones; you would like to change it right now. What's gonna happen? For example, on Monday, you change the secret key. You follow the best practices; you follow the method. What's gonna happen? A: Nothing, because the secret key doesn't store any value in our databases, so everything should work smoothly.

[20:26] B: All signed values keep working until they expire. C: Django automatically re-signs everything, and it's gonna work with the new secret key. Or all signatures can no longer be verified. Who says A? B? C? D? Yes, D. So this is not a peaceful way to follow best practice, because if you do that, a product manager is gonna come and say, 'Okay, I felt like you did the nice thing, you did the right thing, but it wasn't that nice for our customers.' But can there be any other way? Django has solutions for the problems, unlike the other technologies which we have.

[21:11] So this is something I like about Django. In Django, we actually have another setting called secret key fallbacks. Basically, what we're gonna do is move our old secret key to secret key fallbacks, and we're gonna create a new secret key and use it as the secret key. How's it gonna work under the hood? When we are signing new values, we're gonna use the new secret key. When we would like to verify an old value, we're gonna start with the new key. If it doesn't work, okay, we're gonna try with the fallbacks. If all of them fail, then we're gonna fail. Okay, this is a tampered value. So now we saw how the secrets work in Django.

[21:58] But should I just commit these secrets to my Git repository? Some people say that this is not the best practice, but why? So this is the convenient list, this is the happy list. Like, okay, just push it, push. Why? I only have the repository. Maybe some intern is gonna have access, but they're gonna have it for a limited time, and I'm gonna remove them. When they have access to this, they're gonna have access to this. And you cannot change this history of Git. And your codebase is designed to distribute—not to distribute, to access your production databases. Developers are there to contribute code to you.

[22:46] And usually what people do is, okay, let's use environment variables. And there is an environment package, or we can just directly get from here. I'm gonna set each secret as an environment variable, and I'm gonna use them directly. And I'm gonna use curly braces instead of get, so therefore, if I'm missing any of them, deployment is gonna fail and I'm gonna get alerted. It is nice. But once secrets become many, we have a secret key, we have a database password, we have a Stripe secret key. We have a lot, and there's gonna be even more. And we have different environments too, like we have development

[23:31] environment, we have CI/CD environment. We need to also have secrets, different secrets for them. Staging, if you're lucky—not everyone has a staging environment. Production. Not every secret has to be provided wherever the application runs. It became overhead in your head, and this is some kind of image of how it became a matrix: you have lots of secrets to manage. And if one of them is missing, or maybe if one of them has a typo, it's gonna be very hard to understand, because there's gonna be a customer who's gonna complain about it. You can—you can—it's very hard to write tests for your secrets, also. What

[24:16] if we had only one? All the bad things start like this, but I think it's gonna end in a good way. It's not like a ring. Let's create one root encryption key per environment, because we don't want to use the same key for different environments; then it's gonna actually reduce our security best practices. Then encrypt each secret key with the environment root secret, which is a symmetric encryption. In Python, it's called Fernet encryption. And commit the encrypted value to our codebase. So therefore, we can always have a match with our codebase, and we're not gonna deal with the burden of different secret keys.

[25:03] And you're gonna only have one master root key per environment. With this idea, this Django root secret package was created. It's an open-source package; you can use it. How does it work? Since we are in Django, we have a new Django management command called generate root encryption key, and you can pass the environments for which you are creating a secret key. It creates a .env file for you and automatically adds this file to your .gitignore. So therefore, you are sure that you're not gonna commit this root secret key. Then, when you'd like to encrypt your values, you can also use another management command called encrypt secret

[25:54] and --environment. It's gonna prompt you which secret you would like to encrypt, and you can pass the plaintext and you're gonna get the encrypted version. Then in your settings, you can just call a simple function called get_secret, and you're gonna pass this encrypted value there. At runtime, your application is gonna decrypt it once. So encrypted values travel with the code; only the root key must be provided separately, maybe from your CI/CD pipeline or a different way to do it. But we didn't eliminate secret key management. We still need to manage our secrets, but we reduced what has to be distributed.

[26:43] So instead of 30 items, you're gonna manage maybe three. So we are done with where secrets should live. Now the next item is: who can read the secrets of our application? Django can read them, our CD services can read them, our CI/CD can read them because they also have the data. And deployment platforms, developers, dependencies of our third-party packages which we install, and anything we add to our installed apps. They have settings, and they can just call settings.any_value and use our secret values, or logs, error reporting, coding agents, or LLMs.

[27:30] Every reader becomes part of the trust boundary in here. So therefore, we should think about each time we're adding complexity to our projects, we are actually maybe increasing the possibility of leaking our secrets. Okay, this is interesting. And secrets also travel. It starts with a .env file you are adding to your production project. Then Django has it; Django loads it into its memory. Then when you have an error, you might have it in your traceback. So therefore, it's very important to have DEBUG=False in your production environment.

[28:20] Then if you copy-paste your traceback somewhere else, maybe to your friend, or you would like to troubleshoot with LLMs, then it is even more. So most leaks don't require super-quantum, like post-quantum computing. Most of the time, it happens via small mistakes you did to make something a bit fast. And what if the secret leaks? Maybe it's gonna happen, maybe it's not gonna, but there are some precautions which we can take before this happens to you. So the first thing is limiting the capability of your secret.

[29:06] So if you would like to create a secret key for your AWS account because you need to put some files to S3, or maybe you're gonna do some other things, don't have an admin user as an AWS user. If this leaks, the blast radius of this leak is gonna be even bigger. For example, database credentials shouldn't be for a superuser, or an API credential should only have the scope of the apps we are using. And make a rotation routine. If you know how to rotate your secret key, try to plan it every year or six months, depending on your schedule.

[29:51] And avoid long-lived secrets. If you have AWS, it's also possible to have more IAM role-based permissions. Then, if you don't have any secret to hide, it's not a problem. You can create these short-term secrets at runtime. And that's a summary of what happens if a secret leaks. If I would like to have a checklist: is it really a secret value before encrypting it? Think about it. Does it only have the capability we need? Was it generated securely or from somewhere from

[30:37] the cloud? Does it need to be long-lived? Maybe it can be a bit short-term. Where does it really live, and who can read it? Can I replace it? Can I rotate it periodically, and if that's going to happen, did I set up the schedule for it? Thank you very much for your time. That was my presentation. If you have any questions, I'm also happy to answer them if you have time. Otherwise, you can also find me in the hall.

**Speaker 1:** [31:05] Thank you. So we have a bit of time for questions, and obviously security. So...

**Efe Öge:** [31:19] Yeah, tough ones in the hallway are the ones in here. Exactly.

**Speaker 1:** [31:25] All right. Two in the back, I see. Do we have the microphone for them? Oh...

**Efe Öge:** [31:32] This is such a nice movie, by the way. This is an inside joke. Please watch it. It's called Daylive, if you haven't watched it.

**Speaker 3:** [31:40] Yeah. Well, thank you for your talk first. I loved how you had it all so organized. Amazing. But you have a list with old keys. When do you get rid of the old keys from the list? I suppose you have to get rid of them at some point.

**Efe Öge:** [31:59] And Marica, you're mentioning, like, Django secret old keys, or...

**Speaker 3:** [32:03] Yeah, the one where, if it doesn't match the current key, you fall back to the older keys. When do you remove them from the list?

**Efe Öge:** [32:12] So the biggest bottleneck in here is timestamp-signer-related signed values. Depending on your application logic, I can read the source code and say, okay, most of our signatures should expire after 10 days, for example. Then I would like to drop the old key after I change the key, maybe adding a bit of buffer, maybe 15 days later.

**Speaker 3:** [32:38] Okay, thank you.

**Speaker 4:** [32:45] Thank you for your talk. Actually, I was wondering how to deal with compromising one of the keys, the secret key, for example. If this got leaked for some reason in production, this means the whole system got compromised. Changing or rotating the secret key is the solution to overcome this, but this will come with some signing being broken. I don't know. What do you recommend to deal with such things?

**Efe Öge:** [33:23] You're asking, if my secret key is leaked, what should I do on the first day?

**Speaker 4:** [33:29] Yeah.

**Efe Öge:** [33:31] I think the first thing we should do is rotate the secret key without using the fallback methods. What I can do is use the forget password flow to get my admin users, and I can do lots of things. So this is what I would suggest, instead of just letting people log out the next day. It's okay.

**Speaker 4:** [33:55] Security

**Efe Öge:** [33:56] ...is more important, in my opinion. Thank you.

**Speaker 5:** [34:00] Hey, thank you for the presentation.

**Efe Öge:** [34:01] Thank you, but

**Speaker 5:** [34:03] I got a couple of questions. I'll try to make them real quick. The first one is, do you know when it's suggested to roll the secret key?

**Efe Öge:** [34:12] I don't know, to be honest, personally. Yeah.

**Speaker 5:** [34:14] Okay. And the second one is, I always feel nervous about having keys in my repository. Because if the repository leaks, for some reason, I have something else leaking that can be decrypted in the future. I know I can roll the secret, but I'd like to avoid it. Is there any workflow you suggest that can keep this outside of the repository?

**Efe Öge:** [34:41] There are also some secret managers, like cloud secret managers, which we can use. I mentioned AWS, but there are other tools too, or secret vaults. They don't even keep the encrypted value in your codebase.

**Speaker 5:** [35:05] Kubernetes secrets or something in another directory, and you can read it from there.

**Efe Öge:** [35:09] I think this is also definitely a possible way to do it, at a bit higher level of abstraction in your Docker or Kubernetes setups.

**Speaker 5:** [35:18] Thank you. Thank you for the time.

**Efe Öge:** [35:20] Thank you.

**Speaker 1:** [35:21] You had a slide with the name of your project on. I just want to refresh my own and everyone's memory of what that project was called, so we can...

[35:36] pip install

**Efe Öge:** [35:38] Django root secrets. I'm also going to put it—it'll take some time. I'm going to put it on Zulip, and, or yeah, I'm also going to...

**Speaker 6:** [35:48] We have one more question for the audience.

**Speaker 1:** [35:51] Yeah, we're a little bit over time. Let's do it. Let's do it.

**Speaker 7:** [35:55] Okay. Thank you for your talk. Interesting points on a very important topic. I'm a little skeptical about the one master password to rule them all approach. Am I understanding it right that, if you commit these, that means you use the same secrets in all environments? Or am I misunderstanding something? Is your secret key the same in staging as in production?

**Efe Öge:** [36:26] No. We have different root keys for different environments.

**Speaker 7:** [36:31] Right.

**Efe Öge:** [36:31] So, per environment, I suggest having a root secret key. But we are using the same root key for all the keys we have in that environment. For example, in production, if we have a secret key called A, we are using this A key to encrypt all production secret keys.

**Speaker 7:** [36:54] I think I'll catch you in the hallway and get on the book.

**Efe Öge:** [36:56] But, by the way, this is definitely a trade-off. I kind of try to promote having fewer keys to prevent more human errors, which we can pass, copy-paste, like environment variables. But I totally agree with that too. Thank you.

**Speaker 1:** [37:15] Yeah.

**Speaker 7:** [37:16] Thank you.

**Speaker 1:** [37:17] Efe, thank you so much for the wonderful talk.

**Efe Öge:** [37:20] Thank you.

**Speaker 1:** [37:21] And we have the last talk before the break.
