Skip to content

Commit

Permalink
pylxd/client: inspect secret before trying to use it as a token
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Deziel <[email protected]>
  • Loading branch information
simondeziel committed Dec 11, 2024
1 parent 1ef1852 commit eec1d55
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pylxd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import base64
import json
import os
import re
Expand Down Expand Up @@ -545,7 +546,22 @@ def authenticate(self, secret, use_token_auth=True):
return
cert = open(self.api.session.cert[0]).read().encode("utf-8")

if self.has_api_extension("explicit_trust_token") and use_token_auth:
# Quirk to handle 5.21 that supports explicit trust tokens as well as
# password auth. We need to ascertain if the provided secret is indeed a
# token before trying to use it as such.
secret_is_a_token = False
if use_token_auth and self.has_api_extension("explicit_trust_token"):
token = None
try:
b64 = base64.b64decode(secret)
token = json.loads(b64.decode("utf-8"))
except (TypeError, ValueError, json.JSONDecodeError, base64.binascii.Error):
pass

if token:
secret_is_a_token = "secret" in token

if secret_is_a_token:
self.certificates.create(password="", cert_data=cert, secret=secret)
else:
self.certificates.create(password=secret, cert_data=cert)
Expand Down

0 comments on commit eec1d55

Please sign in to comment.