From c2b6b3f5cb3f86a52a1c2901253c463d71015946 Mon Sep 17 00:00:00 2001 From: Ross Schlaikjer Date: Thu, 10 Sep 2026 17:06:12 +0900 Subject: [PATCH 1/3] Add certificate get/put APIs --- CHANGELOG.md | 7 ++++ python/mujinwebstackclient/version.py | 2 +- python/mujinwebstackclient/webstackclient.py | 42 +++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f81935..7f241af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 1.2.0 (2026-09-10) + +- Added `GetCertificate` and `UploadCertificate` for the webstack certificate API. `GetCertificate` + returns the stored PEM chain, or `None` when nothing is stored under that id; the private key is + write only and never comes back. `UploadCertificate` creates or replaces a certificate from a PEM + bundle of the chain and, optionally, its matching private key. + ## 1.1.2 (2026-09-01) - `LazyGraphQuery` no longer requests `meta.totalCount` on every graph list query. The count is diff --git a/python/mujinwebstackclient/version.py b/python/mujinwebstackclient/version.py index 8e31c60..8ae153d 100644 --- a/python/mujinwebstackclient/version.py +++ b/python/mujinwebstackclient/version.py @@ -1,3 +1,3 @@ -__version__ = '1.1.2' +__version__ = '1.2.0' # Do not forget to update CHANGELOG.md diff --git a/python/mujinwebstackclient/webstackclient.py b/python/mujinwebstackclient/webstackclient.py index 78ce824..3be70c0 100644 --- a/python/mujinwebstackclient/webstackclient.py +++ b/python/mujinwebstackclient/webstackclient.py @@ -11,7 +11,7 @@ from email.utils import parsedate import six -from typing import List, Tuple, Any, Dict, Union # noqa: F401 +from typing import List, Optional, Tuple, Any, Dict, Union # noqa: F401 # Mujin imports from . import WebstackClientError @@ -27,6 +27,9 @@ log = logging.getLogger(__name__) +# Content type the certificate API speaks in both directions +CERTIFICATE_CONTENT_TYPE = 'application/x-pem-file' + def GetFilenameFromURI(uri, mujinpath): """Returns the filesystem path that the URI points to. @@ -874,6 +877,43 @@ def DownloadBlob(self, blobId, timeout=5): raise WebstackClientError(response.content.decode('utf-8'), response=response.content) return response + # + # Certificate related + # + + def GetCertificate(self, certificateId, timeout=5): + # type: (str, float) -> Optional[bytes] + """Downloads a stored TLS certificate as PEM. + + The private key is write only, so what comes back is the leaf certificate followed by any + intermediates, never the key it may have been uploaded with. + + :param certificateId: id the certificate is stored under + :param timeout: number of seconds to wait for the response + :return: the PEM encoded certificate chain, or None when nothing is stored under that id + """ + response = self._webclient.Request('GET', '/api/v2/certificate/%s' % certificateId, headers={'Accept': CERTIFICATE_CONTENT_TYPE}, timeout=timeout) + if response.status_code == 404: + return None + if response.status_code != 200: + raise WebstackClientError(_('Failed to download certificate "%s", status code is %d: %s') % (certificateId, response.status_code, response.content.decode('utf-8', 'replace')), response=response) + return response.content + + def UploadCertificate(self, certificateId, certificatePem, timeout=5): + # type: (str, bytes, float) -> Any + """Creates or replaces a TLS certificate from a PEM bundle. + + The bundle holds the leaf certificate followed by any intermediates, and optionally the + matching private key, which is stored but never served back. A key that does not match the + leaf is rejected. Any name and description already set on the certificate are preserved. + + :param certificateId: id to store the certificate under + :param certificatePem: PEM bundle of the certificate chain, optionally with its private key + :param timeout: number of seconds to wait for the response + :return: the stored certificate and its derived metadata, without any key material + """ + return self._webclient.APICall('PUT', 'certificate/%s' % certificateId, data=certificatePem, headers={'Content-Type': CERTIFICATE_CONTENT_TYPE}, expectedStatusCode=200, timeout=timeout, apiVersion='v2') + # # Log related # From 630f373eff6ae6a1df9c4823748a0c3fa47a718d Mon Sep 17 00:00:00 2001 From: Ross Schlaikjer Date: Fri, 11 Sep 2026 16:29:37 +0900 Subject: [PATCH 2/3] Raise on a missing certificate and route the download through APICall Addresses review feedback: GetCertificate now behaves like the other resource APIs, raising APIServerError when nothing is stored under the requested id instead of returning None. APICall gains parseJSONResponse so endpoints whose successful response is not JSON, such as the PEM certificate download, can still go through it and pick up its error handling. Error bodies are decoded as JSON either way, since webstack reports every error that way regardless of the content type it serves on success. Co-Authored-By: Claude Opus 5 --- CHANGELOG.md | 10 +++++++--- .../controllerwebclientraw.py | 10 ++++++++-- python/mujinwebstackclient/webstackclient.py | 19 +++++++------------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f241af..42c4967 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,13 @@ ## 1.2.0 (2026-09-10) - Added `GetCertificate` and `UploadCertificate` for the webstack certificate API. `GetCertificate` - returns the stored PEM chain, or `None` when nothing is stored under that id; the private key is - write only and never comes back. `UploadCertificate` creates or replaces a certificate from a PEM - bundle of the chain and, optionally, its matching private key. + returns the stored PEM chain and raises `APIServerError` when nothing is stored under that id; the + private key is write only and never comes back. `UploadCertificate` creates or replaces a + certificate from a PEM bundle of the chain and, optionally, its matching private key. +- `APICall` accepts `parseJSONResponse`, which returns the response body as `bytes` instead of + decoding it as JSON. Endpoints that serve another content type on success, such as the certificate + download, can now go through `APICall` and get its error handling; error responses are still + decoded as JSON either way. ## 1.1.2 (2026-09-01) diff --git a/python/mujinwebstackclient/controllerwebclientraw.py b/python/mujinwebstackclient/controllerwebclientraw.py index f3f64ab..59ff454 100644 --- a/python/mujinwebstackclient/controllerwebclientraw.py +++ b/python/mujinwebstackclient/controllerwebclientraw.py @@ -419,6 +419,7 @@ def APICall( files: Optional[Dict[str, Any]] = None, timeout: float = 5, apiVersion: str = 'v1', + parseJSONResponse: bool = True, ) -> Any: path = '/api/%s/%s' % (apiVersion, path.lstrip('/')) if apiVersion == 'v1' and not path.endswith('/'): @@ -470,10 +471,11 @@ def APICall( response = self.Request(method, path, params=params, data=data, files=files, headers=headers, timeout=timeout) - # Try to parse response + # Errors always come back as JSON, even from endpoints that serve another content type on + # success, so decode the body whenever it could be carrying one raw = response.content.decode('utf-8', 'replace').strip() content: Optional[Dict[str, Any]] = None - if len(raw) > 0: + if len(raw) > 0 and (parseJSONResponse or response.status_code >= 400): content = self.DecodeJSON(raw) # First check error @@ -502,6 +504,10 @@ def APICall( log.error('response status code is %d, expecting %d for %s %s: %s', response.status_code, expectedStatusCode, method, path, raw) raise APIServerError(_('Unexpected server response %d: %s') % (response.status_code, raw)) + # Hand back the body untouched for endpoints whose successful response is not JSON + if not parseJSONResponse: + return response.content + return content def CallGraphAPI( diff --git a/python/mujinwebstackclient/webstackclient.py b/python/mujinwebstackclient/webstackclient.py index 3be70c0..12c846e 100644 --- a/python/mujinwebstackclient/webstackclient.py +++ b/python/mujinwebstackclient/webstackclient.py @@ -11,7 +11,7 @@ from email.utils import parsedate import six -from typing import List, Optional, Tuple, Any, Dict, Union # noqa: F401 +from typing import List, Tuple, Any, Dict, Union # noqa: F401 # Mujin imports from . import WebstackClientError @@ -882,7 +882,7 @@ def DownloadBlob(self, blobId, timeout=5): # def GetCertificate(self, certificateId, timeout=5): - # type: (str, float) -> Optional[bytes] + # type: (str, float) -> bytes """Downloads a stored TLS certificate as PEM. The private key is write only, so what comes back is the leaf certificate followed by any @@ -890,16 +890,11 @@ def GetCertificate(self, certificateId, timeout=5): :param certificateId: id the certificate is stored under :param timeout: number of seconds to wait for the response - :return: the PEM encoded certificate chain, or None when nothing is stored under that id + :return: the PEM encoded certificate chain """ - response = self._webclient.Request('GET', '/api/v2/certificate/%s' % certificateId, headers={'Accept': CERTIFICATE_CONTENT_TYPE}, timeout=timeout) - if response.status_code == 404: - return None - if response.status_code != 200: - raise WebstackClientError(_('Failed to download certificate "%s", status code is %d: %s') % (certificateId, response.status_code, response.content.decode('utf-8', 'replace')), response=response) - return response.content + return self._webclient.APICall('GET', 'certificate/%s' % certificateId, headers={'Accept': CERTIFICATE_CONTENT_TYPE}, timeout=timeout, apiVersion='v2', parseJSONResponse=False) - def UploadCertificate(self, certificateId, certificatePem, timeout=5): + def UploadCertificate(self, certificateId, pemData, timeout=5): # type: (str, bytes, float) -> Any """Creates or replaces a TLS certificate from a PEM bundle. @@ -908,11 +903,11 @@ def UploadCertificate(self, certificateId, certificatePem, timeout=5): leaf is rejected. Any name and description already set on the certificate are preserved. :param certificateId: id to store the certificate under - :param certificatePem: PEM bundle of the certificate chain, optionally with its private key + :param pemData: PEM bundle of the certificate chain, optionally with its private key :param timeout: number of seconds to wait for the response :return: the stored certificate and its derived metadata, without any key material """ - return self._webclient.APICall('PUT', 'certificate/%s' % certificateId, data=certificatePem, headers={'Content-Type': CERTIFICATE_CONTENT_TYPE}, expectedStatusCode=200, timeout=timeout, apiVersion='v2') + return self._webclient.APICall('PUT', 'certificate/%s' % certificateId, data=pemData, headers={'Content-Type': CERTIFICATE_CONTENT_TYPE}, expectedStatusCode=200, timeout=timeout, apiVersion='v2') # # Log related From a72b2048ae27cfee683221b532dfcbcce11bb34d Mon Sep 17 00:00:00 2001 From: Ross Schlaikjer Date: Fri, 11 Sep 2026 17:10:10 +0900 Subject: [PATCH 3/3] Change casing for Json --- CHANGELOG.md | 2 +- python/mujinwebstackclient/controllerwebclientraw.py | 6 +++--- python/mujinwebstackclient/webstackclient.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42c4967..a32f28d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ returns the stored PEM chain and raises `APIServerError` when nothing is stored under that id; the private key is write only and never comes back. `UploadCertificate` creates or replaces a certificate from a PEM bundle of the chain and, optionally, its matching private key. -- `APICall` accepts `parseJSONResponse`, which returns the response body as `bytes` instead of +- `APICall` accepts `parseJsonResponse`, which returns the response body as `bytes` instead of decoding it as JSON. Endpoints that serve another content type on success, such as the certificate download, can now go through `APICall` and get its error handling; error responses are still decoded as JSON either way. diff --git a/python/mujinwebstackclient/controllerwebclientraw.py b/python/mujinwebstackclient/controllerwebclientraw.py index 59ff454..6b97669 100644 --- a/python/mujinwebstackclient/controllerwebclientraw.py +++ b/python/mujinwebstackclient/controllerwebclientraw.py @@ -419,7 +419,7 @@ def APICall( files: Optional[Dict[str, Any]] = None, timeout: float = 5, apiVersion: str = 'v1', - parseJSONResponse: bool = True, + parseJsonResponse: bool = True, ) -> Any: path = '/api/%s/%s' % (apiVersion, path.lstrip('/')) if apiVersion == 'v1' and not path.endswith('/'): @@ -475,7 +475,7 @@ def APICall( # success, so decode the body whenever it could be carrying one raw = response.content.decode('utf-8', 'replace').strip() content: Optional[Dict[str, Any]] = None - if len(raw) > 0 and (parseJSONResponse or response.status_code >= 400): + if len(raw) > 0 and (parseJsonResponse or response.status_code >= 400): content = self.DecodeJSON(raw) # First check error @@ -505,7 +505,7 @@ def APICall( raise APIServerError(_('Unexpected server response %d: %s') % (response.status_code, raw)) # Hand back the body untouched for endpoints whose successful response is not JSON - if not parseJSONResponse: + if not parseJsonResponse: return response.content return content diff --git a/python/mujinwebstackclient/webstackclient.py b/python/mujinwebstackclient/webstackclient.py index 12c846e..687341b 100644 --- a/python/mujinwebstackclient/webstackclient.py +++ b/python/mujinwebstackclient/webstackclient.py @@ -892,7 +892,7 @@ def GetCertificate(self, certificateId, timeout=5): :param timeout: number of seconds to wait for the response :return: the PEM encoded certificate chain """ - return self._webclient.APICall('GET', 'certificate/%s' % certificateId, headers={'Accept': CERTIFICATE_CONTENT_TYPE}, timeout=timeout, apiVersion='v2', parseJSONResponse=False) + return self._webclient.APICall('GET', 'certificate/%s' % certificateId, headers={'Accept': CERTIFICATE_CONTENT_TYPE}, timeout=timeout, apiVersion='v2', parseJsonResponse=False) def UploadCertificate(self, certificateId, pemData, timeout=5): # type: (str, bytes, float) -> Any