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
fix(ci): mint API key via tinker eval, not <?php stdin pipe
Ronald A. Richardson070da97f1f1d
diff --git a/.github/actions/postman-contract/action.yml b/.github/actions/postman-contract/action.ymlindex ed5ad914..b987559d 100644--- a/.github/actions/postman-contract/action.yml+++ b/.github/actions/postman-contract/action.yml@@ -52,19 +52,29 @@ runs:if: steps.pmauth.outputs.available == 'true'shell: bashrun: |- set -euo pipefail- KEY="$(docker compose exec -T application php artisan tinker < scripts/ci/mint-api-key.php 2>/dev/null \- | grep -oE 'flb_(live|test)_[A-Za-z0-9_-]+' | tail -1)"+ set -uo pipefail+ # Run the mint snippet via `tinker --execute="eval(base64_decode(...))"`.+ # Piping a file with a <?php tag to tinker triggers a PsySH parse error, and+ # inlining the code would let the shell expand its $variables — base64+eval+ # sidesteps both. Capture stdout+stderr so failures are diagnosable.+ CODE="$(base64 scripts/ci/mint-api-key.php | tr -d '\n')"+ OUT="$(docker compose exec -T application php artisan tinker --execute="eval(base64_decode('${CODE}'));" 2>&1 || true)"+ KEY="$(printf '%s\n' "$OUT" | grep -oE 'flb_(live|test)_[A-Za-z0-9_-]+' | tail -1 || true)"if [[ -z "$KEY" ]]; then- echo "::error::Could not mint an API key from the running stack."+ echo "::error::Could not mint an API key from the running stack. Tinker output:"+ printf '%s\n' "$OUT"exit 1fiecho "::add-mask::$KEY"echo "api_key=$KEY" >> "$GITHUB_OUTPUT"echo "Verifying key against ${{ inputs.base-url }}/${{ inputs.namespace }}/organizations/current ..."- curl -fsS -H "Authorization: Bearer $KEY" \- "${{ inputs.base-url }}/${{ inputs.namespace }}/organizations/current" >/dev/null- echo "✔ API key authenticates."+ HTTP_CODE="$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer ${KEY}" \+ "${{ inputs.base-url }}/${{ inputs.namespace }}/organizations/current" || true)"+ if [[ "$HTTP_CODE" != "200" ]]; then+ echo "::error::API key verification returned HTTP ${HTTP_CODE} (expected 200)"+ exit 1+ fi+ echo "✔ API key authenticates (HTTP 200)."- name: Install Postman CLIif: steps.pmauth.outputs.available == 'true'diff --git a/scripts/ci/mint-api-key.php b/scripts/ci/mint-api-key.phpindex 68ece5a9..048bf30d 100644--- a/scripts/ci/mint-api-key.php+++ b/scripts/ci/mint-api-key.php@@ -1,42 +1,46 @@-<?php+// scripts/ci/mint-api-key.php+//+// CI helper: mint a working Fleetbase public-API key against a booted stack.+//+// This file is NOT a standalone script. It is base64-encoded and eval()'d inside+// `php artisan tinker --execute="eval(base64_decode('...'));"`. Piping a file that+// begins with a <?php tag to tinker triggers a PsySH parse error ("unexpected '<'"),+// so this snippet intentionally OMITS the opening <?php tag and uses fully-qualified+// class names instead of `use` statements.+//+// It idempotently creates an organization + owner user + a live API credential and+// prints the flb_live_ key. The key is generated by ApiCredentialObserver on insert,+// so the credential MUST be created through the Eloquent model, then reloaded. The+// company must have an owner or the public-API middleware rejects the key (401).-/*- * CI helper: mint a working Fleetbase public-API key against a booted stack.- *- * Run it by piping into the application container's tinker:- * docker compose exec -T application php artisan tinker < scripts/ci/mint-api-key.php- *- * It creates (idempotently) an organization + owner user + a live API credential and- * prints the `flb_live_...` key on its own line. The public API middleware- * (AuthenticateOnceWithBasicAuth) authenticates `Authorization: Bearer <key>` against- * the api_credentials.key column, and requires the company to have an owner.- *- * The key value is generated by ApiCredentialObserver on insert, so the credential- * MUST be created through the Eloquent model (not a raw insert), then reloaded.- */+try {+ $company = \Fleetbase\Models\Company::firstOrCreate(['name' => 'CI Contract Org']);-use Fleetbase\Models\ApiCredential;-use Fleetbase\Models\Company;-use Fleetbase\Models\User;+ $user = \Fleetbase\Models\User::where('email', '[email protected]')->first();+ if (!$user) {+ $user = new \Fleetbase\Models\User();+ $user->name = 'CI Contract User';+ $user->email = '[email protected]';+ $user->company_uuid = $company->uuid;+ $user->type = 'user';+ $user->status = 'active';+ $user->password = 'CiContract1!' . bin2hex(random_bytes(6));+ $user->save();+ }-$company = Company::firstOrCreate(['name' => 'CI Contract Org']);+ if (empty($company->owner_uuid)) {+ $company->setOwner($user, true);+ $company->save();+ }-$user = User::firstOrCreate(- ['email' => '[email protected]'],- ['name' => 'CI Contract User', 'company_uuid' => $company->uuid, 'status' => 'active']-);-$user->type = 'user';-$user->password = 'CiContract1!' . bin2hex(random_bytes(6));-$user->save();+ // test_mode=false => flb_live_ key stored on the default (mysql) connection.+ $credential = \Fleetbase\Models\ApiCredential::firstOrCreate(+ ['company_uuid' => $company->uuid, 'name' => 'CI Contract Key'],+ ['user_uuid' => $user->uuid, 'test_mode' => false]+ );-// The company must have an owner or the API-key middleware rejects requests (401).-$company->setOwner($user, true);-$company->save();--// test_mode=false => flb_live_ key stored on the default (mysql) connection.-$credential = ApiCredential::firstOrCreate(- ['company_uuid' => $company->uuid, 'name' => 'CI Contract Key'],- ['user_uuid' => $user->uuid, 'test_mode' => false]-);--echo PHP_EOL . $credential->fresh()->key . PHP_EOL;+ echo PHP_EOL . 'MINTED_API_KEY=' . $credential->fresh()->key . PHP_EOL;+} catch (\Throwable $e) {+ echo PHP_EOL . 'MINT_ERROR: ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL;+ echo $e->getFile() . ':' . $e->getLine() . PHP_EOL;+}