<commit_before>import logging
import hashlib
import json
import time
import boto3
import re
import shlex
from os import environ, path
from cgi import escape
from challenge import (
    verify_result,
    value_tests_rand_result,
    value_output_rand_result,
    value_rand_pass,
)
from challenge import bool_to_int_dyn
from docker_cmd import output_from_cmd, DockerValidationError
from dynamo import COMMANDS_TABLE_NAME, SUBMISSIONS_TABLE_NAME
from dynamo import raise_on_rate_limit, DynamoValidationError


LOG = logging.getLogger()
LOG.setLevel(logging.WARN)
DOCKER_HOST = environ["DOCKER_EC2_DNS"]
MAX_OUTPUT_LEN = 10000
SLACK_SNS_ARN = "arn:aws:sns:us-east-1:414252096707:cmdchallenge-slack"
MAX_CMD_LENGTH = 300


class LambdaValidationError(Exception):
    pass


def send_msg(message=None, channel=None, cmd=None, challenge_slug=None):
    client = boto3.client("sns")
    msg_json = json.dumps(
        dict(message=message, channel=channel, cmd=cmd, challenge_slug=challenge_slug)
    )
    client.publish(TargetArn=SLACK_SNS_ARN, Message=msg_json)


def false_if_empty(input_str):
    if isinstance(input_str, str) and input_str == "":
        return False
    return input_str


def merge_two_dicts(x, y):
    """Given two dicts, merge them into a new dict as a shallow copy."""
    z = x.copy()
    z.update(y)
    return z


def default_resp(err=None, res=None, cmd=None, challenge_slug=None):
    LOG.debug(
        f"Responding with: err: {err} res: {res}, cmd: {cmd}, challenge_slug: {challenge_slug}"
    )

    headers = {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
        "Cache-Control": "no-cache, no-store",
    }

    if err:
        LOG.warn(
            f"Responding with error: err: {err} res: {res} cmd: {cmd} challenge_slug: {challenge_slug}"
        )
        send_msg(str(err), channel="ops", cmd=cmd, challenge_slug=challenge_slug)
        return {
            "statusCode": "400",
            "body": str(err),
            "headers": headers,
        }

    if res["cached"]:
        headers.update({"Cache-Control": "max-age=31536000"})

    if not res["cached"] and res["correct"]:
        send_msg(channel="commands", cmd=cmd, challenge_slug=challenge_slug)

    return {
        "statusCode": "200",
        "body": json.dumps(res),
        "headers": headers,
    }


def write_item_to_db(item, table_name):
    dynamo_table = boto3.resource("dynamodb").Table(table_name)
    try:
        dynamo_table.put_item(Item=item)
    except Exception as e:
        LOG.exception(f"Error updating the {table_name} table with {item} : {e}")
        raise LambdaValidationError("Database error :(")


def get_cmd_from_db(hashed_submission):
    dynamo_submissions = boto3.resource("dynamodb").Table(COMMANDS_TABLE_NAME)
    try:
        q = dynamo_submissions.get_item(Key={"id": hashed_submission})
    except Exception as e:
        LOG.exception(f"Error querying the database for {hashed_submission}: {e}")
        raise LambdaValidationError("Database error :(")
    if "Item" not in q:
        return None
    cmd = q["Item"]
    json_data = cmd.pop("cmd_json_data", None)
    if json_data:
        cmd = merge_two_dicts(cmd, json.loads(json_data))
    return cmd


def check_use_cache(cached_response, challenge):
    if not cached_response:
        # If there is no cached response, we can't
        # use the cache obviously
        return False

    cache_correct = challenge.get("cache_correct", True)
    cache_incorrect = challenge.get("cache_incorrect", True)

    return cache_correct if cached_response["correct"] else cache_incorrect


def handler(event, context):
    if "queryStringParameters" not in event:
        return default_resp(err="Missing params.")
    body = event["queryStringParameters"]
    if body is None:
        return default_resp(err="Missing params.")<commit_msg>Adds sentry and dummy error<commit_after>import sentry_sdk
sentry_sdk.init(
    "https://64fce3067b92496891b7eec662513f66@o484121.ingest.sentry.io/5536941",
    traces_sample_rate=1.0
)

