|
| 1 | +--- |
| 2 | +pcx_content_type: reference |
| 3 | +title: Logpush |
| 4 | +sidebar: |
| 5 | + badge: |
| 6 | + text: Beta |
| 7 | +--- |
| 8 | + |
| 9 | +import { Render } from "~/components"; |
| 10 | + |
| 11 | +AI Gateway allows you to securely push logs to an external storage location, where you can decrypt and process them. This guide explains how to set up Logpush for the AI Gateway, generate an RSA key pair for encryption, and decrypt the logs once they are received. |
| 12 | +Logpush has a limit of 4 jobs. |
| 13 | + |
| 14 | +<Render file="limits-increase" product="ai-gateway" /> |
| 15 | + |
| 16 | +## Setting up Logpush |
| 17 | + |
| 18 | +To configure Logpush for AI Gateway, follow these steps: |
| 19 | + |
| 20 | +## 1. Generate an RSA key pair locally |
| 21 | + |
| 22 | +You need to generate a key pair to encrypt and decrypt the logs. This script will output your RSA privateKey and publicKey. Keep the private key secure, as it will be used to decrypt the logs. Below is a sample script to generate the keys using Node.js. |
| 23 | + |
| 24 | +```js title="JavaScript" |
| 25 | +const crypto = require("crypto"); |
| 26 | + |
| 27 | +const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", { |
| 28 | + modulusLength: 4096, |
| 29 | + publicKeyEncoding: { |
| 30 | + type: "spki", |
| 31 | + format: "pem", |
| 32 | + }, |
| 33 | + privateKeyEncoding: { |
| 34 | + type: "pkcs8", |
| 35 | + format: "pem", |
| 36 | + }, |
| 37 | +}); |
| 38 | + |
| 39 | +console.log(publicKey); |
| 40 | +console.log(privateKey); |
| 41 | +``` |
| 42 | + |
| 43 | +## 2. Upload public key to gateway settings |
| 44 | + |
| 45 | +Once you have generated the key pair, upload the public key to your AI Gateway settings. This key will be used to encrypt your logs. |
| 46 | + |
| 47 | +## 3. Receive encrypted logs |
| 48 | + |
| 49 | +After configuring Logpush, logs will be sent encrypted using the public key you uploaded. To access the data, you will need to decrypt it using your private key. |
| 50 | + |
| 51 | +## 4. Decrypting logs |
| 52 | + |
| 53 | +To decrypt the encrypted log bodies and metadata from AI Gateway, you can use the following Node.js script: |
| 54 | + |
| 55 | +```js title="JavaScript" |
| 56 | +const privateKey = `-----BEGIN RSA PRIVATE KEY----- |
| 57 | +.... |
| 58 | +-----END RSA PRIVATE KEY-----`; |
| 59 | + |
| 60 | +const crypto = require("crypto"); |
| 61 | +const key = crypto.createPrivateKey(privateKey); |
| 62 | + |
| 63 | +const fs = require("fs"); |
| 64 | +const zlib = require("zlib"); |
| 65 | +const readline = require("readline"); |
| 66 | + |
| 67 | +function decryptBase64(key, string) { |
| 68 | + const decryptedData = crypto.privateDecrypt( |
| 69 | + { |
| 70 | + key: key, |
| 71 | + oaepHash: "SHA256", |
| 72 | + }, |
| 73 | + Buffer.from(string, "base64"), |
| 74 | + ); |
| 75 | + |
| 76 | + return decryptedData.toString(); |
| 77 | +} |
| 78 | + |
| 79 | +let lineReader = readline.createInterface({ |
| 80 | + input: fs.createReadStream("my_log.log.gz").pipe(zlib.createGunzip()), |
| 81 | +}); |
| 82 | + |
| 83 | +lineReader.on("line", (line) => { |
| 84 | + line = JSON.parse(line); |
| 85 | + |
| 86 | + const { Metadata, RequestBody, ResponseBody, ...remaining } = line; |
| 87 | + |
| 88 | + console.log({ |
| 89 | + ...remaining, |
| 90 | + Metadata: decryptBase64(key, Metadata.data), |
| 91 | + RequestBody: decryptBase64(key, RequestBody.data), |
| 92 | + ResponseBody: decryptBase64(key, ResponseBody.data), |
| 93 | + }); |
| 94 | + console.log("--"); |
| 95 | +}); |
| 96 | +``` |
| 97 | + |
| 98 | +## Script Explanation |
| 99 | + |
| 100 | +The script reads the encrypted log file `(my_log.log.gz)`, decrypts the metadata, request body, and response body, and prints the decrypted data. |
| 101 | +Ensure you replace the `privateKey` variable with your actual private RSA key that you generated in step 1. |
0 commit comments