コード例 #1
0
ファイル: reporting.py プロジェクト: subs-comps-git/toolbox
def main():
    db = 'swgoh.db'
    time_period = '2018-08-29'
    today = maya.now().date.strftime('%Y-%m-%d')
    csv_file = f'gp_delta_hssf_{today}.csv'
    conn = connect_db(db)
    report = report_gp_deltas(conn, time_period)
    if not report:
        print("No rows found.")
    else:
        print(report)
        export_csv(csv_file, report)
    conn.close()
コード例 #2
0
    def _parse_chore_info(self, info: str):
        """Parse chore info from the message."""
        if self.command == "/cancel":
            self.send_message("Okay, no new chore then.")
            return

        args = list(map(str.strip, info.translate(self.TRANSLATION_TABLE).split("|")))
        arg_title, arg_due_at, arg_repetition = args + [None] * (3 - len(args))  # type: ignore

        alarm_at = (maya.when(arg_due_at) if arg_due_at else maya.now()).datetime()
        fields = {"title": arg_title, "due_at": alarm_at, "alarm_at": alarm_at, "repetition": arg_repetition}
        db.session.add(Chore(**fields))
        db.session.commit()

        self.send_message("The chore was added.")
コード例 #3
0
ファイル: test_maya.py プロジェクト: rlugojr/maya
def test_comparison_operations():
    now = maya.now()
    now_copy = copy.deepcopy(now)
    tomorrow = maya.when('tomorrow')

    assert (now == now_copy) is True
    assert (now == tomorrow) is False

    assert (now != now_copy) is False
    assert (now != tomorrow) is True

    assert (now < now_copy) is False
    assert (now < tomorrow) is True

    assert (now <= now_copy) is True
    assert (now <= tomorrow) is True

    assert (now > now_copy) is False
    assert (now > tomorrow) is False

    assert (now >= now_copy) is True
    assert (now >= tomorrow) is False

    # Check Exceptions
    with pytest.raises(TypeError):
        now == 1
    with pytest.raises(TypeError):
        now != 1
    with pytest.raises(TypeError):
        now < 1
    with pytest.raises(TypeError):
        now <= 1
    with pytest.raises(TypeError):
        now > 1
    with pytest.raises(TypeError):
        now >= 1
コード例 #4
0
def test_federated_alice_can_decrypt(federated_alice, federated_bob):
    """
    Test that alice can decrypt data encrypted by an enrico
    for her own derived policy pubkey.
    """

    # Setup the policy details
    m, n = 2, 3
    policy_end_datetime = maya.now() + datetime.timedelta(days=5)
    label = b"this_is_the_path_to_which_access_is_being_granted"

    policy = federated_alice.create_policy(
        bob=federated_bob,
        label=label,
        m=m,
        n=n,
        expiration=policy_end_datetime,
    )

    enrico = Enrico.from_alice(
        federated_alice,
        policy.label,
    )
    plaintext = b"this is the first thing i'm encrypting ever."

    # use the enrico to encrypt the message
    message_kit, signature = enrico.encrypt_message(plaintext)

    # decrypt the data
    decrypted_data = federated_alice.verify_from(enrico,
                                                 message_kit,
                                                 signature=signature,
                                                 decrypt=True,
                                                 label=policy.label)

    assert plaintext == decrypted_data
コード例 #5
0
 def add_pendingtime(self, client, pendingtime, ticket_id):
     # Format date
     try:
         pending_time = maya.MayaDT.from_rfc3339(pendingtime)
     except Exception as e:
         raise Exception(
             "Error with Pending Time. Date may not be formatted correctly")
     time_now = maya.now()
     # create Timedelta
     time_diff = pending_time - time_now
     # get days and hours
     days, hours = time_diff.days, time_diff.seconds // 3600
     try:
         client.ticket_update_set_pending(ticket_id=ticket_id,
                                          pending_days=days,
                                          pending_hours=hours)
     except Exception as e:
         if "TicketUpdate: User does not have access to the ticket!" in e.message:
             raise Exception(
                 f"Ticket {ticket_id} may not exist or user does not have access to the ticket\n"
                 f"Error: {e}")
         else:
             self.logger.error(
                 f"An error occurred while updating the pending time: {e}")
コード例 #6
0
def test_federated_grant(federated_alice, federated_bob):
    # Setup the policy details
    m, n = 2, 3
    policy_end_datetime = maya.now() + datetime.timedelta(days=5)
    label = b"this_is_the_path_to_which_access_is_being_granted"

    # Create the Policy, granting access to Bob
    policy = federated_alice.grant(federated_bob,
                                   label,
                                   m=m,
                                   n=n,
                                   expiration=policy_end_datetime)

    # Check the policy ID
    policy_id = keccak_digest(policy.label + bytes(policy.bob.stamp))
    assert policy_id == policy.id

    # Check Alice's active policies
    assert policy_id in federated_alice.active_policies
    assert federated_alice.active_policies[policy_id] == policy

    # The number of accepted arrangements at least the number of Ursulas we're using (n)
    assert len(policy._accepted_arrangements) >= n

    # The number of actually enacted arrangements is exactly equal to n.
    assert len(policy._enacted_arrangements) == n

    # Let's look at the enacted arrangements.
    for kfrag in policy.kfrags:
        arrangement = policy._enacted_arrangements[kfrag]

        # Get the Arrangement from Ursula's datastore, looking up by the Arrangement ID.
        with arrangement.ursula.datastore.describe(
                PolicyArrangement, arrangement.id.hex()) as policy_arrangement:
            retrieved_kfrag = policy_arrangement.kfrag
        assert kfrag == retrieved_kfrag
