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

Updated grafana request to include custom data per target. - DO NOT MERGE - Awaiting Grafana update #233

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
26 changes: 26 additions & 0 deletions bootstrapping-lambda/src/middleware/auth-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const mockedGetVerifiedUserEmail = mocked(getVerifiedUserEmail, true);
const mockedUserHasPermission = mocked(userHasPermission, true);

const mockNextFunction = jest.fn();
const mockSetHeaderFunction = jest.fn();

describe("auth-middleware", () => {
beforeAll(() => {
Expand All @@ -27,9 +28,14 @@ describe("auth-middleware", () => {
status: jest.fn().mockReturnValue({
send: jest.fn(),
}),
setHeader: mockSetHeaderFunction,
} as unknown) as Response;
await getAuthMiddleware()(mockRequest, mockResponse, mockNextFunction);
expect(mockResponse.status).toHaveBeenCalledWith(401);
expect(mockSetHeaderFunction).toHaveBeenCalledWith(
"Access-Control-Allow-Credentials",
"true"
);
expect(mockNextFunction).not.toHaveBeenCalled();
});

Expand All @@ -42,12 +48,17 @@ describe("auth-middleware", () => {
const mockResponse = ({
status: jest.fn(),
send: jest.fn(),
setHeader: mockSetHeaderFunction,
} as unknown) as Response;
await getAuthMiddleware(true)(mockRequest, mockResponse, mockNextFunction);
expect(mockResponse.status).not.toHaveBeenCalled();
expect(mockResponse.send).toHaveBeenCalledWith(
"console.error('pan-domain auth cookie missing, invalid or expired')"
);
expect(mockSetHeaderFunction).toHaveBeenCalledWith(
"Access-Control-Allow-Credentials",
"true"
);
expect(mockNextFunction).not.toHaveBeenCalled();
});

Expand All @@ -62,6 +73,7 @@ describe("auth-middleware", () => {
status: jest.fn().mockReturnValue({
send: mockSendFunction,
}),
setHeader: mockSetHeaderFunction,
} as unknown) as Response;

mockedGetVerifiedUserEmail.mockResolvedValueOnce("[email protected]");
Expand All @@ -72,6 +84,10 @@ describe("auth-middleware", () => {
expect(mockSendFunction).toHaveBeenCalledWith(
"You do not have permission to use PinBoard"
);
expect(mockSetHeaderFunction).toHaveBeenCalledWith(
"Access-Control-Allow-Credentials",
"true"
);
expect(mockNextFunction).not.toHaveBeenCalled();
expect(mockRequest.userEmail).toBeUndefined();
});
Expand All @@ -86,6 +102,7 @@ describe("auth-middleware", () => {
const mockResponse = ({
status: jest.fn(),
send: mockSendFunction,
setHeader: mockSetHeaderFunction,
} as unknown) as Response;

mockedGetVerifiedUserEmail.mockResolvedValueOnce("[email protected]");
Expand All @@ -96,6 +113,10 @@ describe("auth-middleware", () => {
expect(mockSendFunction).toHaveBeenCalledWith(
"console.log('You do not have permission to use PinBoard')"
);
expect(mockSetHeaderFunction).toHaveBeenCalledWith(
"Access-Control-Allow-Credentials",
"true"
);
expect(mockNextFunction).not.toHaveBeenCalled();
expect(mockRequest.userEmail).toBeUndefined();
});
Expand All @@ -110,12 +131,17 @@ describe("auth-middleware", () => {
status: jest.fn().mockReturnValue({
send: jest.fn(),
}),
setHeader: mockSetHeaderFunction,
} as unknown) as Response;
mockedGetVerifiedUserEmail.mockResolvedValueOnce("[email protected]");
mockedUserHasPermission.mockResolvedValueOnce(true);
await getAuthMiddleware()(mockRequest, mockResponse, mockNextFunction);
expect(mockRequest.userEmail).toBe("[email protected]");
expect(mockResponse.status).not.toHaveBeenCalled();
expect(mockSetHeaderFunction).toHaveBeenCalledWith(
"Access-Control-Allow-Credentials",
"true"
);
expect(mockNextFunction).toHaveBeenCalledTimes(1);
});
});
1 change: 1 addition & 0 deletions bootstrapping-lambda/src/middleware/auth-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const getAuthMiddleware = (sendErrorAsOk = false) => async (
next: NextFunction
) => {
const maybeCookieHeader = request.header("Cookie");
response.setHeader("Access-Control-Allow-Credentials", "true");
const maybeAuthenticatedEmail = await getVerifiedUserEmail(maybeCookieHeader);

if (!maybeAuthenticatedEmail) {
Expand Down
13 changes: 4 additions & 9 deletions bootstrapping-lambda/src/reporting/reportingServiceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,22 @@ const metricEndpointMap: Record<string, Stage> = {
[StageMetric.UNIQUE_USERS_PROD]: "PROD",
};

const stageMetricToMetric = {
[StageMetric.UNIQUE_USERS_CODE]: Metric.UNIQUE_USERS,
[StageMetric.UNIQUE_USERS_PROD]: Metric.UNIQUE_USERS,
};

export const getMetrics = async (
request: GrafanaRequest
): Promise<MetricsResponse[]> => {
const lambda = new AWS.Lambda(standardAwsConfig);
const { range, targets } = request;
const metricsResponse = await Promise.all(
targets.map((target) => {
console.log(`processing grafana request`, target.target);
targets.map(({ target, data }) => {
console.log(`processing grafana request`, target);
return lambda
.invoke({
FunctionName: getDatabaseBridgeLambdaFunctionName(
metricEndpointMap[target.target]
data?.stage || metricEndpointMap[target]
),
Payload: JSON.stringify({
range,
metric: stageMetricToMetric[target.target],
metric: Metric.UNIQUE_USERS,
}),
})
.promise();
Expand Down
2 changes: 0 additions & 2 deletions bootstrapping-lambda/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ server.use(express.json());
server.use(cors(corsOptions));

server.post("/search", getAuthMiddleware(), (_, response) => {
response.setHeader("Access-Control-Allow-Credentials", "true");
return response.json(Object.values(StageMetric));
});

server.post(
"/query",
getAuthMiddleware(),
async (request: AuthenticatedRequest, response) => {
response.setHeader("Access-Control-Allow-Credentials", "true");
const { body: metricsQuery }: { body: GrafanaRequest } = request;
response.json(await getMetrics(metricsQuery));
}
Expand Down
8 changes: 8 additions & 0 deletions shared/types/grafanaType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Stage } from "./stage";

export interface Range {
from: string;
to: string;
Expand All @@ -9,15 +11,21 @@ export type TargetType = "timeserie" | "table";
export enum StageMetric {
UNIQUE_USERS_CODE = "uniqueUsersCode",
UNIQUE_USERS_PROD = "uniqueUsersProd",
UNIQUE_USERS = "uniqueUsers",
}

export enum Metric {
UNIQUE_USERS = "uniqueUsers",
}

export interface customData {
stage?: Stage;
}

export interface Target {
target: StageMetric;
type: TargetType;
data?: customData;
}

export interface GrafanaRequest {
Expand Down