From 5bf7844408d55830d0d7bd7418e02016b06d1f66 Mon Sep 17 00:00:00 2001 From: Anthony Escott-Lawrence Date: Sat, 4 Feb 2023 22:12:23 +0000 Subject: [PATCH 1/4] Use this package's Helpers class rather than deferring to Vapor's. --- src/Commands/CloudflareLoginCommand.php | 7 +++---- src/Commands/CloudflareLogoutCommand.php | 3 +-- src/Commands/RecordsImportCommand.php | 24 ++++++++++++------------ src/Helpers.php | 2 +- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/Commands/CloudflareLoginCommand.php b/src/Commands/CloudflareLoginCommand.php index 9ad6be4..166decc 100644 --- a/src/Commands/CloudflareLoginCommand.php +++ b/src/Commands/CloudflareLoginCommand.php @@ -7,7 +7,6 @@ use Cloudflare\API\Auth\APIToken; use Cloudflare\API\Adapter\Guzzle; use Laravel\VaporCli\Commands\Command; -use Laravel\VaporCli\Helpers as VaporHelpers; use Cumulus\Cumulus\CloudflareEndpoints\UserApiToken; class CloudflareLoginCommand extends Command @@ -20,7 +19,7 @@ class CloudflareLoginCommand extends Command */ public function handle() { - $apiToken = VaporHelpers::secret('API Token'); + $apiToken = Helpers::secret('API Token'); $key = new APIToken($apiToken); $adapter = new Guzzle($key); @@ -35,7 +34,7 @@ public function handle() } } catch (Exception $e) { if ($e->getMessage() === 'Invalid request headers') { - VaporHelpers::abort( + Helpers::abort( 'Invalid credentials' ); } @@ -48,7 +47,7 @@ public function handle() ] ); - VaporHelpers::info('Authenticated successfully.' . PHP_EOL); + Helpers::info('Authenticated successfully.' . PHP_EOL); } /** diff --git a/src/Commands/CloudflareLogoutCommand.php b/src/Commands/CloudflareLogoutCommand.php index f2b7e0c..764d426 100644 --- a/src/Commands/CloudflareLogoutCommand.php +++ b/src/Commands/CloudflareLogoutCommand.php @@ -5,7 +5,6 @@ use Exception; use Cumulus\Cumulus\Helpers; use Laravel\VaporCli\Commands\Command; -use Laravel\VaporCli\Helpers as VaporHelpers; class CloudflareLogoutCommand extends Command { @@ -23,7 +22,7 @@ public function handle() ] ); - VaporHelpers::info('Cloudflare credentials cleared.' . PHP_EOL); + Helpers::info('Cloudflare credentials cleared.' . PHP_EOL); } /** diff --git a/src/Commands/RecordsImportCommand.php b/src/Commands/RecordsImportCommand.php index cd1af8d..734bae8 100644 --- a/src/Commands/RecordsImportCommand.php +++ b/src/Commands/RecordsImportCommand.php @@ -34,7 +34,7 @@ public function handle() $this->proxy = !$this->option('no-proxy'); $this->dryRun = $this->option('dry-run'); - VaporHelpers::ensure_api_token_is_available(); +// VaporHelpers::ensure_api_token_is_available(); Helpers::ensureCloudFlareCredentialsAreAvailable(); $vaporRecords = $this->getVaporRecords(); @@ -81,34 +81,34 @@ function ($vaporRecord) { if ($this->dryRun) { if ($this->totalAddedRecords > 0) { - VaporHelpers::info("Records required to be added:"); + Helpers::info("Records required to be added:"); $this->table(['Type', 'Name', 'Value'], $this->recordsToAdd); } else { - VaporHelpers::info("No records need added to Cloudflare. All records are already imported."); + Helpers::info("No records need added to Cloudflare. All records are already imported."); } if ($this->totalUpdatedRecords > 0) { - VaporHelpers::warn("Records required to be updated:"); + Helpers::warn("Records required to be updated:"); $this->table(['Type', 'Name', 'Value'], $this->recordsToUpdate); } else { - VaporHelpers::info("No records need updated in Cloudflare. All records are already correct."); + Helpers::info("No records need updated in Cloudflare. All records are already correct."); } return; } if ($this->totalAddedRecords > 0) { - VaporHelpers::info("Added {$this->totalAddedRecords} records to Cloudflare."); + Helpers::info("Added {$this->totalAddedRecords} records to Cloudflare."); } else { - VaporHelpers::info("No records were added to Cloudflare. All records are already imported."); + Helpers::info("No records were added to Cloudflare. All records are already imported."); } if ($this->totalUpdatedRecords > 0) { - VaporHelpers::info("Updated {$this->totalUpdatedRecords} records in Cloudflare."); + Helpers::info("Updated {$this->totalUpdatedRecords} records in Cloudflare."); } else { - VaporHelpers::info("No records need updated in Cloudflare. All records are already correct."); + Helpers::info("No records need updated in Cloudflare. All records are already correct."); } } @@ -119,7 +119,7 @@ protected function getVaporRecords(): Collection } if (is_null($vaporZoneId)) { - VaporHelpers::abort('Unable to find a zone with that name / ID in Vapor.'); + Helpers::abort('Unable to find a zone with that name / ID in Vapor.'); } return collect($this->vapor->records($vaporZoneId))->map(function ($record) { @@ -158,7 +158,7 @@ protected function addRecord($type, $name, $value): void $this->totalAddedRecords++; Helpers::info("Added {$type} record {$name}"); } catch (Exception $e) { - VaporHelpers::danger( + Helpers::danger( "Unable to create {$type} record in Cloudflare with name {$name}. Response: {$e->getMessage()}" ); } @@ -181,7 +181,7 @@ protected function updateRecord($record, $value) $this->totalUpdatedRecords++; Helpers::info("Updated {$record->type} record {$record->name}"); } catch (Exception $e) { - VaporHelpers::danger( + Helpers::danger( "Unable to update record in Cloudflare. Response: {$e->getMessage()}" ); } diff --git a/src/Helpers.php b/src/Helpers.php index 87a0856..af66263 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -60,7 +60,7 @@ public static function getCloudflareZoneId($zoneInput) try { return $zone->getZoneId($zoneInput); } catch (Exception $e) { - VaporHelpers::abort('Unable to find a zone with that name / ID in Cloudflare.'); + parent::abort('Unable to find a zone with that name / ID in Cloudflare.'); } } From 20d7804243d7b01152753386f8d64303ac2d98bc Mon Sep 17 00:00:00 2001 From: Anthony Escott-Lawrence Date: Sat, 4 Feb 2023 22:12:40 +0000 Subject: [PATCH 2/4] Read environment variables over config file. --- cumulus | 39 +++++++++++++++++++++++++++++++++++++++ src/Helpers.php | 13 ++++++++++--- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/cumulus b/cumulus index 267f134..f3110d3 100755 --- a/cumulus +++ b/cumulus @@ -2,6 +2,10 @@ addReader($adapter) + ->addWriter($adapter); + } + } else { // V4 + $adapters = array_map(function ($adapterClass) { + return new $adapterClass(); + }, $adapters); + + $repository = RepositoryBuilder::create() + ->withReaders($adapters) + ->withWriters($adapters); + } + + Dotenv::create( + $repository->immutable()->make(), + __DIR__ + )->safeLoad(); + } +})(); + /** * Create the container instance. */ diff --git a/src/Helpers.php b/src/Helpers.php index af66263..6116842 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -19,7 +19,7 @@ class Helpers extends VaporHelpers */ public static function ensureCloudFlareCredentialsAreAvailable() { - if (!static::config('apiToken')) { + if (empty(static::getCloudFlareApiToken())) { throw new Exception("Please authenticate using the 'cloudflare:login' command before proceeding."); } } @@ -52,7 +52,7 @@ public static function config($key, $value = null) */ public static function getCloudflareZoneId($zoneInput) { - $key = new APIToken(Config::get('apiToken')); + $key = new APIToken(static::getCloudFlareApiToken()); $adapter = new Guzzle($key); $zone = new Zones($adapter); @@ -69,9 +69,16 @@ public static function getCloudflareZoneId($zoneInput) */ public static function getCloudflareDnsApi() { - $key = new APIToken(Config::get('apiToken')); + $key = new APIToken(static::getCloudFlareApiToken()); $adapter = new Guzzle($key); return new DNS($adapter); } + + public static function getCloudFlareApiToken() + { + return $_ENV['CLOUDFLARE_API_TOKEN'] + ?? env('CLOUDFLARE_API_TOKEN') + ?? Config::get('apiToken'); + } } \ No newline at end of file From 48119832191839b147edfbc18e6de012874025cf Mon Sep 17 00:00:00 2001 From: Anthony Escott-Lawrence Date: Sat, 4 Feb 2023 23:05:12 +0000 Subject: [PATCH 3/4] Update readme to detail usage of CLOUDFLARE_API_TOKEN --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 0e806cc..255065a 100644 --- a/readme.md +++ b/readme.md @@ -36,6 +36,8 @@ If, for what ever reason, you would like to clear this token from local storage cumulus cloudflare:logout ``` +Alternatively, if you wish to load your API token from your server environment, you can do so by declaring `CLOUDFLARE_API_TOKEN` as an environment variable. When this is defined it will always take priority over your Cumulus config stored in your user profile. + --- ### Importing DNS Records From d7fff980fa40f31603c79c8a8e3d00b6f6073c87 Mon Sep 17 00:00:00 2001 From: Anthony Escott-Lawrence Date: Sun, 5 Feb 2023 11:37:07 +0000 Subject: [PATCH 4/4] Remove debugging comment. --- src/Commands/RecordsImportCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/RecordsImportCommand.php b/src/Commands/RecordsImportCommand.php index 734bae8..526f59f 100644 --- a/src/Commands/RecordsImportCommand.php +++ b/src/Commands/RecordsImportCommand.php @@ -34,7 +34,7 @@ public function handle() $this->proxy = !$this->option('no-proxy'); $this->dryRun = $this->option('dry-run'); -// VaporHelpers::ensure_api_token_is_available(); + VaporHelpers::ensure_api_token_is_available(); Helpers::ensureCloudFlareCredentialsAreAvailable(); $vaporRecords = $this->getVaporRecords();