コード例 #7
0
ファイル: test_actors.py プロジェクト: ngocson2vn/nucypher
    def test_miner_locking_tokens(self, testerchain, three_agents, miner):
        token_agent, miner_agent, policy_agent = three_agents
        testerchain.ether_airdrop(amount=10000)
        assert constants.MIN_ALLOWED_LOCKED < miner.token_balance, "Insufficient miner balance"

        expiration = maya.now().add(days=constants.MIN_LOCKED_PERIODS)
        miner.stake(
            amount=int(constants.MIN_ALLOWED_LOCKED
                       ),  # Lock the minimum amount of tokens
            expiration=expiration)

        # Verify that the escrow is "approved" to receive tokens
        allowance = miner_agent.token_agent.contract.functions.allowance(
            miner.checksum_public_address,
            miner_agent.contract_address).call()
        assert 0 == allowance

        # Staking starts after one period
        locked_tokens = miner_agent.contract.functions.getLockedTokens(
            miner.checksum_public_address).call()
        assert 0 == locked_tokens
        locked_tokens = miner_agent.contract.functions.getLockedTokens(
            miner.checksum_public_address, 1).call()
        assert constants.MIN_ALLOWED_LOCKED == locked_tokens
コード例 #8
0
    def _grant(teacher_uri):

        # Alice fetched Bob's public keys from the side channel
        bob_keys = side_channel.fetch_bob_public_keys()
        bob_encrypting_key = bob_keys.bob_encrypting_key
        bob_verifying_key = bob_keys.bob_verifying_key

        grant_args = ('--mock-networking',
                      '--json-ipc',
                      'alice', 'grant',
                      '--network', TEMPORARY_DOMAIN,
                      '--teacher-uri', teacher_uri,
                      '--config-file', alice_configuration_file_location,
                      '--m', 2,
                      '--n', 3,
                      '--value', Web3.toWei(1, 'ether'),
                      '--expiration', (maya.now() + datetime.timedelta(days=3)).iso8601(),
                      '--label', random_label,
                      '--bob-encrypting-key', bob_encrypting_key,
                      '--bob-verifying-key', bob_verifying_key)

        if federated:
            grant_args += ('--federated-only',)
        else:
            grant_args += ('--provider-uri', TEST_PROVIDER_URI)

        grant_result = click_runner.invoke(nucypher_cli, grant_args, catch_exceptions=False, env=envvars)
        assert grant_result.exit_code == 0

        grant_result = json.loads(grant_result.output)

        # TODO: Expand test to consider manual treasure map handing
        # # Alice puts the Treasure Map somewhere Bob can get it.
        # side_channel.save_treasure_map(treasure_map=grant_result['result']['treasure_map'])

        return grant_result
コード例 #9
0
ファイル: jobs.py プロジェクト: dammitjim/badfeed
def sync_feed(feed: Feed):
    """Sync feed parsing against the database."""
    with configure_scope() as scope:
        logger.debug(f"Processing feed {feed.link}")
        scope.set_tag("feed", feed.title)

        r = requests.get(feed.link,
                         headers={"User-Agent": REQUESTS_USER_AGENT})

        scope.set_extra("body", r.text)

        if r.status_code != 200:
            logger.error(f"{r.status_code} received when scraping {feed.link}",
                         exc_info=True)
            IngestLog.objects.create(feed=feed,
                                     state=IngestLog.STATE_NOT_RESPONDING,
                                     body=r.text)
            return

        parser = RSSParser(feed)
        parser.parse(r)

        feed.date_last_scraped = maya.now().datetime()
        feed.save()
コード例 #10
0
ファイル: df_to_sheet.py プロジェクト: thd-bburrets/SSAPI-101
def new_sheet_from_df(ss, df, sheet_name=''):
    """
    Call if no sheet_id given, as Smartsheet needs somewhere to import the DataFrame. Uses column names of DataFrame.
    TODO: add option to create new sheet in specific location
    :param ss:                      initialized smartsheet client instance
    :param df:                      DataFrame, required; pandas DataFrame
    :param sheet_name:              str, optional; name of new Smartsheet to be created (if '', use date/timestamp)
    :return:                        Smartsheet sheet_id
    """
    # col_list = df_to_ss_col_list(df)                                        # retrieve a list of column dictionaries

    if sheet_name == '':
        # if you need datetime, uncomment the line below, and remove the line below that - but seriously, blech city
        # sheet_name = 'newsheet ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
        sheet_name = 'new sheet ' + str(maya.now())                         # create new sheet with datetime stamp

    temp_sheet = ss.models.Sheet({                                          # new sheets need a name and a column list
        'name': sheet_name,
        'columns': df_to_ss_col_list(df)                                    # retrieve a list of column dictionaries
    })

    new_sheet_id = ss.Home.create_sheet(temp_sheet).result.id               # create a new sheet and get its id

    return new_sheet_id                                                     # return the sheet id for appending df data
コード例 #11
0
ファイル: test_maya.py プロジェクト: aaronjeline/maya
def test_dt_tz_translation():
    d1 = maya.now().datetime()
    d2 = maya.now().datetime(to_timezone='US/Eastern')
    assert (d1.hour - d2.hour) % 24 == 5
