diff --git a/app/Console/Commands/EncryptBackupDestinationsCommand.php b/app/Console/Commands/EncryptBackupDestinationsCommand.php deleted file mode 100644 index 56dc7581..00000000 --- a/app/Console/Commands/EncryptBackupDestinationsCommand.php +++ /dev/null @@ -1,106 +0,0 @@ -isEmpty()) { - $this->components->error('No backup destinations found.'); - - return; - } - - $newlyEncryptedCount = $this->encryptBackupDestinations($backupDestinations); - - $this->components->info($newlyEncryptedCount . ' backup destinations have been updated with encrypted fields.'); - } - - /** - * @param Collection $backupDestinations - */ - protected function encryptBackupDestinations(Collection $backupDestinations): int - { - $newlyEncryptedCount = 0; - - foreach ($backupDestinations as $backupDestination) { - if ($this->encryptBackupDestination($backupDestination)) { - $newlyEncryptedCount++; - } - } - - return $newlyEncryptedCount; - } - - protected function encryptBackupDestination(BackupDestination $backupDestination): bool - { - $updated = false; - - foreach ($this->fieldsToEncrypt as $fieldToEncrypt) { - if ($this->encryptField($backupDestination, $fieldToEncrypt)) { - $updated = true; - } - } - - if ($updated) { - $backupDestination->save(); - } - - return $updated; - } - - protected function encryptField(BackupDestination $backupDestination, string $field): bool - { - $value = $backupDestination->getAttribute($field); - - if (empty($value)) { - $this->components->warn(sprintf("Field '%s' for backup destination %s is empty. Skipping encryption.", $field, $backupDestination->getAttribute('label'))); - - return false; - } - - if ($this->isEncrypted($value)) { - return false; - } - - $backupDestination->setAttribute($field, Crypt::encryptString($value)); - - return true; - } - - protected function isEncrypted(string $value): bool - { - try { - Crypt::decryptString($value); - - return true; - } catch (Exception) { - return false; - } - } -} diff --git a/app/Console/Commands/EncryptDatabasePasswordsCommand.php b/app/Console/Commands/EncryptDatabasePasswordsCommand.php deleted file mode 100644 index b4562a47..00000000 --- a/app/Console/Commands/EncryptDatabasePasswordsCommand.php +++ /dev/null @@ -1,52 +0,0 @@ -isEmpty()) { - $this->components->error('No remote servers found.'); - - return; - } - - $remoteServers->each(function ($remoteServer): void { - - if (! $remoteServer->isDatabasePasswordEncrypted()) { - - if (empty($remoteServer->getAttribute('database_password'))) { - $this->components->warn(sprintf('Database password for remote server %s is empty. Skipping encryption.', $remoteServer->getAttribute('label'))); - - return; - } - - $remoteServer->update([ - 'database_password' => Crypt::encryptString($remoteServer->getAttribute('database_password')), - ]); - - $remoteServer->save(); - - $this->newlyEncryptedCount++; - } - }); - - $this->components->info($this->newlyEncryptedCount . ' database passwords have been encrypted.'); - - } -} diff --git a/tests/Feature/BackupDestinations/Commands/EncryptDatabaseDestinationsCommandTest.php b/tests/Feature/BackupDestinations/Commands/EncryptDatabaseDestinationsCommandTest.php deleted file mode 100644 index 8ecb724a..00000000 --- a/tests/Feature/BackupDestinations/Commands/EncryptDatabaseDestinationsCommandTest.php +++ /dev/null @@ -1,81 +0,0 @@ -artisan(EncryptBackupDestinationsCommand::class) - ->expectsOutputToContain('No backup destinations found.') - ->assertExitCode(0); -}); - -it('skips encryption if the field is empty', function (): void { - $backupDestination = BackupDestination::factory()->create([ - 'label' => 'Test Destination', - 's3_access_key' => '', - 's3_secret_key' => null, - 's3_bucket_name' => 'test-bucket', - 'custom_s3_endpoint' => '', - ]); - - $this->artisan(EncryptBackupDestinationsCommand::class) - ->expectsOutputToContain("Field 's3_access_key' for backup destination Test Destination is empty. Skipping encryption.") - ->expectsOutputToContain("Field 's3_secret_key' for backup destination Test Destination is empty. Skipping encryption.") - ->expectsOutputToContain("Field 'custom_s3_endpoint' for backup destination Test Destination is empty. Skipping encryption.") - ->expectsOutputToContain('1 backup destinations have been updated with encrypted fields.') - ->assertExitCode(0); - - $updatedDestination = $backupDestination->fresh(); - expect($updatedDestination->s3_access_key)->toBe('') - ->and($updatedDestination->s3_secret_key)->toBeNull() - ->and($updatedDestination->s3_bucket_name)->not->toBe('test-bucket') - ->and(Crypt::decryptString($updatedDestination->s3_bucket_name))->toBe('test-bucket') - ->and($updatedDestination->custom_s3_endpoint)->toBe(''); -}); - -it('encrypts the fields', function (): void { - $backupDestination = BackupDestination::factory()->create([ - 's3_access_key' => 'test-access-key', - 's3_secret_key' => 'test-secret-key', - 's3_bucket_name' => 'test-bucket', - 'custom_s3_endpoint' => 'test-endpoint', - ]); - - $this->artisan(EncryptBackupDestinationsCommand::class) - ->expectsOutputToContain('1 backup destinations have been updated with encrypted fields.') - ->assertExitCode(0); - - $updatedDestination = $backupDestination->fresh(); - - expect($updatedDestination->s3_access_key)->not->toBe('test-access-key') - ->and(Crypt::decryptString($updatedDestination->s3_access_key))->toBe('test-access-key') - ->and($updatedDestination->s3_secret_key)->not->toBe('test-secret-key') - ->and(Crypt::decryptString($updatedDestination->s3_secret_key))->toBe('test-secret-key') - ->and($updatedDestination->s3_bucket_name)->not->toBe('test-bucket') - ->and(Crypt::decryptString($updatedDestination->s3_bucket_name))->toBe('test-bucket') - ->and($updatedDestination->custom_s3_endpoint)->not->toBe('test-endpoint') - ->and(Crypt::decryptString($updatedDestination->custom_s3_endpoint))->toBe('test-endpoint'); -}); - -it('does not encrypt the fields if they are already encrypted', function (): void { - $backupDestination = BackupDestination::factory()->create([ - 's3_access_key' => Crypt::encryptString('test-access-key'), - 's3_secret_key' => Crypt::encryptString('test-secret-key'), - 's3_bucket_name' => Crypt::encryptString('test-bucket'), - 'custom_s3_endpoint' => Crypt::encryptString('test-endpoint'), - ]); - - $this->artisan(EncryptBackupDestinationsCommand::class) - ->expectsOutputToContain('0 backup destinations have been updated with encrypted fields.') - ->assertExitCode(0); - - $updatedDestination = $backupDestination->fresh(); - - expect(Crypt::decryptString($updatedDestination->s3_access_key))->toBe('test-access-key') - ->and(Crypt::decryptString($updatedDestination->s3_secret_key))->toBe('test-secret-key') - ->and(Crypt::decryptString($updatedDestination->s3_bucket_name))->toBe('test-bucket') - ->and(Crypt::decryptString($updatedDestination->custom_s3_endpoint))->toBe('test-endpoint'); -}); diff --git a/tests/Feature/RemoteServers/Commands/EncryptAllUnencryptedDatabasePasswordsTest.php b/tests/Feature/RemoteServers/Commands/EncryptAllUnencryptedDatabasePasswordsTest.php deleted file mode 100644 index 33f82247..00000000 --- a/tests/Feature/RemoteServers/Commands/EncryptAllUnencryptedDatabasePasswordsTest.php +++ /dev/null @@ -1,51 +0,0 @@ -artisan(EncryptDatabasePasswordsCommand::class) - ->expectsOutputToContain('No remote servers found.') - ->assertExitCode(0); -}); - -it('skips encryption if the database password is empty', function (): void { - - $remoteServer = RemoteServer::factory()->create([ - 'database_password' => '', - ]); - - $this->artisan(EncryptDatabasePasswordsCommand::class) - ->expectsOutputToContain(sprintf('Database password for remote server %s is empty. Skipping encryption.', $remoteServer->label)) - ->assertExitCode(0); -}); - -it('encrypts the database password', function (): void { - - $remoteServer = RemoteServer::factory()->create([ - 'database_password' => 'password', - ]); - - $this->artisan(EncryptDatabasePasswordsCommand::class) - ->expectsOutputToContain('1 database passwords have been encrypted.') - ->assertExitCode(0); - - $this->assertNotSame('password', $remoteServer->fresh()->database_password); - $this->assertSame('password', Crypt::decryptString($remoteServer->fresh()->database_password)); -}); - -it('does not encrypt the database password if it is already encrypted', function (): void { - - $remoteServer = RemoteServer::factory()->create([ - 'database_password' => Crypt::encryptString('password'), - ]); - - $this->artisan(EncryptDatabasePasswordsCommand::class) - ->expectsOutputToContain('0 database passwords have been encrypted.') - ->assertExitCode(0); - - $this->assertSame('password', Crypt::decryptString($remoteServer->fresh()->database_password)); -});