def test_create_delete_us_east_1(self): m = DynamoDbMutex(name=random_name(), holder=random_name(), region_name='us-east-1') assert (m.lock()) m.release() DynamoDbMutex.delete_table(region_name='us-east-1')
def test_release_expired(self): name = random_name() caller = "caller1" m1 = DynamoDbMutex(name=name, holder=caller, timeoutms=2 * 1000) m1.lock() time.sleep(3) caller = "caller2" m2 = DynamoDbMutex(name=name, holder=caller, timeoutms=2 * 1000) self.assertTrue(m2.lock()) m1.release() self.assertTrue(m2.is_locked()) m2.release()
reloadDNS = False reloadNginx = False if args.dnssec: mutex = DynamoDbMutex('pawnode-certifier-dnssec', holder=getfqdn(), timeoutms=300 * 1000) try: get_dnssec_keys() for zone in zones: reloadDNS |= make_dnssec_keys(zone, mutex) reloadDNS |= sign_zone(zone, args.renew_dnssec) except NoLockError: print('Skipping DNSSEC. Can\'t get lock.') finally: if mutex.locked: mutex.release() if reloadDNS: system('chown -R pdns:pdns %s' % DNSSEC_DIR) system('pdns_control reload') if args.ssl: mutex = DynamoDbMutex('pawnode-certifier-ssl', holder=getfqdn(), timeoutms=300 * 1000) try_acme = args.acme try: for site in sites: try: reloadNginx |= get_ssl_for_site(site, try_acme, mutex, ccConfig) except NoLockError:
def test_mutual_exclusion(self): m = DynamoDbMutex(random_name(), holder=random_name()) m.lock() self.assertFalse(m.lock()) m.release()
def test_timeout(self): m = DynamoDbMutex(random_name(), "myself", 3 * 1000) m.lock() time.sleep(5) self.assertTrue(m.lock()) m.release()
def test_create(self): m = DynamoDbMutex(random_name(), "myself", 3 * 1000) assert (m.lock()) m.release()
def sign(self, handle, test_mode=False): # This code acquires a mutex lock using https://github.com/chiradeep/dyndb-mutex # generate a unique name for this process/thread ddb_region = environ['REGION'] my_name = str(uuid.uuid4()).split("-")[0] if self.is_block(): sig_type = 'Baking' else: sig_type = 'Endorsement' m = DynamoDbMutex(sig_type, holder=my_name, timeoutms=60 * 1000, region_name=ddb_region) locked = m.lock() # attempt to acquire the lock if locked: encoded_sig = '' data_to_sign = self.payload logging.info('About to sign {} with key handle {}'.format( data_to_sign, handle)) if self.valid_block_format(data_to_sign): logging.info('Block format is valid') if self.is_block() or self.is_endorsement(): logging.info('Preamble is valid') if self.not_already_signed(): if test_mode: return self.TEST_SIGNATURE else: logging.info( 'About to sign with HSM client. Slot = {}, lib = {}, handle = {}' .format(self.hsm_slot, self.hsm_libfile, handle)) with HsmClient(slot=self.hsm_slot, pin=self.hsm_pin, pkcs11_lib=self.hsm_libfile) as c: hashed_data = blake2b( hex_to_bytes(data_to_sign), digest_size=32).digest() logging.info('Hashed data to sign: {}'.format( hashed_data)) sig = c.sign(handle=handle, data=hashed_data, mechanism=HsmMech.ECDSA) logging.info('Raw signature: {}'.format(sig)) encoded_sig = RemoteSigner.b58encode_signature( sig) logging.info( 'Base58-encoded signature: {}'.format( encoded_sig)) else: logging.error('Invalid level') m.release() # release the lock raise Exception('Invalid level') else: logging.error('Invalid preamble') m.release() # release the lock raise Exception('Invalid preamble') else: logging.error('Invalid payload') m.release() # release the lock raise Exception('Invalid payload') m.release() # release the lock return encoded_sig else: # lock could not be acquired logging.error('Could not acquire lock') raise Exception('Could not acquire lock')