コード例 #12
0
ファイル: api.py プロジェクト: Marklb/nucypher-api-example
class Api(object):
    """
    Simplified api for using nucypher.

    Some of the methods are not named correctly if thinking about nucypher,
    instead they are simplified to terms that relate to the terms currently in
    the AgriBlockchainApp.

    NOTE: Only meant to assist with providing a rest api for AgriBlockchainApp
    development.
    """
    VERSION = '3.0'

    # TODO: Don't use 1 for `m` and `n`
    DEFAULT_M = 1
    DEFAULT_N = 1

    DEFAULT_POLICY_EXPIRATION = maya.now() + datetime.timedelta(days=365)

    def __init__(self,
                 teacher_dht_port: int = 3500,
                 teacher_rest_port: int = 3600,
                 node_meta_dir: str = "../examples/examples-runtime-cruft"):
        self.teacher_dht_port = teacher_dht_port

        if teacher_rest_port:
            self.teacher_rest_port = teacher_rest_port
        else:
            self.teacher_rest_port = int(self.teacher_dht_port) + 100

        with open(
                "{}/node-metadata-{}".format(node_meta_dir,
                                             self.teacher_rest_port),
                "r") as f:
            f.seek(0)
            teacher_bytes = binascii.unhexlify(f.read())

        self.ursula = Ursula.from_bytes(teacher_bytes, federated_only=True)

    def gen_keypair(self):
        """
        Generate a keypair using Umbral

        :return: private_key, public_key
        """
        private_key = keys.UmbralPrivateKey.gen_key()
        public_key = private_key.get_pubkey()
        return private_key, public_key

    def create_policy(self, label: bytes, alice_privkey: UmbralPrivateKey,
                      bob_pubkey: UmbralPublicKey, policy_expiration, m: int,
                      n: int):
        """
        Create a Policy with Alice granting Bob access to `label` DataSource

        :param label: A label to represent the policies data
        :param alice_privkey: Alice's private key
        :param bob_pubkey: Bob's public key
        :param policy_expiration: Datetime of policy expiration duration
        :param m: Minimum number of KFrags needed to rebuild ciphertext
        :param n: Total number of rekey shares to generate

        :return: The policy granted to Bob
        """
        # This is not how this should be implemented, but I am still figuring out
        # the keying material and why it is randomly generated when a character is
        # initialized, instead of being derived from the keys like the other powers
        # or explained how it should be stored.
        d = DelegatingPower()
        d.umbral_keying_material = UmbralKeyingMaterial.from_bytes(
            alice_privkey.to_bytes() + alice_privkey.get_pubkey().to_bytes())

        # Initialize Alice
        ALICE = Alice(
            crypto_power_ups=[
                SigningPower(keypair=SigningKeypair(alice_privkey)),
                EncryptingPower(keypair=EncryptingKeypair(alice_privkey)),
                # DelegatingPower
                d
            ],
            network_middleware=RestMiddleware(),
            known_nodes=(self.ursula, ),
            federated_only=True,
            always_be_learning=True)

        # Initialize Bob
        BOB = Bob(crypto_power_ups=[
            SigningPower(pubkey=bob_pubkey),
            EncryptingPower(pubkey=bob_pubkey)
        ],
                  known_nodes=(self.ursula, ),
                  federated_only=True,
                  always_be_learning=True)

        # Alice grants a policy for Bob
        policy = ALICE.grant(BOB,
                             label,
                             m=m,
                             n=n,
                             expiration=policy_expiration)

        return policy

    def revoke_policy(self, label, alice_privkey: UmbralPrivateKey,
                      bob_pubkey: UmbralPublicKey):
        """
        TODO: Figure out the correct way to revoke instead of using a custom
            implementation

        Revoke a Policy that Alice granted Bob to access `label` DataSource

        :param label: A label to represent the policies data
        :param alice_privkey: Alice's private key
        :param bob_pubkey: Bob's public key
        """
        # TODO: Figure out a way to allow revoking with the new way arrangements
        #   are stored.

        # NOTE: Not working anymore
        # alice_pubkey = alice_privkey.get_pubkey()
        # hrac = keccak_digest(bytes(alice_pubkey) + bytes(bob_pubkey) + label)

        # db_name = 'non-mining-proxy-node'
        # test_server = ProxyRESTServer('localhost', 3601, db_name)
        # test_server.start_datastore(db_name)

        # with ThreadedSession(test_server.db_engine) as session:
        #     test_server.datastore.del_policy_arrangement(
        #         hrac=hrac.hex().encode(),
        #         session=session
        #     )

    def encrypt_for_policy(self, policy_pubkey: UmbralPublicKey,
                           plaintext: bytes):
        """
        Encrypt data for a Policy

        :param policy_pubkey: Policy public key
        :param plaintext: Plaintext bytes to encrypt

        :return: data_source, message_kit, _signature
        """
        # First we make a DataSource for this policy
        data_source = DataSource(policy_pubkey_enc=policy_pubkey)

        # Generate a MessageKit for the policy
        message_kit, _signature = data_source.encapsulate_single_message(
            plaintext)

        return data_source, message_kit, _signature

    def decrypt_for_policy(self, label: bytes, message_kit: UmbralMessageKit,
                           alice_pubkey: UmbralPublicKey,
                           bob_privkey: UmbralPrivateKey,
                           policy_pubkey: UmbralPublicKey,
                           data_source_pubkey: UmbralPublicKey):
        """
        Decrypt data for a Policy

        :param label: A label to represent the policies data
        :param message_kit: UmbralMessageKit
        :param alice_pubkey: Alice's public key
        :param bob_privkey: Bob's private key
        :param policy_pubkey: Policy's private key
        :param data_source_pubkey: DataSource's private key

        :return: The decrypted cleartext
        """
        print('decrypt_for_policy')
        # Initialize Bob
        BOB = Bob(crypto_power_ups=[
            SigningPower(keypair=SigningKeypair(bob_privkey)),
            EncryptingPower(keypair=EncryptingKeypair(bob_privkey))
        ],
                  known_nodes=(self.ursula, ),
                  federated_only=True,
                  always_be_learning=True)
        print('-=-=-=')
        print(label)
        print(bytes(alice_pubkey))
        # Bob joins the policy so that he can receive data shared on it
        BOB.join_policy(
            label,  # The label - he needs to know what data he's after.
            bytes(alice_pubkey
                  ),  # To verify the signature, he'll need Alice's public key.
            # verify_sig=True,  # And yes, he usually wants to verify that signature.
            # He can also bootstrap himself onto the network more quickly
            # by providing a list of known nodes at this time.
            node_list=[("localhost", 3601)])
        print('-=-=-=2')
        # Bob needs to reconstruct the DataSource.
        datasource_as_understood_by_bob = DataSource.from_public_keys(
            policy_public_key=policy_pubkey,
            datasource_public_key=bytes(data_source_pubkey),
            label=label)
        print('-=-=-=3')
        # NOTE: Not sure if I am doing something wrong or if this is missing
        #   from the serialized bytes
        message_kit.policy_pubkey = policy_pubkey

        # Now Bob can retrieve the original message.  He just needs the MessageKit
        # and the DataSource which produced it.
        cleartexts = BOB.retrieve(message_kit=message_kit,
                                  data_source=datasource_as_understood_by_bob,
                                  alice_verifying_key=alice_pubkey)
        print('-=-=-=4')
        return cleartexts[0]
