Skip to content

Commit b636a11

Browse files
committed
feat: add optional CSR field to enrollment schemas
1 parent 866e472 commit b636a11

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

src/rasenmaeher_api/db/enrollments.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class Enrollment(ORMBaseModel, table=True): # type: ignore[call-arg,misc]
167167
)
168168
state: int = Field(nullable=False, index=False, unique=False, default=EnrollmentState.PENDING)
169169
extra: Dict[str, Any] = Field(sa_type=JSONB, nullable=False, sa_column_kwargs={"server_default": "{}"})
170+
csr: Optional[str] = Field(default=None, nullable=True)
170171

171172
@classmethod
172173
async def by_pk_or_callsign(cls, inval: Union[str, uuid.UUID]) -> "Enrollment":
@@ -272,11 +273,16 @@ async def _generate_unused_code(cls) -> str:
272273

273274
@classmethod
274275
async def create_for_callsign(
275-
cls, callsign: str, pool: Optional[EnrollmentPool] = None, extra: Optional[Dict[str, Any]] = None
276+
cls,
277+
callsign: str,
278+
pool: Optional[EnrollmentPool] = None,
279+
extra: Optional[Dict[str, Any]] = None,
280+
csr: Optional[str] = None,
276281
) -> "Enrollment":
277282
"""Create a new one with random code for the callsign"""
278283
if callsign in RMSettings.singleton().valid_product_cns:
279284
raise CallsignReserved("Using product CNs as callsigns is forbidden")
285+
# FIXME: Verify the CSR has the callsign as CN
280286
with EngineWrapper.get_session() as session:
281287
try:
282288
await Enrollment.by_callsign(callsign)
@@ -293,6 +299,7 @@ async def create_for_callsign(
293299
state=EnrollmentState.PENDING,
294300
extra=extra,
295301
pool=poolpk,
302+
csr=csr,
296303
)
297304
session.add(obj)
298305
session.commit()

src/rasenmaeher_api/web/api/enrollment/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Schema for enrollment."""
2-
from typing import List, Dict, Any
2+
from typing import List, Dict, Any, Optional
33

44
from pydantic import BaseModel, Extra, Field
55

@@ -104,6 +104,7 @@ class EnrollmentInitIn(BaseModel): # pylint: disable=too-few-public-methods
104104
"""Enrollment init in response schema"""
105105

106106
callsign: str = Field(description="Callsign to create enrollment for")
107+
csr: Optional[str] = Field(description="CSR for mTLS key in PEM format", default=None)
107108

108109
class Config: # pylint: disable=too-few-public-methods
109110
"""Example values for schema"""

src/rasenmaeher_api/web/api/enrollment/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ async def request_enrollment_init(
187187

188188
# TODO ADD POOL NAME CHECK
189189

190-
new_enrollment = await Enrollment.create_for_callsign(callsign=request_in.callsign, pool=None, extra={})
190+
new_enrollment = await Enrollment.create_for_callsign(
191+
callsign=request_in.callsign, pool=None, extra={}, csr=request_in.csr
192+
)
191193
# Create JWT token for user
192194
claims = {"sub": request_in.callsign}
193195
new_jwt = Issuer.singleton().issue(claims)

src/rasenmaeher_api/web/api/firstuser/schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Schema for enrollment."""
2-
from pydantic import BaseModel, Extra
2+
from typing import Optional
3+
from pydantic import BaseModel, Extra, Field
34

45

56
class FirstuserCheckCodeIn(BaseModel): # pylint: disable=too-few-public-methods
@@ -50,6 +51,7 @@ class FirstuserAddAdminIn(BaseModel): # pylint: disable=too-few-public-methods
5051

5152
# temp_admin_code: str
5253
callsign: str
54+
csr: Optional[str] = Field(default=None, description="CSR for mTLS key in PEM format")
5355

5456
class Config: # pylint: disable=too-few-public-methods
5557
"""Example values for schema"""

src/rasenmaeher_api/web/api/firstuser/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ async def post_admin_add(
7777
await anon_user.assign_role(role="anon_admin")
7878

7979
# Create new admin user enrollment
80-
enrollment = await Enrollment.create_for_callsign(callsign=request_in.callsign, pool=None, extra={})
80+
enrollment = await Enrollment.create_for_callsign(
81+
callsign=request_in.callsign, pool=None, extra={}, csr=request_in.csr
82+
)
8183

8284
# Get the anon_admin 'user' that will be used to approve the new admin user
8385
# and approve the user

0 commit comments

Comments
 (0)