Skip to content

Commit

Permalink
feat: add annotation to entities base on results
Browse files Browse the repository at this point in the history
  • Loading branch information
xNok committed Nov 11, 2024
1 parent 6ba9be2 commit 3503f17
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
3 changes: 3 additions & 0 deletions plugins/backstage-opa-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@
"@backstage/errors": "^1.2.4",
"@backstage/integration": "^1.12.0",
"@types/express": "*",
"@types/lodash-es": "^4.17.12",
"express": "^4.17.1",
"express-promise-router": "^4.1.0",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"node-fetch": "^2.6.7",
"winston": "^3.2.1",
"yn": "^4.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {Entity} from "@backstage/catalog-model";
import {CatalogOPAEntityValidator} from "./CatalogOPAEntityValidator";
import {mockServices} from "@backstage/backend-test-utils";
import {LocationSpec} from "@backstage/plugin-catalog-common";
import {OpaResult} from "../service/entityCheckerApi";

describe('CatalogOPAEntityValidator', () => {
it('adds annotation, when entity fails validation', async () => {
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'my-component',
},
};

const location: LocationSpec = {
type: 'url',
target:
'https://example.com',
};

const mockLogger = mockServices.logger.mock();
const mockEntityCheckerApi = {
checkEntity: jest.fn((): Promise<OpaResult> => Promise.resolve(
{
good_entity: false,
result: [
{
id: "metadata.tags",
check_title: "metadata.tags",
level: 'error',
message: "You do not have any tags set!"
}
]
}
))
};
const mockCatalogProcessorEmit = jest.fn()

const processor = new CatalogOPAEntityValidator(mockEntityCheckerApi, mockLogger)

expect(await processor.preProcessEntity(entity, location, mockCatalogProcessorEmit)).toEqual({
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'my-component',
annotations: {
'entity-checker.opa/good-entity': 'false',
},
},
});

})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {Entity} from "@backstage/catalog-model";
import { LocationSpec } from '@backstage/plugin-catalog-common'
import {EntityCheckerApi} from "../service/entityCheckerApi";
import {LoggerService} from "@backstage/backend-plugin-api";
import { merge } from 'lodash';

const OPA_ENTITY_CHECKER_GOOD_ENTITY_ANNOTATION = "entity-checker.opa/good-entity"

export class CatalogOPAEntityValidator implements CatalogProcessor {
constructor(
Expand All @@ -21,6 +23,8 @@ export class CatalogOPAEntityValidator implements CatalogProcessor {
emit: CatalogProcessorEmit,
): Promise<Entity> {

let isGoodEntity = true

await this.api.checkEntity({
entityMetadata: JSON.stringify(entity)
}).then(data => {
Expand All @@ -30,9 +34,25 @@ export class CatalogOPAEntityValidator implements CatalogProcessor {
data.result.forEach((e) => {
emit(processingResult.inputError(location, e.message.toString()))
})

isGoodEntity = false
}
})

return entity;
if (isGoodEntity) {
return entity;
}

return merge(
{
metadata: {
annotations: {
[OPA_ENTITY_CHECKER_GOOD_ENTITY_ANNOTATION]: "false"
}
}
},
entity
)

}
}
}
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8633,9 +8633,12 @@ __metadata:
"@backstage/errors": ^1.2.4
"@backstage/integration": ^1.12.0
"@types/express": "*"
"@types/lodash-es": ^4.17.12
"@types/supertest": ^2.0.12
express: ^4.17.1
express-promise-router: ^4.1.0
lodash: ^4.17.21
lodash-es: ^4.17.21
msw: ^1.0.0
node-fetch: ^2.6.7
supertest: ^6.2.4
Expand Down Expand Up @@ -12049,6 +12052,22 @@ __metadata:
languageName: node
linkType: hard

"@types/lodash-es@npm:^4.17.12":
version: 4.17.12
resolution: "@types/lodash-es@npm:4.17.12"
dependencies:
"@types/lodash": "*"
checksum: 990a99e2243bebe9505cb5ad19fbc172beb4a8e00f9075c99fc06c46c2801ffdb40bc2867271cf580d5f48994fc9fb076ec92cd60a20e621603bf22114e5b077
languageName: node
linkType: hard

"@types/lodash@npm:*":
version: 4.17.13
resolution: "@types/lodash@npm:4.17.13"
checksum: d0bf8fbd950be71946e0076b30fd40d492293baea75f05931b6b5b906fd62583708c6229abdb95b30205ad24ce1ed2f48bc9d419364f682320edd03405cc0c7e
languageName: node
linkType: hard

"@types/long@npm:^4.0.0":
version: 4.0.2
resolution: "@types/long@npm:4.0.2"
Expand Down

0 comments on commit 3503f17

Please sign in to comment.