def test_to_csv_returns_csv_str(self):
     result_set = ResultSet(
         job=Job(
             name="foobizz",
             description="FooBizz",
             graph_spec=JobGraphSpec(graph_names=["test"]),
             category=Category.gov,
             severity=Severity.debug,
             query="select ?foo ?fizz where { ?foo ?fizz } order by ?foo",
             active=True,
             created=datetime(2020, 1, 1),
             query_fields=["account_id", "account_name"],
             max_graph_age_sec=10000,
             result_expiration_sec=100000,
             max_result_age_sec=100000,
             notify_if_results=False,
         ),
         graph_spec=ResultSetGraphSpec(
             graph_uris_load_times={"https://alti/alti/1/1234": 1612974818}
         ),
         results=[
             Result(account_id="123456789101", result={"foo": "boo", "fizz": "bizz"}),
             Result(account_id="123456789101", result={"foo": "boo2", "fizz": "bizz2"}),
         ],
     )
     expected_csv = "account_id,foo,fizz\n123456789101,boo,bizz\n123456789101,boo2,bizz2\n"
     self.assertEqual(expected_csv, result_set.to_csv())
Exemplo n.º 2
0
 def lambda_handler(cls, event: Dict[str, Any], _: Any) -> None:
     """lambda entrypoint"""
     config = Config()
     result = Result(**event)
     logger = Logger()
     errors: List[str] = []
     with logger.bind(result=result):
         logger.info(event=QJLogEvents.ResultRemediationStart)
         try:
             session = get_assumed_session(
                 account_id=result.account_id,
                 role_name=config.remediator_target_role_name,
                 external_id=config.remediator_target_role_external_id,
             )
             cls.remediate(session=session,
                           result=result.result,
                           dry_run=config.dry_run)
             logger.info(event=QJLogEvents.ResultRemediationSuccessful)
         except Exception as ex:
             logger.error(event=QJLogEvents.ResultRemediationFailed,
                          error=str(ex))
             errors.append(str(ex))
     if errors:
         raise RemediationError(
             f"Errors found during remediation: {errors}")
Exemplo n.º 3
0
def _invoke_lambda(
    lambda_name: str,
    lambda_timeout: int,
    result: Result,
) -> Any:
    """Invoke a QJ's remediator function"""
    logger = Logger()
    with logger.bind(lambda_name=lambda_name,
                     lambda_timeout=lambda_timeout,
                     result=result):
        logger.info(event=QJLogEvents.InvokeResultRemediationLambdaStart)
        boto_config = botocore.config.Config(
            read_timeout=lambda_timeout + 10,
            retries={"max_attempts": 0},
        )
        session = boto3.Session()
        lambda_client = session.client("lambda", config=boto_config)
        event = result.json().encode("utf-8")
        try:
            resp = lambda_client.invoke(
                FunctionName=lambda_name,
                Payload=event,
            )
        except Exception as invoke_ex:
            error = str(invoke_ex)
            logger.info(event=QJLogEvents.InvokeResultRemediationLambdaError,
                        error=error)
            raise Exception(
                f"Error while invoking {lambda_name} with event: {str(event)}: {error}"
            ) from invoke_ex
        lambda_result: bytes = resp["Payload"].read()
        if resp.get("FunctionError", None):
            error = lambda_result.decode()
            logger.info(event=QJLogEvents.ResultRemediationLambdaRunError,
                        error=error)
            raise Exception(
                f"Function error in {lambda_name} with event {str(event)}: {error}"
            )
        logger.info(event=QJLogEvents.InvokeResultRemediationLambdaEnd)
        return json.loads(lambda_result)
 def test_result_account_id_is_int(self):
     with self.assertRaises(ValueError):
         Result(account_id="abcd", result={"foo": "boo"})
 def test_result_account_id_zero_fill(self):
     result = Result(account_id="1234", result={"foo": "boo"})
     self.assertEqual(result.account_id, "000000001234")