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 openapi linter #208

Merged
merged 27 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7c7478c
feat: add openapi linter
bkioshn Jan 13, 2024
add302d
fix(lint): openapi linting
bkioshn Jan 16, 2024
e7b6d3b
fix(spectral): add version for CDN
bkioshn Jan 16, 2024
9272740
fix(openapi): linting openAPI files in test stage
bkioshn Jan 16, 2024
ae10091
Merge branch 'main' into feat/openapi-lint
bkioshn Jan 25, 2024
05c7241
fix: update rules and ref to doc
bkioshn Jan 25, 2024
a8341bf
fix: typo
bkioshn Jan 25, 2024
fb746ad
fix: openapi lint cat-ci version
bkioshn Jan 25, 2024
a573a0c
Merge branch 'main' into feat/openapi-lint
bkioshn Jan 30, 2024
5b411bb
fix: update spectral rules
bkioshn Jan 30, 2024
970b91b
Merge branch 'main' into feat/openapi-lint
bkioshn Feb 2, 2024
ac97315
fix: update spectral rules
bkioshn Feb 2, 2024
0ea0354
Merge branch 'main' into feat/openapi-lint
bkioshn Feb 2, 2024
a696901
chore: add ref to the rule
bkioshn Feb 2, 2024
b941bbb
fix: server rules
bkioshn Feb 5, 2024
5f3c8f3
fix: remove doc rule from server
bkioshn Feb 5, 2024
60bcc54
Merge branch 'main' into feat/openapi-lint
bkioshn Feb 5, 2024
c16fd9b
fix: ignore id rule to all file
bkioshn Feb 6, 2024
13934a7
fix: remove server rules
bkioshn Feb 6, 2024
cfa851c
Merge branch 'main' into feat/openapi-lint
bkioshn Feb 6, 2024
9c37f19
fix: cspell word
bkioshn Feb 6, 2024
3a0c0b2
fix: cspell word
bkioshn Feb 6, 2024
72e2854
fix: add regex for doc desc
bkioshn Feb 7, 2024
4f474e8
fix: typo
bkioshn Feb 7, 2024
5f32760
Merge branch 'main' into feat/openapi-lint
stevenj Feb 7, 2024
e7a8046
Merge branch 'main' into feat/openapi-lint
bkioshn Feb 7, 2024
abf9043
fix: doc build
bkioshn Feb 7, 2024
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
122 changes: 122 additions & 0 deletions catalyst-gateway/.spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# References to the rules
# OpenAPI: https://docs.stoplight.io/docs/spectral/4dec24461f3af-open-api-rules#openapi-rules
# OWASP Top 10: https://github.com/stoplightio/spectral-owasp-ruleset/blob/v1.4.3/src/ruleset.ts
# Documentations: https://github.com/stoplightio/spectral-documentation/blob/v1.3.1/src/ruleset.ts

# cspell: words OWASP owasp Baction Baccount

# Use CDN hosted version for spectral-documentation and spectral-owasp
extends:
- 'spectral:oas'
- 'https://unpkg.com/@stoplight/[email protected]/dist/ruleset.mjs'
- 'https://unpkg.com/@stoplight/[email protected]/dist/ruleset.mjs'

aliases:
PathItem:
- $.paths[*]
OperationObject:
- $.paths[*][get,put,post,delete,options,head,patch,trace]
DescribableObjects:
- $.info
- $.tags[*]
- '#OperationObject'
- '#OperationObject.responses[*]'
- '#PathItem.parameters[?(@ && @.in)]'
- '#OperationObject.parameters[?(@ && @.in)]'

overrides:
- files: ['*']
rules:
# Override document description rule
# - No limitations on the characters that can start or end a sentence.
# - Length should be >= 20 characters
# Ref: https://github.com/stoplightio/spectral-documentation/blob/a34ca1b49cbd1ac5a75cfcb93c69d1d77bde341e/src/ruleset.ts#L173
docs-description:
given: '#DescribableObjects'
then:
- field: 'description'
function: 'truthy'
- field: 'description'
function: 'length'
functionOptions:
min: 20
- field: 'description'
function: 'pattern'
functionOptions:
# Matches any character that is #, *, uppercase or lowercase letters from A to Z, or digits from 0 to 9 at the beginning of the string.
# with zero or more occurrences of any character except newline.
match: '^[#*A-Za-z0-9].*'

