Skip to content

Commit

Permalink
Merge pull request #116 from azure-ad-b2c/v5.3Features
Browse files Browse the repository at this point in the history
V5.3 features
  • Loading branch information
yoelhor authored Feb 27, 2022
2 parents ceb2d7f + acc4dbb commit 126e63d
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 56 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"project": "./tsconfig.json"
},
"rules": {
"no-shadow": "off",
"i18n-text/no-en": 0,
"import/named": "warn",
"github/no-then": "warn",
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ inputs:
renumberSteps:
description: 'Renumber the orchestration steps. Possible values: true, or false'
required: false
default: 'false'
addAppInsightsStep:
description: 'Add App Insights orchestration steps to the the user journeys.'
required: false
default: 'false'
verbose:
description: 'Log level verbose.'
required: false
default: 'false'
runs:
using: 'node12'
main: 'dist/index.js'
99 changes: 74 additions & 25 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

114 changes: 84 additions & 30 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,116 @@ const fg = require('fast-glob')
// XML parsing
const { DOMParser } = require('xmldom')

async function run(): Promise<void> {
try {
const folder = core.getInput('folder')
const files = core.getInput('files')
const tenant = core.getInput('tenant')
const clientId = core.getInput('clientId')
const clientSecret = core.getInput('clientSecret')
const addAppInsightsStep = core.getInput('addAppInsightsStep')
const renumberSteps = core.getInput('renumberSteps')
enum DeploymentType {
None,
All,
CommaDelimiter,
JSON
}

class Settings {
folder = ''
files: any
tenant = ''
clientId = ''
clientSecret = ''
addAppInsightsStep = false
renumberSteps = false
verbose = false
}

core.info('Deploy custom policy GitHub Action v5.2 started.')
async function run(): Promise<void> {

if (clientId === 'test') {
const settings: Settings = new Settings()
try {
settings.folder = core.getInput('folder')
settings.files = core.getInput('files')
settings.tenant = core.getInput('tenant')
settings.clientId = core.getInput('clientId')
settings.clientSecret = core.getInput('clientSecret')
settings.addAppInsightsStep = core.getInput('addAppInsightsStep') === true || core.getInput('addAppInsightsStep') === 'true'
settings.renumberSteps = core.getInput('renumberSteps') === true || core.getInput('renumberSteps') === 'true'
settings.verbose = core.getInput('verbose') === true || core.getInput('verbose') === 'true'
let deploymentType = DeploymentType.None

core.info('Deploy custom policy GitHub Action v5.3 started.')

if (settings.clientId === 'test') {
core.info('GitHub Action test successfully completed.')
return
}

if (clientId === null || clientId === undefined || clientId === '') {
if (settings.clientId === '') {
core.setFailed("The 'clientId' parameter is missing.")
}

if (folder === null || folder === undefined || folder === '') {
if (settings.folder === '') {
core.setFailed("The 'folder' parameter is missing.")
}

if (files === null || files === undefined || files === '') {
if (settings.files === '') {
core.setFailed("The 'files' parameter is missing.")
}

if (tenant === null || tenant === undefined || tenant === '') {
if (settings.tenant === '') {
core.setFailed("The 'tenant' parameter is missing.")
}

if (clientSecret === null || clientSecret === undefined || clientSecret === ''
if (settings.clientSecret === ''
) {
core.setFailed(`The 'clientSecret' parameter is missing.`)
}

// Print the input parameters
if (settings.verbose)
core.info(JSON.stringify(settings))

// Create OAuth2 client
const client = Client.initWithMiddleware({
authProvider: new ClientCredentialsAuthProvider(
tenant,
clientId,
clientSecret
settings.tenant,
settings.clientId,
settings.clientSecret
),
defaultVersion: 'beta'
})

// Create an array of policy files
let filesArray = files.split(",")
let filesArray = settings.files.split(",")

if (settings.files === "*") {
deploymentType = DeploymentType.All
filesArray = await fg([`${settings.folder}/**/*.xml`], { dot: true })
}
else if (settings.files.indexOf(".json") > 0) {
deploymentType = DeploymentType.JSON
if (!fs.existsSync(`.github/workflows/${settings.files}`)) {
core.setFailed(`Can't find the .github/workflows/${settings.files} file`)
}

const deploymentFile = fs.readFileSync(`.github/workflows/${settings.files}`)
const deploymentJson = JSON.parse(deploymentFile)

if (files === "*") {
filesArray = await fg([`${folder}/**/*.xml`], { dot: true })
filesArray = deploymentJson.files
}
else {
deploymentType = DeploymentType.CommaDelimiter
}

core.info(`Deployment type: ${DeploymentType[deploymentType]}.`)

for (const f of filesArray) {

let filePath = ''

if (files === "*") {
if (deploymentType === DeploymentType.All) {
filePath = f.trim()
}
else {
filePath = path.join(folder, f.trim())
else if (deploymentType === DeploymentType.JSON) {
filePath = path.join(settings.folder, f.path.trim())
}
else if (deploymentType === DeploymentType.CommaDelimiter) {
filePath = path.join(settings.folder, f.trim())
}

if (filePath.length > 0 && fs.existsSync(filePath)) {
Expand All @@ -92,20 +138,28 @@ async function run(): Promise<void> {
// Replace yourtenant.onmicrosoft.com with the tenant name parameter
if (policyXML.indexOf("yourtenant.onmicrosoft.com") > 0) {
//core.info(`Policy ${filePath} replacing yourtenant.onmicrosoft.com with ${tenant}.`)
policyXML = policyXML.replace(new RegExp("yourtenant.onmicrosoft.com", "gi"), tenant)
policyXML = policyXML.replace(new RegExp("yourtenant.onmicrosoft.com", "gi"), settings.tenant)
}

// Use the deployment JSON file to find and replace in the custom policy file
if (deploymentType === DeploymentType.JSON && f.replacements !== undefined) {
for (const r of f.replacements) {
policyXML = policyXML.replace(new RegExp(r.find, "gi"), r.replace)
}
}

// Add Azure AppInsights orchestration step at the begging of the collection
if (addAppInsightsStep !== null && addAppInsightsStep !== undefined || addAppInsightsStep === true) {
if (settings.addAppInsightsStep) {
policyXML = addAppInsightsOrchestrationStep(policyXML)
}

// Renumber the orchestration steps
if (renumberSteps !== null && renumberSteps !== undefined || renumberSteps === true) {
if (settings.renumberSteps) {
policyXML = renumberOrchestrationSteps(policyXML)
}

//core.info(policyXML)
if (settings.verbose)
core.info(policyXML)

const fileStream = new Readable()
fileStream.push(policyXML)
Expand Down Expand Up @@ -152,7 +206,7 @@ function addAppInsightsOrchestrationStep(xmlStringDocument: string) {

OrchestrationStep.appendChild(ClaimsExchanges)
ClaimsExchanges.appendChild(ClaimsExchange)

// There is only one OrchestrationSteps element in a UserJourney, add the new element at the first place
ParentOrchestrationSteps[0].insertBefore(OrchestrationStep, ParentOrchestrationSteps[0].firstChild)
}
Expand Down

0 comments on commit 126e63d

Please sign in to comment.