davsclaus commented on code in PR #26511:
URL: https://github.com/apache/camel/pull/26511#discussion_r4033469777


##########
components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIWebhookConsumer.java:
##########
@@ -0,0 +1,280 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.openai;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import com.openai.client.OpenAIClient;
+import com.openai.client.okhttp.OpenAIOkHttpClient;
+import com.openai.core.http.Headers;
+import com.openai.errors.InvalidWebhookSignatureException;
+import com.openai.errors.OpenAIInvalidDataException;
+import com.openai.models.webhooks.UnwrapWebhookEvent;
+import com.openai.models.webhooks.WebhookVerificationParams;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.Consumer;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.spi.RestConfiguration;
+import org.apache.camel.spi.RestConsumerFactory;
+import org.apache.camel.support.DefaultConsumer;
+import org.apache.camel.support.service.ServiceHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Receives the webhook events OpenAI sends: it serves one HTTP endpoint 
through the REST consumer factory of the
+ * runtime (platform-http unless another component is configured), verifies 
the signature of each request against the
+ * webhook secret, and runs the route with the parsed event.
+ */
+public class OpenAIWebhookConsumer extends DefaultConsumer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(OpenAIWebhookConsumer.class);
+
+    private static final String[] SIGNATURE_HEADERS = { "webhook-id", 
"webhook-timestamp", "webhook-signature" };
+
+    /**
+     * The SDK client requires a credential even when it is only used to 
verify a signature, which needs the webhook
+     * secret alone. A route that just receives events therefore does not have 
to configure an API key.
+     */
+    private static final String VERIFICATION_ONLY_API_KEY = 
"openai-webhook-verification";
+
+    private final OpenAIEndpoint endpoint;
+
+    private OpenAIClient client;
+    private Consumer httpConsumer;
+
+    public OpenAIWebhookConsumer(OpenAIEndpoint endpoint, Processor processor) 
{
+        super(endpoint, processor);
+        this.endpoint = endpoint;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        OpenAIConfiguration configuration = endpoint.getConfiguration();
+        String secret = configuration.getWebhookSecret();
+        if (ObjectHelper.isEmpty(secret)) {
+            throw new IllegalArgumentException(
+                    "webhookSecret is required by the webhook operation: it is 
the signing secret of the endpoint in"
+                                               + " the OpenAI dashboard, and 
without it an event cannot be told from a forged request");
+        }
+
+        RestConsumerFactory factory = resolveRestConsumerFactory();
+        if (factory == null) {
+            throw new IllegalStateException(
+                    "No RestConsumerFactory found. The webhook operation needs 
camel-platform-http, or another HTTP"
+                                            + " server component named in 
httpServerComponent or in the rest configuration");
+        }
+
+        client = createVerificationClient(secret);
+
+        String path = configuration.getWebhookPath();
+        RestConfiguration restConfiguration = 
endpoint.getCamelContext().getRestConfiguration();
+        httpConsumer = factory.createConsumer(endpoint.getCamelContext(), 
this::onWebhookRequest,
+                "POST", path, null, "application/json", null, 
restConfiguration, Collections.emptyMap());
+        endpoint.configureNestedConsumer(httpConsumer);
+        ServiceHelper.startService(httpConsumer);
+
+        LOG.debug("OpenAI webhook consumer listening on POST {}", path);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        ServiceHelper.stopService(httpConsumer);
+        httpConsumer = null;
+        if (client != null) {
+            client.close();
+            client = null;
+        }
+        super.doStop();
+    }
+
+    /**
+     * Answers the HTTP request of OpenAI. The route never sees an event whose 
signature did not verify, and it runs on
+     * an exchange of its own, so that no header of the request reaches it.
+     */
+    private void onWebhookRequest(Exchange httpExchange) {
+        byte[] payload;
+        try {
+            payload = readPayload(httpExchange);
+        } catch (IOException e) {
+            LOG.debug("Rejected an OpenAI webhook request: {}", 
e.getMessage());
+            respond(httpExchange, 413, e.getMessage());
+            return;
+        }
+
+        UnwrapWebhookEvent event;
+        try {
+            event = 
client.webhooks().unwrap(verificationParams(httpExchange.getMessage(), 
payload));
+        } catch (InvalidWebhookSignatureException | IllegalArgumentException | 
OpenAIInvalidDataException e) {
+            // a missing header, a signature that does not match, an event 
older than the tolerance of the SDK
+            // (5 minutes), or a body that is not an event
+            LOG.debug("Rejected an OpenAI webhook request: {}", 
e.getMessage());
+            respond(httpExchange, 400, e.getMessage());

Review Comment:
   This endpoint is reachable by anyone who can reach the port, so I would not 
echo the SDK's exception text back; the detail is already logged at DEBUG above.
   
   ```suggestion
               respond(httpExchange, 400, "Invalid webhook request");
   ```
   
   (Same for the 413 branch in `readPayload`.)
   
   _Claude Code on behalf of davsclaus_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to