# Severity
# warn: Should be implemented, but is blocked by a technical issue.
# info: Good to be implemented.

# Rate limit
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L436
owasp:api4:2019-rate-limit: warn
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L484
owasp:api4:2019-rate-limit-responses-429: warn
# Public API
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L305
owasp:api2:2019-protection-global-safe: info
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L269
owasp:api2:2019-protection-global-unsafe: info
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L287
owasp:api2:2019-protection-global-unsafe-strict: info
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L376
owasp:api3:2019-define-error-responses-401: warn

# UUID rules for name containing "id" is ignored
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L102
owasp:api1:2019-no-numeric-ids: off

- files:
- '**#/paths/~1api~1health~1live/get/responses'
- '**#/paths/~1api~1health~1started/get/responses'
- '**#/paths/~1api~1health~1ready/get/responses'
- '**#/paths/~1api~1test~1test~1%7Bid%7D~1test~1%7Baction%7D/post/responses'
- '**#/paths/~1api~1test~1test~1%7Bid%7D~1test~1%7Baction%7D/get/responses'
# Recheck this, already apply validator but does not work "FragmentId"
- '**#/paths/~1api~1v1~1fragments~1statuses/get/parameters/0/schema'
# Recheck this, already apply validator but does not work "AccountId"
- '**#/paths/~1api~1v1~1votes~1plan~1account-votes~1%7Baccount_id%7D/get/parameters/0/schema'
rules:
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L551
owasp:api4:2019-string-restricted: off

- files:
- '**#/paths/~1api~1health~1live/get/responses'
- '**#/paths/~1api~1health~1started/get/responses'
- '**#/paths/~1api~1health~1ready/get/responses'
- '**#/paths/~1api~1v0~1message/post/requestBody/content'
- '**#/paths/~1api~1test~1test~1%7Bid%7D~1test~1%7Baction%7D/post/responses'
- '**#/paths/~1api~1test~1test~1%7Bid%7D~1test~1%7Baction%7D/get/responses'
- '**#/components/schemas/ServerErrorPayload/properties/id'
- '**#/components/schemas/VoterRegistration/properties/as_at'
- '**#/components/schemas/VoterRegistration/properties/last_updated'
# Recheck this, already apply validator but does not work "FragmentId"
- '**#/paths/~1api~1v1~1fragments~1statuses/get/parameters/0/schema'
# Recheck this, already apply validator but does not work "AccountId"
- '**#/paths/~1api~1v1~1votes~1plan~1account-votes~1%7Baccount_id%7D/get/parameters/0/schema'
rules:
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L521
owasp:api4:2019-string-limit: off

- files:
- '**#/paths/~1api~1v0~1vote~1active~1plans/get/responses'
- '**#/paths/~1api~1v1~1votes~1plan~1account-votes~1%7Baccount_id%7D/get/responses'
rules:
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L506
owasp:api4:2019-array-limit: off

- files:
- '**#/components/schemas/FragmentStatus'
rules:
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L678
owasp:api6:2019-no-additionalProperties: off

- files:
- '**#/paths/~1api~1v1~1fragments~1statuses/get/responses'
rules:
# Ref: https://github.com/stoplightio/spectral-owasp-ruleset/blob/2fd49c377794222352ff10dee99ed2a106c35199/src/ruleset.ts#L698
owasp:api6:2019-constrained-additionalProperties: off
11 changes: 11 additions & 0 deletions catalyst-gateway/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ build:
all-hosts-build:
BUILD --platform=linux/amd64 --platform=linux/arm64 +build


# test-lint-openapi - OpenAPI linting from an artifact
# testing whether the OpenAPI generated during build stage follows good practice.
test-lint-openapi:
FROM github.com/input-output-hk/catalyst-ci/earthly/spectral:v2.4.0+spectral-base
# Copy the doc artifact.
COPY +build/doc ./doc
# Scan the doc directory where type of file is JSON.
DO github.com/input-output-hk/catalyst-ci/earthly/spectral:v2.4.0+BUILD_SPECTRAL --dir=./doc --file_type="json"

# Publish packages if all integration tests have passed. (Failure to pass tests will prevent packages being published.)
# publish:
# FROM scratch
Expand All @@ -58,3 +68,4 @@ local-ci-run:
BUILD +build
# BUILD +package
# BUILD +publish

Loading