|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services {
|
|
|
|
|
final class Config
|
|
|
|
|
{
|
|
|
|
|
public static function get($name, $default = null)
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'appName' => 'Virt Cloud',
|
|
|
|
|
'baseUrl' => 'https://cloud.example.test',
|
|
|
|
|
][$name] ?? $default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Isolate the outbox state machine from a real SMTP server. */
|
|
|
|
|
final class Mail
|
|
|
|
|
{
|
|
|
|
|
public static bool $accepted = true;
|
|
|
|
|
public static array $messages = [];
|
|
|
|
|
|
|
|
|
|
public static function send($to, $subject, $template, $data = [],
|
|
|
|
|
$files = [], $emailType = null): bool
|
|
|
|
|
{
|
|
|
|
|
self::$messages[] = compact('to', 'subject', 'template', 'data',
|
|
|
|
|
'files', 'emailType');
|
|
|
|
|
return self::$accepted;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Models\CloudInstance;
|
|
|
|
|
use App\Models\CloudInvoice;
|
|
|
|
|
use App\Models\CloudJob;
|
|
|
|
|
use App\Models\CloudMailOutbox;
|
|
|
|
|
use App\Models\CloudOrder;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Services\Cloud\CloudInvoiceService;
|
|
|
|
|
use App\Services\Mail;
|
|
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
|
|
|
|
|
|
$assert = static function ($condition, string $message): void {
|
|
|
|
|
if (!$condition) {
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$capsule = new Capsule();
|
|
|
|
|
$capsule->addConnection([
|
|
|
|
|
'driver' => 'sqlite',
|
|
|
|
|
'database' => ':memory:',
|
|
|
|
|
], 'default');
|
|
|
|
|
$capsule->setAsGlobal();
|
|
|
|
|
$capsule->bootEloquent();
|
|
|
|
|
$schema = $capsule->schema('default');
|
|
|
|
|
|
|
|
|
|
$schema->create('user', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('email');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->string('service_status');
|
|
|
|
|
$table->integer('expires_at')->nullable();
|
|
|
|
|
$table->text('spec_json');
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_orders', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->integer('instance_id')->nullable();
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->boolean('finalization_incident')->default(0);
|
|
|
|
|
$table->string('finalization_error_code')->nullable();
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('order_id')->nullable();
|
|
|
|
|
$table->integer('instance_id')->nullable();
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->string('execution_mode')->default('agent');
|
|
|
|
|
$table->string('phase')->default('accepted');
|
|
|
|
|
$table->string('outcome')->nullable();
|
|
|
|
|
$table->string('status');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_invoices', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('invoice_no')->unique();
|
|
|
|
|
$table->string('purpose');
|
|
|
|
|
$table->integer('instance_id')->nullable();
|
|
|
|
|
$table->integer('order_id')->nullable();
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->integer('due_at');
|
|
|
|
|
$table->string('amount');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_mail_outbox', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('invoice_id');
|
|
|
|
|
$table->string('kind');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->integer('attempts');
|
|
|
|
|
$table->integer('available_at');
|
|
|
|
|
$table->string('lease_token')->nullable();
|
|
|
|
|
$table->integer('lease_until');
|
|
|
|
|
$table->integer('sent_at')->nullable();
|
|
|
|
|
$table->string('last_error');
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
$table->unique(['invoice_id', 'kind']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
|
foreach ([1 => 'owner@example.test', 2 => 'other@example.test'] as $id => $email) {
|
|
|
|
|
$user = new User();
|
|
|
|
|
$user->id = $id;
|
|
|
|
|
$user->email = $email;
|
|
|
|
|
$user->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$newInstance = static function (int $userId, string $name, string $ip) use ($now): CloudInstance {
|
|
|
|
|
$instance = new CloudInstance();
|
|
|
|
|
$instance->user_id = $userId;
|
|
|
|
|
$instance->name = $name;
|
|
|
|
|
$instance->status = 'active';
|
|
|
|
|
$instance->service_status = 'active';
|
|
|
|
|
$instance->expires_at = $now + 30 * 86400;
|
|
|
|
|
$instance->spec_json = json_encode([
|
|
|
|
|
'region' => 'HongKong',
|
|
|
|
|
'public_network' => [
|
|
|
|
|
'contract' => 1,
|
|
|
|
|
'network_selector' => 'bridge:br0',
|
|
|
|
|
'ipv4' => [
|
|
|
|
|
'pool_key' => str_repeat('1', 32),
|
|
|
|
|
'pool_cidr' => '8.8.8.0/24',
|
|
|
|
|
'address_cidr' => $ip . '/24',
|
|
|
|
|
'gateway' => '8.8.8.1',
|
|
|
|
|
'gateway_on_link' => false,
|
|
|
|
|
],
|
|
|
|
|
'ipv6' => [
|
|
|
|
|
'pool_key' => str_repeat('2', 32),
|
|
|
|
|
'pool_cidr' => '2404:c140:156:3a::/64',
|
|
|
|
|
'address_cidr' => '2404:c140:156:3a::11/64',
|
|
|
|
|
'gateway' => '2404:c140:156::1',
|
|
|
|
|
'gateway_on_link' => true,
|
|
|
|
|
],
|
|
|
|
|
'dns_servers' => [
|
|
|
|
|
'1.1.1.1',
|
|
|
|
|
'2606:4700:4700::1111',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'os_name' => 'AlmaLinux 10',
|
|
|
|
|
'login_name' => 'root',
|
|
|
|
|
// Secrets in old snapshots must never be forwarded to a template.
|
|
|
|
|
'root_password' => 'not-for-email',
|
|
|
|
|
], JSON_THROW_ON_ERROR);
|
|
|
|
|
$instance->created_at = $instance->updated_at = $now;
|
|
|
|
|
$instance->save();
|
|
|
|
|
return $instance;
|
|
|
|
|
};
|
|
|
|
|
$newInvoice = static function (string $number, string $purpose,
|
|
|
|
|
int $userId, CloudInstance $instance, string $status = 'open',
|
|
|
|
|
?CloudOrder $order = null) use ($now): CloudInvoice {
|
|
|
|
|
$invoice = new CloudInvoice();
|
|
|
|
|
$invoice->invoice_no = $number;
|
|
|
|
|
$invoice->purpose = $purpose;
|
|
|
|
|
$invoice->instance_id = $instance->id;
|
|
|
|
|
$invoice->order_id = $order?->id;
|
|
|
|
|
$invoice->user_id = $userId;
|
|
|
|
|
$invoice->due_at = $now + 86400;
|
|
|
|
|
$invoice->amount = '12.00';
|
|
|
|
|
$invoice->status = $status;
|
|
|
|
|
$invoice->created_at = $invoice->updated_at = $now;
|
|
|
|
|
$invoice->save();
|
|
|
|
|
return $invoice;
|
|
|
|
|
};
|
|
|
|
|
$newMail = static function (CloudInvoice $invoice, string $kind) use ($now): CloudMailOutbox {
|
|
|
|
|
$mail = new CloudMailOutbox();
|
|
|
|
|
$mail->invoice_id = $invoice->id;
|
|
|
|
|
$mail->kind = $kind;
|
|
|
|
|
$mail->status = 'pending';
|
|
|
|
|
$mail->attempts = 0;
|
|
|
|
|
$mail->available_at = $now;
|
|
|
|
|
$mail->lease_token = null;
|
|
|
|
|
$mail->lease_until = 0;
|
|
|
|
|
$mail->sent_at = null;
|
|
|
|
|
$mail->last_error = '';
|
|
|
|
|
$mail->created_at = $mail->updated_at = $now;
|
|
|
|
|
$mail->save();
|
|
|
|
|
return $mail;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$renewalInstance = $newInstance(1, 'renewal-vm', '8.8.8.10');
|
|
|
|
|
$renewal = $newInvoice('VC-MAIL-RENEWAL', 'renewal', 1, $renewalInstance);
|
|
|
|
|
$renewalMail = $newMail($renewal, 'renewal_due');
|
|
|
|
|
$assert(CloudInvoiceService::deliverReminders(1) === 1
|
|
|
|
|
&& (string) $renewalMail->fresh()->status === 'sent'
|
|
|
|
|
&& (Mail::$messages[0]['template'] ?? null) === 'cloud/invoice.tpl'
|
|
|
|
|
&& (Mail::$messages[0]['emailType'] ?? null) === 'cloud_renewal_invoice'
|
|
|
|
|
&& (Mail::$messages[0]['data']['payment_deadline_at'] ?? null)
|
|
|
|
|
=== date('Y-m-d H:i:s', $now + 4 * 86400)
|
|
|
|
|
&& (string) (Mail::$messages[0]['subject'] ?? '')
|
|
|
|
|
=== 'Renewal Invoice Created',
|
|
|
|
|
'An open automatic renewal bill was not delivered and marked sent.');
|
|
|
|
|
|
|
|
|
|
$provisionedInstance = $newInstance(1, 'ready-vm', '8.8.8.11');
|
|
|
|
|
$order = new CloudOrder();
|
|
|
|
|
$order->user_id = 1;
|
|
|
|
|
$order->instance_id = $provisionedInstance->id;
|
|
|
|
|
$order->status = 'succeeded';
|
|
|
|
|
$order->save();
|
|
|
|
|
Capsule::table('cloud_jobs')->insert([
|
|
|
|
|
'order_id' => $order->id,
|
|
|
|
|
'instance_id' => $provisionedInstance->id,
|
|
|
|
|
'user_id' => 1,
|
|
|
|
|
'execution_mode' => 'agent',
|
|
|
|
|
'phase' => 'terminal',
|
|
|
|
|
'outcome' => 'succeeded',
|
|
|
|
|
'status' => 'succeeded',
|
|
|
|
|
]);
|
|
|
|
|
$provision = $newInvoice('VC-MAIL-PROVISION', 'provision', 1,
|
|
|
|
|
$provisionedInstance, 'paid', $order);
|
|
|
|
|
$assert(CloudInvoiceService::queueProvisionSucceeded($provision, $now),
|
|
|
|
|
'A successful provision could not enqueue its email.');
|
|
|
|
|
$assert(CloudInvoiceService::deliverReminders(1) === 1,
|
|
|
|
|
'A successful provision email was not delivered.');
|
|
|
|
|
$provisionMessage = Mail::$messages[1] ?? [];
|
|
|
|
|
$assert(($provisionMessage['template'] ?? null) === 'cloud/provision-success.tpl'
|
|
|
|
|
&& ($provisionMessage['emailType'] ?? null) === 'cloud_provision_success'
|
|
|
|
|
&& (string) ($provisionMessage['subject'] ?? '')
|
|
|
|
|
=== 'Instance Provisioned'
|
|
|
|
|
&& ($provisionMessage['data']['instance_name'] ?? null) === 'ready-vm'
|
|
|
|
|
&& ($provisionMessage['data']['public_ip'] ?? null) === '8.8.8.11'
|
|
|
|
|
&& !array_key_exists('root_password', $provisionMessage['data'] ?? [])
|
|
|
|
|
&& !array_key_exists('private_key', $provisionMessage['data'] ?? []),
|
|
|
|
|
'The provision-success message is incomplete or contains credentials.');
|
|
|
|
|
|
|
|
|
|
Mail::$accepted = false;
|
|
|
|
|
$retryInstance = $newInstance(1, 'retry-vm', '8.8.8.12');
|
|
|
|
|
$retryInvoice = $newInvoice('VC-MAIL-RETRY', 'renewal', 1, $retryInstance);
|
|
|
|
|
$retryMail = $newMail($retryInvoice, 'renewal_due');
|
|
|
|
|
$assert(CloudInvoiceService::deliverReminders(1) === 0
|
|
|
|
|
&& (string) $retryMail->fresh()->status === 'failed'
|
|
|
|
|
&& (int) $retryMail->fresh()->attempts === 1
|
|
|
|
|
&& (int) $retryMail->fresh()->available_at === 0,
|
|
|
|
|
'A rejected SMTP message was not closed after one attempt.');
|
|
|
|
|
Mail::$accepted = true;
|
|
|
|
|
$sentBeforeRetryProbe = count(Mail::$messages);
|
|
|
|
|
$assert(CloudInvoiceService::deliverReminders(1) === 0
|
|
|
|
|
&& (string) $retryMail->fresh()->status === 'failed'
|
|
|
|
|
&& (int) $retryMail->fresh()->attempts === 1
|
|
|
|
|
&& count(Mail::$messages) === $sentBeforeRetryProbe,
|
|
|
|
|
'A terminal failed SMTP message was retried.');
|
|
|
|
|
|
|
|
|
|
$otherInstance = $newInstance(2, 'other-tenant-vm', '8.8.8.13');
|
|
|
|
|
$corruptInvoice = $newInvoice('VC-MAIL-CROSS-TENANT', 'renewal', 1,
|
|
|
|
|
$otherInstance);
|
|
|
|
|
$corruptMail = $newMail($corruptInvoice, 'renewal_due');
|
|
|
|
|
$sentBefore = count(Mail::$messages);
|
|
|
|
|
$assert(CloudInvoiceService::deliverReminders(1) === 0
|
|
|
|
|
&& (string) $corruptMail->fresh()->status === 'cancelled'
|
|
|
|
|
&& count(Mail::$messages) === $sentBefore,
|
|
|
|
|
'A cross-tenant invoice/instance association reached the mail transport.');
|
|
|
|
|
|
|
|
|
|
echo "Cloud renewal and provision email outbox dispatch passed.\n";
|
|
|
|
|
}
|