Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Made github-repo-access-refresh runnable as a job #10935

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion packages/server-core/src/k8s-job-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export const createExecutorJob = async (
jobBody: k8s.V1Job,
jobLabelSelector: string,
timeout: number,
jobId: string
jobId: string,
waitForFinish = true
) => {
const k8BatchClient = getState(ServerState).k8BatchClient

Expand All @@ -51,6 +52,7 @@ export const createExecutorJob = async (
await k8BatchClient.createNamespacedJob('default', jobBody)
let counter = 0
return new Promise((resolve, reject) => {
if (!waitForFinish) resolve({})
const interval = setInterval(async () => {
counter++

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,45 @@ import {
identityProviderPath,
IdentityProviderType
} from '@etherealengine/common/src/schemas/user/identity-provider.schema'
import * as k8s from '@kubernetes/client-node'

import { UserID } from '@etherealengine/common/src/schemas/user/user.schema'
import { Application } from '../../../declarations'
import { getJobBody } from '../../k8s-job-helper'
import { getUserRepos } from '../../projects/project/github-helper'
import logger from '../../ServerLogger'

export interface GithubRepoAccessRefreshParams extends KnexAdapterParams {}

export async function getGithubRepoAccessRefreshJobBody(
app: Application,
jobId: string,
userId: UserID
): Promise<k8s.V1Job> {
const command = [
'npx',
'cross-env',
'ts-node',
'--swc',
'scripts/refresh-gh-repo-access.ts',
'--userId',
userId,
'--jobId',
jobId
]

const labels = {
'etherealengine/ghRepoAccessRefresh': 'true',
'etherealengine/autoUpdate': 'false',
'etherealengine/userId': userId,
'etherealengine/release': process.env.RELEASE_NAME!
}

const name = `${process.env.RELEASE_NAME}-gh-repo-refresh-${userId.slice(0, 8)}-update`

return getJobBody(app, command, name, labels)
}

/**
* A class for Github Repo Access Refresh service
*/
Expand Down
34 changes: 31 additions & 3 deletions packages/server-core/src/user/strategies/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ import { AuthenticationRequest, AuthenticationResult } from '@feathersjs/authent
import { Paginated } from '@feathersjs/feathers'
import { random } from 'lodash'

import { apiJobPath } from '@etherealengine/common/src/schemas/cluster/api-job.schema'
import { avatarPath, AvatarType } from '@etherealengine/common/src/schemas/user/avatar.schema'
import { githubRepoAccessRefreshPath } from '@etherealengine/common/src/schemas/user/github-repo-access-refresh.schema'
import { identityProviderPath } from '@etherealengine/common/src/schemas/user/identity-provider.schema'
import { userApiKeyPath, UserApiKeyType } from '@etherealengine/common/src/schemas/user/user-api-key.schema'
import { InviteCode, UserName, userPath } from '@etherealengine/common/src/schemas/user/user.schema'
import { getDateTimeSql } from '@etherealengine/common/src/utils/datetime-sql'

import { Octokit } from 'octokit'
import { Application } from '../../../declarations'
import config from '../../appconfig'
import { createExecutorJob } from '../../k8s-job-helper'
import { RedirectConfig } from '../../types/OauthStrategies'
import getFreeInviteCode from '../../util/get-free-invite-code'
import makeInitialAdmin from '../../util/make-initial-admin'
import { getGithubRepoAccessRefreshJobBody } from '../github-repo-access-refresh/github-repo-access-refresh.class'
import CustomOAuthStrategy, { CustomOAuthParams } from './custom-oauth'

export class GithubStrategy extends CustomOAuthStrategy {
Expand All @@ -47,6 +51,24 @@ export class GithubStrategy extends CustomOAuthStrategy {
this.app = app
}

async createRefreshJob(userId) {
const date = await getDateTimeSql()
const newJob = await this.app.service(apiJobPath).create({
name: '',
startTime: date,
endTime: date,
returnData: '',
status: 'pending'
})

const jobBody = await getGithubRepoAccessRefreshJobBody(this.app, newJob.id, userId)
await this.app.service(apiJobPath).patch(newJob.id, {
name: jobBody.metadata!.name
})
const jobLabelSelector = `etherealengine/userId=${userId},etherealengine/release=${process.env.RELEASE_NAME},etherealengine/autoUpdate=false`
await createExecutorJob(this.app, jobBody, jobLabelSelector, 1000, newJob.id, false)
}

async getEntityData(profile: any, entity: any, params: CustomOAuthParams): Promise<any> {
const baseData = await super.getEntityData(profile, null, {})
const authResult = entity
Expand Down Expand Up @@ -129,7 +151,9 @@ export class GithubStrategy extends CustomOAuthStrategy {
if (entity.type !== 'guest' && identityProvider.type === 'guest') {
await this.app.service(identityProviderPath)._remove(identityProvider.id)
await this.app.service(userPath).remove(identityProvider.userId)
await this.app.service(githubRepoAccessRefreshPath).find(Object.assign({}, params, { user }))
if (!config.kubernetes.enabled)
await this.app.service(githubRepoAccessRefreshPath).find(Object.assign({}, params, { user }))
else await this.createRefreshJob(user.id)
await this.userLoginEntry(entity, params)

return super.updateEntity(entity, profile, params)
Expand All @@ -141,11 +165,15 @@ export class GithubStrategy extends CustomOAuthStrategy {
profile.oauthRefreshToken = params.refresh_token
const newIP = await super.createEntity(profile, params)
if (entity.type === 'guest') await this.app.service(identityProviderPath)._remove(entity.id)
await this.app.service(githubRepoAccessRefreshPath).find(Object.assign({}, params, { user }))
if (!config.kubernetes.enabled)
await this.app.service(githubRepoAccessRefreshPath).find(Object.assign({}, params, { user }))
else await this.createRefreshJob(user.id)
await this.userLoginEntry(newIP, params)
return newIP
} else if (existingEntity.userId === identityProvider.userId) {
await this.app.service(githubRepoAccessRefreshPath).find(Object.assign({}, params, { user }))
if (!config.kubernetes.enabled)
await this.app.service(githubRepoAccessRefreshPath).find(Object.assign({}, params, { user }))
else await this.createRefreshJob(user.id)
await this.userLoginEntry(existingEntity, params)
return existingEntity
} else {
Expand Down
78 changes: 78 additions & 0 deletions scripts/refresh-gh-repo-access.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
CPAL-1.0 License

The contents of this file are subject to the Common Public Attribution License
Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.

The Original Code is Ethereal Engine.

The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Ethereal Engine team.

All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
Ethereal Engine. All Rights Reserved.
*/

import appRootPath from 'app-root-path'
import cli from 'cli'
import dotenv from 'dotenv-flow'

import { apiJobPath, githubRepoAccessRefreshPath, userPath } from '@etherealengine/common/src/schema.type.module'
import { getDateTimeSql } from '@etherealengine/common/src/utils/datetime-sql'
import { ServerMode } from '@etherealengine/server-core/src/ServerState'
import { createFeathersKoaApp, serverJobPipe } from '@etherealengine/server-core/src/createApp'
import { updateAppConfig } from '@etherealengine/server-core/src/updateAppConfig'

dotenv.config({
path: appRootPath.path,
silent: true
})

const db = {
username: process.env.MYSQL_USER ?? 'server',
password: process.env.MYSQL_PASSWORD ?? 'password',
database: process.env.MYSQL_DATABASE ?? 'etherealengine',
host: process.env.MYSQL_HOST ?? '127.0.0.1',
port: process.env.MYSQL_PORT ?? 3306,
dialect: 'mysql',
url: ''
}

db.url = process.env.MYSQL_URL ?? `mysql://${db.username}:${db.password}@${db.host}:${db.port}/${db.database}`

cli.enable('status')

const options = cli.parse({
userId: [false, 'ID of user updating project', 'string'],
jobId: [false, 'ID of Job record', 'string']
})

cli.main(async () => {
try {
await updateAppConfig()
const app = createFeathersKoaApp(ServerMode.API, serverJobPipe)
await app.setup()
const { userId, jobId } = options
const user = await app.service(userPath).get(userId)
await app.service(githubRepoAccessRefreshPath).find(Object.assign({}, {}, { user }))
const date = await getDateTimeSql()
await app.service(apiJobPath).patch(jobId, {
status: 'succeeded',
endTime: date
})
cli.exit(0)
} catch (err) {
console.log(err)
cli.fatal(err)
}
})
Loading