Netflox: read DB password from env; stop tracking archive with old password

database.py had the PostgreSQL password in plain text (inline literal repeated
in every psycopg2.connect call). It now reads NETFLOX_DB_PASSWORD once at
import time and raises RuntimeError if it is unset, so there is no default.

The committed Projeto_Netflox_*.zip embeds the same database.py and therefore
still carries the old password, so it is removed from the index
(git rm --cached; the file stays on disk outside the working tree). README.md
documents the env var and the archive removal.

Why: the credential must not live in the repository. The value is still in the
history and in any clone/cache, so it must be rotated.
This commit is contained in:
Andre Neto
2026-10-08 14:03:21 +01:00
parent 6509c6c5d6
commit 17f75eccb0
3 changed files with 60 additions and 29 deletions
+39 -29
View File
@@ -1,9 +1,19 @@
import os
import psycopg2
# Read once, at import time. Never hard-code the password here.
DB_PASSWORD = os.environ.get("NETFLOX_DB_PASSWORD")
if not DB_PASSWORD:
raise RuntimeError(
"NETFLOX_DB_PASSWORD is not set. Export the PostgreSQL "
"password before running (see README.md, Configuration)."
)
global USERID
# MENU
def create_user(name, email, password):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
if email.find('@') == -1:
@@ -18,7 +28,7 @@ def create_user(name, email, password):
conn.commit()
conn.close()
def log_in(email, password):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM users WHERE email = '" + email + "' AND password = '" + password + "'")
@@ -52,7 +62,7 @@ def log_in(email, password):
# MESSAGES CLIENT
def show_unread_messages(userid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM messages WHERE users_userid = " + str(userid) + " AND bolread = FALSE")
@@ -81,7 +91,7 @@ def show_unread_messages(userid):
conn.commit()
conn.close()
def show_read_messages(userid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM messages WHERE users_userid = " + str(userid) + " AND bolread = TRUE")
@@ -103,7 +113,7 @@ def show_read_messages(userid):
return messages
def read_message(msgid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("UPDATE messages SET bolread = TRUE WHERE msgid='" + str(msgid) + "'")
@@ -112,7 +122,7 @@ def read_message(msgid):
conn.close()
# MESSAGES ADMIN
def message_all(msg, senderid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT userid FROM users")
@@ -134,7 +144,7 @@ def message_all(msg, senderid):
conn.commit()
conn.close()
def message_client(msg, recieverid, senderid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute(
@@ -148,7 +158,7 @@ def message_client(msg, recieverid, senderid):
# SEARCH ARTICLES
def findby_name(name):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
y=0
c.execute("SELECT * FROM articles WHERE name like '%" + name + "%'")
@@ -169,7 +179,7 @@ def findby_name(name):
conn.close()
return(movies)
def findby_director(director):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
y = 0
c = conn.cursor()
c.execute("SELECT * FROM articles WHERE director like '%" + director + "%'")
@@ -189,7 +199,7 @@ def findby_director(director):
conn.commit()
conn.close()
def findby_type(type):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
y = 0
c = conn.cursor()
c.execute("SELECT * FROM articles WHERE type like '%" + type + "%'")
@@ -210,7 +220,7 @@ def findby_type(type):
conn.commit()
conn.close()
def findby_actor(actor):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
j=0
c = conn.cursor()
c.execute("SELECT * FROM actors WHERE name like('%"+actor+"%')")
@@ -230,7 +240,7 @@ def findby_actor(actor):
conn.commit()
conn.close()
def list_all():
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM articles")
articles = c.fetchall()
@@ -245,7 +255,7 @@ def list_all():
return articles
def view_details(itemid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM articles WHERE itemid = '"+str(itemid)+"'")
article = c.fetchall()[0]
@@ -270,7 +280,7 @@ def view_details(itemid):
conn.commit()
conn.close()
def purchase(itemid, userid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT balance FROM users WHERE userid = '"+str(userid)+"'")
@@ -299,7 +309,7 @@ def purchase(itemid, userid):
# ORDER BY
def order_title():
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM articles ORDER BY name")
movies = c.fetchall()
@@ -314,7 +324,7 @@ def order_title():
return movies
def order_director():
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM articles ORDER BY director")
movies = c.fetchall()
@@ -329,7 +339,7 @@ def order_director():
return movies
def order_year():
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM articles ORDER BY release_year")
movies = c.fetchall()
@@ -344,7 +354,7 @@ def order_year():
return movies
def order_imdb():
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM articles ORDER BY imbd_rating")
movies = c.fetchall()
@@ -359,7 +369,7 @@ def order_imdb():
return movies
def order_genre():
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM articles ORDER BY genre")
movies = c.fetchall()
@@ -374,7 +384,7 @@ def order_genre():
return movies
def order_price():
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM articles ORDER BY price")
movies = c.fetchall()
@@ -389,7 +399,7 @@ def order_price():
return movies
def order_type():
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM articles ORDER BY type")
movies = c.fetchall()
@@ -406,7 +416,7 @@ def order_type():
# MY ARTICLES
def my_articles(userid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM rents WHERE end_date >= CURRENT_DATE AND users_userid = '" + str(userid) + "' ORDER BY articles_itemid")
@@ -429,7 +439,7 @@ def my_articles(userid):
return int(input("\t0) Exit\n\tYour selection: "))
def my_history(userid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM rents WHERE end_date < CURRENT_DATE AND users_userid = '" + str(userid) + "' ORDER BY articles_itemid")
@@ -452,7 +462,7 @@ def my_history(userid):
return int(input("\t0) Exit\n\tYour selection: "))
def time_left(itemid, userid):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("SELECT * FROM rents WHERE articles_itemid = "+str(itemid)+" AND users_userid = '"+str(userid)+"'")
@@ -465,7 +475,7 @@ def time_left(itemid, userid):
# ADMIN
def add_article(name, director, imbd_rating, genre, price, year, time_available, type):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("INSERT INTO articles(name, director, release_year, imbd_rating, genre, price, type, time_available) VALUES('" + name + "','" + director + "', '" + year + "','" + imbd_rating + "','" + genre + "','" + price + "','" + type + "','" + time_available + "')")
@@ -494,7 +504,7 @@ def add_article(name, director, imbd_rating, genre, price, year, time_available,
conn.commit()
conn.close()
def change_price(n_id, newprice):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
itemid = n_id
@@ -521,7 +531,7 @@ def change_price(n_id, newprice):
conn.commit()
conn.close()
def remove_article(n_id):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
itemid = n_id
@@ -543,7 +553,7 @@ def remove_article(n_id):
conn.commit()
conn.close()
def alter_balance(userid, balance):
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
c = conn.cursor()
c.execute("UPDATE users SET balance = '" + str(balance) + "' WHERE userid='" + str(userid) + "'")
@@ -553,7 +563,7 @@ def alter_balance(userid, balance):
conn.commit()
conn.close()
def statistics():
conn = psycopg2.connect("host=localhost dbname=NetfloxFinal user=postgres password=postgres")
conn = psycopg2.connect(f"host=localhost dbname=NetfloxFinal user=postgres password={DB_PASSWORD}")
total=0
c = conn.cursor()
c.execute("SELECT articles_itemid FROM rents")