16d12a
From dc9460f161efce6770f66bb95d60cea6d27df722 Mon Sep 17 00:00:00 2001
16d12a
From: Eduardo Otubo <otubo@redhat.com>
16d12a
Date: Thu, 25 Jun 2020 08:03:59 +0200
16d12a
Subject: [PATCH] ec2: only redact token request headers in logs, avoid
16d12a
 altering request (#230)
16d12a
16d12a
RH-Author: Eduardo Otubo <otubo@redhat.com>
16d12a
Message-id: <20200624112104.376-1-otubo@redhat.com>
16d12a
Patchwork-id: 97793
16d12a
O-Subject: [RHEL-8.3.0 cloud-init PATCH] ec2: only redact token request headers in logs, avoid altering request (#230)
16d12a
Bugzilla: 1822343
16d12a
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
16d12a
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
16d12a
RH-Acked-by: Cathy Avery <cavery@redhat.com>
16d12a
16d12a
From: Chad Smith <chad.smith@canonical.com>
16d12a
16d12a
commit fa1abfec27050a4fb71cad950a17e42f9b43b478
16d12a
Author: Chad Smith <chad.smith@canonical.com>
16d12a
Date:   Tue Mar 3 15:23:33 2020 -0700
16d12a
16d12a
    ec2: only redact token request headers in logs, avoid altering request (#230)
16d12a
16d12a
    Our header redact logic was redacting both logged request headers and
16d12a
    the actual source request. This results in DataSourceEc2 sending the
16d12a
    invalid header "X-aws-ec2-metadata-token-ttl-seconds: REDACTED" which
16d12a
    gets an HTTP status response of 400.
16d12a
16d12a
    Cloud-init retries this failed token request for 2 minutes before
16d12a
    falling back to IMDSv1.
16d12a
16d12a
    LP: #1865882
16d12a
16d12a
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
16d12a
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
16d12a
---
16d12a
 cloudinit/tests/test_url_helper.py | 34 +++++++++++++++++++++++++++++++++-
16d12a
 cloudinit/url_helper.py            | 15 ++++++++-------
16d12a
 2 files changed, 41 insertions(+), 8 deletions(-)
16d12a
16d12a
diff --git a/cloudinit/tests/test_url_helper.py b/cloudinit/tests/test_url_helper.py
16d12a
index 1674120..29b3937 100644
16d12a
--- a/cloudinit/tests/test_url_helper.py
16d12a
+++ b/cloudinit/tests/test_url_helper.py
16d12a
@@ -1,7 +1,8 @@
16d12a
 # This file is part of cloud-init. See LICENSE file for license information.
16d12a
 
16d12a
 from cloudinit.url_helper import (
16d12a
-    NOT_FOUND, UrlError, oauth_headers, read_file_or_url, retry_on_url_exc)
16d12a
+    NOT_FOUND, UrlError, REDACTED, oauth_headers, read_file_or_url,
16d12a
+    retry_on_url_exc)
16d12a
 from cloudinit.tests.helpers import CiTestCase, mock, skipIf
16d12a
 from cloudinit import util
16d12a
 from cloudinit import version
16d12a
@@ -50,6 +51,9 @@ class TestOAuthHeaders(CiTestCase):
16d12a
 
16d12a
 
16d12a
 class TestReadFileOrUrl(CiTestCase):
16d12a
+
16d12a
+    with_logs = True
16d12a
+
16d12a
     def test_read_file_or_url_str_from_file(self):
16d12a
         """Test that str(result.contents) on file is text version of contents.
16d12a
         It should not be "b'data'", but just "'data'" """
16d12a
@@ -71,6 +75,34 @@ class TestReadFileOrUrl(CiTestCase):
16d12a
         self.assertEqual(result.contents, data)
16d12a
         self.assertEqual(str(result), data.decode('utf-8'))
16d12a
 
16d12a
+    @httpretty.activate
16d12a
+    def test_read_file_or_url_str_from_url_redacting_headers_from_logs(self):
16d12a
+        """Headers are redacted from logs but unredacted in requests."""
16d12a
+        url = 'http://hostname/path'
16d12a
+        headers = {'sensitive': 'sekret', 'server': 'blah'}
16d12a
+        httpretty.register_uri(httpretty.GET, url)
16d12a
+
16d12a
+        read_file_or_url(url, headers=headers, headers_redact=['sensitive'])
16d12a
+        logs = self.logs.getvalue()
16d12a
+        for k in headers.keys():
16d12a
+            self.assertEqual(headers[k],  httpretty.last_request().headers[k])
16d12a
+        self.assertIn(REDACTED, logs)
16d12a
+        self.assertNotIn('sekret', logs)
16d12a
+
16d12a
+    @httpretty.activate
16d12a
+    def test_read_file_or_url_str_from_url_redacts_noheaders(self):
16d12a
+        """When no headers_redact, header values are in logs and requests."""
16d12a
+        url = 'http://hostname/path'
16d12a
+        headers = {'sensitive': 'sekret', 'server': 'blah'}
16d12a
+        httpretty.register_uri(httpretty.GET, url)
16d12a
+
16d12a
+        read_file_or_url(url, headers=headers)
16d12a
+        for k in headers.keys():
16d12a
+            self.assertEqual(headers[k], httpretty.last_request().headers[k])
16d12a
+        logs = self.logs.getvalue()
16d12a
+        self.assertNotIn(REDACTED, logs)
16d12a
+        self.assertIn('sekret', logs)
16d12a
+
16d12a
     @mock.patch(M_PATH + 'readurl')
16d12a
     def test_read_file_or_url_passes_params_to_readurl(self, m_readurl):
16d12a
         """read_file_or_url passes all params through to readurl."""
16d12a
diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py
16d12a
index 3e7de9f..e6188ea 100644
16d12a
--- a/cloudinit/url_helper.py
16d12a
+++ b/cloudinit/url_helper.py
16d12a
@@ -291,13 +291,14 @@ def readurl(url, data=None, timeout=None, retries=0, sec_between=1,
16d12a
         for (k, v) in req_args.items():
16d12a
             if k == 'data':
16d12a
                 continue
16d12a
-            filtered_req_args[k] = v
16d12a
-            if k == 'headers':
16d12a
-                for hkey, _hval in v.items():
16d12a
-                    if hkey in headers_redact:
16d12a
-                        filtered_req_args[k][hkey] = (
16d12a
-                            copy.deepcopy(req_args[k][hkey]))
16d12a
-                        filtered_req_args[k][hkey] = REDACTED
16d12a
+            if k == 'headers' and headers_redact:
16d12a
+                matched_headers = [k for k in headers_redact if v.get(k)]
16d12a
+                if matched_headers:
16d12a
+                    filtered_req_args[k] = copy.deepcopy(v)
16d12a
+                    for key in matched_headers:
16d12a
+                        filtered_req_args[k][key] = REDACTED
16d12a
+            else:
16d12a
+                filtered_req_args[k] = v
16d12a
         try:
16d12a
 
16d12a
             if log_req_resp:
16d12a
-- 
16d12a
1.8.3.1
16d12a