コード例 #13
0
ファイル: policies.py プロジェクト: decanus/nucypher
    def sample_essential(
        self,
        handpicked_ursulas: Set[Ursula],
        learner_timeout: int = 1,
        timeout: int = 10,
        discover_on_this_thread: bool = False
    ) -> Set[Ursula]:  # TODO #843: Make timeout configurable

        handpicked_addresses = [
            ursula.checksum_address for ursula in handpicked_ursulas
        ]
        reservoir = self.alice.get_stakers_reservoir(
            duration=self.duration_periods, without=handpicked_addresses)

        quantity_remaining = self.n - len(handpicked_ursulas)
        if len(reservoir) < quantity_remaining:
            error = f"Cannot create policy with {self.n} arrangements"
            raise self.NotEnoughBlockchainUrsulas(error)

        # Handpicked Ursulas are not necessarily known
        to_check = list(handpicked_ursulas) + reservoir.draw(
            quantity_remaining)
        checked = []

        # Sample stakers in a loop and feed them to the learner to check
        # until we have enough in `selected_ursulas`.

        start_time = maya.now()

        while True:

            # Check if the sampled addresses are already known.
            # If we're lucky, we won't have to wait for the learner iteration to finish.
            checked += [x for x in to_check if x in self.alice.known_nodes]
            to_check = [x for x in to_check if x not in self.alice.known_nodes]

            if len(checked) >= self.n:
                break

            # The number of new nodes to draw on each iteration.
            # The choice of this depends on how expensive it is to check a node for validity,
            # and how likely is it for a picked node to be offline.
            # We assume here that it is unlikely, and be conservative.
            drawing_step = self.n - len(checked)

            # Draw a little bit more nodes, if there are any
            to_check += reservoir.draw_at_most(drawing_step)

            delta = maya.now() - start_time
            if delta.total_seconds() >= timeout:
                still_checking = ', '.join(to_check)
                quantity_remaining = self.n - len(checked)
                raise RuntimeError(
                    f"Timed out after {timeout} seconds; "
                    f"need {quantity_remaining} more, still checking {still_checking}."
                )

            self.alice.block_until_specific_nodes_are_known(
                to_check,
                learn_on_this_thread=discover_on_this_thread,
                allow_missing=len(to_check),
                timeout=learner_timeout)

        # We only need `n` nodes. Pick the first `n` ones,
        # since they were the first drawn, and hence have the priority.
        found_ursulas = [
            self.alice.known_nodes[address] for address in checked[:self.n]
        ]

        # Randomize the output to avoid the largest stakers always being the first in the list
        system_random = random.SystemRandom()
        system_random.shuffle(found_ursulas)  # inplace

        return set(found_ursulas)
