Skip to content

Commit

Permalink
0.10.3: re-work 0.10.2, do not sanitze server URL and paths anymore
Browse files Browse the repository at this point in the history
* revert sanitization of server URL and paths
* client prints a warning if the server URL does not end with a slash (\)
* switch to string concatenation to build final URL, avoids urllib dependency
  • Loading branch information
maddes-b committed Sep 14, 2024
1 parent b6fa74a commit 1eff0d4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ Note: `--server` can be omitted if `default_server` is defined in configuration

```
acme-dns-client-2.sh register \
--domain 'test.example.com' --server 'https://acme-dns.example.net'
--domain 'test.example.com' --server 'https://acme-dns.example.net/'
```

==== Add an already registered domain

```
acme-dns-client-2.sh add \
--domain 'test.example.com' --fulldomain 'a1a1a1a1-b2b2-c3c3-d4d4-e5e5e5e5e5e5.acme-dns.example.net' --username '<USERNAME>' --password '<PASSWORD>' --server 'https://acme-dns.example.net'
--domain 'test.example.com' --fulldomain 'a1a1a1a1-b2b2-c3c3-d4d4-e5e5e5e5e5e5.acme-dns.example.net' --username '<USERNAME>' --password '<PASSWORD>' --server 'https://acme-dns.example.net/'
```

=== Check Domain Setup
Expand Down
5 changes: 4 additions & 1 deletion acme-dns-client-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""


__version__ = "0.10.2"
__version__ = "0.10.3"
__author__ = "Matthias \"Maddes\" Bücher"
__license__ = "GPLv2"
__copyright__ = "Copyright (C) 2024 Matthias \"Maddes\" Bücher"
Expand Down Expand Up @@ -301,6 +301,9 @@ def createArgParser() -> argparse.ArgumentParser:
print("Empty/whitespace-only server not allowed. {message:s}".format(message=Server_Message), file=sys.stderr)
sys.exit(1)

if not Server.endswith("/"):
print("WARNING! Server URL does not end with slash (\\). {message:s}".format(message=Server_Message), file=sys.stderr)

if not (Arguments.change is None or Arguments.change.strip()) \
or not (Arguments.clean is None or Arguments.clean.strip()) \
or not (Arguments.deregister is None or Arguments.deregister.strip()) \
Expand Down
63 changes: 24 additions & 39 deletions acmednsclient2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""


__version__ = "0.10.2"
__version__ = "0.10.3"
__author__ = "Matthias \"Maddes\" Bücher"
__license__ = "GPLv2"
__copyright__ = "Copyright (C) 2024 Matthias \"Maddes\" Bücher"
Expand All @@ -28,7 +28,6 @@
import datetime
import os
import typing
import urllib.parse

### 3rd-party modules
## https://pypi.org/project/dnspython/
Expand Down Expand Up @@ -179,11 +178,6 @@ def _loadFromFile(self) -> None:

### overwrite if known setting
if key in self.settings:
### special cases
if key == self.ATTR_URL_DEFAULT_SERVER:
if fileconfigdata and not fileconfigdata.endswith("/"):
fileconfigdata = "/".join((fileconfigdata, ""))

self.settings[key] = fileconfigdata
# --- /Configuration._loadFromFile()