import logging
import hashlib
import json
import time
import boto3
import re
import shlex
from os import environ, path
from cgi import escape
from challenge import (
    verify_result,
    value_tests_rand_result,
    value_output_rand_result,
    value_rand_pass,
)
from challenge import bool_to_int_dyn
from docker_cmd import output_from_cmd, DockerValidationError
from dynamo import COMMANDS_TABLE_NAME, SUBMISSIONS_TABLE_NAME
from dynamo import raise_on_rate_limit, DynamoValidationError

LOG = logging.getLogger()
LOG.setLevel(logging.WARN)
DOCKER_HOST = environ["DOCKER_EC2_DNS"]
MAX_OUTPUT_LEN = 10000
SLACK_SNS_ARN = "arn:aws:sns:us-east-1:414252096707:cmdchallenge-slack"
MAX_CMD_LENGTH = 300


class LambdaValidationError(Exception):
    pass


def send_msg(message=None, channel=None, cmd=None, challenge_slug=None):
    client = boto3.client("sns")
    msg_json = json.dumps(
        dict(message=message, channel=channel, cmd=cmd, challenge_slug=challenge_slug)
    )
    client.publish(TargetArn=SLACK_SNS_ARN, Message=msg_json)


def false_if_empty(input_str):
    if isinstance(input_str, str) and input_str == "":
        return False
    return input_str


def merge_two_dicts(x, y):
    """Given two dicts, merge them into a new dict as a shallow copy."""
    z = x.copy()
    z.update(y)
    return z


def default_resp(err=None, res=None, cmd=None, challenge_slug=None):
    LOG.debug(
        f"Responding with: err: {err} res: {res}, cmd: {cmd}, challenge_slug: {challenge_slug}"
    )

    headers = {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
        "Cache-Control": "no-cache, no-store",
    }

    if err:
        LOG.warn(
            f"Responding with error: err: {err} res: {res} cmd: {cmd} challenge_slug: {challenge_slug}"
        )
        send_msg(str(err), channel="ops", cmd=cmd, challenge_slug=challenge_slug)
        return {
            "statusCode": "400",
            "body": str(err),
            "headers": headers,
        }

    if res["cached"]:
        headers.update({"Cache-Control": "max-age=31536000"})

    if not res["cached"] and res["correct"]:
        send_msg(channel="commands", cmd=cmd, challenge_slug=challenge_slug)

    return {
        "statusCode": "200",
        "body": json.dumps(res),
        "headers": headers,
    }


def write_item_to_db(item, table_name):
    dynamo_table = boto3.resource("dynamodb").Table(table_name)
    try:
        dynamo_table.put_item(Item=item)
    except Exception as e:
        LOG.exception(f"Error updating the {table_name} table with {item} : {e}")
        raise LambdaValidationError("Database error :(")


def get_cmd_from_db(hashed_submission):
    dynamo_submissions = boto3.resource("dynamodb").Table(COMMANDS_TABLE_NAME)
    try:
        q = dynamo_submissions.get_item(Key={"id": hashed_submission})
    except Exception as e:
        LOG.exception(f"Error querying the database for {hashed_submission}: {e}")
        raise LambdaValidationError("Database error :(")
    if "Item" not in q:
        return None
    cmd = q["Item"]
    json_data = cmd.pop("cmd_json_data", None)
    if json_data:
        cmd = merge_two_dicts(cmd, json.loads(json_data))
    return cmd


def check_use_cache(cached_response, challenge):
    if not cached_response:
        # If there is no cached response, we can't
        # use the cache obviously
        return False

    cache_correct = challenge.get("cache_correct", True)
    cache_incorrect = challenge.get("cache_incorrect", True)

    return cache_correct if cached_response["correct"] else cache_incorrect


def handler(event, context):
    division_by_zero = 1 / 0
    if "queryStringParameters" not in event:
        return default_resp(err="Missing params.")
    body = event["queryStringParameters"]
    if body is None:
        return default_resp(err="Missing params.")
{'fraction_non_alphanumeric': 0.109095490047871, 'fraction_numerical': 0.015117157974300832, 'mean_word_length': 3.607661056297156, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5689164', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2569, 'n_words': 595}