@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
86 lines (75 loc) • 3.41 kB
JavaScript
const cds = require('../../../cds')
const { read } = cds.utils
const { readProject } = require('../../projectReader')
const { merge } = require('../../merge')
const { srv4, xsuaa: xsuaaDefault, xsuaa4, mtxSidecar4 } = require('../../registries/mta')
module.exports = class XsuaaTemplate extends require('../../plugin') {
options() {
return {
'plan': {
type: 'string',
help: 'Specify the service plan.',
}
}
}
static help() {
return 'authentication via XSUAA'
}
static hasInProduction(env) {
return env.requires?.auth?.kind === 'xsuaa' || cds.cli.options?.add?.has('xsuaa') || false
}
async run() {
const project = readProject()
const { configFile } = project
project.shortcut = typeof (await read(configFile)).cds?.requires?.auth !== 'object'
await merge(__dirname, 'files/package.json.hbs').into(configFile, { project, forceOverwrite: true })
}
async combine() {
const project = readProject()
const { hasMultitenancy, hasMta, hasHelm, hasHelmUnifiedRuntime, isJava, srvPath } = project
const cds = require('../../../../lib')
const xsSecurity = await (async () => {
try {
const models = await cds.load(cds.env.folders.srv)
return cds.compile.to.xsuaa(models)
} catch (error) { /* ignore */ }
})() ?? { scopes: [], attributes: [], 'role-templates': [], 'authorities-inheritance': false }
const additions = xsSecurity.scopes.map(scope => ({ in: 'scopes', where: { name: scope.name } }))
await merge(xsSecurity).into('xs-security.json', { project, additions })
if (hasMta) {
const srv = srv4(srvPath)
const { plan } = cds.cli.options
const xsuaa = plan ? xsuaa4(plan) : xsuaaDefault
const additions = [srv, xsuaa]
const mtxSidecar = mtxSidecar4(isJava ? 'mtx/sidecar' : 'gen/mtx/sidecar')
if (hasMultitenancy && isJava) additions.push(mtxSidecar)
const overwrites = hasMultitenancy ? [{ item: [xsuaa, 'parameters.config.tenant-mode'], withValue: 'shared' }] : []
const relationships = [{
insert: [xsuaa, 'name'],
into: [srv, 'requires', 'name']
}]
if (hasMultitenancy) relationships.push({
insert: [xsuaa, 'name'],
into: [mtxSidecar, 'requires', 'name']
})
if (plan) project.servicePlan = plan
await merge(__dirname, 'files/mta.yaml.hbs').into('mta.yaml', { project, additions, overwrites, relationships })
}
if (hasHelm || hasHelmUnifiedRuntime) {
const overwrites = hasMultitenancy ? [{
item: [{
in: 'xsuaa',
where: { serviceOfferingName: 'xsuaa' }
}, 'parameters.tenant-mode'], withValue: 'shared'
}] : []
await merge(__dirname, 'files/values.yaml.hbs').into('chart/values.yaml', { with: project, overwrites })
await merge({
xsuaa: {
parameters: {
xsappname: `${project.appName}-{{ .Release.Namespace }}`
}
}
}).into('chart/values.yaml')
}
}
}