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 tls config for ingress gateway #1569

Merged
merged 1 commit into from
Oct 10, 2023
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
4 changes: 4 additions & 0 deletions build/charts/laf-server/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ spec:
value: {{ .Values.default_region.runtime_domain }}
- name: DEFAULT_REGION_WEBSITE_DOMAIN
value: {{ .Values.default_region.website_domain }}
- name: DEFAULT_REGION_TLS_ENABLED
value: {{ .Values.default_region.tls.enabled }}
- name: DEFAULT_REGION_TLS_WILDCARD_CERTIFICATE_SECRET_NAME
value: {{ .Values.default_region.tls.wildcard_certificate_secret_name }}
- name: DEFAULT_REGION_LOG_SERVER_URL
value: {{ .Values.default_region.log_server_url }}
- name: DEFAULT_REGION_LOG_SERVER_SECRET
Expand Down
4 changes: 3 additions & 1 deletion build/charts/laf-server/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ default_region:
minio_root_access_key: ""
minio_root_secret_key: ""
# gateway conf
tls: false
tls:
enabled: false
wildcard_certificate_secret_name: ""
runtime_domain: ""
website_domain: ""
# log-server
Expand Down
2 changes: 1 addition & 1 deletion build/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ helm install server -n ${NAMESPACE} \
--set default_region.minio_root_secret_key=${MINIO_ROOT_SECRET_KEY} \
--set default_region.runtime_domain=${DOMAIN} \
--set default_region.website_domain=site.${DOMAIN} \
--set default_region.tls=false \
--set default_region.tls.enabled=false \
--set default_region.log_server_url=${LOG_SERVER_URL} \
--set default_region.log_server_secret=${LOG_SERVER_SECRET} \
--set default_region.log_server_database_url=${LOG_SERVER_DATABASE_URL} \
Expand Down
18 changes: 16 additions & 2 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,25 @@ export class ServerConfig {
}

static get DEFAULT_REGION_RUNTIME_DOMAIN() {
return process.env.DEFAULT_REGION_RUNTIME_DOMAIN || 'localhost'
if (!process.env.DEFAULT_REGION_RUNTIME_DOMAIN) {
throw new Error('DEFAULT_REGION_RUNTIME_DOMAIN is not defined')
}
return process.env.DEFAULT_REGION_RUNTIME_DOMAIN
}

static get DEFAULT_REGION_WEBSITE_DOMAIN() {
return process.env.DEFAULT_REGION_WEBSITE_DOMAIN || 'localhost'
if (!process.env.DEFAULT_REGION_WEBSITE_DOMAIN) {
throw new Error('DEFAULT_REGION_WEBSITE_DOMAIN is not defined')
}
return process.env.DEFAULT_REGION_WEBSITE_DOMAIN
}

static get DEFAULT_REGION_TLS_ENABLED() {
return process.env.DEFAULT_REGION_TLS_ENABLED === 'true'
}

static get DEFAULT_REGION_TLS_WILDCARD_CERTIFICATE_SECRET_NAME() {
return process.env.DEFAULT_REGION_TLS_WILDCARD_CERTIFICATE_SECRET_NAME
}

static get DEFAULT_REGION_MINIO_DOMAIN() {
Expand Down
21 changes: 15 additions & 6 deletions server/src/gateway/ingress/runtime-ingress.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { V1Ingress } from '@kubernetes/client-node'
import { V1Ingress, V1IngressTLS } from '@kubernetes/client-node'
import { Injectable, Logger } from '@nestjs/common'
import { LABEL_KEY_APP_ID } from 'src/constants'
import { ClusterService } from 'src/region/cluster/cluster.service'
Expand Down Expand Up @@ -55,11 +55,20 @@ export class RuntimeGatewayService {
})

