Hi Gabriel. I'm just getting started with <Permit...
# general
j
Hi Gabriel. I'm just getting started with Permit.IO. Our tech stack is primarily NodeJS running in Google Cloud. I'm trying to setup a NodeJS proxy in front of PDP, both running in CloudRun containers. I'll keep you posted if I have any questions. Thanks for the greeting.
z
Hi @Jon Asbury we have new CloudRun docs that were not pushed yet (from yesterday), please see here: https://github.com/permitio/docs/pull/601 Note that the improved doc was not reviewed yet, but it should help you. This is preview: https://deploy-preview-601--permitio-docs.netlify.app/
j
Wonderful. Thank you. I've been having trouble getting the PDP to work. I'll check it out. If I have questions is this the best channel to use to post questions?
🙌 1
p
Hey, thanks for reaching out ! It seems our team is a little busy right now, a human will be with you within the next hour or so. In the meantime, can I help you ?
j
I spun up this quick and dirty CloudRun NodeJS PDP Proxy. Sharing in case it helps anyone. Feel free to critique. It's our sort term approach until we have time to pull together something more robust. // index.ts
Copy code
import express from 'express';
import cors from 'cors';
import { createProxyMiddleware, Options } from 'http-proxy-middleware';
const PROXY_PORT = process.env.PROXY_PORT || 8080;  // 8080 hooks into CloudRun so use it

const app = express();

// Enable CORS - Restrict to allowed origins
const allowedOrigins = [
  '<http://localhost:3000>',
  ...
];

app.use(express.json());

// Ignore favicon.ico requests
app.get('/favicon.ico', (req, res) => {
  res.status(204).end();
});

// CORS configuration
app.use(cors({
  origin: (origin, callback) => {
    console.log('origin', origin);
    // Allow requests with no origin (like mobile apps or curl requests)
    if (!origin) return callback(null, true);

    // Check if origin is in allowed list
    if (allowedOrigins.includes(origin)) {
      console.log('origin is allowed', origin);
      callback(null, true);
    } else {
      console.log('origin is not allowed', origin);
      callback(new Error('Not allowed by CORS'));
    }
  },
  credentials: true,
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
  allowedHeaders: ['Content-Type', 'Authorization', 'Origin', 'X-Requested-With', 'Accept']
}));

// Get environment variables
const PDP_URL = process.env.PDP_URL;
const PDP_API_KEY = process.env.PDP_API_KEY;

// Validate environment variables
if (!PDP_URL) {
  throw new Error("PDP_URL environment variable is not set");
} else {
  // console.log(`PDP_URL set to ${PDP_URL}.`);
}

if (!PDP_API_KEY) {
  throw new Error("PDP_API_KEY environment variable is not set");
} else {
  // console.log(`PDP_API_KEY set to ${PDP_API_KEY.substring(0, 10)}.`);
}

// Health check endpoint - must be defined BEFORE proxy middleware
app.all('/test', (req, res) => {
  res.json({
    status: 'ok',
    message: 'Proxy is up and running',
    timestamp: new Date().toISOString(),
    proxyTarget: PDP_URL ? `${PDP_URL.substring(0, 20)}...` : 'not configured'
  });
});

// Configure proxy middleware
const proxyOptions: Options = {
  target: PDP_URL,
  changeOrigin: true,
  onProxyReq: (proxyReq, req, res) => {
    // Inject Bearer token into the Authorization header
    proxyReq.setHeader('Authorization', `Bearer ${PDP_API_KEY}`);

    // Log the proxied request
    console.log(`Proxying ${req.method} ${req.url} to ${PDP_URL}${req.url}`);
  },
  onProxyRes: (proxyRes, req, res) => {
    // Return response in unmodified format
    // The proxy middleware handles this automatically, but we log it
    console.log(`Received ${proxyRes.statusCode} from ${PDP_URL}${req.url}`);
  },
  onError: (err, req, res) => {
    console.error('Proxy error:', err);
    if (!res.headersSent) {
      res.status(500).json({ error: 'Proxy error', message: err.message });
    }
  },
};

// Apply proxy middleware to all routes (except /test which is handled above)
app.use('*', createProxyMiddleware(proxyOptions));
app.use(express.json());

// Start server - Cloud Run provides PORT environment variable
const server = app.listen(PROXY_PORT as number, '0.0.0.0', () => {
  console.log(`Proxy server listening on port ${PROXY_PORT}`);
  console.log(`Forwarding requests to ${PDP_URL}`);
});
n
Hi @Jon Asbury - thanks for reaching out. You can use #C02RTML2WNN for these type of questions:)
j
Great. The above is less of a question and more of a comment. Happy to move it if you prefer. Got everything working. Thanks for the correspondence and sharing the pending docs.
💜 2