@@ -5,10 +5,21 @@ import { updateConfiguration } from "../../../../shared/db/configuration/update-
55import { getConfig } from "../../../../shared/utils/cache/get-config" ;
66import { standardResponseSchema } from "../../../schemas/shared-api-schemas" ;
77import { responseBodySchema } from "./get" ;
8+ import { createCustomError } from "../../../middleware/error" ;
9+ import { encrypt } from "../../../../shared/utils/crypto" ;
810
9- export const requestBodySchema = Type . Object ( {
10- domain : Type . String ( ) ,
11- } ) ;
11+ export const requestBodySchema = Type . Partial (
12+ Type . Object ( {
13+ authDomain : Type . String ( ) ,
14+ mtlsCertificate : Type . String ( {
15+ description :
16+ "Engine certificate used for outbound mTLS requests. Must provide the full certificate chain." ,
17+ } ) ,
18+ mtlsPrivateKey : Type . String ( {
19+ description : "Engine private key used for outbound mTLS requests." ,
20+ } ) ,
21+ } ) ,
22+ ) ;
1223
1324export async function updateAuthConfiguration ( fastify : FastifyInstance ) {
1425 fastify . route < {
@@ -29,15 +40,49 @@ export async function updateAuthConfiguration(fastify: FastifyInstance) {
2940 } ,
3041 } ,
3142 handler : async ( req , res ) => {
43+ const { authDomain, mtlsCertificate, mtlsPrivateKey } = req . body ;
44+
45+ if ( mtlsCertificate ) {
46+ if (
47+ ! mtlsCertificate . includes ( "-----BEGIN CERTIFICATE-----\n" ) ||
48+ ! mtlsCertificate . includes ( "\n-----END CERTIFICATE-----" )
49+ ) {
50+ throw createCustomError (
51+ "Invalid mtlsCertificate." ,
52+ StatusCodes . BAD_REQUEST ,
53+ "INVALID_MTLS_CERTIFICATE" ,
54+ ) ;
55+ }
56+ }
57+ if ( mtlsPrivateKey ) {
58+ if (
59+ ! mtlsPrivateKey . startsWith ( "-----BEGIN PRIVATE KEY-----\n" ) ||
60+ ! mtlsPrivateKey . endsWith ( "\n-----END PRIVATE KEY-----" )
61+ ) {
62+ throw createCustomError (
63+ "Invalid mtlsPrivateKey." ,
64+ StatusCodes . BAD_REQUEST ,
65+ "INVALID_MTLS_PRIVATE_KEY" ,
66+ ) ;
67+ }
68+ }
69+
3270 await updateConfiguration ( {
33- authDomain : req . body . domain ,
71+ authDomain,
72+ mtlsCertificateEncrypted : mtlsCertificate
73+ ? encrypt ( mtlsCertificate )
74+ : undefined ,
75+ mtlsPrivateKeyEncrypted : mtlsPrivateKey
76+ ? encrypt ( mtlsPrivateKey )
77+ : undefined ,
3478 } ) ;
3579
3680 const config = await getConfig ( false ) ;
3781
3882 res . status ( StatusCodes . OK ) . send ( {
3983 result : {
40- domain : config . authDomain ,
84+ authDomain : config . authDomain ,
85+ mtlsCertificate : config . mtlsCertificate ,
4186 } ,
4287 } ) ;
4388 } ,
0 commit comments