コード例 #14
0
def test_alices_powers_are_persistent(federated_ursulas, tmpdir):

    # Create a non-learning AliceConfiguration
    alice_config = AliceConfiguration(config_root=os.path.join(
        tmpdir, 'nucypher-custom-alice-config'),
                                      network_middleware=MockRestMiddleware(),
                                      known_nodes=federated_ursulas,
                                      start_learning_now=False,
                                      federated_only=True,
                                      save_metadata=False,
                                      reload_metadata=False)

    # Generate keys and write them the disk
    alice_config.initialize(password=INSECURE_DEVELOPMENT_PASSWORD)

    # Unlock Alice's keyring
    alice_config.keyring.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)

    # Produce an Alice
    alice = alice_config()  # or alice_config.produce()

    # Save Alice's node configuration file to disk for later use
    alice_config_file = alice_config.to_configuration_file()

    # Let's save Alice's public keys too to check they are correctly restored later
    alices_verifying_key = alice.public_keys(SigningPower)
    alices_receiving_key = alice.public_keys(DecryptingPower)

    # Next, let's fix a label for all the policies we will create later.
    label = b"this_is_the_path_to_which_access_is_being_granted"

    # Even before creating the policies, we can know what will be its public key.
    # This can be used by Enrico (i.e., a Data Source) to encrypt messages
    # before Alice grants access to Bobs.
    policy_pubkey = alice.get_policy_encrypting_key_from_label(label)

    # Now, let's create a policy for some Bob.
    m, n = 3, 4
    policy_end_datetime = maya.now() + datetime.timedelta(days=5)

    bob = Bob(federated_only=True,
              start_learning_now=False,
              network_middleware=MockRestMiddleware())

    bob_policy = alice.grant(bob,
                             label,
                             m=m,
                             n=n,
                             expiration=policy_end_datetime)

    assert policy_pubkey == bob_policy.public_key

    # ... and Alice and her configuration disappear.
    del alice
    del alice_config

    ###################################
    #        Some time passes.        #
    #               ...               #
    # (jmyles plays the Song of Time) #
    #               ...               #
    #       Alice appears again.      #
    ###################################

    # A new Alice is restored from the configuration file
    new_alice_config = AliceConfiguration.from_configuration_file(
        filepath=alice_config_file,
        network_middleware=MockRestMiddleware(),
        known_nodes=federated_ursulas,
        start_learning_now=False,
    )

    # Alice unlocks her restored keyring from disk
    new_alice_config.attach_keyring()
    new_alice_config.keyring.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)
    new_alice = new_alice_config()

    # First, we check that her public keys are correctly restored
    assert alices_verifying_key == new_alice.public_keys(SigningPower)
    assert alices_receiving_key == new_alice.public_keys(DecryptingPower)

    # Bob's eldest brother, Roberto, appears too
    roberto = Bob(federated_only=True,
                  start_learning_now=False,
                  network_middleware=MockRestMiddleware())

    # Alice creates a new policy for Roberto. Note how all the parameters
    # except for the label (i.e., recipient, m, n, policy_end) are different
    # from previous policy
    m, n = 2, 5
    policy_end_datetime = maya.now() + datetime.timedelta(days=3)
    roberto_policy = new_alice.grant(roberto,
                                     label,
                                     m=m,
                                     n=n,
                                     expiration=policy_end_datetime)

    # Both policies must share the same public key (i.e., the policy public key)
    assert policy_pubkey == roberto_policy.public_key
コード例 #15
0
def test_bob_joins_policy_and_retrieves(federated_alice,
                                        federated_ursulas,
                                        certificates_tempdir,
                                        ):
    # Let's partition Ursulas in two parts
    a_couple_of_ursulas = list(federated_ursulas)[:2]
    rest_of_ursulas = list(federated_ursulas)[2:]

    # Bob becomes
    bob = Bob(federated_only=True,
              start_learning_now=True,
              network_middleware=MockRestMiddleware(),
              abort_on_learning_error=True,
              known_nodes=a_couple_of_ursulas,
              )

    # Bob only knows a couple of Ursulas initially
    assert len(bob.known_nodes) == 2

    # Alice creates a policy granting access to Bob
    # Just for fun, let's assume she distributes KFrags among Ursulas unknown to Bob
    n = NUMBER_OF_URSULAS_IN_DEVELOPMENT_NETWORK - 2
    label = b'label://' + os.urandom(32)
    contract_end_datetime = maya.now() + datetime.timedelta(days=5)
    policy = federated_alice.grant(bob=bob,
                                   label=label,
                                   m=3,
                                   n=n,
                                   expiration=contract_end_datetime,
                                   handpicked_ursulas=set(rest_of_ursulas),
                                   )

    assert bob == policy.bob
    assert label == policy.label

    # Now, Bob joins the policy
    bob.join_policy(label=label,
                    alice_pubkey_sig=federated_alice.stamp,
                    block=True)

    # In the end, Bob should know all the Ursulas
    assert len(bob.known_nodes) == len(federated_ursulas)

    # Enrico becomes
    enrico = Enrico(policy_encrypting_key=policy.public_key)

    plaintext = b"What's your approach?  Mississippis or what?"
    message_kit, _signature = enrico.encrypt_message(plaintext)

    alices_verifying_key = federated_alice.stamp.as_umbral_pubkey()

    # Bob takes the message_kit and retrieves the message within
    delivered_cleartexts = bob.retrieve(message_kit=message_kit,
                                        data_source=enrico,
                                        alice_verifying_key=alices_verifying_key,
                                        label=policy.label)

    assert plaintext == delivered_cleartexts[0]

    # FIXME: Bob tries to retrieve again
    # delivered_cleartexts = bob.retrieve(message_kit=message_kit,
    #                                     data_source=enrico,
    #                                     alice_verifying_key=alices_verifying_key,
    #                                     label=policy.label)
    #
    # assert plaintext == delivered_cleartexts[0]

    # # Let's try retrieve again, but Alice revoked the policy.
    failed_revocations = federated_alice.revoke(policy)
    assert len(failed_revocations) == 0

    with pytest.raises(Ursula.NotEnoughUrsulas):
        _cleartexts = bob.retrieve(message_kit=message_kit,
                                   data_source=enrico,
                                   alice_verifying_key=alices_verifying_key,
                                   label=policy.label)
コード例 #16
0
ファイル: auth.py プロジェクト: biggeoff/JellyPy
 def check_auth(self, testing_on=False):
     """Check whether the session is still authenticated."""
     if maya.now() > self.auth_expires():
         self.authenticate(testing_on=testing_on)
     else:
         pass