// build tls
const tls = []
if (runtimeDomain.customDomain) {
const secretName =
this.certificate.getRuntimeCertificateName(runtimeDomain)
tls.push({ secretName, hosts })
const tls: Array<V1IngressTLS> = []
if (region.gatewayConf.tls.enabled) {
// add wildcardDomain tls
if (region.gatewayConf.tls.wildcardCertificateSecretName) {
const secretName = region.gatewayConf.tls.wildcardCertificateSecretName
tls.push({ secretName, hosts: [runtimeDomain.domain] })
}

// add customDomain tls
if (runtimeDomain.customDomain) {
const secretName =
this.certificate.getRuntimeCertificateName(runtimeDomain)
tls.push({ secretName, hosts: [runtimeDomain.customDomain] })
}
}

// create ingress
Expand Down
24 changes: 21 additions & 3 deletions server/src/gateway/ingress/website-ingress.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { V1Ingress, V1IngressRule } from '@kubernetes/client-node'
import { V1Ingress, V1IngressRule, V1IngressTLS } from '@kubernetes/client-node'
import { Injectable, Logger } from '@nestjs/common'
import { LABEL_KEY_APP_ID } from 'src/constants'
import { ClusterService } from 'src/region/cluster/cluster.service'
import { Region } from 'src/region/entities/region'
import { GetApplicationNamespace } from 'src/utils/getter'
import { WebsiteHosting } from 'src/website/entities/website'
import { CertificateService } from '../certificate.service'

@Injectable()
export class WebsiteHostingGatewayService {
private readonly logger = new Logger(WebsiteHostingGatewayService.name)

constructor(private readonly clusterService: ClusterService) {}
constructor(
private readonly clusterService: ClusterService,
private readonly certificate: CertificateService,
) {}

getIngressName(websiteHosting: WebsiteHosting) {
return websiteHosting._id.toString()
Expand Down Expand Up @@ -44,6 +48,20 @@ export class WebsiteHostingGatewayService {
http: { paths: [{ path: '/', pathType: 'Prefix', backend }] },
}

// build tls
const tls: Array<V1IngressTLS> = []
if (region.gatewayConf.tls.enabled) {
if (website.isCustom) {
// add custom domain tls
const secretName = this.certificate.getWebsiteCertificateName(website)
tls.push({ secretName, hosts: [website.domain] })
} else if (region.gatewayConf.tls.wildcardCertificateSecretName) {
// add wildcardDomain tls
const secretName = region.gatewayConf.tls.wildcardCertificateSecretName
tls.push({ secretName, hosts: [website.domain] })
}
}

// create ingress
const ingressClassName = region.gatewayConf.driver
const ingressBody: V1Ingress = {
Expand All @@ -62,7 +80,7 @@ export class WebsiteHostingGatewayService {
'nginx.ingress.kubernetes.io/enable-cors': 'true',
},
},
spec: { ingressClassName, rules: [rule] },
spec: { ingressClassName, rules: [rule], tls },
}

const res = await this.clusterService.createIngress(region, ingressBody)
Expand Down
4 changes: 1 addition & 3 deletions server/src/gateway/runtime-domain.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export class RuntimeDomainService {
async checkResolved(appid: string, customDomain: string) {
const runtimeDomain = await this.db
.collection<RuntimeDomain>('RuntimeDomain')
.findOne({
appid,
})
.findOne({ appid })

const cnameTarget = runtimeDomain.domain

Expand Down
1 change: 1 addition & 0 deletions server/src/gateway/website-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class WebsiteTaskService {
assert(bucketDomain, 'bucket domain not found')

// create website custom certificate if custom domain is set
// Warning: create certificate before ingress, otherwise apisix ingress will not work
if (site.isCustom && region.gatewayConf.tls.enabled) {
const waitingTime = Date.now() - site.updatedAt.getTime()

Expand Down
7 changes: 4 additions & 3 deletions server/src/initializer/initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ export class InitializerService {
websiteDomain: ServerConfig.DEFAULT_REGION_WEBSITE_DOMAIN,
port: 80,
tls: {
enabled: false,
issuerRef: { name: 'laf-issuer', kind: 'ClusterIssuer' },
wildcardCertificateSecretName: null,
enabled: ServerConfig.DEFAULT_REGION_TLS_ENABLED,
issuerRef: { name: 'laf-issuer', kind: 'Issuer' },
wildcardCertificateSecretName:
ServerConfig.DEFAULT_REGION_TLS_WILDCARD_CERTIFICATE_SECRET_NAME,
},
},
logServerConf: {
Expand Down