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: add ssl support #179

Merged
merged 1 commit into from
Jul 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
1 change: 1 addition & 0 deletions deploy/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ services:
GATEWAY_TYPE: apisix
SYS_DB_URI: mongodb://${SYS_DB_USER}:${SYS_DB_PASSWORD}@mongo:27017/?authSource=${SYS_DB}&replicaSet=laf&writeConcern=majority
API_SIX_KEY: ${API_SIX_KEY}
APP_SERVICE_DEPLOY_URL_SCHEMA: ${APP_SERVICE_DEPLOY_URL_SCHEMA}
networks:
- laf_shared_network

Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ services:
- ./gateway_conf.yaml:/usr/local/apisix/conf/config.yaml:ro
ports:
- 8080:9080
- 9443:9443
networks:
- laf_shared_network

Expand All @@ -140,10 +141,12 @@ services:
GATEWAY_TYPE: apisix
SYS_DB_URI: mongodb://my_user:password123@mongo:27017/?authSource=laf-sys&replicaSet=laf&writeConcern=majority
API_SIX_KEY: edd1c9f034335f136f87ad84b625c8f1
APP_SERVICE_DEPLOY_URL_SCHEMA: 'http'
command: node ./dist/index.js
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./packages/gateway-controller:/app
- ./cert:/ssl
depends_on:
- gateway
restart: always
Expand Down
9 changes: 9 additions & 0 deletions packages/gateway-controller/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,13 @@ export default class Config {
return process.env['DEPLOY_OSS_DOMAIN']
}

/**
* The schema of app deployed url: `http` | `https`.
* Default value is `http`.
*/
static get APP_SERVICE_DEPLOY_URL_SCHEMA(): string {
return process.env.APP_SERVICE_DEPLOY_URL_SCHEMA ?? 'http'
}


}
6 changes: 5 additions & 1 deletion packages/gateway-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Config from './config'
import {logger} from './support/logger'
import {DatabaseAgent} from './support/db'
import {start_scheduler} from './scheduler'
import {initBaseRoute} from "./support/apisix-gateway-init";
import {initBaseRoute, initBaseSSL} from "./support/apisix-gateway-init";

DatabaseAgent.init(Config.SYS_DB_URI)

Expand All @@ -17,6 +17,10 @@ app.get('/healthz', (_req, res) => {
})
// init base route
initBaseRoute()
// init base ssl
if (Config.APP_SERVICE_DEPLOY_URL_SCHEMA) {
initBaseSSL()
}

start_scheduler()

Expand Down
23 changes: 23 additions & 0 deletions packages/gateway-controller/src/support/apisix-gateway-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
*/
import Config from "../config";
import {ApiSixHttpUtils} from "./apisix-gateway-utils";
import {logger} from "./logger";

const fs = require('fs');

const baseUrl = 'http://gateway:9080'

export function initBaseRoute() {
logger.info('start init base route')
initSystemClientRoute()
initAppConsoleRoute()
initSysApiRoute()
initOssRoute()
initOssSubDomainRoute()
}

export function initBaseSSL() {
logger.info('start init base url')
initGlobalSSL()
}


function initSystemClientRoute() {
let data = {
Expand Down Expand Up @@ -119,4 +128,18 @@ function initOssSubDomainRoute() {
}
}
ApiSixHttpUtils.put(baseUrl, 'base_oss_sub_domain', data)
}


function initGlobalSSL() {
let crt = null
let key = null
try {
crt = fs.readFileSync('/ssl/global.crt','utf8')
key = fs.readFileSync('/ssl/global.key','utf8')
logger.info('load cert successful')
} catch (e) {
logger.error('read global ssl cert fail: {}', e)
}
ApiSixHttpUtils.putSSL(baseUrl, 'global_ssl', '*.' + Config.DEPLOY_DOMAIN, crt, key)
}
36 changes: 36 additions & 0 deletions packages/gateway-controller/src/support/apisix-gateway-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,40 @@ export class ApiSixHttpUtils {
return resStatus
}

static async putSSL(url: string, id: string, sns: string, cert: string, key: string) {
let resStatus = false
let data = {
cert: cert,
key: key,
snis: [sns],
labels: {'update_time': new Date().getTime() + ''}
}
await axios.put(url + '/apisix/admin/ssl/' + id, data, {
headers: this.headers,
})
.then(_ => {
logger.info('create ssl successful')
resStatus = true
})
.catch(err => {
logger.info('create sll failed: ', err)
})
return resStatus
}

static async deleteSSL(url: string, id: string) {
let resStatus = false
await axios.delete(url + '/apisix/admin/ssl/' + id, {
headers: this.headers,
})
.then(_ => {
logger.info('delete ssl successful')
resStatus = true
})
.catch(err => {
logger.info('delete ssl failed: ', err)
})
return resStatus
}

}