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

chore(backend): update OpenAPI specs #799

Merged
merged 7 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 .github/workflows/lint_test_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
- uses: actions/checkout@v2
- uses: ./.github/workflows/rafiki/env-setup
- run: pnpm --filter backend build:deps
- run: pnpm --filter backend fetch-schemas
- run: pnpm --filter backend test

frontend:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ pnpm --filter open-api test
# run all tests
pnpm -r --workspace-concurrency=1 test

# pull in latest openapi specs for auth server:
# pull in latest openapi specs:
pnpm --filter auth fetch-schemas
pnpm --filter backend fetch-schemas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a root level command?
"localenv:fetch-op-schemas": "pnpm --filter auth --filter backend fetch-schemas" , and then include it as part of the local env setup readme?

https://interledgerfoundation.slack.com/archives/C0302LMKBT7/p1670414356297499?thread_ts=1670407497.730339&cid=C0302LMKBT7

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that devs would run through this environment setup before doing local env, but I guess that's not entirely necessary.
https://github.com/interledger/rafiki#environment-setup
Should we split out installing node & pnpm and fetching schemas into a separate prerequisite section?

I also noticed that as is it shouldn't work to have tests before fetch schemas.


# format and lint code:
pnpm format
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ build
.env
src/coverage
tmp
/src/openapi/resource-server.yaml
/src/openapi/schemas.yaml
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"build": "pnpm build:deps && tsc --build tsconfig.json && pnpm copy-files",
"clean": "rm -fr dist/",
"copy-files": "cp src/graphql/schema.graphql dist/graphql/",
"fetch-schemas": "./scripts/get-op-schema.sh",
"prepack": "pnpm build"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions packages/backend/scripts/get-op-schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

## https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
OUTDIR=$( cd -P "$( dirname "$SOURCE" )/../src/openapi" >/dev/null 2>&1 && pwd )

curl -o "$OUTDIR/resource-server.yaml" https://raw.githubusercontent.com/interledger/open-payments/b363d33038fe789e5388f04f80ddd06a4fa97093/openapi/resource-server.yaml
curl -o "$OUTDIR/schemas.yaml" https://raw.githubusercontent.com/interledger/open-payments/b363d33038fe789e5388f04f80ddd06a4fa97093/openapi/schemas.yaml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also update the open-payments commit version in this PR

or would it make sense to export these from open-payments? so we can be sure both are using the same spec? This does seem vaguely familiar though... have we discussed this before?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could even do it for both of backend and auth projects since open-payments contains both, which would eliminate the need for the packages/backend/src/openapi/auth-server.yaml dependency

Copy link
Contributor Author

@wilsonianb wilsonianb Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to export these from open-payments?

I'm favor of defining the (default) commit in a single place.
It seems like that could happen while still allowing subsequent local tinkering of the specs
#684 (comment)

which would eliminate the need for the packages/backend/src/openapi/auth-server.yaml dependency

Did you mean resource-server.yaml?

32 changes: 19 additions & 13 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export class App {
)
const quoteRoutes = await this.container.use('quoteRoutes')
const connectionRoutes = await this.container.use('connectionRoutes')
const openApi = await this.container.use('openApi')
const { resourceServerSpec } = await this.container.use('openApi')
const toRouterPath = (path: string): string =>
path.replace(/{/g, ':').replace(/}/g, '')

Expand Down Expand Up @@ -293,8 +293,8 @@ export class App {
[AccessAction.ListAll]: 'list'
}

