def __init__(self, dict_src, from_cache=False):
            self.statements = []

            super(CloudWatchLogGroup.LogStream,
                  self).__init__(dict_src, from_cache=from_cache)

            if from_cache:
                self.init_log_stream_from_cache(dict_src)
                return

            init_options = {
                "logStreamName":
                self.init_default_attr,
                "creationTime":
                lambda name, value:
                (name, CommonUtils.timestamp_to_datetime(value / 1000.0)),
                "firstEventTimestamp":
                self.init_default_attr,
                "lastEventTimestamp":
                self.init_default_attr,
                "lastIngestionTime":
                self.init_default_attr,
                "uploadSequenceToken":
                self.init_default_attr,
                "arn":
                self.init_default_attr,
                "storedBytes":
                self.init_default_attr
            }

            self.init_attrs(dict_src, init_options)
Beispiel #2
0
 def init_from_python_file(self):
     config = CommonUtils.load_object_from_module(
         self.configuration_file_full_path, "main")
     self.init_from_dictionary(
         config.__dict__,
         custom_source_log="Init attribute '{}' from python file: '" +
         self.configuration_file_full_path + "'")
Beispiel #3
0
 def init_configuration(self):
     """
     Sets current active account from configuration
     """
     if self.configuration is None:
         return
     accounts = CommonUtils.load_object_from_module(self.configuration.accounts_file, "main")
     AzureAccount.set_azure_account(accounts[self.configuration.azure_account])
Beispiel #4
0
 def update_tags(self, objects):
     if len(objects) == 0:
         return
     for response in self.execute(
             self.client.describe_tags,
             "TagDescriptions",
             filters_req={"ResourceArns": [obj.arn for obj in objects]}):
         obj = \
             CommonUtils.find_objects_by_values(objects, {"arn": response["ResourceArn"]}, max_count=1)[0]
         obj.tags = response["Tags"]
Beispiel #5
0
    def update_iam_role_managed_policies(self, iam_role, policies):
        """
        Full information part update.
        :param iam_role:
        :return:
        """
        for managed_policy in self.execute(self.client.list_attached_role_policies, "AttachedPolicies", filters_req={"RoleName": iam_role.name, "MaxItems": 1000}):
            found_policies = CommonUtils.find_objects_by_values(policies, {"arn": managed_policy["PolicyArn"]})

            if len(found_policies) != 1:
                found_policies = [managed_policy["PolicyArn"]]

            policy = found_policies[0]
            iam_role.add_policy(policy)
Beispiel #6
0
    def provision_iam_role(self, iam_role: IamRole):
        all_roles = self.get_all_roles(full_information=False)
        found_role = CommonUtils.find_objects_by_values(all_roles, {"name": iam_role.name})

        if found_role:
            found_role = found_role[0]
            role_dict_src = found_role.dict_src
        else:
            role_dict_src = self.provision_iam_role_raw(iam_role.generate_create_request())

        iam_role.update_from_raw_response(role_dict_src)

        for request in iam_role.generate_attach_policies_requests():
            self.attach_role_policy_raw(request)
Beispiel #7
0
    def get_all_users(self, full_information=True):
        """
        Get all users.
        :param full_information:
        :return:
        """
        final_result = list()

        for response in self.execute(self.client.list_users, "Users"):
            user = IamUser(response)
            final_result.append(user)

        if full_information:
            for update_info in self.execute(self.client.get_account_authorization_details, "UserDetailList"):
                user = CommonUtils.find_objects_by_values(final_result, {"id": update_info["UserId"]}, max_count=1)[0]
                user.update_attributes(update_info)

        return final_result
Beispiel #8
0
 def provision_security_group(self, security_group):
     try:
         group_id = self.raw_create_security_group(security_group.generate_create_request())
         security_group.id = group_id
         # wait for propagation?
         #for i in range(60):
         #    region_groups = self.get_all_security_groups_in_region(security_group.region, full_information=False)
         #    named_region_groups = CommonUtils.find_objects_by_values(region_groups, {"name": security_group.name}, max_count=1)
         #    if len(named_region_groups) > 0:
         #        break
         #    time.sleep(5)
     except Exception as exception_inst:
         repr_exception_inst = repr(exception_inst)
         if "already exists for VPC" not in repr_exception_inst:
             raise
         logger.warning(repr_exception_inst)
         region_groups = self.get_region_security_groups(security_group.region, full_information=False)
         existing_group = CommonUtils.find_objects_by_values(region_groups, {"name": security_group.name}, max_count=1)[0]
         security_group.update_from_raw_create(existing_group.dict_src)
Beispiel #9
0
    def clear_indices(self, time_limit=None):
        if time_limit is None:
            time_limit = datetime.datetime.now() - datetime.timedelta(days=30 *
                                                                      2)

        self.init_indices()
        to_del_indices = []
        for es_index_name, es_index in self.indices.items():
            if es_index_name.startswith("."):
                continue

            created_date = CommonUtils.timestamp_to_datetime(
                es_index["settings"]["index"]["creation_date"],
                microseconds_value=True)
            if created_date < time_limit:
                logger.info(
                    f"Deleting index '{es_index_name}' created at {created_date}"
                )
                to_del_indices.append(es_index_name)
        pdb.set_trace()
        self.client.indices.delete(to_del_indices)
        logger.info(
            f"Deleted {len(to_del_indices)} out of {len(self.indices)}")
