blob: 5f95d74bf1c640e7d9c896f158b50048bb24d278 [file] [log] [blame]
Andrew Geisslereff27472021-10-29 15:35:00 -05001From 3540ddcc7448dc784b65c74424c8a25132cb8534 Mon Sep 17 00:00:00 2001
Brad Bishopd7bf8c12018-02-25 22:55:05 -05002From: Hongxu Jia <hongxu.jia@windriver.com>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08003Date: Tue, 31 Jul 2018 17:24:47 +0800
Andrew Geisslereff27472021-10-29 15:35:00 -05004Subject: [PATCH] support authentication for kickstart
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005
6While download kickstart file from web server,
7we support basic/digest authentication.
8
9Add KickstartAuthError to report authentication failure,
10which the invoker could parse this specific error.
11
12Upstream-Status: inappropriate [oe specific]
13
14Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
Andrew Geisslereff27472021-10-29 15:35:00 -050015
Brad Bishopd7bf8c12018-02-25 22:55:05 -050016---
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080017 pykickstart/errors.py | 17 +++++++++++++++++
18 pykickstart/load.py | 34 ++++++++++++++++++++++++++++------
Brad Bishopd7bf8c12018-02-25 22:55:05 -050019 pykickstart/parser.py | 4 ++--
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080020 3 files changed, 47 insertions(+), 8 deletions(-)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050021
22diff --git a/pykickstart/errors.py b/pykickstart/errors.py
Andrew Geisslereff27472021-10-29 15:35:00 -050023index 8294f59a..3d20bf82 100644
Brad Bishopd7bf8c12018-02-25 22:55:05 -050024--- a/pykickstart/errors.py
25+++ b/pykickstart/errors.py
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080026@@ -32,6 +32,9 @@ This module exports several exception classes:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050027 KickstartVersionError - An exception for errors relating to unsupported
28 syntax versions.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080029
Brad Bishopd7bf8c12018-02-25 22:55:05 -050030+ KickstartAuthError - An exception for errors relating to authentication
31+ failed while downloading kickstart from web server
32+
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080033 And some warning classes:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050034
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080035 KickstartWarning - A generic warning class.
Andrew Geissler748a4832020-07-24 16:24:21 -050036@@ -125,3 +128,17 @@ class KickstartDeprecationWarning(KickstartParseWarning, DeprecationWarning):
37 """A class for warnings occurring during parsing related to using deprecated
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080038 commands and options.
39 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040+
41+class KickstartAuthError(KickstartError):
42+ """An exception for errors relating to authentication failed while
43+ downloading kickstart from web server
44+ """
45+ def __init__(self, msg):
46+ """Create a new KickstartAuthError exception instance with the
Andrew Geissler748a4832020-07-24 16:24:21 -050047+ descriptive message val. val should be the return value of
Brad Bishopd7bf8c12018-02-25 22:55:05 -050048+ formatErrorMsg.
49+ """
50+ KickstartError.__init__(self, msg)
51+
52+ def __str__(self):
53+ return self.value
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054diff --git a/pykickstart/load.py b/pykickstart/load.py
Andrew Geisslereff27472021-10-29 15:35:00 -050055index 30e2fcfa..b984876d 100644
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056--- a/pykickstart/load.py
57+++ b/pykickstart/load.py
Andrew Geisslereff27472021-10-29 15:35:00 -050058@@ -18,9 +18,12 @@
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059 # with the express permission of Red Hat, Inc.
60 #
61 import requests
62+from requests.auth import HTTPDigestAuth
63+from requests.auth import HTTPBasicAuth
64+
65 import shutil
Brad Bishopd7bf8c12018-02-25 22:55:05 -050066
67-from pykickstart.errors import KickstartError
68+from pykickstart.errors import KickstartError, KickstartAuthError
69 from pykickstart.i18n import _
70 from requests.exceptions import SSLError, RequestException
71
Andrew Geisslereff27472021-10-29 15:35:00 -050072@@ -28,7 +31,7 @@ _is_url = lambda location: '://' in location # RFC 3986
Brad Bishopd7bf8c12018-02-25 22:55:05 -050073
74 SSL_VERIFY = True
75
76-def load_to_str(location):
77+def load_to_str(location, user=None, passwd=None):
78 '''Load a destination URL or file into a string.
79 Type of input is inferred automatically.
80
Andrew Geisslereff27472021-10-29 15:35:00 -050081@@ -39,7 +42,7 @@ def load_to_str(location):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050082 Raises: KickstartError on error reading'''
83
84 if _is_url(location):
85- return _load_url(location)
86+ return _load_url(location, user=user, passwd=passwd)
87 else:
88 return _load_file(location)
89
Andrew Geisslereff27472021-10-29 15:35:00 -050090@@ -69,11 +72,30 @@ def load_to_file(location, destination):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050091 _copy_file(location, destination)
92 return destination
93
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080094-def _load_url(location):
95- '''Load a location (URL or filename) and return contents as string'''
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096+def _get_auth(location, user=None, passwd=None):
97+
98+ auth = None
99+ request = requests.get(location, verify=SSL_VERIFY)
100+ if request.status_code == requests.codes.unauthorized:
101+ if user is None or passwd is None:
102+ log.info("Require Authentication")
103+ raise KickstartAuthError("Require Authentication.\nAppend 'ksuser=<username> kspasswd=<password>' to boot command")
104
105+ reasons = request.headers.get("WWW-Authenticate", "").split()
106+ if reasons:
107+ auth_type = reasons[0]
108+ if auth_type == "Basic":
109+ auth = HTTPBasicAuth(user, passwd)
110+ elif auth_type == "Digest":
111+ auth=HTTPDigestAuth(user, passwd)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800112+
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500113+ return auth
114+
115+def _load_url(location, user=None, passwd=None):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800116+ '''Load a location (URL or filename) and return contents as string'''
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500117+ auth = _get_auth(location, user=user, passwd=passwd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118 try:
119- request = requests.get(location, verify=SSL_VERIFY)
120+ request = requests.get(location, verify=SSL_VERIFY, auth=auth)
121 except SSLError as e:
122 raise KickstartError(_('Error securely accessing URL "%s"') % location + ': {e}'.format(e=str(e)))
123 except RequestException as e:
124diff --git a/pykickstart/parser.py b/pykickstart/parser.py
Andrew Geisslereff27472021-10-29 15:35:00 -0500125index b23e54f1..e10f06b5 100644
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126--- a/pykickstart/parser.py
127+++ b/pykickstart/parser.py
Andrew Geisslereff27472021-10-29 15:35:00 -0500128@@ -796,7 +796,7 @@ class KickstartParser(object):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500129 i = PutBackIterator(s.splitlines(True) + [""])
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800130 self._stateMachine(i)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500131
132- def readKickstart(self, f, reset=True):
133+ def readKickstart(self, f, reset=True, username=None, password=None):
134 """Process a kickstart file, given by the filename f."""
135 if reset:
136 self._reset()
Andrew Geisslereff27472021-10-29 15:35:00 -0500137@@ -817,7 +817,7 @@ class KickstartParser(object):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500138 self.currentdir[self._includeDepth] = cd
139
140 try:
141- s = load_to_str(f)
142+ s = load_to_str(f, user=username, passwd=password)
143 except KickstartError as e:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800144 raise KickstartError(_("Unable to open input kickstart file: %s") % str(e), lineno=0)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145