def load_config(challenge): # Get a random certificate and config certdb = Db.Challenge(challenge).certificates.srandmember() fd, path = tempfile.mkstemp(prefix='naumachia', suffix='.ovpn') with open(fd, 'w') as f: f.write(certdb.text) return path
def load_config(challenge): """Load a random ovpn config for the challenge and save it to a temp file""" certdb = Db.Challenge(challenge).certificates.srandmember() fd, path = tempfile.mkstemp(prefix='naumachia', suffix='.ovpn') with open(fd, 'w') as f: f.write(certdb.text) return path
def load_strategy(challenge): """Load a random strategy from the list provided in the config or all compatible challenges if that list is empty""" stratname = Db.Challenge(challenge).strategies.srandmember() if stratname is not None: strats = [s for s in strategies if s.name == stratname] if not strats: raise ValueError("Unkown named strategy {:s}".format(stratname)) else: strats = [s for s in strategies if challenge in s.challenges] if not strats: raise ValueError( "No strategy for challenge {:s}".format(challenge)) return random.choice(strats)
def load_config(): with open(LOADER_CONFIG, 'r') as f: return yaml.load(f) if __name__ == "__main__": # Open the connection to redis Db.redis = redis.Redis(host=REDIS_ADDR, port=REDIS_PORT) # Load the yaml config for this test config = load_config() logging.debug("Configuration to satisfy: %r", config) for challenge, settings in config.items(): chaldb = Db.Challenge(challenge) chaldb.ready = False Db.challenges.add(chaldb) logger.info('Loading certificates for %s', challenge) ls = requests.get(urljoin(REGISTRAR_URL, challenge + '/list')).json() cns = {entry['cn'] for entry in ls if istestcn(entry['cn'])} logger.debug("Exisitng test certificates: %r", cns) diff = settings['certificates'] - len(cns) if diff > 0: logger.info('Adding %d certificates', diff) for _ in range(diff): cn = gentestcn()