예제 #1
0
 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()
예제 #2
0
 def test_with_fail(self):
     name = random_name()
     m1 = DynamoDbMutex(name=name, holder=random_name())
     m1.lock()
     m2 = DynamoDbMutex(name=name, holder=random_name())
     exceptionHappened = False
     try:
         with m2:
             time.sleep(3)
     except AcquireLockFailedError:
         print("In exception handler")
         self.assertFalse(m2.is_locked())
         exceptionHappened = True
     self.assertTrue(exceptionHappened)
예제 #3
0
 def test_ttl(self):
     os.environ['DD_MUTEX_TABLE_NAME'] = random_name()
     name = random_name()
     caller = "myself"
     m1 = DynamoDbMutex(name=name,
                        holder=caller,
                        timeoutms=2 * 1000,
                        ttl_minutes=2)
     m1.lock()
     item = m1.get_raw_lock()
     diff = item['Item']['ttl'] - item['Item']['expire_ts'] // 1000
     print(diff)
     self.assertTrue(diff > 0 and diff <= 2 * 60)
     DynamoDbMutex.delete_table()
     del os.environ['DD_MUTEX_TABLE_NAME']
예제 #4
0
 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')
예제 #5
0
 def test_mutual_exclusion(self):
     m = DynamoDbMutex(random_name(), holder=random_name())
     m.lock()
     self.assertFalse(m.lock())
     m.release()
예제 #6
0
 def test_timeout(self):
     m = DynamoDbMutex(random_name(), "myself", 3 * 1000)
     m.lock()
     time.sleep(5)
     self.assertTrue(m.lock())
     m.release()
예제 #7
0
 def test_create(self):
     m = DynamoDbMutex(random_name(), "myself", 3 * 1000)
     assert (m.lock())
     m.release()
예제 #8
0
 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')