Skip to content

Commit

Permalink
fix endpoint, data field, and spec generation (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi authored Oct 19, 2022
1 parent 1b190e8 commit 2c12831
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
36 changes: 19 additions & 17 deletions backend/src/services/data-field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class DataFieldService {
apiEndpoint: ApiEndpoint,
dataValue: any,
): void {
const existingMatch = `${dataSection}.${dataPath}`
const existingMatch = `${dataSection}${dataPath ? `.${dataPath}` : ""}`
const dataType = getDataType(dataValue)
if (!this.dataFields[existingMatch]) {
const dataField = new DataField()
Expand Down Expand Up @@ -214,31 +214,33 @@ export class DataFieldService {
this.dataFields = apiEndpoint.dataFields.reduce((obj, item) => {
return {
...obj,
[`${item.dataSection}.${item.dataPath}`]: item,
[`${item.dataSection}${item.dataPath ? `.${item.dataPath}` : ""}`]: item,
}
}, {})
this.updatedFields = {}
this.findPathDataFields(apiTrace.path, apiEndpoint)
this.findPairObjectDataFields(
DataSection.REQUEST_QUERY,
apiTrace.requestParameters,
apiEndpoint,
)
this.findPairObjectDataFields(
DataSection.REQUEST_HEADER,
apiTrace.requestHeaders,
apiEndpoint,
)
if (apiTrace.responseStatus < 400) {
this.findPairObjectDataFields(
DataSection.REQUEST_QUERY,
apiTrace.requestParameters,
apiEndpoint,
)
this.findPairObjectDataFields(
DataSection.REQUEST_HEADER,
apiTrace.requestHeaders,
apiEndpoint,
)
this.findBodyDataFields(
DataSection.REQUEST_BODY,
apiTrace.requestBody,
apiEndpoint,
)
}
this.findPairObjectDataFields(
DataSection.RESPONSE_HEADER,
apiTrace.responseHeaders,
apiEndpoint,
)
this.findBodyDataFields(
DataSection.REQUEST_BODY,
apiTrace.requestBody,
apiEndpoint,
)
this.findBodyDataFields(
DataSection.RESPONSE_BODY,
apiTrace.responseBody,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/jobs/generate-endpoints-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const generateEndpointsFromTraces = async (): Promise<void> => {
}
}
if (pathRegex.length > 0) {
pathRegex = String.raw`^${pathRegex}$`
pathRegex = String.raw`^${pathRegex}(/)*$`
const regexKey = `${trace.host}-${trace.method}-${pathRegex}`
if (regexToTracesMap[regexKey]) {
regexToTracesMap[regexKey].traces.push(trace)
Expand Down
36 changes: 20 additions & 16 deletions backend/src/services/jobs/generate-openapi-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,23 @@ const generateOpenApiSpec = async (): Promise<void> => {
)
}
}
for (const requestParameter of requestParamters) {
const key = `${requestParameter.name}<>query`
parameters[key] = parseSchema(
parameters[key] ?? {},
parsedJsonNonNull(requestParameter.value, true),
)
}
for (const requestHeader of requestHeaders) {
const key = `${requestHeader.name}<>header`
parameters[key] = parseSchema(
parameters[key] ?? {},
parsedJsonNonNull(requestHeader.value, true),
)
if (requestHeader.name.toLowerCase() === "content-type") {
requestContentType = requestHeader.value.toLowerCase()
if (trace.responseStatus < 400) {
for (const requestParameter of requestParamters) {
const key = `${requestParameter.name}<>query`
parameters[key] = parseSchema(
parameters[key] ?? {},
parsedJsonNonNull(requestParameter.value, true),
)
}
for (const requestHeader of requestHeaders) {
const key = `${requestHeader.name}<>header`
parameters[key] = parseSchema(
parameters[key] ?? {},
parsedJsonNonNull(requestHeader.value, true),
)
if (requestHeader.name.toLowerCase() === "content-type") {
requestContentType = requestHeader.value.toLowerCase()
}
}
}
for (const responseHeader of responseHeaders) {
Expand All @@ -155,7 +157,9 @@ const generateOpenApiSpec = async (): Promise<void> => {
)
}

parseContent(requestBodySpec, requestBody, requestContentType)
if (trace.responseStatus < 400) {
parseContent(requestBodySpec, requestBody, requestContentType)
}
if (responseBody) {
if (!responses[responseStatusString]?.content) {
responses[responseStatusString] = {
Expand Down
5 changes: 3 additions & 2 deletions backend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ export const getRiskScore = (dataFields: DataField[]): RiskScore => {
if (!dataFields) {
return RiskScore.NONE
}
let numRiskySensitiveDataClasses = 0
let uniqueSensitiveDataClasses = new Set<string>()
for (const dataField of dataFields) {
if (dataField.dataClasses) {
numRiskySensitiveDataClasses += dataField.dataClasses?.length ?? 0
dataField.dataClasses.forEach(e => uniqueSensitiveDataClasses.add(e))
}
}
const numRiskySensitiveDataClasses = uniqueSensitiveDataClasses.size
switch (true) {
case numRiskySensitiveDataClasses >= 3:
return RiskScore.HIGH
Expand Down

0 comments on commit 2c12831

Please sign in to comment.