class TestSecretManager(fake_filesystem_unittest.TestCase): def setUp(self): self.setUpPyfakefs() os.makedirs(TELESERVER_DIR) self.manager = SecretManager() def test_empty_secret_file(self): self.manager.save_secrets() secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/secret.ini') self.assertIn('TOKEN', secrets) self.assertIn('KEY', secrets) self.assertIn('key', secrets['KEY']) self.assertNotEqual(secrets['KEY']['key'], '') self.assertIsInstance(secrets['KEY']['key'], str) self.assertEqual(secrets['TOKEN'], {}) def test_get_secret_key(self): self.assertNotEqual(self.manager.get_secret_key(), '') self.assertIsInstance(self.manager.get_secret_key(), str) def test_create_token(self): token = self.manager.create_token(name='test') secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/secret.ini') self.assertIn('test', secrets['TOKEN']) self.assertEqual(token, secrets['TOKEN']['test'])
def setUp(self): self.setUpPyfakefs() os.makedirs(TELESERVER_DIR) sec = SecretManager() sec.create_secrets_for_thermal_camera(self.login, self.password, self.ip_address, self.channel) self.camera = ThermalCamera()
class TestSecretManager(fake_filesystem_unittest.TestCase): def setUp(self): self.setUpPyfakefs() os.makedirs(TELESERVER_DIR) self.manager = SecretManager() def test_empty_secret_file(self): self.manager.save_secrets() secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/IoT_secret.ini') self.assertIn('THERMAL_CAMERA', secrets) self.assertIn('KEY', secrets) self.assertIn('key', secrets['KEY']) self.assertNotEqual(secrets['KEY']['key'], '') self.assertIsInstance(secrets['KEY']['key'], str) self.assertEqual(secrets['THERMAL_CAMERA'], {}) def test_get_secret_key(self): self.assertNotEqual(self.manager.get_secret_key(), '') self.assertIsInstance(self.manager.get_secret_key(), str) def test_create_secrets_for_thermal_camera(self): self.manager.create_secrets_for_thermal_camera('test', 'secret', '127.0.0.1', '1') secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/IoT_secret.ini') self.assertEqual('test', secrets['THERMAL_CAMERA']['login']) self.assertNotEqual('secret', secrets['THERMAL_CAMERA']['password']) self.assertEqual('127.0.0.1', secrets['THERMAL_CAMERA']['ip_address']) self.assertEqual('1', secrets['THERMAL_CAMERA']['channel']) decrypted_password = self.manager.decrypt( secrets['KEY']['key'], secrets['THERMAL_CAMERA']['password']) self.assertEqual('secret', decrypted_password) def test_thermal_camera_credentials(self): self.manager.create_secrets_for_thermal_camera('test', 'secret', '127.0.0.1', '1') out = self.manager.thermal_camera_credentials() self.assertIn('login', out) self.assertIn('password', out) self.assertIn('ip_address', out) self.assertIn('channel', out) self.assertEqual(out['login'], 'test') self.assertEqual(out['password'], 'secret') self.assertEqual(out['ip_address'], '127.0.0.1') self.assertEqual(out['channel'], '1')
def __init__(self, file=None): """Initializes class and reads settings from .ini file. :param file: path and name of the .ini file :type file: str """ if file: sec = SecretManager(secret_file=file) else: sec = SecretManager() config = sec.thermal_camera_credentials() self.login = config['login'] self.password = config['password'] self.ip_address = config['ip_address'] self.channel = config['channel'] self.address = ( f'rtsp://{self.login}:{self.password}@{self.ip_address}' f'/cam/realmonitor?channel={self.channel}&subtype=0') self.cam = cv2.VideoCapture(self.address)
import jwt import os import datetime import dash_html_components as html from layouts.keyboard_layout import FLAT_KEYBOARD_KEYS, KEYBOARD_NAMES from layouts.key_control_layout import SHORTCUT_NAMES, SHORTCUTS from layouts.main_layout import gui_layout, tab_render import tools.app_callbacks as callback from tools.common import OPENMEET_var from tools.secret_manager import SecretManager from tools.calendar_generation import sendToGoogleCalendar, initializeCalendar, desk_available import tools.system_calls as system from IoT_master.tmp_tab import desk_reservations sec = SecretManager() VALID_USERNAME_PASSWORD_PAIRS = sec.get_credentials_for_GUI() external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] server = flask.Flask(__name__) server.config['SECRET_KEY'] = sec.get_secret_key() app = dash.Dash(__name__, server=server, external_stylesheets=external_stylesheets) app.layout = gui_layout() app.title = 'teleserver' app.config['suppress_callback_exceptions'] = True if VALID_USERNAME_PASSWORD_PAIRS != {}: auth = dash_auth.BasicAuth(app, VALID_USERNAME_PASSWORD_PAIRS)
#!/usr/bin/python3 from tools.secret_manager import SecretManager import getpass import sys if __name__ == "__main__": sec = SecretManager() u = input('Please choose new username: '******'Please choose new password: '******'Please confirm new password: ', stream=None) if p == p2: sec.set_gui_credentials(u, p) sys.exit(0) else: sys.exit(1)
#!/usr/bin/python3 import argparse import flask from flask import jsonify from functools import wraps import jwt from data_drainer import get_data_for_variable, set_data_for_variable from tools.secret_manager import SecretManager server = flask.Flask(__name__) sec = SecretManager() server.config['SECRET_KEY'] = sec.get_secret_key() def token_required(f): """This is a decorator to verify whether API user provided valid token Token is required to operate through API :param f: Function to decorate :type f: function """ @wraps(f) def decorated(*args, **kwargs): """Wrapper to parser token argument, check whether token is correct and return unchanged function when correct """ post_args = flask.request.form.to_dict()
def setUp(self): self.setUpPyfakefs() os.makedirs(TELESERVER_DIR) self.manager = SecretManager()
class TestSecretManager(fake_filesystem_unittest.TestCase): def setUp(self): self.setUpPyfakefs() os.makedirs(TELESERVER_DIR) self.manager = SecretManager() def test_empty_secret_file(self): self.manager.save_secrets() secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/secret.ini') self.assertIn('PASS', secrets) self.assertIn('SERVICE_PRINCIPAL', secrets) self.assertIn('TOKEN_COOKIES', secrets) self.assertIn('KEY', secrets) self.assertIn('key', secrets['KEY']) self.assertNotEqual(secrets['KEY']['key'], '') self.assertIsInstance(secrets['KEY']['key'], str) self.assertEqual(secrets['PASS'], {}) self.assertEqual(secrets['SERVICE_PRINCIPAL'], {}) self.assertEqual(secrets['TOKEN_COOKIES'], {}) def test_get_secret_key(self): self.assertNotEqual(self.manager.get_secret_key(), '') self.assertIsInstance(self.manager.get_secret_key(), str) def test_set_gui_credentials_for_one_user(self): self.manager.set_gui_credentials(user='******', password='******') secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/secret.ini') self.assertIn('test', secrets['PASS']) decrypted_password = self.manager.decrypt(secrets['KEY']['key'], secrets['PASS']['test']) self.assertEqual('secret', decrypted_password) def test_set_gui_credentials_for_multiple_user(self): self.manager.set_gui_credentials(user='******', password='******') self.manager.set_gui_credentials(user='******', password='******') secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/secret.ini') self.assertIn('test', secrets['PASS']) decrypted_password = self.manager.decrypt(secrets['KEY']['key'], secrets['PASS']['test']) self.assertEqual('secret', decrypted_password) self.assertIn('test2', secrets['PASS']) decrypted_password = self.manager.decrypt(secrets['KEY']['key'], secrets['PASS']['test2']) self.assertEqual('secret2', decrypted_password) def test_verify_credentials(self): self.manager.set_gui_credentials(user='******', password='******') self.manager.set_gui_credentials(user='******', password='******') self.assertTrue( self.manager.verify_credentials(user='******', password='******')) self.assertTrue( self.manager.verify_credentials(user='******', password='******')) self.assertFalse( self.manager.verify_credentials(user='******', password='******')) self.assertFalse( self.manager.verify_credentials(user='******', password='******')) def test_delete_credentials_for_user(self): self.manager.set_gui_credentials(user='******', password='******') self.assertTrue( self.manager.verify_credentials(user='******', password='******')) self.manager.delete_credentails_for_user(user='******') self.assertFalse( self.manager.verify_credentials(user='******', password='******')) def test_get_credentials_fot_GUI(self): self.manager.set_gui_credentials(user='******', password='******') self.manager.set_gui_credentials(user='******', password='******') output = {'test': 'secret', 'test2': 'secret2'} self.assertDictEqual(self.manager.get_credentials_for_GUI(), output) def test_create_service_principal(self): token = self.manager.create_service_principal(name='test') self.assertNotEqual(token, '') self.assertIsInstance(token, str) secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/secret.ini') self.assertIn('test', secrets['SERVICE_PRINCIPAL']) self.assertNotEqual(secrets['SERVICE_PRINCIPAL']['test'], '') self.assertIsInstance(secrets['SERVICE_PRINCIPAL']['test'], str) def test_create_time_token_empty_data(self): self.manager.set_gui_credentials(user='******', password='******') test_data = {} out = self.manager.create_time_token(test_data) self.assertEqual(out['rc'], 1) def test_create_time_token_empty_password(self): self.manager.set_gui_credentials(user='******', password='******') test_data = {'user': '******'} out = self.manager.create_time_token(test_data) self.assertEqual(out['rc'], 1) def test_create_time_token_empty_lease(self): self.manager.set_gui_credentials(user='******', password='******') test_data = {'user': '******', 'password': '******'} out = self.manager.create_time_token(test_data) self.assertEqual(out['rc'], 1) def test_create_time_token_partial_empty_lease(self): self.manager.set_gui_credentials(user='******', password='******') test_data = { 'user': '******', 'password': '******', 'lease_days': '3', 'lease_hours': '0' } out = self.manager.create_time_token(test_data) self.assertEqual(out['rc'], 1) def test_create_time_token_correct_data(self): self.manager.set_gui_credentials(user='******', password='******') test_data = { 'user': '******', 'password': '******', 'lease_days': '3', 'lease_hours': '0', 'lease_minutes': '0', 'lease_seconds': '0' } out = self.manager.create_time_token(test_data) self.assertEqual(out['rc'], 0) secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/secret.ini') self.assertEqual(len(secrets['TOKEN_COOKIES']), 1) test_data = { 'user': '******', 'password': '******', 'lease_days': '0', 'lease_hours': '1', 'lease_minutes': '30', 'lease_seconds': '0' } out = self.manager.create_time_token(test_data) self.assertEqual(out['rc'], 0) secrets = configparser.ConfigParser() secrets.read(f'{TELESERVER_DIR}/secret.ini') self.assertEqual(len(secrets['TOKEN_COOKIES']), 2) def test_delete_time_token(self): self.manager.set_gui_credentials(user='******', password='******') test_data = { 'user': '******', 'password': '******', 'lease_days': '3', 'lease_hours': '0' } self.manager.create_time_token(test_data) out = self.manager.delete_time_token({'token_name': 'testuser'}) self.assertEqual(out['rc'], 0)