def __init__(self, device): self.environment = device.environment self.base_url = device.url # Prepare the certificate if self.environment == Environment.PRODUCTION: self.cert = device.temp_file_for_cert, device.temp_file_for_key elif self.environment == Environment.STAGING: self.cert = read_in_file(device.cert[0], 'utf-8').replace( '\r\n', '\n').replace('\n', '\t') else: self.cert = read_in_file('tests/dev.testing.crt', 'utf-8').replace( '\r\n', '\n').replace('\n', '\t')
def run(): parser = argparse.ArgumentParser(prog='socatlord', usage='''Call with a single argument *install* will install and enable socatlord to work as a systemd service (socatlord.service) *run* will shut down all socats that it previously spawned (free-range socats won't be touched) and restart them *stop* will terminate socats *view* will display config file''') parser.add_argument( '-v', action='store_true', help='Display what commands are ran and pipe socats to stdout') parser.add_argument( '--config', default='/etc/socatlord', help='Location of config file (default is /etc/socatlord)') parser.add_argument('operation', choices=['install', 'run', 'stop', 'view'], help='Operation to do') args = parser.parse_args() do_precheck(args.config, args.v) if args.operation == 'install': install_socatlord(args.v) elif args.operation == 'view': print(read_in_file(args.config, 'utf-8')) else: kill_all_socats(args.v) if args.operation == 'run': start_all_socats(args.config, args.v)
def get_dev_ca_cert() -> bytes: """ :return: the bytes sequence for SMOK's device signing CA """ ca_file = pkg_resources.resource_filename( __name__, '../certs/dev.crt', ) return read_in_file(ca_file)
def get_root_cert() -> bytes: """ :return: the bytes sequence for SMOK's master CA certificate """ ca_file = pkg_resources.resource_filename( __name__, '../certs/root.crt', ) return read_in_file(ca_file)
def command(): if len(sys.argv) < 2 or '-h' in sys.argv or '--help' in sys.argv: return print_help() script = read_in_file(sys.argv[1], 'utf-8') for file in sys.argv[2:]: img = Image(file) exec(script) img.save()
def kill_all_socats(verbose: bool = False): for socat in os.listdir('/var/run/socatlord'): path = os.path.join('/var/run/socatlord', socat) pid = int(read_in_file(path, 'utf-8')) try: if verbose: print('Killing %s' % (pid, )) os.kill(pid, 9) except PermissionError: print('Failed to kill %s with EPERM' % (pid, )) except OSError: print('Failed to kill %s' % (pid, )) os.unlink(path)
def install_socatlord(verbose: bool = False) -> None: filename = pkg_resources.resource_filename(__name__, 'systemd/socatlord.service') contents = read_in_file(filename, 'utf-8') if verbose: print('Writing /lib/systemd/system/socatlord.service') write_to_file('/lib/systemd/system/socatlord.service', contents, 'utf-8') if verbose: print('Calling systemctl daemon-reload') os.system('systemctl daemon-reload') if verbose: print('Calling systemctl enable socatlord.service') os.system('systemctl enable socatlord.service')
def get_baob_value(self, key: str) -> bytes: return read_in_file(os.path.join(self.__path, key))
def add_certificate(self, name: str): ca_file = pkg_resources.resource_filename(__name__, '../certs/%s' % (name, )) cert_pem_data = read_in_file(ca_file) cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_pem_data) self.store.add_cert(cert)