Ejemplo n.º 1
0
from __future__ import annotations

from typing import TYPE_CHECKING

from grapl_common.grapl_logger import get_module_grapl_logger

if TYPE_CHECKING:
    from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource, Table

LOGGER = get_module_grapl_logger(log_to_stdout=True)


def is_grapl_provisioned(
    dynamodb: DynamoDBServiceResource,
    schema_table: str,
) -> bool:
    """
    We are doing a very simple check - "is there >0 things in schema_table?"
    to infer whether any provisioning has taken place.
    """
    table = dynamodb.Table(schema_table)
    return not _table_is_empty(table)


def _table_is_empty(table: Table) -> bool:
    """
    fun fact: some_table.item_count? It's only updated every 6 hours.
    you have to do a scan.
    """
    items = table.scan()["Items"]
    if items:
Ejemplo n.º 2
0
from typing import Optional, Union

import redis
from grapl_common.grapl_logger import get_module_grapl_logger

LOGGER = get_module_grapl_logger()


def construct_redis_client(addr: Optional[str],
                           port: Optional[int]) -> redis.Redis:
    if addr and port:
        LOGGER.debug(f"connecting to redis at {addr}:{port}")
        return redis.Redis(host=addr, port=port, db=0)
    else:
        raise ValueError(
            f"Failed connecting to redis | addr:\t{addr} | port:\t{port}")


class NopCache(object):
    def set(self, key: str, value: str) -> None:
        pass

    def get(self, key: str) -> bool:
        return False

    def delete(self, key: str) -> None:
        pass


EitherCache = Union[NopCache, redis.Redis]
Ejemplo n.º 3
0
from __future__ import annotations

import os
from typing import Tuple

import boto3
from grapl_common.env_helpers import SecretsManagerClientFactory
from grapl_common.grapl_logger import get_module_grapl_logger

LOGGER = get_module_grapl_logger(default_log_level="DEBUG")


def get_test_user_creds() -> Tuple[str, str]:
    username = os.environ["GRAPL_TEST_USER_NAME"]
    password_secret_id = os.environ["GRAPL_TEST_USER_PASSWORD_SECRET_ID"]
    LOGGER.debug(f"Retrieving secret id: {password_secret_id}")
    secrets_client = SecretsManagerClientFactory(boto3).from_env()
    password = secrets_client.get_secret_value(SecretId=password_secret_id)[
        "SecretString"
    ]
    return (username, password)