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

feat: wildcard and sans certificates #89

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

DavidePrincipi
Copy link
Member

@DavidePrincipi DavidePrincipi commented Mar 6, 2025

This PR adds support for certificate with SANs, wildcard included.

image

  • Adds SAN support to the export-certificate script. Additional DNS names are used to create Redis keys as well as the certificate subject name. This is limited to the internal (ACME) certificate, used as default for Traefik configuration.
  • The old event certificate-updated is retained for full backward compatibility.
  • Define a new event, certificate-changed that is notified when a custom certificate is uploaded, or obtained/renewed by ACME. The new event includes a full list of certificate names: applications that subscribe it can match their host name(s) with the list of names in the event payload.
  • Extend the get-certificate response to include a list of certificates that match the requested FQDN. The list is sorted by decreasing relevance, so a client can safely assume that certificates[0] is the best option available.

Relevance rules are (decreasing values):

  • custom certificate with wildcard match
  • custom certificate with exact name match
  • internal (ACME) certificate with wildcard match
  • default internal certificate with exact name match
  • internal certificate with exact name match

If no match is found, the self-signed certificate is returned.

The use of Redis keys is deprecated and a log message is printed when they are set. In future releases the export-certificate script could be removed to avoid publishing the certificate private key in Redis.

Refs NethServer/dev#7004

See also https://github.com/NethServer/ns8-core/tree/feat-7004

Write Redis keys for certificate SAN as well.
Notify applications when the TLS certificate(s) for some names has
changed.
Publish certificate-changed event when a custom TLS certificate has been
uploaded.
Change the get-certificate output to return a list of certificates for
the given FQDN. The list is sorted by decreasing relevance, so the first
item has the higher relevance. This is a list of relevance factors, from
most important to less important:

- custom certificate with wildcard match
- custom certificate with exact name match
- internal (ACME) certificate with wildcard match
- default internal certificate with exact name match
- internal certificate with exact name match

The match occurs in both the certificate subject and SAN extension.

If two certificates score the same relevance the order depends on the
underlying storage (custom certs or acme.json).

If no match is found, the self-signed certificate is returned.

Expiration date is not considered.
@gsanchietti gsanchietti requested a review from Copilot March 6, 2025 16:35
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This PR adds support for certificates with Subject Alternative Names (SAN) and wildcard entries. It introduces a new event (certificate-changed), refactors the get-certificate action to handle multiple certificate matching rules and scores, and updates the certificate helper module with cryptography dependency support.

Reviewed Changes

File Description
imageroot/actions/upload-certificate/30certificate_changed Adds a new script to notify certificate-changed events via Redis publish.
imageroot/actions/get-certificate/20readconfig Refactors certificate matching logic, introduces iterators for custom and ACME certificates, and sorts matches based on relevance.
imageroot/pypkg/cert_helpers.py Updates the utility to extract certified names from certificates using the cryptography module.

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

imageroot/pypkg/cert_helpers.py:19

  • [nitpick] Consider updating the type hint from 'bytearray' to 'bytes' since certificate data is typically handled as bytes.
def extract_certified_names(cert_data : bytearray) -> set:

Comment on lines +66 to +69
with open(cert_path, 'rb') as f:
bcert = f.read()
with open(f"custom_certificates/{main}.key", "rb") as f:
bkey = f.read()
Copy link
Preview

Copilot AI Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling in case the corresponding key file does not exist to prevent runtime exceptions during certificate loading.

Suggested change
with open(cert_path, 'rb') as f:
bcert = f.read()
with open(f"custom_certificates/{main}.key", "rb") as f:
bkey = f.read()
try:
with open(cert_path, 'rb') as f:
bcert = f.read()
with open(f"custom_certificates/{main}.key", "rb") as f:
bkey = f.read()
except FileNotFoundError as e:
print(f"Error: {e}", file=sys.stderr)
continue

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Consider a wildcard hostname like *.example.net valid for certificate
actions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant