1+ /* eslint-disable no-shadow */
12const express = require ( 'express' ) ;
23const path = require ( 'path' ) ;
34const url = require ( 'url' ) ;
@@ -21,14 +22,48 @@ const OPENSHIFT_PROXY_PATH = '/proxy/openshift';
2122const app = express ( ) ;
2223
2324const adoc = asciidoctor ( ) ;
25+ const LOCAL_DEV_INSTALLED_SERVICES = {
26+ '3scale' : {
27+ Host : 'https://3scale-admin.apps.demo.com' ,
28+ Version : '2.7'
29+ } ,
30+ amqonline : {
31+ Host : 'http://localhost:3003' ,
32+ Version : '1.3.1'
33+ } ,
34+ apicurito : {
35+ Host : 'http://localhost:4200' ,
36+ Version : '1.0.1'
37+ } ,
38+ codeready : {
39+ Host : 'https://codeready-redhat-rhmi-codeready-workspaces.apps.demo.com' ,
40+ Version : '2.0.0'
41+ } ,
42+ 'fuse-managed' : {
43+ Host : 'http://localhost:3000' ,
44+ Version : '7.5'
45+ } ,
46+ ups : {
47+ Host : 'https://ups-unifiedpush-proxy-redhat-rhmi-ups.apps.demo.com' ,
48+ Version : '2.3.2'
49+ } ,
50+ 'user-rhsso' : {
51+ Host : 'https://keycloak-edge-redhat-rhmi-user-sso.apps.demo.com' ,
52+ Version : '8.0.1'
53+ }
54+ } ;
55+ const CROSS_CONSOLE_ENABLED = false ;
2456
2557app . use ( bodyParser . json ( ) ) ;
26- app . use ( OPENSHIFT_PROXY_PATH , proxy ( `https://${ process . env . OPENSHIFT_API } ` , {
27- proxyReqOptDecorator : function ( proxyReqOpts , _ ) {
28- proxyReqOpts . rejectUnauthorized = false ;
29- return proxyReqOpts ;
30- }
31- } ) ) ;
58+ app . use (
59+ OPENSHIFT_PROXY_PATH ,
60+ proxy ( `https://${ process . env . OPENSHIFT_API } ` , {
61+ proxyReqOptDecorator ( proxyReqOpts , _ ) {
62+ proxyReqOpts . rejectUnauthorized = false ;
63+ return proxyReqOpts ;
64+ }
65+ } )
66+ ) ;
3267
3368// prometheus metrics endpoint
3469app . use (
@@ -79,6 +114,21 @@ const backendRequiredRoles = [
79114const walkthroughs = [ ] ;
80115let server ;
81116
117+ app . get ( '/services' , ( req , res ) => {
118+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
119+ res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
120+ res . setHeader ( 'Access-Control-Allow-Headers' , 'Origin, X-Requested-With, Content-Type, Accept' ) ;
121+ if ( ( isOpenShift4 ( ) || process . env . CROSS_CONSOLE_LOCAL_DEV_MODE === 'true' ) && CROSS_CONSOLE_ENABLED ) {
122+ if ( process . env . INSTALLED_SERVICES ) {
123+ res . send ( process . env . INSTALLED_SERVICES ) ;
124+ } else {
125+ res . send ( JSON . stringify ( LOCAL_DEV_INSTALLED_SERVICES ) ) ;
126+ }
127+ } else {
128+ res . send ( JSON . stringify ( { } ) ) ;
129+ }
130+ } ) ;
131+
82132app . get ( '/customWalkthroughs' , ( req , res ) => {
83133 res . status ( 200 ) . json ( walkthroughs ) ;
84134} ) ;
@@ -97,9 +147,8 @@ app.get('/user_walkthroughs', (req, res) =>
97147 if ( data ) {
98148 const { value } = data ;
99149 return res . json ( value ) ;
100- } else {
101- return res . end ( ) ;
102150 }
151+ return res . end ( ) ;
103152 } )
104153 . catch ( err => {
105154 console . error ( err ) ;
@@ -281,89 +330,88 @@ function resolveWalkthroughLocations(locations) {
281330 }
282331
283332 const tmpDirPrefix = uuid . v4 ( ) ;
284- const mappedLocations = locations . map (
285- location =>
286- new Promise ( ( resolve , reject ) => {
287- const locationResultTemplate = { origin : location } ;
288- if ( ! location ) {
289- return reject ( new Error ( `Invalid location ${ location } ` ) ) ;
290- } else if ( isPath ( location ) ) {
291- console . log ( `Importing walkthrough from path ${ location } ` ) ;
292- const locationResult = Object . assign (
293- {
294- parentId : path . basename ( location ) ,
295- walkthroughLocationInfo : Object . assign ( { } , WALKTHROUGH_LOCATION_DEFAULT , {
296- type : WALKTHROUGH_LOCATION_TYPE_PATH ,
297- directory : path . basename ( location )
298- } )
299- } ,
300- locationResultTemplate ,
301- { local : location }
302- ) ;
303- return resolve ( locationResult ) ;
304- } else if ( isGitRepo ( location ) ) {
305- const clonePath = path . join ( TMP_DIR , tmpDirPrefix ) ;
306-
307- // Need to parse out query params for walkthroughs, e.g custom directory
308- const cloneUrl = generateCloneUrlFromLocation ( location ) ;
309- const repoName = getWalkthroughRepoNameFromLocation ( location ) ;
310- const walkthroughParams = querystring . parse ( url . parse ( location ) . query ) ;
311-
312- console . log ( `Importing walkthrough from git ${ cloneUrl } ` ) ;
313- return gitClient
314- . cloneRepo ( cloneUrl , clonePath )
315- . then ( cloned => {
316- gitClient . latestLog ( cloned . localDir ) . then ( log => {
317- getWalkthroughHeader ( cloned . localDir )
318- . then ( head => {
319- let wtHeader ;
320- if ( head === null ) {
321- wtHeader = null ;
322- } else {
323- wtHeader = head . prettyName ;
324- }
325- const walkthroughFolders = [ ] ;
326- if ( ! Array . isArray ( walkthroughParams . walkthroughsFolder ) ) {
327- walkthroughFolders . push ( walkthroughParams . walkthroughsFolder || 'walkthroughs' ) ;
328- } else {
329- walkthroughFolders . push ( ...walkthroughParams . walkthroughsFolder ) ;
330- }
331- // Get the folders to import in the repository.
332- const walkthroughInfos = walkthroughFolders . map ( folder => {
333- const walkthroughLocationInfo = Object . assign ( { } , WALKTHROUGH_LOCATION_DEFAULT , {
334- type : WALKTHROUGH_LOCATION_TYPE_GIT ,
335- commitHash : log . latest . hash ,
336- commitDate : log . latest . date ,
337- remote : cloned . repoName ,
338- directory : folder ,
339- header : wtHeader
340- } ) ;
341-
342- return Object . assign ( { } , locationResultTemplate , {
343- parentId : `${ repoName } -${ path . basename ( folder ) } ` ,
344- walkthroughLocationInfo,
345- local : path . join ( cloned . localDir , folder )
346- } ) ;
347- } ) ;
348- resolve ( walkthroughInfos ) ;
349- } )
350- . catch ( reject ) ;
351- } ) ;
333+ const mappedLocations = locations . map ( location =>
334+ new Promise ( ( resolve , reject ) => {
335+ const locationResultTemplate = { origin : location } ;
336+ if ( ! location ) {
337+ return reject ( new Error ( `Invalid location ${ location } ` ) ) ;
338+ } else if ( isPath ( location ) ) {
339+ console . log ( `Importing walkthrough from path ${ location } ` ) ;
340+ const locationResult = Object . assign (
341+ {
342+ parentId : path . basename ( location ) ,
343+ walkthroughLocationInfo : Object . assign ( { } , WALKTHROUGH_LOCATION_DEFAULT , {
344+ type : WALKTHROUGH_LOCATION_TYPE_PATH ,
345+ directory : path . basename ( location )
352346 } )
353- . catch ( reject ) ;
354- }
347+ } ,
348+ locationResultTemplate ,
349+ { local : location }
350+ ) ;
351+ return resolve ( locationResult ) ;
352+ } else if ( isGitRepo ( location ) ) {
353+ const clonePath = path . join ( TMP_DIR , tmpDirPrefix ) ;
354+
355+ // Need to parse out query params for walkthroughs, e.g custom directory
356+ const cloneUrl = generateCloneUrlFromLocation ( location ) ;
357+ const repoName = getWalkthroughRepoNameFromLocation ( location ) ;
358+ const walkthroughParams = querystring . parse ( url . parse ( location ) . query ) ;
359+
360+ console . log ( `Importing walkthrough from git ${ cloneUrl } ` ) ;
361+ return gitClient
362+ . cloneRepo ( cloneUrl , clonePath )
363+ . then ( cloned => {
364+ gitClient . latestLog ( cloned . localDir ) . then ( log => {
365+ getWalkthroughHeader ( cloned . localDir )
366+ . then ( head => {
367+ let wtHeader ;
368+ if ( head === null ) {
369+ wtHeader = null ;
370+ } else {
371+ wtHeader = head . prettyName ;
372+ }
373+ const walkthroughFolders = [ ] ;
374+ if ( ! Array . isArray ( walkthroughParams . walkthroughsFolder ) ) {
375+ walkthroughFolders . push ( walkthroughParams . walkthroughsFolder || 'walkthroughs' ) ;
376+ } else {
377+ walkthroughFolders . push ( ...walkthroughParams . walkthroughsFolder ) ;
378+ }
379+ // Get the folders to import in the repository.
380+ const walkthroughInfos = walkthroughFolders . map ( folder => {
381+ const walkthroughLocationInfo = Object . assign ( { } , WALKTHROUGH_LOCATION_DEFAULT , {
382+ type : WALKTHROUGH_LOCATION_TYPE_GIT ,
383+ commitHash : log . latest . hash ,
384+ commitDate : log . latest . date ,
385+ remote : cloned . repoName ,
386+ directory : folder ,
387+ header : wtHeader
388+ } ) ;
355389
356- return reject ( new Error ( `${ location } is neither a path nor a git repo` ) ) ;
357- } ) . catch ( err => {
358- console . error ( err ) ;
359- return undefined ;
360- } )
390+ return Object . assign ( { } , locationResultTemplate , {
391+ parentId : `${ repoName } -${ path . basename ( folder ) } ` ,
392+ walkthroughLocationInfo,
393+ local : path . join ( cloned . localDir , folder )
394+ } ) ;
395+ } ) ;
396+ resolve ( walkthroughInfos ) ;
397+ } )
398+ . catch ( reject ) ;
399+ } ) ;
400+ } )
401+ . catch ( reject ) ;
402+ }
403+
404+ return reject ( new Error ( `${ location } is neither a path nor a git repo` ) ) ;
405+ } ) . catch ( err => {
406+ console . error ( err ) ;
407+ return undefined ;
408+ } )
361409 ) ;
362410
363- return Promise . all ( mappedLocations ) . then ( promises => {
411+ return Promise . all ( mappedLocations ) . then ( promises =>
364412 // Ignore all locations that could not be resolved
365- return flattenDeep ( promises . filter ( p => ! ! p ) ) ;
366- } ) ;
413+ flattenDeep ( promises . filter ( p => ! ! p ) )
414+ ) ;
367415}
368416
369417/**
@@ -623,13 +671,19 @@ function getConfigData(req) {
623671 logoutRedirectUri = 'http://localhost:3006' ;
624672 }
625673 if ( ! process . env . OPENSHIFT_OAUTH_HOST ) {
626- console . warn ( 'OPENSHIFT_OAUTH_HOST not set, using OPENSHIFT_HOST instead. This is okay on OCP 3.11, but will not work on 4.x, see INTLY-2791.' ) ;
674+ console . warn (
675+ 'OPENSHIFT_OAUTH_HOST not set, using OPENSHIFT_HOST instead. This is okay on OCP 3.11, but will not work on 4.x, see INTLY-2791.'
676+ ) ;
627677 process . env . OPENSHIFT_OAUTH_HOST = process . env . OPENSHIFT_HOST ;
628678 }
629679
630680 const masterUri = isOpenShift4 ( ) ? OPENSHIFT_PROXY_PATH : `https://${ process . env . OPENSHIFT_HOST } ` ;
631681 const wssMasterUri = isOpenShift4 ( ) ? OPENSHIFT_PROXY_PATH : `wss://${ process . env . OPENSHIFT_HOST } ` ;
632- const ssoLogoutUri = isOpenShift4 ( ) ? '/' : `https://${ process . env . SSO_ROUTE } /auth/realms/openshift/protocol/openid-connect/logout?redirect_uri=${ logoutRedirectUri } ` ;
682+ const ssoLogoutUri = isOpenShift4 ( )
683+ ? '/'
684+ : `https://${
685+ process . env . SSO_ROUTE
686+ } /auth/realms/openshift/protocol/openid-connect/logout?redirect_uri=${ logoutRedirectUri } `;
633687
634688 const installedServices = process . env . INSTALLED_SERVICES || '{}' ;
635689 return `window.OPENSHIFT_CONFIG = {
0 commit comments