Add configuring hashing func
via SF_NOTIFIER_ALERT_ID_HASH_FUNC env var
Change-Id: I7c4a2592a85825d75fe0c18c18789fcfc3d0ceff
Related-bug: PROD-25673 (PROD:25673)
diff --git a/sf_notifier/salesforce/client.py b/sf_notifier/salesforce/client.py
index 5adc691..ae265bc 100644
--- a/sf_notifier/salesforce/client.py
+++ b/sf_notifier/salesforce/client.py
@@ -47,6 +47,8 @@
'sandbox_enabled': 'domain',
}
+ALLOWED_HASHING = ('md5', 'sha256')
+
logger = logging.getLogger(__name__)
@@ -75,11 +77,19 @@
}
self.session = requests.Session()
self.config = self._validate_config(config)
+ self.hash_func = self._hash_func()
self.environment = self.config.pop('environment_id')
self._auth()
self._registered_alerts = {}
@staticmethod
+ def _hash_func():
+ name = os.environ.get('SF_NOTIFIER_ALERT_ID_HASH_FUNC', 'sha256')
+ if name in ALLOWED_HASHING:
+ return getattr(hashlib, name)
+ return hashlib.sha256
+
+ @staticmethod
def _validate_config(config):
kwargs = {}
@@ -115,12 +125,11 @@
logger.info('Salesforce authentication successful.')
self.metrics['sf_auth_ok'].set(1)
- @staticmethod
- def _get_alert_id(labels):
+ def _get_alert_id(self, labels):
alert_id_data = ''
for key in sorted(labels):
alert_id_data += labels[key].replace(".", "\\.")
- return hashlib.sha256(alert_id_data).hexdigest()
+ return self.hash_func(alert_id_data).hexdigest()
@sf_auth_retry
def _create_case(self, subject, body, labels, alert_id):
diff --git a/sf_notifier/server.py b/sf_notifier/server.py
index e9414bc..ba1017f 100644
--- a/sf_notifier/server.py
+++ b/sf_notifier/server.py
@@ -42,7 +42,10 @@
@app.route('/info', methods=['GET'])
def info():
- return jsonify({'version': settings.VERSION})
+ return jsonify({
+ 'version': settings.VERSION,
+ 'hashing': sf_cli.hash_func.__name__
+ })
@app.route('/hook', methods=['POST'])
diff --git a/sf_notifier/tests/test_client.py b/sf_notifier/tests/test_client.py
index b62eaa0..c6a2a2e 100644
--- a/sf_notifier/tests/test_client.py
+++ b/sf_notifier/tests/test_client.py
@@ -70,46 +70,3 @@
assert SalesforceClient._validate_config(OK_CONFIG_2[0]) == OK_CONFIG_2[1]
pytest.raises(SfNotifierError,
SalesforceClient._validate_config, WRONG_CONFIG)
-
-
-def test_alert_id_generate():
- LABELS = {
- 'alertname': 'ApacheServiceDown',
- 'environment': 'testing_env',
- 'host': 'mdb01',
- 'instance': 'mdb01;telegraf',
- 'job': 'telegraf',
- 'region': 'region1',
- 'route': 'email,salesforce',
- 'service': 'apache',
- 'severity': 'minor'
- }
- SAME_LABELS = {
- 'environment': 'testing_env',
- 'service': 'apache',
- 'route': 'email,salesforce',
- 'instance': 'mdb01;telegraf',
- 'host': 'mdb01',
- 'severity': 'minor',
- 'region': 'region1',
- 'alertname': 'ApacheServiceDown',
- 'job': 'telegraf'
- }
- DIFF_LABELS = {
- 'environment': 'testing_env',
- 'service': 'apache',
- 'route': 'email,salesforce',
- 'instance': 'mdb01;telegraf',
- 'host': 'mdb01',
- 'severity': 'minor',
- 'region': 'region1',
- 'alertname': 'ApacheServiceDow',
- 'job': 'telegraf'
- }
-
- unset_env_vars()
-
- alert_id = SalesforceClient._get_alert_id(LABELS)
-
- assert alert_id == SalesforceClient._get_alert_id(SAME_LABELS)
- assert alert_id != SalesforceClient._get_alert_id(DIFF_LABELS)