Skip to content

Commit d428f1e

Browse files
committed
feat: pass the enrollment CSR to Person creation and skip keypair generation if CSR is provided
1 parent d196e3c commit d428f1e

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/rasenmaeher_api/db/enrollments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ async def by_pk_or_callsign(cls, inval: Union[str, uuid.UUID]) -> "Enrollment":
181181
async def approve(self, approver: Person) -> Person:
182182
"""Creates the person record, their certs etc"""
183183
with EngineWrapper.get_session() as session:
184-
person = await Person.create_with_cert(self.callsign, extra=self.extra)
184+
person = await Person.create_with_cert(self.callsign, extra=self.extra, csrpem=self.csr)
185185
self.state = EnrollmentState.APPROVED
186186
self.decided_by = approver.pk
187187
self.decided_on = datetime.datetime.now(datetime.UTC)

src/rasenmaeher_api/db/people.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ async def by_pk_or_callsign(cls, inval: Union[str, uuid.UUID], allow_deleted: bo
9090
return await cls.by_callsign(str(inval), allow_deleted)
9191

9292
@classmethod
93-
async def create_with_cert(cls, callsign: str, extra: Optional[Dict[str, Any]] = None) -> "Person":
93+
async def create_with_cert(
94+
cls, callsign: str, extra: Optional[Dict[str, Any]] = None, csrpem: Optional[str] = None
95+
) -> "Person":
9496
"""Create the cert etc and save the person"""
97+
# FIXME: Verify the CSR has the callsign as CN
9598
cnf = RMSettings.singleton()
9699
if callsign in cnf.valid_product_cns:
97100
raise CallsignReserved("Using product CNs as callsigns is forbidden")
@@ -110,8 +113,11 @@ async def create_with_cert(cls, callsign: str, extra: Optional[Dict[str, Any]] =
110113
newperson = Person(pk=puuid, callsign=callsign, certspath=str(certspath), extra=extra)
111114
session.add(newperson)
112115
session.commit()
113-
ckp = await async_create_keypair(newperson.privkeyfile, newperson.pubkeyfile)
114-
csrpem = await async_create_client_csr(ckp, newperson.csrfile, newperson.certsubject)
116+
if csrpem:
117+
newperson.csrfile.write_text(csrpem, encoding="utf-8")
118+
else:
119+
ckp = await async_create_keypair(newperson.privkeyfile, newperson.pubkeyfile)
120+
csrpem = await async_create_client_csr(ckp, newperson.csrfile, newperson.certsubject)
115121
certpem = (await sign_csr(csrpem)).replace("\\n", "\n")
116122
newperson.certfile.write_text(certpem)
117123
except Exception as exc:
@@ -150,7 +156,10 @@ async def create_pfx(self) -> Path:
150156
def write_pfx() -> None:
151157
"""Do the IO"""
152158
nonlocal self
153-
p12bytes = convert_pem_to_pkcs12(self.certfile, self.privkeyfile, self.callsign, None, self.callsign)
159+
if self.privkeyfile.exists():
160+
p12bytes = convert_pem_to_pkcs12(self.certfile, self.privkeyfile, self.callsign, None, self.callsign)
161+
else:
162+
p12bytes = convert_pem_to_pkcs12(self.certfile, None, self.callsign, None, self.callsign)
154163
self.pfxfile.write_bytes(p12bytes)
155164

156165
await asyncio.get_event_loop().run_in_executor(None, write_pfx)

0 commit comments

Comments
 (0)