Skip to content

Commit

Permalink
API: Add Access logging (#1384)
Browse files Browse the repository at this point in the history
We now log request and response metadata in our middleware.
Request: Method, URL, IP, OS, Browser, Device
Response: Status

The two are linked by a UUID
  • Loading branch information
Amogh-Bharadwaj authored Feb 27, 2024
1 parent 92d4218 commit d73839b
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ui/app/api/mirrors/cdc/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GetFlowHttpAddressFromEnv } from '@/rpc/http';
export async function POST(request: Request) {
const body = await request.json();
const { config } = body;
console.log('/mirrors/cdc config: ', config);

const flowServiceAddr = GetFlowHttpAddressFromEnv();
const req: CreateCDCFlowRequest = {
connectionConfigs: config,
Expand Down
4 changes: 2 additions & 2 deletions ui/app/api/mirrors/cdc/validate/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
ValidateCDCMirrorResponse,
} from '@/grpc_generated/route';
import { GetFlowHttpAddressFromEnv } from '@/rpc/http';
import { NextRequest } from 'next/server';

export async function POST(request: Request) {
export async function POST(request: NextRequest) {
const body = await request.json();
const { config } = body;
console.log('/mirrors/cdc/validate config: ', config);
const flowServiceAddr = GetFlowHttpAddressFromEnv();
const req: CreateCDCFlowRequest = {
connectionConfigs: config,
Expand Down
2 changes: 1 addition & 1 deletion ui/app/api/mirrors/drop/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function POST(request: Request) {
destinationPeer,
removeFlowEntry: true,
};
console.log('/mirrors/drop: req:', req);

try {
const dropStatus: ShutdownResponse = await fetch(
`${flowServiceAddr}/v1/mirrors/drop`,
Expand Down
2 changes: 1 addition & 1 deletion ui/app/api/mirrors/qrep/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GetFlowHttpAddressFromEnv } from '@/rpc/http';
export async function POST(request: Request) {
const body = await request.json();
const { config } = body;
console.log('/qrep/post config:', config);

const flowServiceAddr = GetFlowHttpAddressFromEnv();
const req: CreateQRepFlowRequest = {
qrepConfig: config,
Expand Down
1 change: 0 additions & 1 deletion ui/app/api/mirrors/state/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { GetFlowHttpAddressFromEnv } from '@/rpc/http';
export async function POST(request: Request) {
const body: MirrorStatusRequest = await request.json();
const flowServiceAddr = GetFlowHttpAddressFromEnv();
console.log('/mirrors/state: req:', body);
try {
const res: MirrorStatusResponse = await fetch(
`${flowServiceAddr}/v1/mirrors/${body.flowJobName}`,
Expand Down
2 changes: 1 addition & 1 deletion ui/app/api/mirrors/state_change/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GetFlowHttpAddressFromEnv } from '@/rpc/http';
export async function POST(request: Request) {
const body = await request.json();
const flowServiceAddr = GetFlowHttpAddressFromEnv();
console.log('/mirrors/state_change: req:', body);

try {
const res: FlowStateChangeResponse = await fetch(
`${flowServiceAddr}/v1/mirrors/state_change`,
Expand Down
1 change: 0 additions & 1 deletion ui/app/api/peers/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export const dynamic = 'force-dynamic';

export async function POST(request: Request) {
const body = await request.json();
console.log('POST Validate Peer:', body);
const { name, type, config, mode } = body;
const flowServiceAddr = GetFlowHttpAddressFromEnv();
const peer = constructPeer(name, type, config);
Expand Down
23 changes: 21 additions & 2 deletions ui/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import { Configuration } from '@/app/config/config';
import { withAuth } from 'next-auth/middleware';
import { NextRequest, NextResponse } from 'next/server';
import { NextRequest, NextResponse, userAgent } from 'next/server';

const authMiddleware = withAuth({});

export default async function middleware(req: NextRequest, resp: NextResponse) {
const accessLogUUID = crypto.randomUUID();
const agent = userAgent(req);
const xForwardedFor = req.headers.get('x-forwarded-for');
console.log(
`[${accessLogUUID}] [${req.method} ${req.url}] [${req.ip} ${xForwardedFor}] (${JSON.stringify(agent.device)} ${JSON.stringify(agent.os)} ${JSON.stringify(agent.browser)})`
);

if (Configuration.authentication.PEERDB_PASSWORD) {
return (authMiddleware as any)(req);
const authRes = await (authMiddleware as any)(req);

const authResJson = NextResponse.next();
console.log(
`[${accessLogUUID}] [${req.method} ${req.url}] [${req.ip} ${xForwardedFor}] (${JSON.stringify(agent.device)} ${JSON.stringify(agent.os)} ${JSON.stringify(agent.browser)}) ${authResJson.status}`
);

return authRes;
}

const res = NextResponse.next();
console.log(
`[${accessLogUUID}] [${req.method} ${req.url}] [${req.ip} ${xForwardedFor}] (${JSON.stringify(agent.device)} ${JSON.stringify(agent.os)} ${JSON.stringify(agent.browser)}) ${res.status}`
);
}

export const config = {
Expand Down

0 comments on commit d73839b

Please sign in to comment.