コード例 #17
0
ファイル: bbrest.py プロジェクト: jhenry/BbRest
    def __init__(self, key, secret, url, headers=None, threelegoauth=False):
        #these variables are accessible in the class, but not externally.
        self.__key = key
        self.__secret = secret
        self.__url = url
        self.__headers = headers

        #Authenticate the session
        session = requests.Session()
        payload = {'grant_type': 'client_credentials'}

        if self.__headers:
            session.headers.update(self.__headers)

        #Sends a post to get the authentication token
        r = session.post(f"{self.__url}/learn/api/public/v1/oauth2/token",
                         data=payload,
                         auth=(self.__key, self.__secret))

        #Adds the token to the headers for future requests.
        if r.status_code == 200:
            token = r.json()["access_token"]
            session.headers.update({"Authorization": f"Bearer {token}"})
            self.expiration_epoch = maya.now() + r.json()["expires_in"]

        else:
            print('Authorization failed, check your key, secret and url')
            return

        #set the session within the class
        self.session = session

        #get the current version via a REST call
        r = self.session.get(f'{url}/learn/api/public/v1/system/version')
        if r.status_code == 200:
            major = r.json()['learn']['major']
            minor = r.json()['learn']['minor']
            version = f"{major}.{minor}.0"  #Ignore incremental patches
        else:
            print(f"Could not retrieve version, error code: {r.status_code}")
            version = '3000.0.0'  #Version wasn't supported until 3000.3.0

        #This helps only pull down APIs your version has access to.
        self.version = version

        #use the functions that exist in functions.p,
        #or retrieve from the swagger_json definitions

        swagger_json = requests.get(
            f'https://developer.blackboard.com/portal/docs/apis/learn-swagger.json'
        ).json()
        p = r'\d+.\d+.\d+'
        functions = []
        for path in swagger_json['paths']:
            for call in swagger_json['paths'][path].keys():
                meta = swagger_json['paths'][path][call]
                functions.append({
                    'summary': meta['summary'].replace(' ', ''),
                    'description': meta['description'],
                    'parameters': meta['parameters'],
                    'method': call,
                    'path': path,
                    'version': re.findall(p, meta['description'])
                })

        #store all functions in a class visible list
        self.__all_functions = functions
        self.supported_functions()
        self.method_generator()
コード例 #18
0
def calculate_period_duration(future_time: maya.MayaDT) -> int:
    """Takes a future MayaDT instance and calculates the duration from now, returning in periods"""
    future_period = datetime_to_period(datetime=future_time)
    current_period = datetime_to_period(datetime=maya.now())
    periods = future_period - current_period
    return periods
