Skip to content

Commit

Permalink
Save issued certificates to database
Browse files Browse the repository at this point in the history
  • Loading branch information
jschlyter committed Dec 19, 2024
1 parent 9d59a4e commit 519210a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions nodeman/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,24 @@ class TapirNodeEnrollment(Document):

name = StringField(unique=True)
key = DictField()


class TapirCertificate(Document):
meta = {
"collection": "certificates",
"indexes": [
{"fields": ["name"]},
{"fields": ["issuer", "serial"], "unique": True},
],
}

name = StringField()

issuer = StringField()
subject = StringField()
serial = StringField()

not_valid_before = DateTimeField()
not_valid_after = DateTimeField()

certificate = StringField()
1 change: 1 addition & 0 deletions nodeman/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class NodeCertificate(BaseModel):
x509_certificate: str = Field(title="X.509 Client Certificate Bundle")
x509_ca_certificate: str = Field(title="X.509 CA Certificate Bundle")
x509_certificate_serial_number: int | None = Field(default=None, exclude=True)
x509_certificate_not_valid_after: datetime

@field_validator("x509_certificate", "x509_ca_certificate")
@classmethod
Expand Down
1 change: 1 addition & 0 deletions nodeman/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ async def enroll_node(
x509_certificate=node_certificate.x509_certificate,
x509_ca_certificate=node_certificate.x509_ca_certificate,
x509_certificate_serial_number=node_certificate.x509_certificate_serial_number,
x509_certificate_not_valid_after=node_certificate.x509_certificate_not_valid_after,
)


Expand Down
12 changes: 12 additions & 0 deletions nodeman/x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from cryptography.x509.oid import ExtensionOID, NameOID
from fastapi import HTTPException, Request, status

from .db_models import TapirCertificate
from .models import NodeCertificate

RSA_EXPONENT = 65537
Expand Down Expand Up @@ -145,6 +146,16 @@ def process_csr_request(request: Request, csr: x509.CertificateSigningRequest, n
x509_certificate_serial_number = x509_certificate.serial_number
x509_not_valid_after_utc = x509_certificate.not_valid_after_utc.isoformat()

TapirCertificate(
name=name,
issuer=x509_certificate.issuer.rfc4514_string(),
subject=x509_certificate.subject.rfc4514_string(),
certificate=x509_certificate.public_bytes(serialization.Encoding.PEM).decode(),
serial=str(x509_certificate.serial_number),
not_valid_before=x509_certificate.not_valid_before_utc,
not_valid_after=x509_certificate.not_valid_after_utc,
).save()

logger.info(
"Issued certificate for name=%s serial=%d not_valid_after=%s",
name,
Expand All @@ -161,6 +172,7 @@ def process_csr_request(request: Request, csr: x509.CertificateSigningRequest, n
x509_certificate=x509_certificate_pem,
x509_ca_certificate=x509_ca_certificate_pem,
x509_certificate_serial_number=x509_certificate_serial_number,
x509_certificate_not_valid_after=x509_certificate.not_valid_after_utc,
)


Expand Down

0 comments on commit 519210a

Please sign in to comment.