Exemplo n.º 1
0
 def prepare_environment():
     # set environment
     os.environ["AWS_REGION"] = config.DEFAULT_REGION
     os.environ["ENV"] = ENV_DEV
     # register signal handlers
     if not config.is_local_test_mode():
         register_signal_handlers()
     # make sure AWS credentials are configured, otherwise boto3 bails on us
     check_aws_credentials()
     patch_moto_request_handling()
Exemplo n.º 2
0
def read_client_metadata() -> ClientMetadata:
    return ClientMetadata(
        session_id=get_session_id(),
        machine_id=get_machine_id(),
        api_key=read_api_key_safe(),
        system=get_system(),
        version=get_version_string(),
        is_ci=os.getenv("CI") is not None,
        is_docker=config.is_in_docker,
        is_testing=config.is_local_test_mode(),
    )
Exemplo n.º 3
0
    def is_tracking_disabled(self):
        if self.force_tracking:
            return False

        # don't track if event tracking is disabled globally
        if config.DISABLE_EVENTS:
            return True
        # don't track for internal test runs (like integration tests)
        if config.is_local_test_mode():
            return True
        if self.tracking_disabled:
            return True

        return False
Exemplo n.º 4
0
def events_handler_put_events(self):
    entries = self._get_param("Entries")

    # keep track of events for local integration testing
    if config.is_local_test_mode():
        TEST_EVENTS_CACHE.extend(entries)

    events = list(map(lambda event: {"event": event, "uuid": str(long_uid())}, entries))

    _dump_events_to_files(events)
    event_rules = self.events_backend.rules

    for event_envelope in events:
        event = event_envelope["event"]
        event_bus = event.get("EventBusName") or DEFAULT_EVENT_BUS_NAME

        matchine_rules = [r for r in event_rules.values() if r.event_bus_name == event_bus]
        if not matchine_rules:
            continue

        formatted_event = {
            "version": "0",
            "id": event_envelope["uuid"],
            "detail-type": event.get("DetailType"),
            "source": event.get("Source"),
            "account": TEST_AWS_ACCOUNT_ID,
            "time": datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
            "region": self.region,
            "resources": event.get("Resources", []),
            "detail": json.loads(event.get("Detail", "{}")),
        }

        targets = []
        for rule in matchine_rules:
            if filter_event_based_on_event_format(self, rule.name, formatted_event):
                targets.extend(self.events_backend.list_targets_by_rule(rule.name)["Targets"])

        # process event
        process_events(formatted_event, targets)

    content = {
        "FailedEntryCount": 0,  # TODO: dynamically set proper value when refactoring
        "Entries": list(map(lambda event: {"EventId": event["uuid"]}, events)),
    }

    self.response_headers.update(
        {"Content-Type": APPLICATION_AMZ_JSON_1_1, "x-amzn-RequestId": short_uid()}
    )

    return json.dumps(content), self.response_headers
Exemplo n.º 5
0
def fire_event(event_type, payload=None):
    # TODO: replace fire_event calls with analytics.log.event
    if config.DISABLE_EVENTS:
        return

    from localstack.utils.analytics import log

    if payload is None:
        payload = {}
    if isinstance(payload, dict):
        if is_travis():
            payload["travis"] = True
        if config.is_local_test_mode():
            payload["int"] = True

    log.event("legacy", {"event": event_type, "payload": payload})
Exemplo n.º 6
0
    def forward_request(self, method, path, data, headers):
        if method == "OPTIONS":
            return 200

        if method == "POST" and path == "/":
            action = headers.get("X-Amz-Target", "").split(".")[-1]
            parsed_data = json.loads(to_str(data))

            if action == "PutRule":
                return handle_put_rule(parsed_data)

            elif action == "DeleteRule":
                handle_delete_rule(rule_name=parsed_data.get("Name", None))

            elif action == "DisableRule":
                handle_disable_rule(rule_name=parsed_data.get("Name", None))

            elif action == "PutEvents":
                # keep track of events for local integration testing
                if config.is_local_test_mode():
                    TEST_EVENTS_CACHE.extend(parsed_data.get("Entries", []))

        return True
Exemplo n.º 7
0
def is_local_test_mode():
    return config.is_local_test_mode()