UNPKG

@sap/cds-dk

Version:

Command line client and development toolkit for the SAP Cloud Application Programming Model

101 lines (87 loc) 4.08 kB
const cds = require('../../../cds') const { exists, read } = cds.utils const { readProject } = require('../../projectReader') const { merge } = require('../../merge') const { srv4, xsuaa: xsuaaDefault, xsuaa4, mtxSidecar4 } = require('../../registries/mta') const KymaTemplate = require('../kyma') 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 = !exists(configFile) || typeof (await read(configFile)).cds?.requires?.auth !== 'object' project.profile ??= 'production' await merge(__dirname, 'files/package.json.hbs').into(configFile, { project, forceOverwrite: true }) } async combine() { const project = readProject() const { addMultitenancy, hasMultitenancy, addMta, hasMta, addKyma, isJava, srvPath, hasKyma } = 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 { /* 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 (addMta || addMultitenancy && hasMta) { const additions = [], overwrites = [], relationships = [] const { plan } = cds.cli.options const xsuaa = plan ? xsuaa4(plan) : xsuaaDefault additions.push(xsuaa) if (addMta) { const srv = srv4(srvPath) relationships.push({ insert: [xsuaa, 'name'], into: [srv, 'requires', 'name'] }) } if (hasMultitenancy) { const mtxSidecar = mtxSidecar4(isJava ? 'mtx/sidecar' : 'gen/mtx/sidecar') overwrites.push({ item: [xsuaa, 'parameters.config.tenant-mode'], withValue: 'shared' }) relationships.push({ insert: [xsuaa, 'name'], into: [mtxSidecar, 'requires', 'name'] }) } if (plan) project.servicePlan = plan project.hasRoles = xsSecurity['role-templates'].length > 0 project.roles = xsSecurity['role-templates'].map(role => role) if (!project.hasCustomRoleCollections) { for (const role of xsSecurity['role-templates']) { additions.push({ in: [xsuaa, 'parameters.role-collections'], where: { 'role-template-references': ['XSAPPNAME'+role.name] } }) } } await merge(__dirname, 'files/mta.yaml.hbs').into('mta.yaml', { project, additions, overwrites, relationships }) } if (addKyma || addMultitenancy && hasKyma) { const overwrites = addMultitenancy ? [{ item: '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') await KymaTemplate.mergeDependency('service-instance', 'xsuaa') } } }