Example #1
0
async def test_gcs_signed_url(bucket_name, creds, data):
    object_name = f'{uuid.uuid4().hex}/{uuid.uuid4().hex}.txt'

    async with Session() as session:
        storage = Storage(service_file=creds, session=session)
        await storage.upload(bucket_name,
                             object_name,
                             data,
                             force_resumable_upload=True)

        bucket = Bucket(storage, bucket_name)
        blob = await bucket.get_blob(object_name, session=session)

        iam_client = IamClient(service_file=creds, session=session)

        signed_url = await blob.get_signed_url(60, iam_client=iam_client)

        resp = await session.get(signed_url)

        try:
            downloaded_data: str = await resp.text()
        except (AttributeError, TypeError):
            downloaded_data: str = str(resp.text)

        try:
            assert data == downloaded_data
        finally:
            await storage.delete(bucket_name, blob.name)
Example #2
0
async def test_sign_blob(creds: str) -> None:
    data = 'Testing Can be confidential!'

    async with Session(timeout=10) as s:
        iam_client = IamClient(service_file=creds, session=s)
        resp = await iam_client.sign_blob(data)
        signed_data = resp['signedBlob']
        await verify_signature(data, signed_data, resp['keyId'], iam_client)
Example #3
0
async def test_get_service_account_public_key(creds: str) -> None:
    async with aiohttp.ClientSession(conn_timeout=10, read_timeout=10) as s:
        iam_client = IamClient(service_file=creds, session=s)
        resp = await iam_client.list_public_keys(session=s)
        pub_key_data = await iam_client.get_public_key(key=resp[0]['name'],
                                                       session=s)

        assert pub_key_data['name'] == resp[0]['name']
        assert 'publicKeyData' in pub_key_data

        key_id = resp[0]['name'].split('/')[-1]
        pub_key_by_key_id_data = await iam_client.get_public_key(key_id=key_id,
                                                                 session=s)
        assert pub_key_data == pub_key_by_key_id_data
Example #4
0
async def test_get_service_account_public_key(creds: str) -> None:
    async with Session(timeout=10) as s:
        iam_client = IamClient(service_file=creds, session=s)
        resp = await iam_client.list_public_keys(session=s)
        pub_key_data = await iam_client.get_public_key(key=resp[0]['name'],
                                                       session=s)

        assert pub_key_data['name'] == resp[0]['name']
        assert 'publicKeyData' in pub_key_data

        key_id = resp[0]['name'].split('/')[-1]
        pub_key_by_key_id_data = await iam_client.get_public_key(key_id=key_id,
                                                                 session=s)

        # Sometimes, one or both keys will be created with "no" expiry.
        pub_key_time = pub_key_data.pop('validBeforeTime')
        pub_key_by_key_id_time = pub_key_by_key_id_data.pop('validBeforeTime')
        assert (pub_key_time == pub_key_by_key_id_time
                or '9999-12-31T23:59:59Z'
                in {pub_key_time, pub_key_by_key_id_time})

        assert pub_key_data == pub_key_by_key_id_data
Example #5
0
async def test_gcs_signed_url(bucket_name, creds, data):
    object_name = f'{uuid.uuid4().hex}/{uuid.uuid4().hex}.txt'

    async with aiohttp.ClientSession() as session:
        storage = Storage(service_file=creds, session=session)
        await storage.upload(bucket_name, object_name, data,
                             force_resumable_upload=True)

        bucket = Bucket(storage, bucket_name)
        blob = await bucket.get_blob(object_name, session=session)

        iam_client = IamClient(service_file=creds, session=session)

        signed_url = await blob.get_signed_url(60, iam_client=iam_client)

        resp = await session.get(signed_url)
        resp.raise_for_status()
        downloaded_data = await resp.text()

        try:
            assert data == downloaded_data
        finally:
            await storage.delete(bucket_name, blob.name)