コード例 #19
0
def test_collect_rewards_integration(click_runner, testerchain,
                                     stakeholder_configuration_file_location,
                                     blockchain_alice, blockchain_bob,
                                     random_policy_label, manual_staker,
                                     manual_worker, token_economics,
                                     policy_value, policy_rate):

    half_stake_time = token_economics.minimum_locked_periods // 2  # Test setup
    logger = Logger("Test-CLI")  # Enter the Teacher's Logger, and
    current_period = 0  # State the initial period for incrementing

    staker_address = manual_staker
    worker_address = manual_worker

    staker = Staker(is_me=True,
                    checksum_address=staker_address,
                    blockchain=testerchain)

    # The staker is staking.
    assert staker.stakes
    assert staker.is_staking
    assert staker.worker_address == worker_address

    ursula_port = select_test_port()
    ursula = Ursula(is_me=True,
                    checksum_address=staker_address,
                    worker_address=worker_address,
                    blockchain=testerchain,
                    rest_host='127.0.0.1',
                    rest_port=ursula_port,
                    network_middleware=MockRestMiddleware())

    MOCK_KNOWN_URSULAS_CACHE[ursula_port] = ursula
    assert ursula.worker_address == worker_address
    assert ursula.checksum_address == staker_address

    # Mock TransactingPower consumption (Worker-Ursula)
    ursula.blockchain.transacting_power = TransactingPower(
        account=worker_address,
        password=INSECURE_DEVELOPMENT_PASSWORD,
        blockchain=testerchain)
    ursula.blockchain.transacting_power.activate()

    # Confirm for half the first stake duration
    for _ in range(half_stake_time):
        logger.debug(
            f">>>>>>>>>>> TEST PERIOD {current_period} <<<<<<<<<<<<<<<<")
        ursula.confirm_activity()
        testerchain.time_travel(periods=1)
        current_period += 1

    # Alice creates a policy and grants Bob access
    blockchain_alice.selection_buffer = 1

    M, N = 1, 1
    expiration = maya.now() + datetime.timedelta(days=3)
    blockchain_policy = blockchain_alice.grant(bob=blockchain_bob,
                                               label=random_policy_label,
                                               m=M,
                                               n=N,
                                               value=policy_value,
                                               expiration=expiration,
                                               handpicked_ursulas={ursula})

    # Ensure that the handpicked Ursula was selected for the policy
    arrangement = list(blockchain_policy._accepted_arrangements)[0]
    assert arrangement.ursula == ursula

    # Bob learns about the new staker and joins the policy
    blockchain_bob.start_learning_loop()
    blockchain_bob.remember_node(node=ursula)
    blockchain_bob.join_policy(random_policy_label,
                               bytes(blockchain_alice.stamp))

    # Enrico Encrypts (of course)
    enrico = Enrico(policy_encrypting_key=blockchain_policy.public_key,
                    network_middleware=MockRestMiddleware())

    verifying_key = blockchain_alice.stamp.as_umbral_pubkey()

    for index in range(half_stake_time - 5):
        ursula.confirm_activity()

        logger.debug(
            f">>>>>>>>>>> TEST PERIOD {current_period} <<<<<<<<<<<<<<<<")

        # Encrypt
        random_data = os.urandom(random.randrange(20, 100))
        ciphertext, signature = enrico.encrypt_message(message=random_data)

        # Decrypt
        cleartexts = blockchain_bob.retrieve(message_kit=ciphertext,
                                             data_source=enrico,
                                             alice_verifying_key=verifying_key,
                                             label=random_policy_label)
        assert random_data == cleartexts[0]

        # Ursula Staying online and the clock advancing
        testerchain.time_travel(periods=1)
        current_period += 1

    # Finish the passage of time for the first Stake
    for _ in range(5):  # plus the extended periods from stake division
        ursula.confirm_activity()
        current_period += 1
        logger.debug(
            f">>>>>>>>>>> TEST PERIOD {current_period} <<<<<<<<<<<<<<<<")
        testerchain.time_travel(periods=1)

    #
    # WHERES THE MONEY URSULA?? - Collecting Rewards
    #

    # The address the client wants Ursula to send rewards to
    burner_wallet = testerchain.w3.eth.account.create(
        INSECURE_DEVELOPMENT_PASSWORD)

    # The rewards wallet is initially empty, because it is freshly created
    assert testerchain.client.get_balance(burner_wallet.address) == 0

    # Rewards will be unlocked after the
    # final confirmed period has passed (+1).
    testerchain.time_travel(periods=1)
    current_period += 1
    logger.debug(f">>>>>>>>>>> TEST PERIOD {current_period} <<<<<<<<<<<<<<<<")

    # Half of the tokens are unlocked.
    assert staker.locked_tokens() == token_economics.minimum_allowed_locked

    # Simulate "Reconnection" within the CLI process to the testerchain
    def connect(self, *args, **kwargs):
        self._attach_provider(testerchain.provider)
        self.w3 = self.Web3(provider=self._provider)
        self.client = Web3Client.from_w3(w3=self.w3)

    BlockchainInterface.connect = connect

    # Since we are mocking the blockchain connection, manually consume the transacting power of the Staker.
    testerchain.transacting_power = TransactingPower(
        account=staker_address,
        password=INSECURE_DEVELOPMENT_PASSWORD,
        blockchain=testerchain)
    testerchain.transacting_power.activate()

    # Collect Policy Reward
    collection_args = ('stake', 'collect-reward', '--mock-networking',
                       '--config-file',
                       stakeholder_configuration_file_location,
                       '--policy-reward', '--no-staking-reward',
                       '--staking-address', staker_address,
                       '--withdraw-address', burner_wallet.address, '--force')

    result = click_runner.invoke(nucypher_cli,
                                 collection_args,
                                 input=INSECURE_DEVELOPMENT_PASSWORD,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    # Policy Reward
    collected_policy_reward = testerchain.client.get_balance(
        burner_wallet.address)
    expected_collection = policy_rate * 30
    assert collected_policy_reward == expected_collection

    # Finish the passage of time... once and for all
    # Extended periods from stake division
    for _ in range(9):
        ursula.confirm_activity()
        current_period += 1
        logger.debug(
            f">>>>>>>>>>> TEST PERIOD {current_period} <<<<<<<<<<<<<<<<")
        testerchain.time_travel(periods=1)

    # Collect Inflation Reward
    collection_args = ('stake', 'collect-reward', '--mock-networking',
                       '--config-file',
                       stakeholder_configuration_file_location,
                       '--no-policy-reward', '--staking-reward',
                       '--staking-address', staker_address,
                       '--withdraw-address', burner_wallet.address, '--force')

    result = click_runner.invoke(nucypher_cli,
                                 collection_args,
                                 input=INSECURE_DEVELOPMENT_PASSWORD,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    # The burner wallet has the reward ethers
    assert staker.token_agent.get_balance(address=staker_address)
コード例 #20
0
 def before_request_callback():
     request.start_time = maya.now()
コード例 #21
0
ファイル: main.py プロジェクト: clumsyme/learn
import requests
import maya

response = requests.get('https://httpbin.org/ip')

print(f"Your IP is {response.json()['origin']}")

now = maya.now()

print(maya)
コード例 #22
0
 def after_request_callback(response):
     if not 'COMMON_POWERED_BY_DISABLED' in current_app.config:
         response.headers['X-Powered-By'] = 'Flask'
     if not 'COMMON_PROCESSED_TIME_DISABLED' in current_app.config:
         response.headers['X-Processed-Time'] = maya.now().epoch - request.start_time.epoch
     return response
コード例 #23
0
ファイル: elephant.py プロジェクト: kennethreitz/elephant
 def __init__(self):
     self.uuid = str(uuid4())
     self.data = {}
     self.epoch = maya.now().epoch
コード例 #24
0
def init_db():
    datas = process_data(get_mail_db())
    insert_to_sql(datas)
    mongo_ids = dump_to_mongo(datas)
    print(str(maya.now().datetime(to_timezone='Asia/Jakarta')) + ' - ' + str(mongo_ids))
    return str(mongo_ids)
コード例 #25
0
ファイル: elephant.py プロジェクト: kennethreitz/elephant
    def save(self):
        self.epoch = maya.now().epoch

        self._persist()
        self._index()
コード例 #26
0
ファイル: clients.py プロジェクト: y3v63n/nucypher
 def check_for_timeout(t):
     last_update = maya.now()
     duration = (last_update - start_time).total_seconds()
     if duration > t:
         raise self.SyncTimeout
コード例 #27
0
def test_interval_requires_end_time_after_or_on_start_time():
    with pytest.raises(ValueError):
        maya.MayaInterval(start=maya.now(), duration=0)
        maya.MayaInterval(start=maya.now(), duration=-1)
コード例 #28
0
ファイル: bbrest.py プロジェクト: jhenry/BbRest
 def is_expired(self):
     return maya.now() > self.expiration_epoch
コード例 #29
0
def test_interval_init_start_end():
    start = maya.now()
    end = start.add(hours=1)
    interval = maya.MayaInterval(start=start, end=end)
    assert interval.start == start
    assert interval.end == end
コード例 #30
0
# In this example, we generate it on the fly (for demonstration purposes)
from doctor_keys import get_doctor_pubkeys
doctor_pubkeys = get_doctor_pubkeys()

powers_and_material = {
    DecryptingPower: doctor_pubkeys['enc'],
    SigningPower: doctor_pubkeys['sig']
}

# We create a view of the Bob who's going to be granted access.
doctor_strange = Bob.from_public_keys(powers_and_material=powers_and_material,
                                      federated_only=True)

# Here are our remaining Policy details, such as:
# - Policy duration
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
# - m-out-of-n: This means Alicia splits the re-encryption key in 5 pieces and
#               she requires Bob to seek collaboration of at least 3 Ursulas
m, n = 2, 3

# With this information, Alicia creates a policy granting access to Bob.
# The policy is sent to the NuCypher network.
print("Creating access policy for the Doctor...")
policy = alicia.grant(bob=doctor_strange,
                      label=label,
                      m=m,
                      n=n,
                      expiration=policy_end_datetime)
print("Done!")

# For the demo, we need a way to share with Bob some additional info
コード例 #31
0
def test_interval_init_start_duration():
    start = maya.now()
    duration = 1
    interval = maya.MayaInterval(start=start, duration=duration)
    assert interval.start == start
    assert interval.end == start.add(seconds=duration)
コード例 #32
0
# Alice can get the policy's public key even before creating the policy.
label = b"secret/files/42"
policy_public_key = alice.get_policy_encrypting_key_from_label(label)

# From this moment on, anyone that knows the public key
# can encrypt data originally intended for Alice, but that
# can be shared with any Bob that Alice grants access.

# Alice already knows Bob's public keys from a side-channel.
remote_bob = Bob.from_public_keys(
    encrypting_key=encrypting_key,
    verifying_key=verifying_key,
)

# These are the policy details.
expiration = maya.now() + datetime.timedelta(days=1)
threshold, shares = 2, 3
price = alice.payment_method.quote(expiration=expiration.epoch,
                                   shares=shares).value

# Alice grants access to Bob...
policy = alice.grant(
    remote_bob,
    label,
    threshold=threshold,
    shares=shares,
    value=price,
    expiration=expiration,
)

# ...and then disappears from the internet.
コード例 #33
0
def test_interval_init_end_duration():
    end = maya.now()
    duration = 1
    interval = maya.MayaInterval(end=end, duration=duration)
    assert interval.end == end
    assert interval.start == end.subtract(seconds=duration)
コード例 #34
0
import datetime
import maya
import base64

from umbral.keys import UmbralPrivateKey, UmbralPublicKey

from api import Api

policy_expiration = maya.now() + datetime.timedelta(days=365)
m = 1
n = 1

api = Api(node_meta_dir='../examples/examples-runtime-cruft')

# private_key1, public_key1 = api.gen_keypair()
# private_key2, public_key2 = api.gen_keypair()

private_key1 = UmbralPrivateKey.from_bytes(
    'DGgxOtqZOrqY-lh_E_L5H2YpNBoT3HEW6whMcVcqf5c=',
    decoder=base64.urlsafe_b64decode)
public_key1 = private_key1.get_pubkey()
print(bytes(public_key1))

private_key2 = UmbralPrivateKey.from_bytes(
    '8886EWu9cnGOCoZjNcI1SPEoyOiUTHBYwflfAA5YgCA=',
    decoder=base64.urlsafe_b64decode)
public_key2 = private_key2.get_pubkey()

print(private_key1.to_bytes())

label = 'test-2'.encode('utf-8')
コード例 #35
0
ファイル: policies.py プロジェクト: ronmnm/nucypher
    def sample_essential(self,
                         quantity: int,
                         handpicked_ursulas: Set[Ursula],
                         learner_timeout: int = 1,
                         timeout: int = 10) -> Set[Ursula]: # TODO #843: Make timeout configurable

        selected_ursulas = set(handpicked_ursulas)
        quantity_remaining = quantity

        # Need to sample some stakers

        handpicked_addresses = [ursula.checksum_address for ursula in handpicked_ursulas]
        reservoir = self.alice.get_stakers_reservoir(duration=self.duration_periods,
                                                     without=handpicked_addresses)
        if len(reservoir) < quantity_remaining:
            error = f"Cannot create policy with {quantity} arrangements"
            raise self.NotEnoughBlockchainUrsulas(error)

        to_check = set(reservoir.draw(quantity_remaining))

        # Sample stakers in a loop and feed them to the learner to check
        # until we have enough in `selected_ursulas`.

        start_time = maya.now()
        new_to_check = to_check

        while True:

            # Check if the sampled addresses are already known.
            # If we're lucky, we won't have to wait for the learner iteration to finish.
            known = {x for x in to_check if x in self.alice.known_nodes}
            to_check = to_check - known

            known = random.sample(known, min(len(known), quantity_remaining)) # we only need so many
            selected_ursulas.update([self.alice.known_nodes[address] for address in known])
            quantity_remaining -= len(known)

            if quantity_remaining == 0:
                break
            else:
                new_to_check = reservoir.draw_at_most(quantity_remaining)
                to_check.update(new_to_check)

            # Feed newly sampled stakers to the learner
            self.alice.learn_about_specific_nodes(new_to_check)

            # TODO: would be nice to wait for the learner to finish an iteration here,
            # because if it hasn't, we really have nothing to do.
            time.sleep(learner_timeout)

            delta = maya.now() - start_time
            if delta.total_seconds() >= timeout:
                still_checking = ', '.join(to_check)
                raise RuntimeError(f"Timed out after {timeout} seconds; "
                                   f"need {quantity_remaining} more, still checking {still_checking}.")

        found_ursulas = list(selected_ursulas)

        # Randomize the output to avoid the largest stakers always being the first in the list
        system_random = random.SystemRandom()
        system_random.shuffle(found_ursulas) # inplace

        return set(found_ursulas)