Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added create, update and delete for auth for subdomains #41

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 49 additions & 26 deletions src/Auth_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ public function create( $args, $assoc_args ) {

verify_htpasswd_is_present();

$global = $this->populate_info( $args, __FUNCTION__ );
$ips = \EE\Utils\get_flag_value( $assoc_args, 'ip' );
$site_url = $global ? 'default' : $this->site_data->site_url;
$global = $this->populate_info( $args, __FUNCTION__ );
$ips = \EE\Utils\get_flag_value( $assoc_args, 'ip' );
$site_url = $global ? 'default' : $this->site_data->site_url;
$site_type = $global ? 'wp' : $this->site_data->app_sub_type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to consider other site types. html and PHP site types have blank $this->site_data->app_sub_type.
So, only check for app_sub_type when 'wp' === $this->site_data->site_type.


if ( $ips ) {
$this->create_whitelist( $site_url, $ips );
} else {
$this->create_auth( $assoc_args, $global, $site_url );
$this->create_auth( $assoc_args, $global, $site_url, $site_type );
}
}

Expand All @@ -98,10 +99,11 @@ public function create( $args, $assoc_args ) {
* @param array $assoc_args Assoc args passed to command
* @param bool $global Enable auth on global
* @param string $site_url URL of site
* @param string $site_type Type of site: wp|subdir|subdom
*
* @throws Exception
*/
private function create_auth( array $assoc_args, bool $global, string $site_url ) {
private function create_auth( array $assoc_args, bool $global, string $site_url, string $site_type ) {
$user = \EE\Utils\get_flag_value( $assoc_args, 'user', 'ee-' . EE\Utils\random_password( 6 ) );
$pass = \EE\Utils\get_flag_value( $assoc_args, 'pass', EE\Utils\random_password() );
$auth_data = [
Expand Down Expand Up @@ -133,9 +135,9 @@ private function create_auth( array $assoc_args, bool $global, string $site_url
Auth::create( $auth_data );

if ( 'default' === $site_url ) {
$this->generate_global_auth_files();
$this->generate_global_auth_files( $site_type );
} else {
$this->generate_site_auth_files( $site_url );
$this->generate_site_auth_files( $site_url, $site_type );
}

EE::log( 'Reloading global reverse proxy.' );
Expand Down Expand Up @@ -221,9 +223,11 @@ private function regen_admin_tools_auth() {
/**
* Generates auth files for global auth and all sites.
*
* @param string $site_type Type of site: wp|subdir|subdom
*
* @throws Exception
*/
private function generate_global_auth_files() {
private function generate_global_auth_files( $site_type ) {

$global_admin_tools_auth = Auth::get_global_admin_tools_auth();

Expand Down Expand Up @@ -256,7 +260,7 @@ private function generate_global_auth_files() {
);

foreach ( $sites as $site ) {
$this->generate_site_auth_files( $site );
$this->generate_site_auth_files( $site, $site_type );
}
}
}
Expand All @@ -265,11 +269,28 @@ private function generate_global_auth_files() {
* Generates auth files for a site
*
* @param string $site_url URL of site
* @param string $site_type Type of site: wp|subdir|subdom
*
* @throws Exception
*/
private function generate_site_auth_files( string $site_url ) {
$site_auth_file = EE_ROOT_DIR . '/services/nginx-proxy/htpasswd/' . $site_url;
private function generate_site_auth_files( string $site_url, string $site_type ) {

$auth_file_name = '';
switch ( $site_type ) {
case 'wp':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the same cases can go in one. No need to create a seprate block of subdir, just declare all blocks as in sugesstion. Or better yet, as there is a change in only one type of site, i.e., subdom, you can also convert this to single if-else.

Suggested change
case 'wp':
case 'wp':
case 'subdir':
case 'html':
case 'php':

$auth_file_name = $site_url;
break;
case 'subdir':
$auth_file_name = $site_url;
break;
case 'subdom':
$auth_file_name = '*.' . $site_url;
Copy link
Member

@mrrobot47 mrrobot47 Jul 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if we still need to have the sitename.tld file in /opt/easyengine/services/nginx-proxy/htpasswd when we already have *.sitename.tld

It is necessary to have both the files.

break;
default:
throw new Exception( 'unexpected site type' );
}
$site_auth_file = EE_ROOT_DIR . '/services/nginx-proxy/htpasswd/' . $auth_file_name;

$this->fs->remove( $site_auth_file );

$auths = array_merge(
Expand All @@ -280,10 +301,10 @@ private function generate_site_auth_files( string $site_url ) {
foreach ( $auths as $key => $auth ) {
$flags = 'b';

if ( $key === 0 ) {
if ( 0 === $key ) {
$flags = 'bc';
}
EE::exec( sprintf( 'docker exec %s htpasswd -%s /etc/nginx/htpasswd/%s %s %s', EE_PROXY_TYPE, $flags, $site_url, $auth->username, $auth->password ) );
EE::exec( sprintf( 'docker exec %s htpasswd -%s /etc/nginx/htpasswd/%s %s %s', EE_PROXY_TYPE, $flags, $auth_file_name, $auth->username, $auth->password ) );
}
}

Expand Down Expand Up @@ -390,14 +411,15 @@ public function update( $args, $assoc_args ) {

verify_htpasswd_is_present();

$global = $this->populate_info( $args, __FUNCTION__ );
$site_url = $global ? 'default' : $this->site_data->site_url;
$global = $this->populate_info( $args, __FUNCTION__ );
$site_url = $global ? 'default' : $this->site_data->site_url;
$ips = EE\Utils\get_flag_value( $assoc_args, 'ip' );
$site_type = $global ? 'wp' : $this->site_data->app_sub_type;

if ( $ips ) {
$this->update_whitelist( $site_url, $ips );
} else {
$this->update_auth( $assoc_args, $site_url );
$this->update_auth( $assoc_args, $site_url, $site_type );
}
}

Expand All @@ -406,8 +428,9 @@ public function update( $args, $assoc_args ) {
*
* @param array $assoc_args
* @param string $site_url
* @param string $site_type Type of site: wp|subdir|subdom
*/
private function update_auth( array $assoc_args, string $site_url ) {
private function update_auth( array $assoc_args, string $site_url, string $site_type ) {
$user = EE\Utils\get_flag_value( $assoc_args, 'user' );

if ( ! $user ) {
Expand All @@ -424,9 +447,9 @@ private function update_auth( array $assoc_args, string $site_url ) {
}

if ( 'default' === $site_url ) {
$this->generate_global_auth_files();
$this->generate_global_auth_files( $site_type );
} else {
$this->generate_site_auth_files( $site_url );
$this->generate_site_auth_files( $site_url, $site_type );
}

EE::log( 'Reloading global reverse proxy.' );
Expand Down Expand Up @@ -549,22 +572,23 @@ public function delete( $args, $assoc_args ) {

verify_htpasswd_is_present();

$global = $this->populate_info( $args, __FUNCTION__ );
$site_url = $global ? 'default' : $this->site_data->site_url;
$ip = EE\Utils\get_flag_value( $assoc_args, 'ip' );
$global = $this->populate_info( $args, __FUNCTION__ );
$site_url = $global ? 'default' : $this->site_data->site_url;
$ip = EE\Utils\get_flag_value( $assoc_args, 'ip' );
$site_type = $global ? 'wp' : $this->site_data->app_sub_type;

if ( ! $ip ) {
$user = EE\Utils\get_flag_value( $assoc_args, 'user' );
$user = EE\Utils\get_flag_value( $assoc_args, 'user' );
$auths = $this->get_auths( $site_url, $user );

foreach ( $auths as $auth ) {
$auth->delete();
}

if ( 'default' === $site_url ) {
$this->generate_global_auth_files();
$this->generate_global_auth_files( $site_type );
} else {
$this->generate_site_auth_files( $site_url );
$this->generate_site_auth_files( $site_url, $site_type );
}

if ( $user ) {
Expand Down Expand Up @@ -682,7 +706,6 @@ public function list( $args, $assoc_args ) {
if ( 'table' === $format ) {
$log_msg = $admin_tools_auth ? 'This auth is applied only on admin-tools.' : '';
}

} else {
$auths = $this->get_auths( $site_url, false );
}
Expand Down