@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
82 lines (70 loc) • 4.25 kB
JavaScript
const { readProject } = require('../../projectReader')
const { merge, sort } = require('../../merge')
const { srv4, approuter, ias, mtxSidecar4 } = require('../../registries/mta')
const { exists, read } = require('../../../cds').utils
module.exports = class IasTemplate extends require('../../plugin') {
static help() {
return 'authentication via IAS'
}
static hasInProduction(env) {
return env.requires?.auth?.kind === 'ias'
}
async run() {
const project = readProject()
const { configFile, isNodejs } = project
project.profile ??= 'production'
project.shortcut = exists(configFile) ? typeof (await read(configFile)).cds?.requires?.db !== 'object' : true
await merge(__dirname, 'files/package.json.hbs').into(configFile, { project, forceOverwrite: true })
if (isNodejs) await sort('package.json', 'dependencies')
}
async combine() {
const project = readProject()
const { isJava, addMta, hasMta, addCfManifest, addKyma, addMultitenancy, hasApprouter, hasMultitenancy, srvPath } = project
if (addMultitenancy) {
const conf = exists('mtx/sidecar/package.json') ? await read('mtx/sidecar/package.json') : {}
conf.cds ??= {}
const profile = conf.cds.profile
if (profile) delete conf.cds.profile
conf.cds.profiles ??= profile ? [profile] : []
if (!conf.cds.profiles.includes('subscription-manager')) {
conf.cds.profiles.push('subscription-manager')
}
await merge(conf).into('mtx/sidecar/package.json')
await merge(__dirname, 'files/sidecar.package.json.hbs').into('mtx/sidecar/package.json')
}
if (addMta || (hasApprouter || hasMultitenancy) && hasMta) {
const srv = srv4(srvPath)
const srvIdentityBinding = { in: [srv, 'requires'], where: { 'parameters.config.app-identifier': 'srv' } }
const srvCertUrlApi = { in: [srv, 'provides'], where: { 'name': 'srv-api' } }
const additions = [srv, ias, srvIdentityBinding, srvCertUrlApi]
const overwrites = []
if (hasApprouter) {
additions.push(approuter)
additions.push({ in: [approuter, 'requires'], where: { 'parameters.config.app-identifier': 'approuter' }})
const approuterSrvApi = { in: [approuter, 'requires'], where: { 'name': 'srv-api' } }
additions.push(approuterSrvApi)
overwrites.push({ item: [approuterSrvApi, 'properties.url'], withValue: '~{srv-cert-url}' })
overwrites.push({ item: [approuterSrvApi, 'properties.forwardAuthCertificates'], withValue: true })
overwrites.push({ item: [approuterSrvApi, 'properties.strictSSL'], withValue: true })
}
if (hasMultitenancy) {
const mtxSidecar = mtxSidecar4(isJava ? 'mtx/sidecar' : 'gen/mtx/sidecar')
const mtxIdentityBinding = { in: [mtxSidecar, 'requires'], where: { 'parameters.config.app-identifier': 'mtx' } }
additions.push(mtxSidecar, mtxIdentityBinding)
}
await merge(__dirname, 'files/mta.yaml.hbs').into('mta.yaml', { project, additions, overwrites })
}
if (addKyma) {
const identity = { in: 'dependencies', where: { 'alias': 'identity' } }
await merge(__dirname, 'files/Chart.yaml.hbs').into('chart/Chart.yaml', { project, additions: [identity] })
await merge(__dirname, 'files/values.yaml.hbs').into('chart/values.yaml', { project })
}
if (addCfManifest) {
const srv = { in: 'applications', where: { 'name': `${project.appName}-srv` } }
const identityBinding = { in: 'services', where: { 'parameters.app-identifier': 'srv' } }
await merge(__dirname, 'files/manifest.yml.hbs').into('manifest.yml', { project, additions: [srv, identityBinding] })
const identity = { in: 'create-services', where: { 'broker': 'identity' } }
await merge(__dirname, 'files/services-manifest.yml.hbs').into('services-manifest.yml', { project, additions: [identity] })
}
}
}