Skip to content

Commit

Permalink
Merge pull request #42 from Fgerthoffert/develop
Browse files Browse the repository at this point in the history
Workaround to still support direct push of groovy script through tools
  • Loading branch information
Fgerthoffert authored Jul 22, 2020
2 parents bbd2566 + e37c3ac commit d2489a0
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 27 deletions.
51 changes: 46 additions & 5 deletions src/commands/groovy/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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();
Expand Down
105 changes: 94 additions & 11 deletions src/commands/manifest/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
43 changes: 32 additions & 11 deletions src/utils/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/<legend style="color: red">Error<\/legend>/);
const successRegexp = new RegExp(
/<legend style="color: blue">Successfully executed in/,
Expand All @@ -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());
Expand All @@ -48,6 +68,7 @@ export const submitGroovyFile = async (
password,
url,
encodedScript,
jsessionid,
);
return groovyResponse;
};

0 comments on commit d2489a0

Please sign in to comment.