diff --git a/src/commands/groovy/execute.ts b/src/commands/groovy/execute.ts index 4317515..d1bef9f 100644 --- a/src/commands/groovy/execute.ts +++ b/src/commands/groovy/execute.ts @@ -5,6 +5,11 @@ import * as fs from 'fs'; import Command from '../../base'; import { submitGroovyFile } from '../../utils/tools'; +import launchPuppeteer from '../../utils/puppeteer/launch'; +import openJahia from '../../utils/openJahia'; +import navPage from '../../utils/navPage'; + +import closePuppeteer from '../../utils/puppeteer/close'; import { exit } from '@oclif/errors'; @@ -45,14 +50,50 @@ export default class GroovyExecute extends Command { flags.jahiaToolsPassword, flags.jahiaAdminUrl + '/modules/tools/groovyConsole.jsp?', flags.file, + null, ); - if (submitForm === false) { + if (submitForm === true) { + console.log('Groovy script successfully executed: ' + flags.file); + } else { console.log( - 'ERROR: Unable execute the groovy script, try running it manually through Jahia Tools', + 'Unable to access tools directly, will be trying to authenticate and fetch the session ID from a cookie', ); - exit(); - } else { - console.log('Groovy script successfully executed: ' + flags.file); + + const browser = await launchPuppeteer(!flags.debug); + const jahiaPage = await openJahia(browser, flags); + await navPage( + jahiaPage, + flags.jahiaAdminUrl + '/modules/tools/groovyConsole.jsp?', + ); + const cookies = await jahiaPage.cookies(); + const jsession = cookies.find((c: any) => c.name === 'JSESSIONID'); + await closePuppeteer(browser); + + if (jsession === undefined) { + console.log( + 'ERROR: Unable to log-in with puppeteer to execute the groovy script (unable to get JSESSIONID)', + ); + exit(); + } else { + console.log('Cookie found: ' + jsession.value); + const submitFormCookie = await submitGroovyFile( + flags.jahiaToolsUsername, + flags.jahiaToolsPassword, + flags.jahiaAdminUrl + '/modules/tools/groovyConsole.jsp?', + flags.file, + jsession.value, + ); + if (submitFormCookie === true) { + console.log( + 'Groovy script successfully executed (via browser auth and cookie): ' + + flags.file, + ); + } else { + console.log( + 'Unable to access tools directly, will be trying to authenticate and fetch the session ID from a cookie', + ); + } + } } const t1 = performance.now(); diff --git a/src/commands/manifest/run.ts b/src/commands/manifest/run.ts index dafb4db..60c61d2 100644 --- a/src/commands/manifest/run.ts +++ b/src/commands/manifest/run.ts @@ -126,15 +126,56 @@ export default class ManifestRun extends Command { flags.jahiaToolsPassword, flags.jahiaAdminUrl + '/modules/tools/groovyConsole.jsp?', job.filepath, + null, ); - if (submitForm === false) { + if (submitForm === true) { + console.log('Groovy script successfully executed: ' + job.filepath); + } else { console.log( - 'ERROR: Unable execute the groovy script, try running it manually through Jahia Tools: ' + - job.filepath, + 'Unable to access tools directly, will be trying to authenticate and fetch the session ID from a cookie', ); - exit(); - } else { - console.log('Groovy script successfully executed: ' + job.filepath); + // eslint-disable-next-line no-await-in-loop + const browser = await launchPuppeteer(!flags.debug); + // eslint-disable-next-line no-await-in-loop + const jahiaPage = await openJahia(browser, flags); + // eslint-disable-next-line no-await-in-loop + await navPage( + jahiaPage, + flags.jahiaAdminUrl + '/modules/tools/groovyConsole.jsp?', + ); + // eslint-disable-next-line no-await-in-loop + const cookies = await jahiaPage.cookies(); + const jsession = cookies.find((c: any) => c.name === 'JSESSIONID'); + // eslint-disable-next-line no-await-in-loop + await closePuppeteer(browser); + + if (jsession === undefined) { + console.log( + 'ERROR: Unable to log-in with puppeteer to execute the groovy script (unable to get JSESSIONID)', + ); + exit(); + } else { + console.log('Cookie found: ' + jsession.value); + // eslint-disable-next-line no-await-in-loop + const submitFormCookie = await submitGroovyFile( + flags.jahiaToolsUsername, + flags.jahiaToolsPassword, + flags.jahiaAdminUrl + '/modules/tools/groovyConsole.jsp?', + job.filepath, + jsession.value, + ); + // eslint-disable-next-line max-depth + if (submitFormCookie === true) { + console.log( + 'Groovy script successfully executed (via browser auth and cookie): ' + + job.filepath, + ); + } else { + console.log( + 'Unable to access tools directly, will be trying to authenticate and fetch the session ID from a cookie', + ); + } + } } } else if (job.type === 'modulesite') { let groovyScript = enableModule @@ -151,14 +192,56 @@ export default class ManifestRun extends Command { flags.jahiaToolsPassword, flags.jahiaAdminUrl + '/modules/tools/groovyConsole.jsp?', groovyScript, + null, ); - if (groovySubmit === false) { + if (groovySubmit === true) { + console.log('Groovy script successfully executed'); + } else { console.log( - 'ERROR: Unable execute the groovy script, try running it manually through Jahia Tools', + 'Unable to access tools directly, will be trying to authenticate and fetch the session ID from a cookie', ); - exit(); - } else { - console.log('Groovy script successfully executed'); + // eslint-disable-next-line no-await-in-loop + const browser = await launchPuppeteer(!flags.debug); + // eslint-disable-next-line no-await-in-loop + const jahiaPage = await openJahia(browser, flags); + // eslint-disable-next-line no-await-in-loop + await navPage( + jahiaPage, + flags.jahiaAdminUrl + '/modules/tools/groovyConsole.jsp?', + ); + // eslint-disable-next-line no-await-in-loop + const cookies = await jahiaPage.cookies(); + const jsession = cookies.find((c: any) => c.name === 'JSESSIONID'); + // eslint-disable-next-line no-await-in-loop + await closePuppeteer(browser); + + if (jsession === undefined) { + console.log( + 'ERROR: Unable to log-in with puppeteer to execute the groovy script (unable to get JSESSIONID)', + ); + exit(); + } else { + console.log('Cookie found: ' + jsession.value); + + // eslint-disable-next-line no-await-in-loop + const submitFormCookie = await submitGroovy( + flags.jahiaToolsUsername, + flags.jahiaToolsPassword, + flags.jahiaAdminUrl + '/modules/tools/groovyConsole.jsp?', + groovyScript, + jsession.value, + ); + // eslint-disable-next-line max-depth + if (submitFormCookie === true) { + console.log( + 'Groovy script successfully executed (via browser auth and cookie)', + ); + } else { + console.log( + 'Unable to access tools directly, will be trying to authenticate and fetch the session ID from a cookie', + ); + } + } } } else if (job.type === 'shell') { // eslint-disable-next-line no-await-in-loop diff --git a/src/utils/tools/index.ts b/src/utils/tools/index.ts index 809bd80..9f2fb84 100644 --- a/src/utils/tools/index.ts +++ b/src/utils/tools/index.ts @@ -7,19 +7,37 @@ export const submitGroovy = async ( password: string, url: string, encodedScript: string, + jsessionid: string | null, + // eslint-disable-next-line max-params ) => { - const response = await fetch(url, { - credentials: 'include', - headers: { - authorization: 'Basic ' + base64.encode(username + ':' + password), - 'content-type': 'application/x-www-form-urlencoded', - }, - body: 'toolAccessToken=&runScript=true&script=' + encodedScript, - method: 'POST', - mode: 'cors', - }); + let response: any = {}; + if (jsessionid === null) { + response = await fetch(url, { + credentials: 'include', + headers: { + authorization: 'Basic ' + base64.encode(username + ':' + password), + 'content-type': 'application/x-www-form-urlencoded', + }, + body: 'toolAccessToken=&runScript=true&script=' + encodedScript, + method: 'POST', + mode: 'cors', + }); + } else { + console.log('Executing call manually'); + response = await fetch(url, { + credentials: 'include', + headers: { + Cookie: 'JSESSIONID=' + jsessionid, + 'content-type': 'application/x-www-form-urlencoded', + }, + body: 'toolAccessToken=&runScript=true&script=' + encodedScript, + method: 'POST', + mode: 'cors', + }); + } + const data = await response.text(); - // console.log(data); + // console.log(data); const failRegexp = new RegExp(/Error<\/legend>/); const successRegexp = new RegExp( /Successfully executed in/, @@ -39,6 +57,8 @@ export const submitGroovyFile = async ( password: string, url: string, groovyFile: string, + jsessionid: string | null, + // eslint-disable-next-line max-params ) => { const groovyScript = fs.readFileSync(groovyFile); const encodedScript = encodeURIComponent(groovyScript.toString()); @@ -48,6 +68,7 @@ export const submitGroovyFile = async ( password, url, encodedScript, + jsessionid, ); return groovyResponse; };