You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2020/11/01 20:54:40 UTC

[libcloud] 01/06: Fix error when creating SSHFP DNS records with cloudflare

This is an automated email from the ASF dual-hosted git repository.

tomaz pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git

commit a9c3c64f553ca9f9aef5b76176eb91ab9807a80e
Author: William Hughes <wi...@willhughes.name>
AuthorDate: Mon Oct 26 17:12:47 2020 +1300

    Fix error when creating SSHFP DNS records with cloudflare
    
    Fixes #1512
---
 libcloud/dns/drivers/cloudflare.py   | 35 +++++++++++++++++++++++++++--------
 libcloud/test/dns/test_cloudflare.py | 19 ++++++++++++++++---
 2 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/libcloud/dns/drivers/cloudflare.py b/libcloud/dns/drivers/cloudflare.py
index 5715b79..62dcbf4 100644
--- a/libcloud/dns/drivers/cloudflare.py
+++ b/libcloud/dns/drivers/cloudflare.py
@@ -317,14 +317,20 @@ class CloudFlareDNSDriver(DNSDriver):
         or iodef.
 
         For example: 0 issue test.caa.com
+
+        NOTE: For SSHFP RecordType, data need to be in the format:
+        <algorithm> <type> <fingerprint>
         """
         url = '{}/zones/{}/dns_records'.format(API_BASE, zone.id)
 
-        data = self._normalize_record_data_for_api(type=type, data=data,)
+        content, data = self._normalize_record_data_for_api(
+            type=type, data=data
+        )
         body = {
             'type': type,
             'name': name,
-            'content': data,
+            'content': content,
+            'data': data
         }
 
         merge_valid_keys(body, RECORD_CREATE_ATTRIBUTES, extra)
@@ -349,11 +355,14 @@ class CloudFlareDNSDriver(DNSDriver):
         url = '{}/zones/{}/dns_records/{}'.format(API_BASE, record.zone.id,
                                                   record.id)
 
-        data = self._normalize_record_data_for_api(type=type, data=data,)
+        content, data = self._normalize_record_data_for_api(
+            type=type, data=data
+        )
         body = {
             'type': record.type if type is None else type,
             'name': record.name if name is None else name,
-            'content': record.data if data is None else data,
+            'content': content,
+            'data': data,
             'extra': record.extra or {},
         }
 
@@ -436,18 +445,28 @@ class CloudFlareDNSDriver(DNSDriver):
 
     def _normalize_record_data_for_api(self, type, data):
         """
-        Normalize record data for "special" records such as CAA so it can be
-        used with the CloudFlare API.
+        Normalize record data for "special" records such as CAA and SSHFP
+        so it can be used with the CloudFlare API.
         """
+        cf_data = {}
         if not data:
-            return data
+            return data, cf_data
 
         if type == RecordType.CAA:
             # Replace whitespace with \t character which CloudFlare API
             # expects
             data = data.replace(' ', '\t')
 
-        return data
+        elif type == RecordType.SSHFP:
+            _fp = data.split(" ")
+            cf_data = {
+                "algorithm": _fp[0],
+                "type": _fp[1],
+                "fingerprint": _fp[2]
+            }
+            data = None
+
+        return data, cf_data
 
     def _normalize_record_data_from_api(self, type, data):
         """
diff --git a/libcloud/test/dns/test_cloudflare.py b/libcloud/test/dns/test_cloudflare.py
index 93ffcae..e851be8 100644
--- a/libcloud/test/dns/test_cloudflare.py
+++ b/libcloud/test/dns/test_cloudflare.py
@@ -261,9 +261,22 @@ class CloudFlareDNSDriverTestCase(unittest.TestCase):
 
         self.assertEqual(zone, updated_zone)
 
-    def test_normalize_record_data_for_api(self):
-        result = self.driver._normalize_record_data_for_api(RecordType.CAA, '0 issue foo.bar')
-        self.assertEqual(result, '0\tissue\tfoo.bar')
+    def test_caa_normalize_record_data_for_api(self):
+        content, data = self.driver._normalize_record_data_for_api(RecordType.CAA, '0 issue foo.bar')
+        self.assertEqual(content, '0\tissue\tfoo.bar')
+        self.assertEqual(data, {})
+
+    def test_sshfp_normalize_record_data_for_api(self):
+        content, data = self.driver._normalize_record_data_for_api(RecordType.SSHFP, '2 1 ABCDEF12345')
+        self.assertIsNone(content)
+        self.assertEqual(
+            data,
+            {
+                "algorithm": "2",
+                "type": "1",
+                "fingerprint": "ABCDEF12345"
+            }
+        )
 
     def test_normalize_record_data_from_apu(self):
         result = self.driver._normalize_record_data_from_api(RecordType.CAA, '0\tissue\tfoo.bar')