-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a diagnostic for TLS certificate files (#15470)
* Diagnose TLS setup * Add tests for diagnoses
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# (C) Datadog, Inc. 2023-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
import json | ||
from operator import itemgetter | ||
|
||
from datadog_checks.base.utils.diagnose import Diagnosis | ||
|
||
|
||
def test_no_diagnosis_when_no_certificates_are_specified(instance, check): | ||
mongo_check = check(instance) | ||
diagnoses = json.loads(mongo_check.get_diagnoses()) | ||
assert len(diagnoses) == 0 | ||
|
||
|
||
def test_certificate_files_success(instance, check, tmp_path): | ||
certificate_key_path = tmp_path / 'client.pem' | ||
ca_path = tmp_path / 'ca.pem' | ||
|
||
# Create dummy certificate files | ||
certificate_key_path.touch() | ||
ca_path.touch() | ||
|
||
instance['tls_certificate_key_file'] = str(certificate_key_path) | ||
instance['tls_ca_file'] = str(ca_path) | ||
|
||
mongo_check = check(instance) | ||
diagnoses = sorted(json.loads(mongo_check.get_diagnoses()), key=itemgetter('diagnosis')) | ||
assert len(diagnoses) == 2 | ||
assert diagnoses[0]['result'] == Diagnosis.DIAGNOSIS_SUCCESS | ||
assert str(ca_path) in diagnoses[0]['diagnosis'] | ||
assert diagnoses[1]['result'] == Diagnosis.DIAGNOSIS_SUCCESS | ||
assert str(certificate_key_path) in diagnoses[1]['diagnosis'] | ||
|
||
|
||
def test_certificate_files_failure_not_exist(instance, check, tmp_path): | ||
certificate_key_path = tmp_path / 'client.pem' | ||
ca_path = tmp_path / 'ca.pem' | ||
|
||
instance['tls_certificate_key_file'] = str(certificate_key_path) | ||
instance['tls_ca_file'] = str(ca_path) | ||
|
||
mongo_check = check(instance) | ||
diagnoses = sorted(json.loads(mongo_check.get_diagnoses()), key=itemgetter('diagnosis')) | ||
assert len(diagnoses) == 2 | ||
assert diagnoses[0]['result'] == Diagnosis.DIAGNOSIS_FAIL | ||
assert str(ca_path) in diagnoses[0]['diagnosis'] | ||
assert "does not exist" in diagnoses[0]['diagnosis'] | ||
assert diagnoses[1]['result'] == Diagnosis.DIAGNOSIS_FAIL | ||
assert str(certificate_key_path) in diagnoses[1]['diagnosis'] | ||
assert "does not exist" in diagnoses[1]['diagnosis'] |