Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add access logging #1383

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading