Skip to content

Commit 80111d3

Browse files
pbailiebmcutler
authored andcommitted
Script updates (#8)
db_backup.py and accounts.php Pull DB connection info from Submitty. map_course.php Prefer encrypted connection to DB
1 parent c832542 commit 80111d3

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

nightly_db_backup/db_backup.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
:file: db_backup.py
66
:language: python3
77
:author: Peter Bailie (Systems Programmer, Dept. of Computer Science, RPI)
8-
:date: May 22 2018
8+
:date: August 22 2018
99
1010
This script will take backup dumps of each individual Submitty course
1111
database. This should be set up by a sysadmin to be run on the Submitty
@@ -31,16 +31,17 @@
3131

3232
import argparse
3333
import datetime
34+
import json
3435
import os
3536
import re
3637
import subprocess
3738
import sys
3839

39-
# CONFIGURATION
40-
DB_HOST = 'submitty.cs.myuniversity.edu'
41-
DB_USER = 'hsdbu'
42-
DB_PASS = 'DB.p4ssw0rd' # CHANGE THIS! DO NOT USE 'DB.p4ssw0rd'
43-
DUMP_PATH = '/var/local/submitty-dumps'
40+
# DATABASE CONFIGURATION PATH
41+
DB_CONFIG_PATH = '/usr/local/submitty/config/database.json'
42+
43+
# WHERE DUMP FILES ARE WRITTEN
44+
DUMP_PATH = '/var/local/submitty/submitty-dumps'
4445

4546
def delete_obsolete_dumps(working_path, expiration_stamp):
4647
"""
@@ -111,6 +112,14 @@ def main():
111112
else:
112113
semester = args.t
113114

115+
# GET DATABASE CONFIG FROM SUBMITTY
116+
fh = open(DB_CONFIG_PATH, "r")
117+
db_config = json.load(fh)
118+
fh.close()
119+
DB_HOST = db_config['database_host']
120+
DB_USER = db_config['database_user']
121+
DB_PASS = db_config['database_password']
122+
114123
# GET ACTIVE COURSES FROM 'MASTER' DB
115124
try:
116125
sql = "select course from courses where semester='{}'".format(semester)

pam_accounts/accounts.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@
3232
error_reporting(null);
3333
ini_set('display_errors', '0');
3434

35-
//Database access
36-
define('DB_LOGIN', 'submitty_dbuser');
37-
define('DB_PASSWD', 'submitty_dbuser_pa55W0rd');
38-
define('DB_HOST', 'localhost');
35+
//Database access configuration from Submitty
36+
define('DB_CONFIG_PATH', '/usr/local/submitty/config/database.json');
3937

4038
//Location of accounts creation error log file
4139
define('ERROR_LOG_FILE', 'accounts_script_error.log');
@@ -88,6 +86,11 @@ public function __construct() {
8886
exit("This script must be run as root." . PHP_EOL);
8987
}
9088

89+
//This is run from the command line, not a webpage.
90+
if (PHP_SAPI !== 'cli') {
91+
exit("This script must be run from the command line." . PHP_EOL);
92+
}
93+
9194
//Init class properties, quit on error.
9295
if ($this->init() === false) {
9396
exit(1);
@@ -211,10 +214,13 @@ private function process() {
211214
* @return boolean TRUE on success, FALSE when there is a problem.
212215
*/
213216
private function db_connect() {
214-
$db_user = DB_LOGIN;
215-
$db_pass = DB_PASSWD;
216-
$db_host = DB_HOST;
217-
self::$db_conn = pg_connect("host={$db_host} dbname=submitty user={$db_user} password={$db_pass} sslmode=prefer");
217+
$json_str = file_get_contents(DB_CONFIG_PATH);
218+
$db_config = json_decode($json_str, true);
219+
$db_host = $db_config['database_host'];
220+
$db_user = $db_config['database_user'];
221+
$db_pass = $db_config['database_password'];
222+
223+
self::$db_conn = pg_connect("dbname=submitty host={$db_host} user={$db_user} password={$db_pass} sslmode=prefer");
218224
if (pg_connection_status(self::$db_conn) !== PGSQL_CONNECTION_OK) {
219225
$this->log_it(pg_last_error(self::$db_conn));
220226
return false;

sample_bin/map_course.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
$db_config = json_decode($json_str, true);
3737

3838
//Connect to master DB.
39-
$db_conn = pg_connect("dbname=submitty host={$db_config['database_host']} user={$db_config['database_user']} password={$db_config['database_password']}");
39+
$db_conn = pg_connect("dbname=submitty host={$db_config['database_host']} user={$db_config['database_user']} password={$db_config['database_password']} sslmode=prefer");
4040
if (pg_connection_status($db_conn) !== PGSQL_CONNECTION_OK) {
4141
exit(sprintf("ERROR: Could not establish connection to Submitty Master DB%sCheck configuration at %s%s", PHP_EOL, DB_CONFIG_PATH, PHP_EOL));
4242
}

0 commit comments

Comments
 (0)