Skip to content

Commit

Permalink
Repurpose introspect to return the correct introspection payload
Browse files Browse the repository at this point in the history
  • Loading branch information
KiKoS0 committed Sep 7, 2024
1 parent 239b059 commit f77b498
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
59 changes: 57 additions & 2 deletions inngest/src/main/kotlin/com/inngest/Comm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import com.beust.klaxon.Json
import com.beust.klaxon.Klaxon
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.inngest.signingkey.checkHeadersAndValidateSignature
import com.inngest.signingkey.getAuthorizationHeader
import com.inngest.signingkey.hashedSigningKey
import java.io.IOException
import java.security.MessageDigest

data class ExecutionRequestPayload(
val ctx: ExecutionContext,
Expand Down Expand Up @@ -166,8 +169,50 @@ class CommHandler(
// TODO
// fun sync(): Result<InngestSyncResult> = Result.success(InngestSyncResult.None)

fun introspect(origin: String): String {
val requestPayload = getRegistrationRequestPayload(origin)
fun introspect(
signature: String?,
requestBody: String,
serverKind: String?,
): String {
val insecureIntrospection =
InsecureIntrospection(
functionCount = functions.size,
hasEventKey = Environment.isInngestEventKeySet(client.eventKey),
hasSigningKey = config.hasSigningKey(),
mode = if (client.env == InngestEnv.Dev) "dev" else "cloud",
)

val requestPayload =
when (client.env) {
InngestEnv.Dev -> insecureIntrospection

else ->
runCatching {
checkHeadersAndValidateSignature(signature, requestBody, serverKind, config)

SecureIntrospection(
functionCount = functions.size,
hasEventKey = Environment.isInngestEventKeySet(client.eventKey),
hasSigningKey = config.hasSigningKey(),
authenticationSucceeded = true,
mode = "cloud",
env = client.env.value,
appId = config.appId(),
apiOrigin = "${config.baseUrl()}/",
framework = framework.value,
sdkVersion = Version.getVersion(),
sdkLanguage = "java",
servePath = config.servePath(),
serveOrigin = config.serveOrigin(),
signingKeyHash = hashedSigningKey(config.signingKey()),
eventApiOrigin = "${Environment.inngestEventApiBaseUrl(client.env)}/",
eventKeyHash = if (config.hasSigningKey()) hashedEventKey(client.eventKey) else null,
)
}.getOrElse {
insecureIntrospection.apply { authenticationSucceeded = false }
}
}

return serializePayload(requestPayload)
}

Expand All @@ -187,4 +232,14 @@ class CommHandler(
val servePath = config.servePath() ?: "/api/inngest"
return "$serveOrigin$servePath"
}

private fun hashedEventKey(eventKey: String): String? =
eventKey
.takeIf { Environment.isInngestEventKeySet(it) }
?.let {
MessageDigest
.getInstance("SHA-256")
.digest(it.toByteArray())
.joinToString("") { byte -> "%02x".format(byte) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ val SIGNING_KEY_REGEX = Regex("""(?<prefix>^signkey-\w+-)(?<key>.*)""")
* @throws InvalidSigningKeyException If signingKey is not in the form "signkey-<env>-<key>"
*/
@OptIn(ExperimentalStdlibApi::class)
private fun hashedSigningKey(signingKey: String): String {
internal fun hashedSigningKey(signingKey: String): String {
val matchResult = SIGNING_KEY_REGEX.matchEntire(signingKey) ?: throw InvalidSigningKeyException()

// We aggressively assert non-null here because if `matchEntire` had failed (and thus these capture groups didn't
Expand Down

0 comments on commit f77b498

Please sign in to comment.