Python Quick Tip: Hiding Passwords and Secret Keys in Environment Variables (Mac & Linux)
4:31

Python Quick Tip: Hiding Passwords and Secret Keys in Environment Variables (Mac & Linux)

Corey Schafer 01.02.2018 169 979 просмотров 4 448 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
In this Python Programming Tutorial, we will be learning how to hide passwords and secret information within environment variables on Mac and Linux machines. Hard-coding secret information is a common mistake that beginners make when learning Python. Hiding this information within environment variables allows you to access your secret information within your code without anyone else being able to see these values from your source code. Let's get started... The code from this video can be found at: https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Python-Environment-Variables Python Environment Variables (Windows): https://youtu.be/IolxqkL7cD8 ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python

Оглавление (1 сегментов)

Segment 1 (00:00 - 04:00)

hey there how's it going everybody in this video we're just going to go over a quick to showing how you can hide secret information and environment variables and then access that information from your Python code now a common problem that I see from people who are just getting started out is that they'll put information directly in their code that actually shouldn't be there so for example if you're working with databases or are accessing an API and have a secret key then they'll have their passwords or secret keys directly in the Python script now the problem with that is that if you're working with a team of people or pushing your code to a repository of some kind then anyone who can see the code will also have access to that secret information but if you have that information saved and environment variables then you'll still be able to share your code but with your secret information saved on your local machine so let's see how we can do this so I have a script open here with some fake credentials and we can see that we have a database username and password hard-coded into our script and this could also be in a secret API key or any information that you don't want to share with everyone else who has access to the code so let's move these two environment variables now setting environment variables is done differently on Linux and Windows machines and in this tutorial we'll be covering how to do this on Mac and Linux but if you're on a Windows machine then I'll have a separate video on how to do this on Windows as well and I'll leave a link to that video in the description section below if anyone needs that but if you're on Mac or Linux then we can set these environment variables by modifying a file called dot Bash underscore profile so first let's open up our terminal and then we need to navigate to our home directory now I'm already in my home directory but if you need to navigate there then you can do that just by typing CD and pressing enter and now we need to open our dot file where we can set environment variables and I'll just open mine and Nano since that's an easy editor for everyone but you can also do this in whatever editor you'd like so I'll say Nano and that file was dot bash underscore profile now I might have some more information in here than you see in your file but don't worry about that so at the top of this file we can go ahead and add our environment variable now to do this we can just say export and then the name of our variable so we'll do an all uppercase DB user and then an equal sign with some quotes and we don't any spaces between the equal sign and the value and now let's go back to our script and grab this value so I can copy and paste it in and now let's do the same thing with the password so we'll say DB pass is equal to go back here and grab this from our file grab the whole thing copy that and paste that in and once we have those variables set then we can save this in Nano by pressing control X and then a yes to save and then enter to keep the same filename and now we can quit out of our terminal and now to actually see those changes we're also going to need to restart sublime text so I will shut that down and then open that back up to the same file and now let's access those variables that we just set now to do that we need to import the OS module and then with the OS module we can say OS dot and vine and this is a dictionary so we can access the key that we want by doing a dot git and then passing in what we want so we wanted the DB user environment variable and now let's also paste this in for the password so that was DB underscore pass so now let's save this and run it and now we can see that we get the values that we'd set in our environment variables without them actually being hard-coded into our script so that's going to be a lot better for us since we no longer have to worry about our passwords or secret information being added to our source code okay so I think that's going to do it for this video like I said this was just a quick short tip video about a common problem that I see amongst people who are just getting started out and if you've run into this before then hopefully you found this useful but if you do have any questions about what we covered then feel free to ask in the comment section below and I'll be sure to answer those and also if you enjoy these tutorials and would like to support them then there are several ways you can do that these are sways to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you

Другие видео автора — Corey Schafer

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник