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: opt ssl create and code style #282

Merged
merged 2 commits into from
Aug 21, 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
Next Next commit
feat: opt ssl create
  • Loading branch information
skyoct committed Aug 21, 2022
commit f04867a4723b6f67f4209227f6ceac3739ebbd3a
26 changes: 2 additions & 24 deletions packages/gateway-controller/src/support/apisix-gateway-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,12 @@ export function initBaseRoute() {
export async function initBaseSSL() {
logger.info('start init base url')
initGlobalSSL()
initGlobalOssSSL()

// 每天查询一次ssl证书情况,判断是否需要更新
setInterval(async () => {
let ssl = await getGlobalSSL()
if (ssl == null || ssl.validity_end < new Date().getTime() / 1000) {
initGlobalSSL()
initGlobalOssSSL()
}
ssl = await getGlobalOssSSL()
if (ssl == null || ssl.validity_end < new Date().getTime() / 1000) {
initGlobalSSL()
initGlobalOssSSL()
}
}, 1000 * 60 * 60 * 24)

Expand Down Expand Up @@ -157,26 +150,11 @@ function initGlobalSSL() {
} catch (e) {
logger.error('read global ssl cert fail: {}', e)
}
ApiSixHttpUtils.putSSL(baseUrl, 'global_ssl', '*.' + Config.DEPLOY_DOMAIN, crt, key)
let ssl_domain = ['*.' + Config.DEPLOY_DOMAIN, '*.' + Config.DEPLOY_OSS_DOMAIN];
ApiSixHttpUtils.putSSL(baseUrl, 'global_ssl', ssl_domain, crt, key)
}

function initGlobalOssSSL() {
let crt = null
let key = null
try {
crt = fs.readFileSync('/ssl/oss.crt', 'utf8')
key = fs.readFileSync('/ssl/oss.key', 'utf8')
logger.info('load cert successful')
} catch (e) {
logger.error('read global ssl cert fail: {}', e)
}
ApiSixHttpUtils.putSSL(baseUrl, 'global_oss_ssl', '*.' + Config.DEPLOY_OSS_DOMAIN, crt, key)
}

async function getGlobalSSL() {
return await ApiSixHttpUtils.getSSL(baseUrl, 'global_ssl')
}

async function getGlobalOssSSL() {
return await ApiSixHttpUtils.getSSL(baseUrl, 'global_oss_ssl')
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ApiSixHttpUtils {
return resStatus
}

static async putSSL(url: string, id: string, sns: string, cert: string, key: string) {
static async putSSL(url: string, id: string, snis: Array<string>, cert: string, key: string) {
let resStatus = false

// check ssl cert valid date
Expand All @@ -53,7 +53,7 @@ export class ApiSixHttpUtils {
let data = {
cert: cert,
key: key,
snis: [sns],
snis: snis,
validity_end: new Date(certInfo.validTo).getTime() / 1000 - 3600 * 24 * 10,
}
await axios.put(url + '/apisix/admin/ssl/' + id, data, {
Expand Down