Modular logistics and supply chain operating system (LSOS)
JavaScript driver-appfleet-managementfreight-managementinfrastructureinventory-managementlast-mile-deliverylogisticsondemandopen-sourceorder-managementroute-optimizationsupply-chain-managementtracking-and-monitoringtransport-management-systemtransportationwarehouse-management
// scripts/ci/mint-k6-keys.php
//
// CI helper: mint the three public-API keys the k6 release benchmark needs
// (tests/k6/api-release.js) against a booted stack.
//
// Like mint-api-key.php this is NOT a standalone script. It is base64-encoded and
// eval()'d inside `php artisan tinker --execute="eval(base64_decode('...'));"`, so it
// intentionally OMITS the opening <?php tag and uses fully-qualified class names.
//
// docker compose exec -T application php artisan tinker \
// --execute="eval(base64_decode('$(base64 < scripts/ci/mint-k6-keys.php | tr -d '\n')'));"
//
// Each role gets its OWN organization, owner and live credential:
//
// load — the throughput scenario. The workflow adds this key to
// THROTTLE_UNLIMITED_API_KEYS after minting, so it measures the API rather
// than the rate limiter.
// noisy — the noisy-neighbour scenario's flood. Normal per-key limit; expected to
// be throttled hard.
// victim — a different tenant sending a trickle during the flood. Must never be
// throttled: that is the regression gate for the shared-bucket incident.
//
// Separate organizations matter: per-organization overrides (ApiRateLimits) key on
// company_uuid, and the incident was one tenant starving every other tenant.
//
// Idempotent: reruns reuse the same org, user and credential (firstOrCreate on stable
// names), so a local rerun prints the same keys and does not pile up rows.
//
// Output (one per line, parsed by .github/workflows/performance.yml):
// K6_LOAD_KEY=flb_live_... K6_NOISY_KEY=flb_live_... K6_VICTIM_KEY=flb_live_...
// K6_RATE_LIMIT=<effective per-key requests/minute, or "unlimited">
// K6_THROTTLE_ENABLED=true|false
// K6_MINT_OK=1 (last line; its absence means the snippet aborted)
// K6_MINT_ERROR: ... (on exception)
try {
$roles = [
'load' => ['org' => 'CI k6 Load Org', 'email' => '[email protected]', 'name' => 'CI k6 Load'],
'noisy' => ['org' => 'CI k6 Noisy Org', 'email' => '[email protected]', 'name' => 'CI k6 Noisy'],
'victim' => ['org' => 'CI k6 Victim Org', 'email' => '[email protected]', 'name' => 'CI k6 Victim'],
];
$keys = [];
foreach ($roles as $role => $spec) {
$company = \Fleetbase\Models\Company::firstOrCreate(['name' => $spec['org']]);
$user = \Fleetbase\Models\User::where('email', $spec['email'])->withoutGlobalScopes()->first();
if (!$user) {
$user = new \Fleetbase\Models\User();
$user->name = $spec['name'];
$user->email = $spec['email'];
$user->company_uuid = $company->uuid;
$user->type = 'user';
$user->status = 'active';
// Never used to log in; random so no guessable password exists on the stack.
$user->password = 'CiK6!' . bin2hex(random_bytes(12));
$user->save();
}
// The public-API middleware rejects a key whose company has no owner (401).
if (empty($company->owner_uuid)) {
$company->setOwner($user, true);
$company->save();
}
// test_mode=false => flb_live_ key on the default (mysql) connection. The key is
// generated by ApiCredentialObserver on insert, hence create-then-fresh().
$credential = \Fleetbase\Models\ApiCredential::firstOrCreate(
['company_uuid' => $company->uuid, 'name' => 'CI k6 ' . ucfirst($role) . ' Key'],
['user_uuid' => $user->uuid, 'test_mode' => false]
);
$keys[$role] = $credential->fresh()->key;
if (empty($keys[$role])) {
throw new \RuntimeException("Credential for role '{$role}' has no key after insert.");
}
}
echo PHP_EOL;
echo 'K6_LOAD_KEY=' . $keys['load'] . PHP_EOL;
echo 'K6_NOISY_KEY=' . $keys['noisy'] . PHP_EOL;
echo 'K6_VICTIM_KEY=' . $keys['victim'] . PHP_EOL;
// Report what the limiter will actually enforce for the noisy tenant, so the k6 run
// can size its flood and the report can state the limit it was measured against.
// Administrator overrides (system.rate-limits) win over the environment.
// ApiRateLimits arrived in core-api 1.6.66; on an older core-api the limits are
// environment-only, so read them from config instead of failing the whole run.
if (class_exists(\Fleetbase\Support\ApiRateLimits::class)) {
$settings = \Fleetbase\Support\ApiRateLimits::settings();
$noisyOrg = \Fleetbase\Models\Company::where('name', $roles['noisy']['org'])->value('uuid');
$noisyLimit = \Fleetbase\Support\ApiRateLimits::limitFor($noisyOrg, $settings);
} else {
$settings = ['enabled' => config('api.throttle.enabled', true) !== false];
$noisyLimit = (int) config('api.throttle.max_attempts', 120);
}
echo 'K6_THROTTLE_ENABLED=' . ($settings['enabled'] ? 'true' : 'false') . PHP_EOL;
echo 'K6_RATE_LIMIT=' . ($noisyLimit === null ? 'unlimited' : $noisyLimit) . PHP_EOL;
echo 'K6_MINT_OK=1' . PHP_EOL;
} catch (\Throwable $e) {
echo PHP_EOL . 'K6_MINT_ERROR: ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL;
echo $e->getFile() . ':' . $e->getLine() . PHP_EOL;
}