Example #6
0
    async def get_signed_url(  # pylint: disable=too-many-locals
            self,
            expiration: int,
            headers: Optional[dict] = None,
            query_params: Optional[dict] = None,
            http_method: str = 'GET',
            iam_client: Optional[IamClient] = None,
            service_account_email: Optional[str] = None,
            service_file: Optional[str] = None,
            token: Optional[Token] = None,
            session: Optional[aiohttp.ClientSession] = None) -> str:
        """
        Create a temporary access URL for Storage Blob accessible by anyone
        with the link.

        Adapted from Google Documentation:
        https://cloud.google.com/storage/docs/access-control/signing-urls-manually#python-sample
        """
        if expiration > 604800:
            raise ValueError("expiration time can't be longer than 604800 "
                             'seconds (7 days)')

        iam_client = iam_client or IamClient(
            service_file=service_file, token=token, session=session)

        quoted_name = quote(self.name, safe='')
        canonical_uri = f'/{self.bucket.name}/{quoted_name}'

        datetime_now = datetime.datetime.utcnow()
        request_timestamp = datetime_now.strftime('%Y%m%dT%H%M%SZ')
        datestamp = datetime_now.strftime('%Y%m%d')

        service_account_email = (service_account_email
                                 or iam_client.service_account_email)
        credential_scope = f'{datestamp}/auto/storage/goog4_request'
        credential = f'{service_account_email}/{credential_scope}'

        headers = headers or {}
        headers['host'] = HOST

        ordered_headers = collections.OrderedDict(sorted(headers.items()))
        canonical_headers = ''.join(f'{str(k).lower()}:{str(v).lower()}\n'
                                    for k, v in ordered_headers.items())

        signed_headers = ';'.join(f'{str(k).lower()}'
                                  for k in ordered_headers.keys())

        query_params = query_params or {}
        query_params['X-Goog-Algorithm'] = 'GOOG4-RSA-SHA256'
        query_params['X-Goog-Credential'] = credential
        query_params['X-Goog-Date'] = request_timestamp
        query_params['X-Goog-Expires'] = expiration
        query_params['X-Goog-SignedHeaders'] = signed_headers

        ordered_query_params = collections.OrderedDict(
            sorted(query_params.items()))

        canonical_query_str = '&'.join(
            f'{quote(str(k), safe="")}={quote(str(v), safe="")}'
            for k, v in ordered_query_params.items())

        canonical_req = '\n'.join([
            http_method, canonical_uri, canonical_query_str, canonical_headers,
            signed_headers, 'UNSIGNED-PAYLOAD'
        ])
        canonical_req_hash = hashlib.sha256(canonical_req.encode()).hexdigest()

        str_to_sign = '\n'.join([
            'GOOG4-RSA-SHA256', request_timestamp, credential_scope,
            canonical_req_hash
        ])
        signed_resp = await iam_client.sign_blob(
            str_to_sign,
            service_account_email=service_account_email,
            session=session)

        signature = binascii.hexlify(decode(
            signed_resp['signedBlob'])).decode()

        return (f'https://{HOST}{canonical_uri}?{canonical_query_str}'
                f'&X-Goog-Signature={signature}')
Example #7
0
async def test_get_service_account_public_key_names(creds: str) -> None:
    async with Session(timeout=10) as s:
        iam_client = IamClient(service_file=creds, session=s)
        resp = await iam_client.list_public_keys()
        assert len(resp) >= 1, '0 public keys found.'
from google.cloud import language
from google.cloud.language import enums, types


#Authentication import
from gcloud.aio.auth import IamClient
from gcloud.rest.auth import Token
client = IamClient()
pubkeys = await client.list_public_keys()
token = Token()
print(token.get())
print("-----------------")



def analyze_text_sentiment(text):
    client = language.LanguageServiceClient()
    document = types.Document(
        content=text,
        type=enums.Document.Type.PLAIN_TEXT)

    response = client.analyze_sentiment(document=document)

    sentiment = response.document_sentiment
    results = [
        ('text', text),
        ('score', sentiment.score),
        ('magnitude', sentiment.magnitude),
    ]
    for k, v in results:
        print('{:10}: {}'.format(k, v))