I am trying to create protected code which doesn't leave itself open for SQL Injection attacks. Currently, I want to create 3 users with different passwords. Here is what is looks like:
import psycopg2
from psycopg2 import connect, extensions, sql
# Importing a 0 integer so the process can pass without bothering w/ extensions
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
user1 = "jimmy"
user2 = "ray"
user3 = "billy"
secret1 = "gelatto"
secret3 = "cookies"
secret2 = "vanilla"
cursor.execute(sql.SQL("CREATE USER {users} WITH PASSWORD {password}")
.format(users=sql.Identifier(user1),
password=sql.Identifier(secret1)))
cursor.execute(sql.SQL("CREATE USER {users} WITH PASSWORD {password}")
.format(users=sql.Identifier(user2),
password=sql.Identifier(secret2)))
cursor.execute(sql.SQL("CREATE USER {users} WITH PASSWORD {password}")
.format(users=sql.Identifier(user3),
password=sql.Identifier(secret3)))
cursor.execute(sql.SQL("GRANT {role} TO {user}")
.format(role=sql.Identifier(readWrite),
user=sql.Identifier(user1)))
cursor.execute(sql.SQL("GRANT {role} TO {user}")
.format(role=sql.Identifier(readWrite),
user=sql.Identifier(user2)))
cursor.execute(sql.SQL("GRANT {role} TO {user}")
.format(role=sql.Identifier(readOnly),
user=sql.Identifier(user3)))
However, I receive an error to since the passwords are being closed in " " when they need to be ' '. Can anybody help me out on how they figured this out?
LINE 1: CREATE USER "jimmy" WITH PASSWORD "gelatto"
password=sql.Literal(secret1)