for (const path in openApi.paths) {
for (const method in openApi.paths[path]) {
for (const path in resourceServerSpec.paths) {
for (const method in resourceServerSpec.paths[path]) {
if (isHttpMethod(method)) {
const action = toAction({ path, method })
assert.ok(action)
Expand All @@ -315,10 +315,13 @@ export class App {
route = connectionRoutes.get
router[method](
toRouterPath(path),
createValidatorMiddleware<ContextType<typeof route>>(openApi, {
path,
method
}),
createValidatorMiddleware<ContextType<typeof route>>(
resourceServerSpec,
{
path,
method
}
),
route
)
} else if (path !== '/' || method !== HttpMethod.GET) {
Expand All @@ -330,10 +333,13 @@ export class App {
router[method](
PAYMENT_POINTER_PATH + toRouterPath(path),
createPaymentPointerMiddleware(),
createValidatorMiddleware<ContextType<typeof route>>(openApi, {
path,
method
}),
createValidatorMiddleware<ContextType<typeof route>>(
resourceServerSpec,
{
path,
method
}
),
createAuthMiddleware({
type,
action
Expand All @@ -349,7 +355,7 @@ export class App {
router.get(
PAYMENT_POINTER_PATH + '/jwks.json',
createPaymentPointerMiddleware(),
createValidatorMiddleware<PaymentPointerContext>(openApi, {
createValidatorMiddleware<PaymentPointerContext>(resourceServerSpec, {
path: '/jwks.json',
method: HttpMethod.GET
}),
Expand All @@ -364,7 +370,7 @@ export class App {
router.get(
PAYMENT_POINTER_PATH,
createPaymentPointerMiddleware(),
createValidatorMiddleware<PaymentPointerContext>(openApi, {
createValidatorMiddleware<PaymentPointerContext>(resourceServerSpec, {
path: '/',
method: HttpMethod.GET
}),
Expand Down
8 changes: 0 additions & 8 deletions packages/backend/src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,6 @@ export const Config = {
signatureVersion: envInt('SIGNATURE_VERSION', 1),
bypassSignatureValidation: envBool('BYPASS_SIGNATURE_VALIDATION', false),

openPaymentsSpec: envString(
'OPEN_PAYMENTS_SPEC',
'https://raw.githubusercontent.com/interledger/open-payments/f365dbec4b9dec98b9f622bc49a92aea9ee01568/openapi/RS/openapi.yaml'
),
authServerSpec: envString(
'AUTH_SERVER_SPEC',
'https://raw.githubusercontent.com/interledger/open-payments/77462cd0872be8d0fa487a4b233defe2897a7ee4/auth-server-open-api-spec.yaml'
),
keyId: envString('KEY_ID', 'rafiki'),
privateKey: parseOrProvisionKey(envString('PRIVATE_KEY_FILE', undefined)),

Expand Down
22 changes: 14 additions & 8 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter } from 'events'
import { Server } from 'http'
import path from 'path'
import createLogger from 'pino'
import { knex } from 'knex'
import { Model } from 'objection'
Expand Down Expand Up @@ -109,13 +110,17 @@ export function initIocContainer(
replica_addresses: config.tigerbeetleReplicaAddresses
})
})
container.singleton('openApi', async (deps) => {
const config = await deps.use('config')
return await createOpenAPI(config.openPaymentsSpec)
})
container.singleton('authOpenApi', async (deps) => {
const config = await deps.use('config')
return await createOpenAPI(config.authServerSpec)
container.singleton('openApi', async () => {
const authServerSpec = await createOpenAPI(
path.resolve(__dirname, './openapi/auth-server.yaml')
)
const resourceServerSpec = await createOpenAPI(
path.resolve(__dirname, './openapi/resource-server.yaml')
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@wilsonianb wilsonianb Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As is, we get something like

    ResolverError: Error opening file "/home/brandon/rafiki/packages/auth/src/openapi/auth-server.yaml"

      at ReadFileContext.callback (node_modules/.pnpm/@apidevtools+json-schema-ref-parser@9.0.9/node_modules/@apidevtools/json-schema-ref-parser/lib/resolvers/file.js:52:20)

Are you thinking of having it mention the command to fetch the schemas?

Copy link
Contributor

@mkurapov mkurapov Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I was thinking, something along the lines of "Could not find Open API schema files. Did you run <command> ?"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long we remember not to change the command 😅

return {
authServerSpec,
resourceServerSpec
}
})
container.singleton('openPaymentsClient', async (deps) => {
const config = await deps.use('config')
Expand Down Expand Up @@ -170,10 +175,11 @@ export function initIocContainer(
})
container.singleton('authService', async (deps) => {
const config = await deps.use('config')
const { authServerSpec } = await deps.use('openApi')
return await createAuthService({
logger: await deps.use('logger'),
authServerIntrospectionUrl: config.authServerIntrospectionUrl,
authOpenApi: await deps.use('authOpenApi')
authServerSpec
})
})
container.singleton('paymentPointerService', async (deps) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/open_payments/auth/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ describe('Auth Middleware', (): void => {
type: AccessType.IncomingPayment,
action: AccessAction.Read
})
const authOpenApi = await deps.use('authOpenApi')
const { authServerSpec } = await deps.use('openApi')
requestPath = '/introspect'
validateRequest = authOpenApi.createRequestValidator({
validateRequest = authServerSpec.createRequestValidator({
path: requestPath,
method: HttpMethod.POST
})
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/open_payments/auth/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface AuthService {

interface ServiceDependencies {
authServerIntrospectionUrl: string
authOpenApi: OpenAPI
authServerSpec: OpenAPI
logger: Logger
validateResponse: ResponseValidator<TokenInfoJSON>
}
Expand All @@ -50,7 +50,7 @@ export async function createAuthService(
service: 'AuthService'
})
const validateResponse =
deps_.authOpenApi.createResponseValidator<TokenInfoJSON>({
deps_.authServerSpec.createResponseValidator<TokenInfoJSON>({
path: '/introspect',
method: HttpMethod.POST
})
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/open_payments/connection/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('Connection Routes', (): void => {
appContainer = await createTestApp(deps)
knex = await deps.use('knex')
grantReferenceService = await deps.use('grantReferenceService')
jestOpenAPI(await deps.use('openApi'))
const { resourceServerSpec } = await deps.use('openApi')
jestOpenAPI(resourceServerSpec)
})

const asset = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('Incoming Payment Routes', (): void => {
deps = await initIocContainer(config)
appContainer = await createTestApp(deps)
knex = await deps.use('knex')
jestOpenAPI(await deps.use('openApi'))
const { resourceServerSpec } = await deps.use('openApi')
jestOpenAPI(resourceServerSpec)
})

const asset = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ describe('Outgoing Payment Routes', (): void => {
config = await deps.use('config')
outgoingPaymentRoutes = await deps.use('outgoingPaymentRoutes')
outgoingPaymentService = await deps.use('outgoingPaymentService')
jestOpenAPI(await deps.use('openApi'))
const { resourceServerSpec } = await deps.use('openApi')
jestOpenAPI(resourceServerSpec)
})

beforeEach(async (): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ describe('Payment Pointer Routes', (): void => {
deps = await initIocContainer(config)
appContainer = await createTestApp(deps)
knex = await deps.use('knex')
jestOpenAPI(await deps.use('openApi'))
const { resourceServerSpec } = await deps.use('openApi')
jestOpenAPI(resourceServerSpec)
})

beforeEach(async (): Promise<void> => {
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/open_payments/quote/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ describe('Quote Routes', (): void => {
config = await deps.use('config')
quoteRoutes = await deps.use('quoteRoutes')
quoteService = await deps.use('quoteService')
jestOpenAPI(await deps.use('openApi'))
const { resourceServerSpec } = await deps.use('openApi')
jestOpenAPI(resourceServerSpec)
})

beforeEach(async (): Promise<void> => {
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/openapi/auth-server.yaml
3 changes: 2 additions & 1 deletion packages/backend/src/paymentPointerKey/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ describe('Payment Pointer Keys Routes', (): void => {
deps.bind('messageProducer', async () => mockMessageProducer)
appContainer = await createTestApp(deps)
knex = await deps.use('knex')
jestOpenAPI(await deps.use('openApi'))
const { resourceServerSpec } = await deps.use('openApi')
jestOpenAPI(resourceServerSpec)
paymentPointerKeyService = await deps.use('paymentPointerKeyService')
})

Expand Down