|
| 1 | +import 'jsr:@supabase/functions-js/edge-runtime.d.ts' |
| 2 | +import { X509Certificate } from 'node:crypto' |
| 3 | +import { NoSuchKey, S3 } from 'npm:@aws-sdk/client-s3' |
| 4 | +import { createClient } from 'jsr:@supabase/supabase-js@2' |
| 5 | +import * as ACME from 'https://deno.land/x/[email protected]/acme.ts' |
| 6 | +import { env } from './env.ts' |
| 7 | + |
| 8 | +const supabaseClient = createClient(env.SUPABASE_URL, env.SUPABASE_SERVICE_ROLE_KEY) |
| 9 | + |
| 10 | +const s3Client = new S3({ |
| 11 | + forcePathStyle: true, |
| 12 | +}) |
| 13 | + |
| 14 | +Deno.serve(async (req) => { |
| 15 | + // Check if the request is authorized |
| 16 | + if (!(await isAuthorized(req))) { |
| 17 | + return Response.json( |
| 18 | + { |
| 19 | + status: 'error', |
| 20 | + message: 'Unauthorized', |
| 21 | + }, |
| 22 | + { status: 401 } |
| 23 | + ) |
| 24 | + } |
| 25 | + |
| 26 | + const { domainName } = await req.json() |
| 27 | + |
| 28 | + if (!domainName) { |
| 29 | + return Response.json( |
| 30 | + { |
| 31 | + status: 'error', |
| 32 | + message: 'Domain name is required', |
| 33 | + }, |
| 34 | + { status: 400 } |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + // Check if we need to renew the certificate |
| 39 | + const certificate = await getObject('tls/cert.pem') |
| 40 | + if (certificate) { |
| 41 | + const { validTo } = new X509Certificate(certificate) |
| 42 | + // if the validity is more than 30 days, no need to renew |
| 43 | + const day = 24 * 60 * 60 * 1000 |
| 44 | + if (new Date(validTo) > new Date(Date.now() + 30 * day)) { |
| 45 | + return new Response(null, { status: 304 }) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Load account keys if they exist |
| 50 | + const [publicKey, privateKey] = await Promise.all([ |
| 51 | + getObject('tls/account/publicKey.pem'), |
| 52 | + getObject('tls/account/privateKey.pem'), |
| 53 | + ]) |
| 54 | + let accountKeys: { privateKeyPEM: string; publicKeyPEM: string } | undefined |
| 55 | + if (publicKey && privateKey) { |
| 56 | + accountKeys = { |
| 57 | + privateKeyPEM: privateKey, |
| 58 | + publicKeyPEM: publicKey, |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + const { domainCertificates, pemAccountKeys } = await ACME.getCertificatesWithCloudflare( |
| 63 | + env.CLOUDFLARE_API_TOKEN, |
| 64 | + [ |
| 65 | + { |
| 66 | + domainName, |
| 67 | + subdomains: ['*'], |
| 68 | + }, |
| 69 | + ], |
| 70 | + { |
| 71 | + acmeDirectoryUrl: env.ACME_DIRECTORY_URL, |
| 72 | + yourEmail: env.ACME_EMAIL, |
| 73 | + pemAccountKeys: accountKeys, |
| 74 | + } |
| 75 | + ) |
| 76 | + |
| 77 | + const persistOperations = [ |
| 78 | + s3Client.putObject({ |
| 79 | + Bucket: env.AWS_S3_BUCKET, |
| 80 | + Key: `tls/${domainName}/key.pem`, |
| 81 | + Body: domainCertificates[0].pemPrivateKey, |
| 82 | + }), |
| 83 | + s3Client.putObject({ |
| 84 | + Bucket: env.AWS_S3_BUCKET, |
| 85 | + Key: `tls/${domainName}/cert.pem`, |
| 86 | + Body: domainCertificates[0].pemCertificate, |
| 87 | + }), |
| 88 | + ] |
| 89 | + |
| 90 | + if (!accountKeys) { |
| 91 | + persistOperations.push( |
| 92 | + s3Client.putObject({ |
| 93 | + Bucket: env.AWS_S3_BUCKET, |
| 94 | + Key: 'tls/account/publicKey.pem', |
| 95 | + Body: pemAccountKeys.publicKeyPEM, |
| 96 | + }), |
| 97 | + s3Client.putObject({ |
| 98 | + Bucket: env.AWS_S3_BUCKET, |
| 99 | + Key: 'tls/account/privateKey.pem', |
| 100 | + Body: pemAccountKeys.privateKeyPEM, |
| 101 | + }) |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + await Promise.all(persistOperations) |
| 106 | + |
| 107 | + if (certificate) { |
| 108 | + return Response.json({ |
| 109 | + status: 'renewed', |
| 110 | + message: `Certificate renewed successfully for domain ${domainName}`, |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + return Response.json( |
| 115 | + { |
| 116 | + status: 'created', |
| 117 | + message: `New certificate created successfully for domain ${domainName}`, |
| 118 | + }, |
| 119 | + { status: 201 } |
| 120 | + ) |
| 121 | +}) |
| 122 | + |
| 123 | +async function isAuthorized(req: Request) { |
| 124 | + const authHeader = req.headers.get('Authorization') |
| 125 | + |
| 126 | + if (!authHeader) { |
| 127 | + return false |
| 128 | + } |
| 129 | + |
| 130 | + const bearerToken = authHeader.split(' ')[1] |
| 131 | + |
| 132 | + const { data: sharedSecret } = await supabaseClient.rpc('supabase_functions_certificate_secret') |
| 133 | + |
| 134 | + if (sharedSecret !== bearerToken) { |
| 135 | + return false |
| 136 | + } |
| 137 | + |
| 138 | + return true |
| 139 | +} |
| 140 | + |
| 141 | +async function getObject(key: string) { |
| 142 | + const response = await s3Client |
| 143 | + .getObject({ |
| 144 | + Bucket: env.AWS_S3_BUCKET, |
| 145 | + Key: key, |
| 146 | + }) |
| 147 | + .catch((e) => { |
| 148 | + if (e instanceof NoSuchKey) { |
| 149 | + return undefined |
| 150 | + } |
| 151 | + throw e |
| 152 | + }) |
| 153 | + |
| 154 | + return await response?.Body?.transformToString() |
| 155 | +} |
0 commit comments