Esempio n. 1
0
def test_get_aws_secrets_manager_binary_secret(mock_boto3):
    mock_boto3.get_secret_value.return_value = {"binary": "it_will_fail"}
    with pytest.raises(AwsSecretsManagerTokenRetrievalException):
        get_token([{
            "secret_name": "hq_token",
            "type": "AWS_SECRETS_MANAGER",
            "region": "us-west-2",
            "secret_key": "token"
        }])
    mock_boto3.get_secret_value.assert_called_with(SecretId="hq_token")
Esempio n. 2
0
def test_get_aws_secrets_manager_incorrect_secret_key(mock_boto3):
    mock_boto3.get_secret_value.return_value = {
        "SecretString": '{"token": "my_token"}'
    }
    with pytest.raises(AwsSecretsManagerTokenRetrievalException):
        get_token([{
            "secret_name": "hq_token",
            "type": "AWS_SECRETS_MANAGER",
            "region": "us-west-2",
            "secret_key": "wrong_key",
        }])
    mock_boto3.get_secret_value.assert_called_with(SecretId="hq_token")
Esempio n. 3
0
def test_get_aws_secrets_manager_invalid_parameter_exception(mock_boto3):
    mock_boto3.get_secret_value.side_effect = ClientError(
        error_response={"Error": {
            "Code": "InvalidParameterException"
        }},
        operation_name="get_secret_value")
    with pytest.raises(AwsSecretsManagerTokenRetrievalException):
        get_token([{
            "secret_name": "hq_token",
            "type": "AWS_SECRETS_MANAGER",
            "region": "us-west-2",
            "secret_key": "token"
        }])
    mock_boto3.get_secret_value.assert_called_with(SecretId="hq_token")
Esempio n. 4
0
def test_get_token_second_succeeds(mock_os):
    mock_os.environ.get.return_value = None
    token = get_token([{
        "value": "HQ_TOKEN",
        "type": "ENVIRONMENT_VARIABLE"
    }, {
        "value": "my_token",
        "type": "TOKEN"
    }])
    assert token == "my_token"
    mock_os.environ.get.assert_called_with("HQ_TOKEN")
Esempio n. 5
0
def test_get_aws_secrets_manager(mock_boto3):
    mock_boto3.get_secret_value.return_value = {
        "SecretString": '{"token": "my_token"}'
    }
    token = get_token([{
        "secret_name": "hq_token",
        "type": "AWS_SECRETS_MANAGER",
        "region": "us-west-2",
        "secret_key": "token"
    }])
    assert token == "my_token"
    mock_boto3.get_secret_value.assert_called_with(SecretId="hq_token")
    if len(sys.argv) <= 1:
        fatal_error("Missing configuration file argument")
        sys.exit(1)

    try:
        config_data = load(open(sys.argv[1], "r"), SafeLoader)
    except FileNotFoundError:
        fatal_error(f"File not found: {sys.argv[1]}")
        sys.exit(1)

    validate_config()

    webhooks: List[Webhook] = []
    for webhook in config_data.get("webhooks", []):
        webhooks.append(Webhook(webhook["url"], webhook.get("params", {})))

    token: str = get_token(config_data["cachet"]["token"])
    api_url: str = os.environ.get(
        "CACHET_API_URL") or config_data["cachet"]["api_url"]
    client: CachetClient = CachetClient(api_url, token)
    for endpoint_index in range(len(config_data["endpoints"])):
        configuration = Configuration(config_data, endpoint_index, client,
                                      webhooks)
        NewThread(
            Scheduler(
                configuration,
                build_agent(configuration,
                            logging.getLogger(
                                "cachet_url_monitor.scheduler")))).start()
Esempio n. 7
0
def test_get_token_string_configuration():
    token = get_token("my_token")
    assert token == "my_token"
Esempio n. 8
0
def test_get_token_no_token_found(mock_os):
    mock_os.environ.get.return_value = None
    with pytest.raises(TokenNotFoundException):
        get_token([{"value": "HQ_TOKEN", "type": "ENVIRONMENT_VARIABLE"}])
    mock_os.environ.get.assert_called_with("HQ_TOKEN")