Skip to content

feature: add initial graphql checks #65

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

Merged
merged 5 commits into from
Nov 2, 2022
Merged
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
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-session": "^1.17.3",
"graphql": "^16.6.0",
"ioredis": "^5.2.3",
"js-yaml": "^4.1.0",
"js-yaml-source-map": "^0.2.2",
Expand Down
42 changes: 26 additions & 16 deletions backend/src/analyze-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AlertService } from "services/alert"
import { DatabaseService } from "services/database"
import { RedisClient } from "utils/redis"
import { TRACES_QUEUE } from "~/constants"
import { QueryRunner, Raw } from "typeorm"
import { QueryRunner } from "typeorm"
import { QueuedApiTrace } from "@common/types"
import {
endpointAddNumberParams,
Expand All @@ -17,6 +17,7 @@ import {
} from "utils"
import { getPathTokens } from "@common/utils"
import { AlertType } from "@common/enums"
import { isGraphQlEndpoint } from "services/graphql"

const GET_ENDPOINT_QUERY = `
SELECT
Expand Down Expand Up @@ -158,23 +159,29 @@ const generateEndpoint = async (
trace: QueuedApiTrace,
queryRunner: QueryRunner,
): Promise<void> => {
const pathTokens = getPathTokens(trace.path)
const isGraphQl = isGraphQlEndpoint(trace.path)
let paramNum = 1
let parameterizedPath = ""
let pathRegex = String.raw``
for (let j = 0; j < pathTokens.length; j++) {
const tokenString = pathTokens[j]
if (tokenString === "/") {
parameterizedPath += "/"
pathRegex += "/"
} else if (tokenString.length > 0) {
if (isSuspectedParamater(tokenString)) {
parameterizedPath += `/{param${paramNum}}`
pathRegex += String.raw`/[^/]+`
paramNum += 1
} else {
parameterizedPath += `/${tokenString}`
pathRegex += String.raw`/${tokenString}`
if (isGraphQl) {
parameterizedPath = trace.path
pathRegex = trace.path
} else {
const pathTokens = getPathTokens(trace.path)
for (let j = 0; j < pathTokens.length; j++) {
const tokenString = pathTokens[j]
if (tokenString === "/") {
parameterizedPath += "/"
pathRegex += "/"
} else if (tokenString.length > 0) {
if (isSuspectedParamater(tokenString)) {
parameterizedPath += `/{param${paramNum}}`
pathRegex += String.raw`/[^/]+`
paramNum += 1
} else {
parameterizedPath += `/${tokenString}`
pathRegex += String.raw`/${tokenString}`
}
}
}
}
Expand All @@ -188,6 +195,9 @@ const generateEndpoint = async (
apiEndpoint.method = trace.method
endpointAddNumberParams(apiEndpoint)
apiEndpoint.dataFields = []
if (isGraphQl) {
apiEndpoint.isGraphQl = true
}

try {
await queryRunner.startTransaction()
Expand Down Expand Up @@ -259,7 +269,7 @@ const analyzeTraces = async (): Promise<void> => {
apiEndpoint.dataFields = dataFields
await analyze(trace, apiEndpoint, queryRunner)
} else {
if (trace.responseStatus !== 404) {
if (trace.responseStatus !== 404 && trace.responseStatus !== 405) {
await generateEndpoint(trace, queryRunner)
}
}
Expand Down
2 changes: 2 additions & 0 deletions backend/src/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { initMigration1665782029662 } from "migrations/1665782029662-init-migrat
import { addUniqueConstraintApiEndpoint1666678487137 } from "migrations/1666678487137-add-unique-constraint-api-endpoint"
import { dropAnalyzedColumnFromApiTrace1666752646836 } from "migrations/1666752646836-drop-analyzed-column-from-api-trace"
import { addIndexForDataField1666941075032 } from "migrations/1666941075032-add-index-for-data-field"
import { addIsgraphqlColumnApiEndpoint1667095325334 } from "migrations/1667095325334-add-isgraphql-column-api-endpoint"

export const AppDataSource: DataSource = new DataSource({
type: "postgres",
Expand All @@ -47,6 +48,7 @@ export const AppDataSource: DataSource = new DataSource({
addUniqueConstraintApiEndpoint1666678487137,
dropAnalyzedColumnFromApiTrace1666752646836,
addIndexForDataField1666941075032,
addIsgraphqlColumnApiEndpoint1667095325334,
],
migrationsRun: runMigration,
logging: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class addIsgraphqlColumnApiEndpoint1667095325334
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE api_endpoint ADD COLUMN IF NOT EXISTS "isGraphQl" BOOLEAN NOT NULL DEFAULT FALSE`,
)
await queryRunner.query(
`CREATE INDEX IF NOT EXISTS "isGraphQl_api_endpoint" ON "api_endpoint" ("isGraphQl")`,
)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX IF EXISTS "isGraphQl_api_endpoint"`)
await queryRunner.query(
`ALTER TABLE api_endpoint DROP COLUMN IF EXISTS "isGraphQl"`,
)
}
}
3 changes: 3 additions & 0 deletions backend/src/models/api-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export class ApiEndpoint extends BaseEntity {
@Column({ type: "bool", nullable: true })
isAuthenticatedUserSet: boolean

@Column({ type: "bool", default: false })
isGraphQl: boolean

@Column({ nullable: true })
openapiSpecName: string

Expand Down
9 changes: 9 additions & 0 deletions backend/src/services/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const getGraphQlPaths = () => {
return ["/graphql"]
}

export const isGraphQlEndpoint = (path: string) => {
if (getGraphQlPaths().includes(path)) {
return true
}
}
1 change: 1 addition & 0 deletions backend/src/utils/words.json
Original file line number Diff line number Diff line change
Expand Up @@ -129063,6 +129063,7 @@
"grapewise": 1,
"grapewort": 1,
"graph": 1,
"graphql": 1,
"graphalloy": 1,
"graphanalysis": 1,
"graphed": 1,
Expand Down
5 changes: 5 additions & 0 deletions backend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2315,6 +2315,11 @@ graceful-fs@^4.1.9, graceful-fs@^4.2.9:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.10.tgz#147d3a006da4ca3ce14728c7aefc287c367d7a6c"
integrity sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==

graphql@^16.6.0:
version "16.6.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.6.0.tgz#c2dcffa4649db149f6282af726c8c83f1c7c5fdb"
integrity sha512-KPIBPDlW7NxrbT/eh4qPXz5FiFdL5UbaA0XUNz2Rp3Z3hqBSkbj0GVjwFDztsWVauZUWsbKHgMg++sk8UX0bkw==

gtoken@^6.0.0:
version "6.1.1"
resolved "https://registry.yarnpkg.com/gtoken/-/gtoken-6.1.1.tgz#29ebf3e6893719176d180f5694f1cad525ce3c04"
Expand Down
5 changes: 5 additions & 0 deletions common/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,9 @@ export enum API_KEY_TYPE {
GCP = "GCP",
AWS = "AWS",
GENERIC = "GENERIC"
}

export enum OperationType {
QUERY = "query",
MUTATION = "mutation",
}
1 change: 1 addition & 0 deletions common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export interface ApiEndpoint {
openapiSpecName: string
isAuthenticatedDetected: boolean
isAuthenticatedUserSet: boolean
isGraphQl: boolean
}

export interface ApiEndpointDetailed extends ApiEndpoint {
Expand Down