Expand Down Expand Up @@ -275,8 +269,6 @@ def _loadFromFile(self, config:Configuration) -> None:
accdata[self.ATTR_USERNAME] = fileaccdata.pop(self.ATTR_USERNAME)
accdata[self.ATTR_PASSWORD] = fileaccdata.pop(self.ATTR_PASSWORD)
server_url = fileaccdata.pop(self.ATTR_SERVER_URL)
if server_url and not server_url.endswith("/"):
server_url = "/".join((server_url, ""))
accdata[self.ATTR_SERVER_URL] = server_url
#
try:
Expand All @@ -286,7 +278,7 @@ def _loadFromFile(self, config:Configuration) -> None:
path = config.settings[config.ATTR_URL_DEFAULT_PATH_CHANGE]
else:
path = config.DEFAULT_URL_PATH_CHANGE
accdata[self.ATTR_SERVER_URL_PATH_CHANGE] = path.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_CHANGE] = path
#
try:
path = fileaccdata.pop(self.ATTR_SERVER_URL_PATH_CLEAN)
Expand All @@ -295,7 +287,7 @@ def _loadFromFile(self, config:Configuration) -> None:
path = config.settings[config.ATTR_URL_DEFAULT_PATH_CLEAN]
else:
path = config.DEFAULT_URL_PATH_CLEAN
accdata[self.ATTR_SERVER_URL_PATH_CLEAN] = path.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_CLEAN] = path
#
try:
path = fileaccdata.pop(self.ATTR_SERVER_URL_PATH_DEREGISTER)
Expand All @@ -304,7 +296,7 @@ def _loadFromFile(self, config:Configuration) -> None:
path = config.settings[config.ATTR_URL_DEFAULT_PATH_DEREGISTER]
else:
path = config.DEFAULT_URL_PATH_DEREGISTER
accdata[self.ATTR_SERVER_URL_PATH_DEREGISTER] = path.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_DEREGISTER] = path
#
try:
path = fileaccdata.pop(self.ATTR_SERVER_URL_PATH_REGISTER)
Expand All @@ -313,7 +305,7 @@ def _loadFromFile(self, config:Configuration) -> None:
path = config.settings[config.ATTR_URL_DEFAULT_PATH_REGISTER]
else:
path = config.DEFAULT_URL_PATH_REGISTER
accdata[self.ATTR_SERVER_URL_PATH_REGISTER] = path.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_REGISTER] = path
#
try:
path = fileaccdata.pop(self.ATTR_SERVER_URL_PATH_UPDATE)
Expand All @@ -322,7 +314,7 @@ def _loadFromFile(self, config:Configuration) -> None:
path = config.settings[config.ATTR_URL_DEFAULT_PATH_UPDATE]
else:
path = config.DEFAULT_URL_PATH_UPDATE
accdata[self.ATTR_SERVER_URL_PATH_UPDATE] = path.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_UPDATE] = path
#
try:
accdata[self.ATTR_ADDED_ON] = fileaccdata.pop(self.ATTR_ADDED_ON)
Expand Down Expand Up @@ -458,15 +450,13 @@ def add(self, key:str, fulldomain:str, subdomain:str,
accdata[self.ATTR_SUBDOMAIN] = subdomain
accdata[self.ATTR_USERNAME] = username
accdata[self.ATTR_PASSWORD] = password
if server_url and not server_url.endswith("/"):
server_url = "/".join((server_url, ""))
accdata[self.ATTR_SERVER_URL] = server_url
#
accdata[self.ATTR_SERVER_URL_PATH_CHANGE] = server_path_change.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_CLEAN] = server_path_clean.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_DEREGISTER] = server_path_deregister.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_REGISTER] = server_path_register.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_UPDATE] = server_path_update.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_CHANGE] = server_path_change
accdata[self.ATTR_SERVER_URL_PATH_CLEAN] = server_path_clean
accdata[self.ATTR_SERVER_URL_PATH_DEREGISTER] = server_path_deregister
accdata[self.ATTR_SERVER_URL_PATH_REGISTER] = server_path_register
accdata[self.ATTR_SERVER_URL_PATH_UPDATE] = server_path_update
#
accdata[self.ATTR_ADDED_ON] = datetime.datetime.now(datetime.timezone.utc).isoformat()
accdata[self.ATTR_ADDED_VIA] = "add"
Expand Down Expand Up @@ -584,12 +574,7 @@ def register(self, key:str,
### convert request data into JSON format; post() json parameter = requests 2.4.2+
request_data = json.dumps(request_data, **JSON5_DUMP_KWARGS)

### sanitize arguments
if server_url and not server_url.endswith("/"):
server_url = "/".join((server_url, ""))
server_path_register = server_path_register.lstrip("/")

request_url = urllib.parse.urljoin(server_url, server_path_register)
request_url = "".join((server_url, server_path_register))
request_response = requests.post(
url=request_url,
headers=request_headers,
Expand Down Expand Up @@ -621,26 +606,26 @@ def register(self, key:str,
accdata[self.ATTR_SERVER_URL] = server_url
#
try:
accdata[self.ATTR_SERVER_URL_PATH_CHANGE] = apiaccdata.pop(self.ATTR_SERVER_URL_PATH_CHANGE).lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_CHANGE] = apiaccdata.pop(self.ATTR_SERVER_URL_PATH_CHANGE)
except KeyError as e:
accdata[self.ATTR_SERVER_URL_PATH_CHANGE] = server_path_change.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_CHANGE] = server_path_change
#
try:
accdata[self.ATTR_SERVER_URL_PATH_CLEAN] = apiaccdata.pop(self.ATTR_SERVER_URL_PATH_CLEAN).lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_CLEAN] = apiaccdata.pop(self.ATTR_SERVER_URL_PATH_CLEAN)
except KeyError as e:
accdata[self.ATTR_SERVER_URL_PATH_CLEAN] = server_path_clean.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_CLEAN] = server_path_clean
#
try:
accdata[self.ATTR_SERVER_URL_PATH_DEREGISTER] = apiaccdata.pop(self.ATTR_SERVER_URL_PATH_DEREGISTER).lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_DEREGISTER] = apiaccdata.pop(self.ATTR_SERVER_URL_PATH_DEREGISTER)
except KeyError as e:
accdata[self.ATTR_SERVER_URL_PATH_DEREGISTER] = server_path_deregister.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_DEREGISTER] = server_path_deregister
#
accdata[self.ATTR_SERVER_URL_PATH_REGISTER] = server_path_register
#
try:
accdata[self.ATTR_SERVER_URL_PATH_UPDATE] = apiaccdata.pop(self.ATTR_SERVER_URL_PATH_UPDATE).lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_UPDATE] = apiaccdata.pop(self.ATTR_SERVER_URL_PATH_UPDATE)
except KeyError as e:
accdata[self.ATTR_SERVER_URL_PATH_UPDATE] = server_path_update.lstrip("/")
accdata[self.ATTR_SERVER_URL_PATH_UPDATE] = server_path_update
#
accdata[self.ATTR_ADDED_ON] = datetime.datetime.now(datetime.timezone.utc).isoformat()
accdata[self.ATTR_ADDED_VIA] = "register"
Expand Down Expand Up @@ -674,7 +659,7 @@ def update(cls, accdata:dict, token:str) -> typing.Tuple[bool,str,int]:
### convert request data into JSON format; post() json parameter = requests 2.4.2+
request_data = json.dumps(request_data, **JSON5_DUMP_KWARGS)

