Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pylxd/models/certificate: re-add password arg for backward compat #603

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pylxd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,15 @@ def assert_has_api_extension(self, name):
if not self.has_api_extension(name):
raise exceptions.LXDAPIExtensionNotAvailable(name)

def authenticate(self, secret):
def authenticate(self, secret, use_token_auth=True):
if self.trusted:
return
cert = open(self.api.session.cert[0]).read().encode("utf-8")
self.certificates.create(secret, cert)

if self.has_api_extension("explicit_trust_token") and use_token_auth:
self.certificates.create(password="", cert_data=cert, secret=secret)
else:
self.certificates.create(password=secret, cert_data=cert)

# Refresh the host info
response = self.api.get()
Expand Down
13 changes: 9 additions & 4 deletions pylxd/models/certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ def all(cls, client):
def create(
cls,
client,
secret,
password,
cert_data,
cert_type="client",
name="",
projects=None,
restricted=False,
secret="",
):
"""Create a new certificate."""
cert = x509.load_pem_x509_certificate(cert_data, default_backend())
Expand All @@ -68,14 +69,18 @@ def create(
data = {
"type": cert_type,
"certificate": base64_cert,
"password": password,
"name": name,
"restricted": restricted,
"projects": projects,
}
if client.has_api_extension("explicit_trust_token"):

# secret/trust_token are safer than password but support for password is kept for
# backward compatibility
if client.has_api_extension("explicit_trust_token") and secret:
data["trust_token"] = secret
else:
data["password"] = secret
del data["password"]

response = client.api.certificates.post(json=data)
location = response.headers["Location"]
fingerprint = location.split("/")[-1]
Expand Down