def set_session_credentials(arguments, configs_dict) -> None:
    configuration = AWSAPIConfigurationPolicy()
    configuration.configuration_file_full_path = "~/Desktop/tmp/configuration_values.py"
    configuration.init_from_file()

    accounts = CommonUtils.load_object_from_module(configuration.accounts_file,
                                                   "main")
    AWSAccount.set_aws_account(accounts[configuration.aws_api_account])

    session = SessionsManager.connect_session()
    credentials = session.get_credentials()
    credentials = credentials.get_frozen_credentials()

    ret = f"\n\n[{arguments.profile_name}]"
    ret += f"\naws_access_key_id = {credentials.access_key}"
    ret += f"\naws_secret_access_key = {credentials.secret_key}"
    ret += f"\naws_session_token = {credentials.token}"

    with open("~/.aws/credentials") as file_handler:
        contents = file_handler.read()

    if arguments.profile_name in contents:
        start_index = contents.index(f"[{arguments.profile_name}]")

        try:
            end_index = contents.index("[", start_index + 1)
            tail_string = "\n\n" + contents[end_index:].strip("\n")
        except ValueError:
            tail_string = ""

        new_contents = contents[:start_index].strip("\n") + ret + tail_string
        with open("~/.aws/credentials", "w+") as file_handler:
            file_handler.write(new_contents)

    else:
        with open("~/.aws/credentials", "a+") as file_handler:
            file_handler.write(ret)
Beispiel #11
0
def publish(arguments) -> None:
    accounts = CommonUtils.load_object_from_module(arguments.accounts_file,
                                                   "main")
    AWSAccount.set_aws_account(accounts[arguments.account])
    SNSClient().raw_publish(arguments.topic_arn, arguments.subject,
                            arguments.message)
Beispiel #12
0
from unittest.mock import Mock
from horey.aws_api.base_entities.aws_account import AWSAccount
from horey.aws_api.aws_services_entities.ec2_launch_template import EC2LaunchTemplate

configuration_values_file_full_path = os.path.join(
    os.path.dirname(os.path.abspath(__file__)),
    "h_logger_configuration_values.py")
logger = get_logger(
    configuration_values_file_full_path=configuration_values_file_full_path)

accounts_file_full_path = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "ignore",
                 "aws_api_managed_accounts.py"))

accounts = CommonUtils.load_object_from_module(accounts_file_full_path, "main")
AWSAccount.set_aws_account(accounts["1111"])
AWSAccount.set_aws_region(accounts["1111"].regions['us-west-2'])

mock_values_file_path = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "ignore",
                 "mock_values.py"))
mock_values = CommonUtils.load_object_from_module(mock_values_file_path,
                                                  "main")


def test_init_ec2_client():
    assert isinstance(EC2Client(), EC2Client)


DICT_CREATE_SECURITY_GROUP_REQUEST = {
Beispiel #13
0
import pdb

from horey.docker_api.docker_api import DockerAPI
import os
from horey.common_utils.common_utils import CommonUtils
from horey.aws_api.aws_api import AWSAPI

mock_values_file_path = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "ignore", "mock_values.py"))
mock_values = CommonUtils.load_object_from_module(mock_values_file_path, "main")

src_aws_region = "us-west-2"
dst_aws_region = "us-west-2"


def test_init_docker_api():
    assert isinstance(DockerAPI(), DockerAPI)


def login(docker_api):
    aws_api = AWSAPI()
    credentials = aws_api.get_ecr_authorization_info(region=src_aws_region)
    credentials = credentials[0]
    registry, username, password = credentials["proxy_host"], credentials["user_name"], credentials["decoded_token"]
    docker_api.login(registry, username, password)


def test_login():
    docker_api = DockerAPI()
    aws_api = AWSAPI()
    credentials = aws_api.get_ecr_authorization_info(region=src_aws_region)
Beispiel #14
0
import os
import pdb

from horey.aws_api.aws_clients.route53_client import Route53Client
from horey.aws_api.aws_api_configuration_policy import AWSAPIConfigurationPolicy
from horey.h_logger import get_logger
from horey.common_utils.common_utils import CommonUtils

from horey.aws_api.base_entities.aws_account import AWSAccount

logger = get_logger()

configuration = AWSAPIConfigurationPolicy()
configuration.configuration_file_full_path = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "..",
                 "ignore", "aws_api_configuration_values.py"))
configuration.init_from_file()

accounts = CommonUtils.load_object_from_module(configuration.accounts_file,
                                               "main")
AWSAccount.set_aws_account(accounts[configuration.aws_api_account])


def test_init_route53_client():
    assert isinstance(Route53Client(), Route53Client)


if __name__ == "__main__":
    test_init_route53_client()