request_url = urllib.parse.urljoin(accdata[cls.ATTR_SERVER_URL], accdata[cls.ATTR_SERVER_URL_PATH_UPDATE])
request_url = "".join((accdata[cls.ATTR_SERVER_URL], accdata[cls.ATTR_SERVER_URL_PATH_UPDATE]))
request_response = requests.post(
url=request_url,
headers=request_headers,
Expand Down Expand Up @@ -715,7 +700,7 @@ def deregister(cls, accdata:dict) -> typing.Tuple[bool,str,int]:
### convert request data into JSON format; post() json parameter = requests 2.4.2+
request_data = json.dumps(request_data, **JSON5_DUMP_KWARGS)

request_url = urllib.parse.urljoin(accdata[cls.ATTR_SERVER_URL], accdata[cls.ATTR_SERVER_URL_PATH_DEREGISTER])
request_url = "".join((accdata[cls.ATTR_SERVER_URL], accdata[cls.ATTR_SERVER_URL_PATH_DEREGISTER]))
request_response = requests.post(
url=request_url,
headers=request_headers,
Expand Down Expand Up @@ -757,7 +742,7 @@ def clean(cls, accdata:dict, token:str) -> typing.Tuple[bool,str,int]:
### convert request data into JSON format; post() json parameter = requests 2.4.2+
request_data = json.dumps(request_data, **JSON5_DUMP_KWARGS)

request_url = urllib.parse.urljoin(accdata[cls.ATTR_SERVER_URL], accdata[cls.ATTR_SERVER_URL_PATH_CLEAN])
request_url = "".join((accdata[cls.ATTR_SERVER_URL], accdata[cls.ATTR_SERVER_URL_PATH_CLEAN]))
request_response = requests.post(
url=request_url,
headers=request_headers,
Expand Down Expand Up @@ -813,7 +798,7 @@ def change(self, accdata:dict, allowfrom:typing.Union[list,None]=None) -> typing
### convert request data into JSON format; post() json parameter = requests 2.4.2+
request_data = json.dumps(request_data, **JSON5_DUMP_KWARGS)

request_url = urllib.parse.urljoin(accdata[self.ATTR_SERVER_URL], accdata[self.ATTR_SERVER_URL_PATH_CHANGE])
request_url = "".join((accdata[self.ATTR_SERVER_URL], accdata[self.ATTR_SERVER_URL_PATH_CHANGE]))
request_response = requests.post(
url=request_url,
headers=request_headers,
Expand Down

0 comments on commit 1eff0d4

Please sign in to comment.