Skip to content

Commit

Permalink
fix(app-service): change debug-token to bearer token;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Apr 26, 2022
1 parent 3ebeee1 commit d4901f1
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 22 deletions.
6 changes: 3 additions & 3 deletions packages/app-console/src/api/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ export function compileFunctionCode(func_id, function_data) {
/**
* Debug cloud function
*/
export async function launchFunction(func, param, debug = false) {
export async function launchFunction(func, param, debug_token) {
const app_url = getAppAccessUrl()
const res = await axios({
url: app_url + `/func/debug/${func.name}`,
url: app_url + `/debug/${func.name}`,
method: 'post',
data: {
func,
param
},
headers: {
'debug-token': debug
'Authorization': `Bearer ${debug_token}`
}
})

Expand Down
2 changes: 1 addition & 1 deletion packages/app-console/src/views/cloudfunction/debug.vue
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export default {
const res = await launchFunction(r.data, param, debug_token)
.finally(() => { this.loading = false })
this.invokeRequestId = res.headers['requestid']
this.invokeRequestId = res.headers['x-request-id'] || res.headers['request-id'] || res.headers['requestid'] || res.headers['requestId']
await this.getLogByRequestId(this.invokeRequestId)
.finally(() => { this.loading = false })
Expand Down
4 changes: 0 additions & 4 deletions packages/app-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
FROM node:16-alpine
RUN apk add --no-cache openssl
ENV DOCKERIZE_VERSION v0.6.1
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz

RUN npm install npm -g

Expand Down
6 changes: 2 additions & 4 deletions packages/app-service/src/handler/debug-func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { Request, Response } from 'express'
import { FunctionContext } from 'cloud-function-engine'
import { parseToken } from '../support/token'
import { logger } from '../support/logger'
import { addFunctionLog } from '../support/function'
import { ObjectId } from 'bson'
Expand All @@ -20,7 +19,6 @@ export async function handleDebugFunction(req: Request, res: Response) {

const requestId = req['requestId']
const func_name = req.params?.name
const debug_token = req.get('debug-token') ?? undefined
const func_data = req.body?.func
const param = req.body?.param

Expand All @@ -29,8 +27,8 @@ export async function handleDebugFunction(req: Request, res: Response) {
}

// verify the debug token
const parsed = parseToken(debug_token as string)
if (!parsed || parsed.type !== 'debug') {
const auth = req['auth']
if (!auth || auth.type !== 'debug') {
return res.status(403).send('permission denied: invalid debug token')
}

Expand Down
8 changes: 1 addition & 7 deletions packages/app-service/src/handler/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,12 @@ export const router = Router()
router.post('/proxy/:policy', handleDatabaseProxy)
router.get('/typing/package', handlePackageTypings)

/**
* Debug cloud function through HTTP request.
* @deprecated compatible for history versions
* @method POST
*/
router.post('/func/debug/:name', uploader.any(), handleDebugFunction)

/**
* Debug cloud function through HTTP request.
* @method POST
*/
router.post('/debug/:name', uploader.any(), handleDebugFunction)
router.all('/debug/:name', uploader.any(), handleDebugFunction)


/**
Expand Down
5 changes: 2 additions & 3 deletions packages/app-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@ process.on('uncaughtException', err => {
/**
* Parsing bearer token
*/
app.use(function (req, res, next) {
app.use(function (req, _res, next) {
const token = splitBearerToken(req.headers['authorization'] ?? '')
const auth = parseToken(token) || null
req['auth'] = auth

const requestId = req['requestId'] = generateUUID()
const requestId = req['requestId'] = req.headers['x-request-id'] || generateUUID()
if (req.url !== '/healthz') {
logger.info(requestId, `${req.method} "${req.url}" - referer: ${req.get('referer') || '-'} ${req.get('user-agent')}`)
logger.trace(requestId, `${req.method} ${req.url}`, { body: req.body, headers: req.headers, auth })
}
res.set('requestId', requestId)
next()
})

Expand Down

0 comments on commit d4901f1

Please sign in to comment.