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(server): add function debug token api #527

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 21 additions & 16 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"dotenv": "^16.0.3",
"fast-json-patch": "^3.1.1",
"mongodb": "^4.12.1",
"ms": "^2.1.3",
"nanoid": "^3.3.4",
"passport": "^0.6.0",
"passport-jwt": "^4.0.0",
Expand Down
9 changes: 7 additions & 2 deletions server/src/applications/applications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { CreateApplicationDto } from './dto/create-application.dto'
import { UpdateApplicationDto } from './dto/update-application.dto'
import { ApplicationsService } from './applications.service'
import { ApplicationCoreService } from 'src/core/application.cr.service'
import { ServerConfig } from 'src/constants'

@ApiTags('Application')
@Controller('applications')
Expand Down Expand Up @@ -83,14 +84,17 @@ export class ApplicationsController {
// get sub resources
const resources = await this.appService.getSubResources(appid)

const data = await this.appService.findOne(appid)
const sts = await this.appService.getApplicationSTS(appid, resources.oss)
const data = await this.appService.findOne(appid, { configuration: true })
const sts = await this.appService.getOssSTS(appid, resources.oss)
const credentials = {
endpoint: ServerConfig.OSS_ENDPOINT,
accessKeyId: sts.Credentials?.AccessKeyId,
secretAccessKey: sts.Credentials?.SecretAccessKey,
sessionToken: sts.Credentials?.SessionToken,
expiration: sts.Credentials?.Expiration,
}

const debug_token = await this.appService.getDebugFunctionToken(appid)
const res = {
...data,
gateway: resources.gateway,
Expand All @@ -99,6 +103,7 @@ export class ApplicationsController {
...resources.oss,
credentials,
},
function_debug_token: debug_token,
}

return ResponseUtil.ok(res)
Expand Down
2 changes: 2 additions & 0 deletions server/src/applications/applications.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ApplicationsService } from './applications.service'
import { PrismaService } from '../prisma.service'
import { ApplicationTaskService } from './application-task.service'
import { InstanceService } from 'src/instance/instance.service'
import { JwtService } from '@nestjs/jwt'

@Module({
imports: [CoreModule],
Expand All @@ -15,6 +16,7 @@ import { InstanceService } from 'src/instance/instance.service'
PrismaService,
ApplicationTaskService,
InstanceService,
JwtService,
],
exports: [ApplicationsService],
})
Expand Down
30 changes: 27 additions & 3 deletions server/src/applications/applications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { OSSUserCoreService } from 'src/core/oss-user.cr.service'
import { APPLICATION_SECRET_KEY, ServerConfig } from 'src/constants'
import { GenerateAlphaNumericPassword } from 'src/common/random'
import { OSSUser } from 'src/core/api/oss-user.cr'
import * as assert from 'node:assert'
import { JwtService } from '@nestjs/jwt'

@Injectable()
export class ApplicationsService {
Expand All @@ -20,6 +22,7 @@ export class ApplicationsService {
private readonly databaseCore: DatabaseCoreService,
private readonly gatewayCore: GatewayCoreService,
private readonly ossCore: OSSUserCoreService,
private readonly jwtService: JwtService,
) {}

async create(userid: string, dto: CreateApplicationDto) {
Expand Down Expand Up @@ -160,7 +163,7 @@ export class ApplicationsService {
* @param app
* @returns
*/
public getSTSClient(oss: OSSUser) {
private getSTSClient(oss: OSSUser) {
return new STSClient({
endpoint: ServerConfig.OSS_ENDPOINT,
credentials: {
Expand All @@ -172,12 +175,12 @@ export class ApplicationsService {
}

/**
* Generate application full-granted STS
* Generate application full-granted OSS STS
* @param bucket
* @param duration_seconds
* @returns
*/
public async getApplicationSTS(
public async getOssSTS(
appid: string,
user: OSSUser,
duration_seconds?: number,
Expand Down Expand Up @@ -209,4 +212,25 @@ export class ApplicationsService {
}
return JSON.stringify(policy)
}

async getDebugFunctionToken(appid: string) {
const conf = await this.prisma.applicationConfiguration.findUnique({
where: { appid },
})

// get secret from envs
const secret = conf?.environments.find(
(env) => env.name === APPLICATION_SECRET_KEY,
)
assert(secret?.value, 'application secret not found')

// generate token
const exp = Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7 // 7 days

const token = this.jwtService.sign(
{ appid, type: 'debug', exp },
{ secret: secret.value },
)
return token
}
}
3 changes: 1 addition & 2 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ async function bootstrap() {
.setTitle('Open API Documentation of laf')
.setDescription('`The APIs of laf server`')
.setVersion('1.0.alpha')
.addServer('http://localhost:3000', 'local server')
.addServer(ServerConfig.SERVER, 'current server')
.addServer('http://dev.server:3000', 'dev server')
.addServer(ServerConfig.SERVER, 'prod server')
.addBearerAuth(
{ type: 'http', scheme: 'bearer', bearerFormat: 'JWT', in: 'header' },
'Authorization',
Expand Down