Skip to content

Commit

Permalink
only support allowPublicAccess for production deploys, #148
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Sep 18, 2019
1 parent 7981386 commit 9712fdd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
5 changes: 3 additions & 2 deletions js/build-server/taskWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async function taskWorker( { api, repos, locales, simName, version, email, brand
const htaccessLocation = ( chipperVersion.major === 2 && chipperVersion.minor === 0 ) ?
simDir + '/build/phet-io' :
simDir + '/build';
await writePhetioHtaccess( htaccessLocation, simName );
await writePhetioHtaccess( htaccessLocation );
}
await devDeploy( simDir, simName, version, chipperVersion, brands );
}
Expand Down Expand Up @@ -347,7 +347,8 @@ async function taskWorker( { api, repos, locales, simName, version, email, brand
} )
;
winston.debug( 'server notified' );
await writePhetioHtaccess( targetVersionDir, simName, {
await writePhetioHtaccess( targetVersionDir, {
simName: simName,
version: originalVersion,
directory: constants.PHET_IO_SIMS_DIRECTORY
} );
Expand Down
33 changes: 20 additions & 13 deletions js/common/writePhetioHtaccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ const writeFile = require( './writeFile' );
/**
* Writes the htaccess file to password protect the exclusive content for phet-io sims
* @param {string} passwordProtectPath - deployment location
* @param {string} simName
* @param {{version:string, directory:string}} [latestOption] - if provided, then write the /latest/ redirect .htaccess file.
* - this is only to be used for production deploys by the build-server
* @param {{simName:string, version:string, directory:string}} [latestOption]
* if provided, then we are publishing to production. We then write the /latest/ redirect .htaccess file.
* This is only to be used for production deploys by the build-server.
* @param {string} [devVersionPath] - if provided, scp the htaccess files to here, relatively
*/
module.exports = async function writePhetioHtaccess( passwordProtectPath, simName, latestOption, devVersionPath ) {
module.exports = async function writePhetioHtaccess( passwordProtectPath, latestOption, devVersionPath ) {
const authFilepath = '/etc/httpd/conf/phet-io_pw';

const isProductionDeploy = !!latestOption;

// This option is for production deploys by the build-server
// If we are provided a simName and version then write a .htaccess file to redirect
// https://phet-io.colorado.edu/sims/{{sim-name}}/{{major}}.{{minor}} to https://phet-io.colorado.edu/sims/{{sim-name}}/{{major}}.{{minor}}.{{latest}}{{[-suffix]}}
if ( latestOption ) {
if ( simName && latestOption.version && latestOption.directory ) {
const redirectFilepath = latestOption.directory + simName + '/.htaccess';
if ( isProductionDeploy ) {
if ( latestOption.simName && latestOption.version && latestOption.directory ) {
const redirectFilepath = latestOption.directory + latestOption.simName + '/.htaccess';
let latestRedirectContents = 'RewriteEngine on\n' +
`RewriteBase /sims/${simName}/\n`;
const versions = JSON.parse( await request( buildLocal.productionServerURL + `/services/metadata/phetio?name=${simName}&latest=true` ) );
`RewriteBase /sims/${latestOption.simName}/\n`;
const versions = JSON.parse( await request( buildLocal.productionServerURL + `/services/metadata/phetio?name=${latestOption.simName}&latest=true` ) );
for ( const v of versions ) {
// Add a trailing slash to /sims/sim-name/x.y
latestRedirectContents += `RewriteRule ^${v.versionMajor}.${v.versionMinor}$ ${v.versionMajor}.${v.versionMinor}/ [R=301,L]\n`;
Expand All @@ -48,28 +50,33 @@ module.exports = async function writePhetioHtaccess( passwordProtectPath, simNam
}
}
else {
winston.error( `simName: ${simName}` );
winston.error( `simName: ${latestOption.simName}` );
winston.error( `version: ${latestOption.version}` );
winston.error( `directory: ${latestOption.directory}` );
return Promise.reject( 'latestOption is missing one of the required parameters (simName, version, or directory)' );
}
}

// Write a file to add authentication to the ./wrappers directory
const simPackage = JSON.parse( fs.readFileSync( `../${simName}/package.json` ) );
if ( !( simPackage.phet && simPackage.phet[ 'phet-io' ] && simPackage.phet[ 'phet-io' ].allowPublicAccess ) ) {
const simPackage = isProductionDeploy ? JSON.parse( fs.readFileSync( `../${latestOption.simName}/package.json` ) ) : null;

// Only skip htaccess creation if in production deploy when the "allowPublicAccess" flag is present
if ( !( simPackage && simPackage.phet && simPackage.phet[ 'phet-io' ] && simPackage.phet[ 'phet-io' ].allowPublicAccess ) ) {
try {
const passwordProtectWrapperContents = 'AuthType Basic\n' +
'AuthName "PhET-iO Password Protected Area"\n' +
'AuthUserFile ' + authFilepath + '\n' +
'Require valid-user\n';

// Write a file to add authentication to the ./wrappers directory
let filePath = 'wrappers/.htaccess';
await writeFile( `${passwordProtectPath}/${filePath}`, passwordProtectWrapperContents );
if ( devVersionPath ) {
await devScp( `${passwordProtectPath}/${filePath}`, `${devVersionPath}/phet-io/${filePath}` );
}

const phetioPackage = JSON.parse( fs.readFileSync( '../phet-io/package.json' ) );

// Write a file to add authentication to the top level index pages
if ( phetioPackage.phet && phetioPackage.phet.addRootHTAccessFile ) {
const passwordProtectIndexContents = '<FilesMatch "index.*">\n'
+ passwordProtectWrapperContents
Expand Down
2 changes: 1 addition & 1 deletion js/grunt/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ module.exports = async function( repo, brands, noninteractive, branch, message )
// https://github.com/phetsims/phet-io/issues/641
if ( brands.includes( 'phet-io' ) && buildLocal.devDeployServer === 'bayes.colorado.edu' ) {
const htaccessLocation = `../${repo}/build/phet-io/`;
await writePhetioHtaccess( htaccessLocation, repo, null, versionPath );
await writePhetioHtaccess( htaccessLocation, null, versionPath );
}

// Move over dependencies.json and commit/push
Expand Down

0 comments on commit 9712fdd

Please sign in to comment.