feat(probe): add a function measuring the open platform factsUnverified
A dependency-free PHP function whose handler reports the raw Event and Context, process reuse, filesystem writability, superglobals, environment and ini settings, and returns multi-value headers, binary bodies, errors and stdout on demand. Its zip ships a hand-written vendor/ to reveal whether Scaleway runs Composer over it. The README lists the curl commands to run and what to report back; the Terraform code was never applied.
0fec100 parent: fb654f0 added
probe/.gitignore +3 -0 | new file mode 100644 | ||
| @@ -0,0 +1,3 @@ | ||
| 1 | +/terraform/.build/ | |
| 2 | +/terraform/.terraform/ | |
| 3 | +/terraform/terraform.tfstate* | |
| new file mode 100644 | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | +/terraform/.build/ | ||
| 2 | +/terraform/.terraform/ | ||
| 3 | +/terraform/terraform.tfstate* | ||
added
probe/README.md +102 -0 | new file mode 100644 | ||
| @@ -0,0 +1,102 @@ | ||
| 1 | +# Platform probe | |
| 2 | + | |
| 3 | +A dependency-free PHP function that reports what Scaleway Serverless Functions pass to, and accept from, a PHP handler. | |
| 4 | +Its answers settle the open questions of the [implementation plan](../docs/plan.md) before the runtime relies on them. | |
| 5 | + | |
| 6 | +The Terraform code is unvalidated: it was written against the `scaleway/scaleway` provider v2.92 documentation and never applied. | |
| 7 | + | |
| 8 | +## Deploy | |
| 9 | + | |
| 10 | +Requirements: Terraform 1.5+, `curl`, and Scaleway API credentials. | |
| 11 | + | |
| 12 | +```sh | |
| 13 | +export SCW_ACCESS_KEY=... SCW_SECRET_KEY=... SCW_DEFAULT_PROJECT_ID=... | |
| 14 | +terraform -chdir=probe/terraform init | |
| 15 | +terraform -chdir=probe/terraform apply | |
| 16 | +export PROBE_URL=$(terraform -chdir=probe/terraform output -raw endpoint) | |
| 17 | +``` | |
| 18 | + | |
| 19 | +To probe the PHP 8.4 runtime too, run `terraform -chdir=probe/terraform apply -var runtime=php84` afterwards and repeat the checks. | |
| 20 | + | |
| 21 | +## Run the checks | |
| 22 | + | |
| 23 | +Save every output to a file and send them back. | |
| 24 | +The probe redacts the values of variables whose name contains SECRET, TOKEN, PASSWORD or KEY, but review the files before sharing them. | |
| 25 | + | |
| 26 | +```sh | |
| 27 | +mkdir -p probe-results && cd probe-results | |
| 28 | + | |
| 29 | +# 1. Event, Context, $_SERVER, argv, environment, ini, extensions, filesystem facts. | |
| 30 | +curl -sS "$PROBE_URL/some/path?single=1&repeated=a&repeated=b&list[]=x&list[]=y&encoded=caf%C3%A9&plus=a+b" \ | |
| 31 | + -H 'X-Custom: hello' -H 'Cookie: a=1; b=2' > 01-facts.json | |
| 32 | + | |
| 33 | +# 2. Warm Process reuse: invocationsInThisProcess and pid across calls a few seconds apart. | |
| 34 | +for i in 1 2 3; do curl -sS "$PROBE_URL/" | grep -E '"(pid|invocationsInThisProcess|firstInvocationAt)"'; sleep 2; done > 02-warm.txt | |
| 35 | + | |
| 36 | +# 3. X-Forwarded-* sent by the client: passed through, appended to or overwritten? | |
| 37 | +curl -sS "$PROBE_URL/" -H 'X-Forwarded-For: 203.0.113.66' -H 'X-Forwarded-Proto: http' \ | |
| 38 | + -H 'X-Forwarded-Host: spoofed.example' -H 'X-Forwarded-Port: 1234' -H 'Forwarded: for=203.0.113.66' > 03-forwarded.json | |
| 39 | + | |
| 40 | +# 4. Multi-value response headers: are the list values accepted and sent as repeated headers? | |
| 41 | +curl -sS -i "$PROBE_URL/?mode=headers" > 04-headers.txt | |
| 42 | + | |
| 43 | +# 5. Binary responses and the minimal documented response. | |
| 44 | +curl -sS "$PROBE_URL/?mode=binary" | od -An -tx1 | head -3 > 05-binary.txt | |
| 45 | +curl -sS -i "$PROBE_URL/?mode=no-headers" > 05-no-headers.txt | |
| 46 | +curl -sS -i "$PROBE_URL/?mode=status&code=418" > 05-status.txt | |
| 47 | + | |
| 48 | +# 6. Request bodies: text, binary, form, multipart, and the size limit (6 MiB documented). | |
| 49 | +curl -sS "$PROBE_URL/?mode=body" -H 'Content-Type: application/json' --data '{"a":"é"}' > 06-body-text.json | |
| 50 | +head -c 4096 /dev/urandom > random.bin | |
| 51 | +curl -sS "$PROBE_URL/?mode=body" -H 'Content-Type: application/octet-stream' --data-binary @random.bin > 06-body-binary.json | |
| 52 | +openssl dgst -sha256 random.bin > 06-body-binary.sha256 | |
| 53 | +curl -sS "$PROBE_URL/?mode=facts" -F field=value -F file=@random.bin > 06-body-multipart.json | |
| 54 | +for size in 5 6 7; do | |
| 55 | + head -c $((size * 1024 * 1024)) /dev/zero | tr '\0' a > body-$size.txt | |
| 56 | + curl -sS -o 06-body-${size}MiB.txt -w "${size} MiB: HTTP %{http_code}\n" "$PROBE_URL/?mode=body" --data-binary @body-$size.txt | |
| 57 | +done > 06-body-sizes.txt | |
| 58 | + | |
| 59 | +# 7. Output and failures: where does stdout go, what does an exception produce? | |
| 60 | +curl -sS -i "$PROBE_URL/?mode=stdout" > 07-stdout.txt | |
| 61 | +curl -sS -i "$PROBE_URL/?mode=exception" > 07-exception.txt | |
| 62 | + | |
| 63 | +# 8. Other methods and HEAD. | |
| 64 | +curl -sS -X PUT "$PROBE_URL/" --data 'put body' > 08-put.json | |
| 65 | +curl -sS -I "$PROBE_URL/" > 08-head.txt | |
| 66 | + | |
| 67 | +# 9. CRON: wait for a run (every 5 minutes), then read the Events this instance recorded. | |
| 68 | +# If the CRON ran on another instance, the history misses it: use the logs (step 10) instead. | |
| 69 | +sleep 330; curl -sS "$PROBE_URL/?mode=history" > 09-history.ndjson | |
| 70 | +``` | |
| 71 | + | |
| 72 | +10. In the Scaleway console, open the function's logs (Cockpit) and export the lines starting with `probe:` for the last hour, including the CRON ones, plus the `probe: written to stdout` and exception lines. | |
| 73 | + | |
| 74 | +## Report back | |
| 75 | + | |
| 76 | +- All files of `probe-results/` (except `random.bin` and `body-*.txt`) and the exported logs. | |
| 77 | +- The runtime(s) probed (`php85`, `php84`), and anything surprising during `terraform apply` (for example whether Scaleway ran Composer over the shipped `vendor/`). | |
| 78 | + | |
| 79 | +The answers the files settle: | |
| 80 | + | |
| 81 | +| Question | Where to look | | |
| 82 | +| --- | --- | | |
| 83 | +| HTTP Event shape (keys, path encoding, repeated query keys, header name case) | `01-facts.json` `event` | | |
| 84 | +| CRON Event shape (method, path, body, headers) | `09-history.ndjson`, logs | | |
| 85 | +| Context type and content | `01-facts.json` `contextType`, `context` | | |
| 86 | +| Warm Process reuse | `02-warm.txt` | | |
| 87 | +| X-Forwarded-* spoofing | `03-forwarded.json` `event.headers` | | |
| 88 | +| Multi-value response headers | `04-headers.txt` | | |
| 89 | +| Default Content-Type, binary responses | `05-*.txt` | | |
| 90 | +| Body encoding (base64 or not), size limit | `06-*` | | |
| 91 | +| `$_SERVER`, `argv`, `SCRIPT_FILENAME`, superglobals | `01-facts.json` `globals` | | |
| 92 | +| Read-only code dir, writable `/tmp`, free space | `01-facts.json` `filesystem` | | |
| 93 | +| Shipped `vendor/` kept, Composer run at deploy | `01-facts.json` `vendorMarkerKept`, `composerLockPresent`, `vendorListing` | | |
| 94 | +| stdout, uncaught exceptions | `07-*.txt`, logs | | |
| 95 | + | |
| 96 | +## Clean up | |
| 97 | + | |
| 98 | +The probe is public and echoes request headers, destroy it once done: | |
| 99 | + | |
| 100 | +```sh | |
| 101 | +terraform -chdir=probe/terraform destroy | |
| 102 | +``` | |
| new file mode 100644 | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | +# Platform probe | ||
| 2 | + | ||
| 3 | +A dependency-free PHP function that reports what Scaleway Serverless Functions pass to, and accept from, a PHP handler. | ||
| 4 | +Its answers settle the open questions of the [implementation plan](../docs/plan.md) before the runtime relies on them. | ||
| 5 | + | ||
| 6 | +The Terraform code is unvalidated: it was written against the `scaleway/scaleway` provider v2.92 documentation and never applied. | ||
| 7 | + | ||
| 8 | +## Deploy | ||
| 9 | + | ||
| 10 | +Requirements: Terraform 1.5+, `curl`, and Scaleway API credentials. | ||
| 11 | + | ||
| 12 | +```sh | ||
| 13 | +export SCW_ACCESS_KEY=... SCW_SECRET_KEY=... SCW_DEFAULT_PROJECT_ID=... | ||
| 14 | +terraform -chdir=probe/terraform init | ||
| 15 | +terraform -chdir=probe/terraform apply | ||
| 16 | +export PROBE_URL=$(terraform -chdir=probe/terraform output -raw endpoint) | ||
| 17 | +``` | ||
| 18 | + | ||
| 19 | +To probe the PHP 8.4 runtime too, run `terraform -chdir=probe/terraform apply -var runtime=php84` afterwards and repeat the checks. | ||
| 20 | + | ||
| 21 | +## Run the checks | ||
| 22 | + | ||
| 23 | +Save every output to a file and send them back. | ||
| 24 | +The probe redacts the values of variables whose name contains SECRET, TOKEN, PASSWORD or KEY, but review the files before sharing them. | ||
| 25 | + | ||
| 26 | +```sh | ||
| 27 | +mkdir -p probe-results && cd probe-results | ||
| 28 | + | ||
| 29 | +# 1. Event, Context, $_SERVER, argv, environment, ini, extensions, filesystem facts. | ||
| 30 | +curl -sS "$PROBE_URL/some/path?single=1&repeated=a&repeated=b&list[]=x&list[]=y&encoded=caf%C3%A9&plus=a+b" \ | ||
| 31 | + -H 'X-Custom: hello' -H 'Cookie: a=1; b=2' > 01-facts.json | ||
| 32 | + | ||
| 33 | +# 2. Warm Process reuse: invocationsInThisProcess and pid across calls a few seconds apart. | ||
| 34 | +for i in 1 2 3; do curl -sS "$PROBE_URL/" | grep -E '"(pid|invocationsInThisProcess|firstInvocationAt)"'; sleep 2; done > 02-warm.txt | ||
| 35 | + | ||
| 36 | +# 3. X-Forwarded-* sent by the client: passed through, appended to or overwritten? | ||
| 37 | +curl -sS "$PROBE_URL/" -H 'X-Forwarded-For: 203.0.113.66' -H 'X-Forwarded-Proto: http' \ | ||
| 38 | + -H 'X-Forwarded-Host: spoofed.example' -H 'X-Forwarded-Port: 1234' -H 'Forwarded: for=203.0.113.66' > 03-forwarded.json | ||
| 39 | + | ||
| 40 | +# 4. Multi-value response headers: are the list values accepted and sent as repeated headers? | ||
| 41 | +curl -sS -i "$PROBE_URL/?mode=headers" > 04-headers.txt | ||
| 42 | + | ||
| 43 | +# 5. Binary responses and the minimal documented response. | ||
| 44 | +curl -sS "$PROBE_URL/?mode=binary" | od -An -tx1 | head -3 > 05-binary.txt | ||
| 45 | +curl -sS -i "$PROBE_URL/?mode=no-headers" > 05-no-headers.txt | ||
| 46 | +curl -sS -i "$PROBE_URL/?mode=status&code=418" > 05-status.txt | ||
| 47 | + | ||
| 48 | +# 6. Request bodies: text, binary, form, multipart, and the size limit (6 MiB documented). | ||
| 49 | +curl -sS "$PROBE_URL/?mode=body" -H 'Content-Type: application/json' --data '{"a":"é"}' > 06-body-text.json | ||
| 50 | +head -c 4096 /dev/urandom > random.bin | ||
| 51 | +curl -sS "$PROBE_URL/?mode=body" -H 'Content-Type: application/octet-stream' --data-binary @random.bin > 06-body-binary.json | ||
| 52 | +openssl dgst -sha256 random.bin > 06-body-binary.sha256 | ||
| 53 | +curl -sS "$PROBE_URL/?mode=facts" -F field=value -F file=@random.bin > 06-body-multipart.json | ||
| 54 | +for size in 5 6 7; do | ||
| 55 | + head -c $((size * 1024 * 1024)) /dev/zero | tr '\0' a > body-$size.txt | ||
| 56 | + curl -sS -o 06-body-${size}MiB.txt -w "${size} MiB: HTTP %{http_code}\n" "$PROBE_URL/?mode=body" --data-binary @body-$size.txt | ||
| 57 | +done > 06-body-sizes.txt | ||
| 58 | + | ||
| 59 | +# 7. Output and failures: where does stdout go, what does an exception produce? | ||
| 60 | +curl -sS -i "$PROBE_URL/?mode=stdout" > 07-stdout.txt | ||
| 61 | +curl -sS -i "$PROBE_URL/?mode=exception" > 07-exception.txt | ||
| 62 | + | ||
| 63 | +# 8. Other methods and HEAD. | ||
| 64 | +curl -sS -X PUT "$PROBE_URL/" --data 'put body' > 08-put.json | ||
| 65 | +curl -sS -I "$PROBE_URL/" > 08-head.txt | ||
| 66 | + | ||
| 67 | +# 9. CRON: wait for a run (every 5 minutes), then read the Events this instance recorded. | ||
| 68 | +# If the CRON ran on another instance, the history misses it: use the logs (step 10) instead. | ||
| 69 | +sleep 330; curl -sS "$PROBE_URL/?mode=history" > 09-history.ndjson | ||
| 70 | +``` | ||
| 71 | + | ||
| 72 | +10. In the Scaleway console, open the function's logs (Cockpit) and export the lines starting with `probe:` for the last hour, including the CRON ones, plus the `probe: written to stdout` and exception lines. | ||
| 73 | + | ||
| 74 | +## Report back | ||
| 75 | + | ||
| 76 | +- All files of `probe-results/` (except `random.bin` and `body-*.txt`) and the exported logs. | ||
| 77 | +- The runtime(s) probed (`php85`, `php84`), and anything surprising during `terraform apply` (for example whether Scaleway ran Composer over the shipped `vendor/`). | ||
| 78 | + | ||
| 79 | +The answers the files settle: | ||
| 80 | + | ||
| 81 | +| Question | Where to look | | ||
| 82 | +| --- | --- | | ||
| 83 | +| HTTP Event shape (keys, path encoding, repeated query keys, header name case) | `01-facts.json` `event` | | ||
| 84 | +| CRON Event shape (method, path, body, headers) | `09-history.ndjson`, logs | | ||
| 85 | +| Context type and content | `01-facts.json` `contextType`, `context` | | ||
| 86 | +| Warm Process reuse | `02-warm.txt` | | ||
| 87 | +| X-Forwarded-* spoofing | `03-forwarded.json` `event.headers` | | ||
| 88 | +| Multi-value response headers | `04-headers.txt` | | ||
| 89 | +| Default Content-Type, binary responses | `05-*.txt` | | ||
| 90 | +| Body encoding (base64 or not), size limit | `06-*` | | ||
| 91 | +| `$_SERVER`, `argv`, `SCRIPT_FILENAME`, superglobals | `01-facts.json` `globals` | | ||
| 92 | +| Read-only code dir, writable `/tmp`, free space | `01-facts.json` `filesystem` | | ||
| 93 | +| Shipped `vendor/` kept, Composer run at deploy | `01-facts.json` `vendorMarkerKept`, `composerLockPresent`, `vendorListing` | | ||
| 94 | +| stdout, uncaught exceptions | `07-*.txt`, logs | | ||
| 95 | + | ||
| 96 | +## Clean up | ||
| 97 | + | ||
| 98 | +The probe is public and echoes request headers, destroy it once done: | ||
| 99 | + | ||
| 100 | +```sh | ||
| 101 | +terraform -chdir=probe/terraform destroy | ||
| 102 | +``` | ||
added
probe/function/composer.json +5 -0 | new file mode 100644 | ||
| @@ -0,0 +1,5 @@ | ||
| 1 | +{ | |
| 2 | + "name": "jdecool/scaleway-runtime-probe", | |
| 3 | + "description": "Ships vendor/ to find out whether Scaleway runs Composer over it at deploy time", | |
| 4 | + "require": {} | |
| 5 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | +{ | ||
| 2 | + "name": "jdecool/scaleway-runtime-probe", | ||
| 3 | + "description": "Ships vendor/ to find out whether Scaleway runs Composer over it at deploy time", | ||
| 4 | + "require": {} | ||
| 5 | +} | ||
added
probe/function/handler.php +159 -0 | new file mode 100644 | ||
| @@ -0,0 +1,159 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/* | |
| 4 | + * Probe function: answers the open platform questions by reporting what Scaleway passes to, | |
| 5 | + * and accepts from, a PHP handler. Dependency-free on purpose; see ../README.md. | |
| 6 | + */ | |
| 7 | + | |
| 8 | +// Loading a shipped vendor/ from the handler's directory is one of the questions. | |
| 9 | +require __DIR__.'/vendor/autoload.php'; | |
| 10 | + | |
| 11 | +const PROBE_HISTORY = '/tmp/probe-history.jsonl'; | |
| 12 | + | |
| 13 | +function handle($event, $context) | |
| 14 | +{ | |
| 15 | + static $invocations = 0; | |
| 16 | + static $firstInvocationAt = null; | |
| 17 | + ++$invocations; | |
| 18 | + $firstInvocationAt ??= date(DATE_ATOM); | |
| 19 | + | |
| 20 | + // CRON Invocations have nobody to answer to: the logs and the history keep their Events. | |
| 21 | + $record = ['at' => date(DATE_ATOM), 'pid' => getmypid(), 'invocation' => $invocations, 'event' => $event, 'context' => $context]; | |
| 22 | + error_log('probe: '.json_encode($record, JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES)); | |
| 23 | + @file_put_contents(PROBE_HISTORY, json_encode($record, JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES).PHP_EOL, FILE_APPEND); | |
| 24 | + | |
| 25 | + $query = is_array($event['queryStringParameters'] ?? null) ? $event['queryStringParameters'] : []; | |
| 26 | + | |
| 27 | + switch ($query['mode'] ?? 'facts') { | |
| 28 | + case 'headers': | |
| 29 | + // Are list values accepted, and sent as repeated headers? | |
| 30 | + return [ | |
| 31 | + 'statusCode' => 200, | |
| 32 | + 'headers' => [ | |
| 33 | + 'Content-Type' => 'text/plain', | |
| 34 | + 'X-Probe-Single' => 'one', | |
| 35 | + 'X-Probe-Multi' => ['first', 'second'], | |
| 36 | + 'Set-Cookie' => ['probe_a=1; Path=/', 'probe_b=2; Path=/; HttpOnly'], | |
| 37 | + ], | |
| 38 | + 'body' => "Check the response headers.\n", | |
| 39 | + ]; | |
| 40 | + | |
| 41 | + case 'binary': | |
| 42 | + return [ | |
| 43 | + 'statusCode' => 200, | |
| 44 | + 'headers' => ['Content-Type' => 'application/octet-stream'], | |
| 45 | + 'body' => base64_encode(implode('', array_map('chr', range(0, 255)))), | |
| 46 | + 'isBase64Encoded' => true, | |
| 47 | + ]; | |
| 48 | + | |
| 49 | + case 'status': | |
| 50 | + return ['statusCode' => (int) ($query['code'] ?? 418), 'headers' => ['Content-Type' => 'text/plain'], 'body' => "status\n"]; | |
| 51 | + | |
| 52 | + case 'no-headers': | |
| 53 | + // The documented minimal response: which Content-Type does the platform add? | |
| 54 | + return ['statusCode' => 200, 'body' => 'no headers']; | |
| 55 | + | |
| 56 | + case 'body': | |
| 57 | + $body = (string) ($event['body'] ?? ''); | |
| 58 | + $decoded = true === ($event['isBase64Encoded'] ?? null) ? base64_decode($body) : $body; | |
| 59 | + | |
| 60 | + return json_response([ | |
| 61 | + 'isBase64Encoded' => $event['isBase64Encoded'] ?? null, | |
| 62 | + 'receivedLength' => strlen($body), | |
| 63 | + 'decodedLength' => strlen($decoded), | |
| 64 | + 'decodedSha256' => hash('sha256', $decoded), | |
| 65 | + ]); | |
| 66 | + | |
| 67 | + case 'stdout': | |
| 68 | + echo "probe: written to stdout by the handler\n"; | |
| 69 | + | |
| 70 | + return ['statusCode' => 200, 'headers' => ['Content-Type' => 'text/plain'], 'body' => "Is the stdout line in the logs or in this body?\n"]; | |
| 71 | + | |
| 72 | + case 'exception': | |
| 73 | + throw new RuntimeException('probe: exception escaping the handler'); | |
| 74 | + | |
| 75 | + case 'history': | |
| 76 | + return ['statusCode' => 200, 'headers' => ['Content-Type' => 'application/x-ndjson'], 'body' => (string) @file_get_contents(PROBE_HISTORY)]; | |
| 77 | + | |
| 78 | + default: | |
| 79 | + return json_response(facts($event, $context, $invocations, $firstInvocationAt)); | |
| 80 | + } | |
| 81 | +} | |
| 82 | + | |
| 83 | +function facts($event, $context, int $invocations, string $firstInvocationAt): array | |
| 84 | +{ | |
| 85 | + $tmpProbe = '/tmp/probe-write-'.getmypid(); | |
| 86 | + $codeProbe = __DIR__.'/probe-write-'.getmypid(); | |
| 87 | + | |
| 88 | + return [ | |
| 89 | + 'process' => [ | |
| 90 | + 'pid' => getmypid(), | |
| 91 | + 'ppid' => function_exists('posix_getppid') ? posix_getppid() : null, | |
| 92 | + 'uid' => function_exists('posix_geteuid') ? posix_geteuid() : getmyuid(), | |
| 93 | + 'user' => get_current_user(), | |
| 94 | + 'invocationsInThisProcess' => $invocations, | |
| 95 | + 'firstInvocationAt' => $firstInvocationAt, | |
| 96 | + 'sapi' => PHP_SAPI, | |
| 97 | + 'phpVersion' => PHP_VERSION, | |
| 98 | + 'memoryUsage' => memory_get_usage(true), | |
| 99 | + 'obLevel' => ob_get_level(), | |
| 100 | + 'includedFiles' => get_included_files(), | |
| 101 | + ], | |
| 102 | + 'event' => $event, | |
| 103 | + 'eventKeys' => is_array($event) ? array_keys($event) : null, | |
| 104 | + 'context' => $context, | |
| 105 | + 'contextType' => get_debug_type($context), | |
| 106 | + 'filesystem' => [ | |
| 107 | + 'cwd' => getcwd(), | |
| 108 | + 'handlerFile' => __FILE__, | |
| 109 | + 'codeDirWritable' => is_writable(__DIR__), | |
| 110 | + 'codeDirWriteWorks' => false !== @file_put_contents($codeProbe, 'x') && @unlink($codeProbe), | |
| 111 | + 'codeDirListing' => scandir(__DIR__), | |
| 112 | + 'vendorListing' => @scandir(__DIR__.'/vendor'), | |
| 113 | + 'vendorMarkerKept' => defined('PROBE_VENDOR_MARKER'), | |
| 114 | + 'composerLockPresent' => is_file(__DIR__.'/composer.lock'), | |
| 115 | + 'tmpWritable' => is_writable('/tmp'), | |
| 116 | + 'tmpWriteWorks' => false !== @file_put_contents($tmpProbe, 'x') && @unlink($tmpProbe), | |
| 117 | + 'tmpFreeBytes' => @disk_free_space('/tmp'), | |
| 118 | + 'tmpListing' => @scandir('/tmp'), | |
| 119 | + 'sysTempDir' => sys_get_temp_dir(), | |
| 120 | + ], | |
| 121 | + 'globals' => [ | |
| 122 | + 'server' => redact($_SERVER), | |
| 123 | + 'argv' => $GLOBALS['argv'] ?? null, | |
| 124 | + 'get' => $_GET, | |
| 125 | + 'post' => $_POST, | |
| 126 | + 'cookie' => $_COOKIE, | |
| 127 | + 'files' => $_FILES, | |
| 128 | + ], | |
| 129 | + 'env' => redact(getenv()), | |
| 130 | + 'ini' => array_combine($names = [ | |
| 131 | + 'memory_limit', 'max_execution_time', 'upload_max_filesize', 'post_max_size', 'output_buffering', | |
| 132 | + 'display_errors', 'log_errors', 'error_log', 'error_reporting', 'enable_post_data_reading', 'variables_order', | |
| 133 | + 'opcache.enable', 'opcache.enable_cli', 'opcache.validate_timestamps', 'opcache.preload', 'disable_functions', | |
| 134 | + 'date.timezone', 'default_socket_timeout', 'arg_separator.output', | |
| 135 | + ], array_map(static fn (string $name) => ini_get($name), $names)), | |
| 136 | + 'extensions' => get_loaded_extensions(), | |
| 137 | + ]; | |
| 138 | +} | |
| 139 | + | |
| 140 | +// The facts are meant to be pasted back, so secrets must not travel with them. | |
| 141 | +function redact(array $values): array | |
| 142 | +{ | |
| 143 | + foreach ($values as $name => $value) { | |
| 144 | + if (is_string($value) && preg_match('/SECRET|TOKEN|PASSWORD|KEY/i', (string) $name)) { | |
| 145 | + $values[$name] = 'redacted, '.strlen($value).' chars'; | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | + return $values; | |
| 150 | +} | |
| 151 | + | |
| 152 | +function json_response(array $data): array | |
| 153 | +{ | |
| 154 | + return [ | |
| 155 | + 'statusCode' => 200, | |
| 156 | + 'headers' => ['Content-Type' => 'application/json'], | |
| 157 | + 'body' => json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE), | |
| 158 | + ]; | |
| 159 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,159 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +/* | ||
| 4 | + * Probe function: answers the open platform questions by reporting what Scaleway passes to, | ||
| 5 | + * and accepts from, a PHP handler. Dependency-free on purpose; see ../README.md. | ||
| 6 | + */ | ||
| 7 | + | ||
| 8 | +// Loading a shipped vendor/ from the handler's directory is one of the questions. | ||
| 9 | +require __DIR__.'/vendor/autoload.php'; | ||
| 10 | + | ||
| 11 | +const PROBE_HISTORY = '/tmp/probe-history.jsonl'; | ||
| 12 | + | ||
| 13 | +function handle($event, $context) | ||
| 14 | +{ | ||
| 15 | + static $invocations = 0; | ||
| 16 | + static $firstInvocationAt = null; | ||
| 17 | + ++$invocations; | ||
| 18 | + $firstInvocationAt ??= date(DATE_ATOM); | ||
| 19 | + | ||
| 20 | + // CRON Invocations have nobody to answer to: the logs and the history keep their Events. | ||
| 21 | + $record = ['at' => date(DATE_ATOM), 'pid' => getmypid(), 'invocation' => $invocations, 'event' => $event, 'context' => $context]; | ||
| 22 | + error_log('probe: '.json_encode($record, JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES)); | ||
| 23 | + @file_put_contents(PROBE_HISTORY, json_encode($record, JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES).PHP_EOL, FILE_APPEND); | ||
| 24 | + | ||
| 25 | + $query = is_array($event['queryStringParameters'] ?? null) ? $event['queryStringParameters'] : []; | ||
| 26 | + | ||
| 27 | + switch ($query['mode'] ?? 'facts') { | ||
| 28 | + case 'headers': | ||
| 29 | + // Are list values accepted, and sent as repeated headers? | ||
| 30 | + return [ | ||
| 31 | + 'statusCode' => 200, | ||
| 32 | + 'headers' => [ | ||
| 33 | + 'Content-Type' => 'text/plain', | ||
| 34 | + 'X-Probe-Single' => 'one', | ||
| 35 | + 'X-Probe-Multi' => ['first', 'second'], | ||
| 36 | + 'Set-Cookie' => ['probe_a=1; Path=/', 'probe_b=2; Path=/; HttpOnly'], | ||
| 37 | + ], | ||
| 38 | + 'body' => "Check the response headers.\n", | ||
| 39 | + ]; | ||
| 40 | + | ||
| 41 | + case 'binary': | ||
| 42 | + return [ | ||
| 43 | + 'statusCode' => 200, | ||
| 44 | + 'headers' => ['Content-Type' => 'application/octet-stream'], | ||
| 45 | + 'body' => base64_encode(implode('', array_map('chr', range(0, 255)))), | ||
| 46 | + 'isBase64Encoded' => true, | ||
| 47 | + ]; | ||
| 48 | + | ||
| 49 | + case 'status': | ||
| 50 | + return ['statusCode' => (int) ($query['code'] ?? 418), 'headers' => ['Content-Type' => 'text/plain'], 'body' => "status\n"]; | ||
| 51 | + | ||
| 52 | + case 'no-headers': | ||
| 53 | + // The documented minimal response: which Content-Type does the platform add? | ||
| 54 | + return ['statusCode' => 200, 'body' => 'no headers']; | ||
| 55 | + | ||
| 56 | + case 'body': | ||
| 57 | + $body = (string) ($event['body'] ?? ''); | ||
| 58 | + $decoded = true === ($event['isBase64Encoded'] ?? null) ? base64_decode($body) : $body; | ||
| 59 | + | ||
| 60 | + return json_response([ | ||
| 61 | + 'isBase64Encoded' => $event['isBase64Encoded'] ?? null, | ||
| 62 | + 'receivedLength' => strlen($body), | ||
| 63 | + 'decodedLength' => strlen($decoded), | ||
| 64 | + 'decodedSha256' => hash('sha256', $decoded), | ||
| 65 | + ]); | ||
| 66 | + | ||
| 67 | + case 'stdout': | ||
| 68 | + echo "probe: written to stdout by the handler\n"; | ||
| 69 | + | ||
| 70 | + return ['statusCode' => 200, 'headers' => ['Content-Type' => 'text/plain'], 'body' => "Is the stdout line in the logs or in this body?\n"]; | ||
| 71 | + | ||
| 72 | + case 'exception': | ||
| 73 | + throw new RuntimeException('probe: exception escaping the handler'); | ||
| 74 | + | ||
| 75 | + case 'history': | ||
| 76 | + return ['statusCode' => 200, 'headers' => ['Content-Type' => 'application/x-ndjson'], 'body' => (string) @file_get_contents(PROBE_HISTORY)]; | ||
| 77 | + | ||
| 78 | + default: | ||
| 79 | + return json_response(facts($event, $context, $invocations, $firstInvocationAt)); | ||
| 80 | + } | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +function facts($event, $context, int $invocations, string $firstInvocationAt): array | ||
| 84 | +{ | ||
| 85 | + $tmpProbe = '/tmp/probe-write-'.getmypid(); | ||
| 86 | + $codeProbe = __DIR__.'/probe-write-'.getmypid(); | ||
| 87 | + | ||
| 88 | + return [ | ||
| 89 | + 'process' => [ | ||
| 90 | + 'pid' => getmypid(), | ||
| 91 | + 'ppid' => function_exists('posix_getppid') ? posix_getppid() : null, | ||
| 92 | + 'uid' => function_exists('posix_geteuid') ? posix_geteuid() : getmyuid(), | ||
| 93 | + 'user' => get_current_user(), | ||
| 94 | + 'invocationsInThisProcess' => $invocations, | ||
| 95 | + 'firstInvocationAt' => $firstInvocationAt, | ||
| 96 | + 'sapi' => PHP_SAPI, | ||
| 97 | + 'phpVersion' => PHP_VERSION, | ||
| 98 | + 'memoryUsage' => memory_get_usage(true), | ||
| 99 | + 'obLevel' => ob_get_level(), | ||
| 100 | + 'includedFiles' => get_included_files(), | ||
| 101 | + ], | ||
| 102 | + 'event' => $event, | ||
| 103 | + 'eventKeys' => is_array($event) ? array_keys($event) : null, | ||
| 104 | + 'context' => $context, | ||
| 105 | + 'contextType' => get_debug_type($context), | ||
| 106 | + 'filesystem' => [ | ||
| 107 | + 'cwd' => getcwd(), | ||
| 108 | + 'handlerFile' => __FILE__, | ||
| 109 | + 'codeDirWritable' => is_writable(__DIR__), | ||
| 110 | + 'codeDirWriteWorks' => false !== @file_put_contents($codeProbe, 'x') && @unlink($codeProbe), | ||
| 111 | + 'codeDirListing' => scandir(__DIR__), | ||
| 112 | + 'vendorListing' => @scandir(__DIR__.'/vendor'), | ||
| 113 | + 'vendorMarkerKept' => defined('PROBE_VENDOR_MARKER'), | ||
| 114 | + 'composerLockPresent' => is_file(__DIR__.'/composer.lock'), | ||
| 115 | + 'tmpWritable' => is_writable('/tmp'), | ||
| 116 | + 'tmpWriteWorks' => false !== @file_put_contents($tmpProbe, 'x') && @unlink($tmpProbe), | ||
| 117 | + 'tmpFreeBytes' => @disk_free_space('/tmp'), | ||
| 118 | + 'tmpListing' => @scandir('/tmp'), | ||
| 119 | + 'sysTempDir' => sys_get_temp_dir(), | ||
| 120 | + ], | ||
| 121 | + 'globals' => [ | ||
| 122 | + 'server' => redact($_SERVER), | ||
| 123 | + 'argv' => $GLOBALS['argv'] ?? null, | ||
| 124 | + 'get' => $_GET, | ||
| 125 | + 'post' => $_POST, | ||
| 126 | + 'cookie' => $_COOKIE, | ||
| 127 | + 'files' => $_FILES, | ||
| 128 | + ], | ||
| 129 | + 'env' => redact(getenv()), | ||
| 130 | + 'ini' => array_combine($names = [ | ||
| 131 | + 'memory_limit', 'max_execution_time', 'upload_max_filesize', 'post_max_size', 'output_buffering', | ||
| 132 | + 'display_errors', 'log_errors', 'error_log', 'error_reporting', 'enable_post_data_reading', 'variables_order', | ||
| 133 | + 'opcache.enable', 'opcache.enable_cli', 'opcache.validate_timestamps', 'opcache.preload', 'disable_functions', | ||
| 134 | + 'date.timezone', 'default_socket_timeout', 'arg_separator.output', | ||
| 135 | + ], array_map(static fn (string $name) => ini_get($name), $names)), | ||
| 136 | + 'extensions' => get_loaded_extensions(), | ||
| 137 | + ]; | ||
| 138 | +} | ||
| 139 | + | ||
| 140 | +// The facts are meant to be pasted back, so secrets must not travel with them. | ||
| 141 | +function redact(array $values): array | ||
| 142 | +{ | ||
| 143 | + foreach ($values as $name => $value) { | ||
| 144 | + if (is_string($value) && preg_match('/SECRET|TOKEN|PASSWORD|KEY/i', (string) $name)) { | ||
| 145 | + $values[$name] = 'redacted, '.strlen($value).' chars'; | ||
| 146 | + } | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + return $values; | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +function json_response(array $data): array | ||
| 153 | +{ | ||
| 154 | + return [ | ||
| 155 | + 'statusCode' => 200, | ||
| 156 | + 'headers' => ['Content-Type' => 'application/json'], | ||
| 157 | + 'body' => json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE), | ||
| 158 | + ]; | ||
| 159 | +} | ||
added
probe/function/vendor/autoload.php +4 -0 | new file mode 100644 | ||
| @@ -0,0 +1,4 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +// Hand-written on purpose: if Scaleway runs Composer at deploy time, it replaces this file and the marker disappears. | |
| 4 | +const PROBE_VENDOR_MARKER = 'shipped-in-zip'; | |
| new file mode 100644 | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +// Hand-written on purpose: if Scaleway runs Composer at deploy time, it replaces this file and the marker disappears. | ||
| 4 | +const PROBE_VENDOR_MARKER = 'shipped-in-zip'; | ||
added
probe/terraform/main.tf +77 -0 | new file mode 100644 | ||
| @@ -0,0 +1,77 @@ | ||
| 1 | +# Deploys the probe function. Unvalidated: written against the scaleway provider docs (v2.92), never applied. | |
| 2 | +# Credentials come from the usual SCW_ACCESS_KEY, SCW_SECRET_KEY and SCW_DEFAULT_PROJECT_ID variables. | |
| 3 | + | |
| 4 | +terraform { | |
| 5 | + required_version = ">= 1.5" | |
| 6 | + | |
| 7 | + required_providers { | |
| 8 | + scaleway = { | |
| 9 | + source = "scaleway/scaleway" | |
| 10 | + version = "~> 2.92" | |
| 11 | + } | |
| 12 | + archive = { | |
| 13 | + source = "hashicorp/archive" | |
| 14 | + version = "~> 2.8" | |
| 15 | + } | |
| 16 | + } | |
| 17 | +} | |
| 18 | + | |
| 19 | +variable "region" { | |
| 20 | + type = string | |
| 21 | + default = "fr-par" | |
| 22 | +} | |
| 23 | + | |
| 24 | +variable "runtime" { | |
| 25 | + type = string | |
| 26 | + default = "php85" | |
| 27 | +} | |
| 28 | + | |
| 29 | +provider "scaleway" { | |
| 30 | + region = var.region | |
| 31 | +} | |
| 32 | + | |
| 33 | +data "archive_file" "probe" { | |
| 34 | + type = "zip" | |
| 35 | + source_dir = "${path.module}/../function" | |
| 36 | + output_path = "${path.module}/.build/probe.zip" | |
| 37 | +} | |
| 38 | + | |
| 39 | +resource "scaleway_function_namespace" "probe" { | |
| 40 | + name = "scaleway-runtime-probe" | |
| 41 | + description = "Probe measuring the PHP runtime behavior, destroy after use" | |
| 42 | +} | |
| 43 | + | |
| 44 | +resource "scaleway_function" "probe" { | |
| 45 | + namespace_id = scaleway_function_namespace.probe.id | |
| 46 | + name = "probe-${var.runtime}" | |
| 47 | + runtime = var.runtime | |
| 48 | + handler = "handler.handle" | |
| 49 | + privacy = "public" | |
| 50 | + # A single instance makes Warm Process reuse observable between consecutive calls. | |
| 51 | + min_scale = 0 | |
| 52 | + max_scale = 1 | |
| 53 | + memory_limit = 256 | |
| 54 | + timeout = 60 | |
| 55 | + zip_file = data.archive_file.probe.output_path | |
| 56 | + zip_hash = data.archive_file.probe.output_sha256 | |
| 57 | + deploy = true | |
| 58 | + | |
| 59 | + # Do plain and secret variables both reach getenv() and $_SERVER? | |
| 60 | + environment_variables = { | |
| 61 | + PROBE_PLAIN = "plain-value" | |
| 62 | + } | |
| 63 | + secret_environment_variables = { | |
| 64 | + PROBE_SECRET = "secret-value" | |
| 65 | + } | |
| 66 | +} | |
| 67 | + | |
| 68 | +resource "scaleway_function_cron" "probe" { | |
| 69 | + function_id = scaleway_function.probe.id | |
| 70 | + name = "probe-cron" | |
| 71 | + schedule = "*/5 * * * *" | |
| 72 | + args = jsonencode({ probe = "cron", nested = { list = [1, 2] } }) | |
| 73 | +} | |
| 74 | + | |
| 75 | +output "endpoint" { | |
| 76 | + value = "https://${scaleway_function.probe.domain_name}" | |
| 77 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | +# Deploys the probe function. Unvalidated: written against the scaleway provider docs (v2.92), never applied. | ||
| 2 | +# Credentials come from the usual SCW_ACCESS_KEY, SCW_SECRET_KEY and SCW_DEFAULT_PROJECT_ID variables. | ||
| 3 | + | ||
| 4 | +terraform { | ||
| 5 | + required_version = ">= 1.5" | ||
| 6 | + | ||
| 7 | + required_providers { | ||
| 8 | + scaleway = { | ||
| 9 | + source = "scaleway/scaleway" | ||
| 10 | + version = "~> 2.92" | ||
| 11 | + } | ||
| 12 | + archive = { | ||
| 13 | + source = "hashicorp/archive" | ||
| 14 | + version = "~> 2.8" | ||
| 15 | + } | ||
| 16 | + } | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +variable "region" { | ||
| 20 | + type = string | ||
| 21 | + default = "fr-par" | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +variable "runtime" { | ||
| 25 | + type = string | ||
| 26 | + default = "php85" | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +provider "scaleway" { | ||
| 30 | + region = var.region | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +data "archive_file" "probe" { | ||
| 34 | + type = "zip" | ||
| 35 | + source_dir = "${path.module}/../function" | ||
| 36 | + output_path = "${path.module}/.build/probe.zip" | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +resource "scaleway_function_namespace" "probe" { | ||
| 40 | + name = "scaleway-runtime-probe" | ||
| 41 | + description = "Probe measuring the PHP runtime behavior, destroy after use" | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +resource "scaleway_function" "probe" { | ||
| 45 | + namespace_id = scaleway_function_namespace.probe.id | ||
| 46 | + name = "probe-${var.runtime}" | ||
| 47 | + runtime = var.runtime | ||
| 48 | + handler = "handler.handle" | ||
| 49 | + privacy = "public" | ||
| 50 | + # A single instance makes Warm Process reuse observable between consecutive calls. | ||
| 51 | + min_scale = 0 | ||
| 52 | + max_scale = 1 | ||
| 53 | + memory_limit = 256 | ||
| 54 | + timeout = 60 | ||
| 55 | + zip_file = data.archive_file.probe.output_path | ||
| 56 | + zip_hash = data.archive_file.probe.output_sha256 | ||
| 57 | + deploy = true | ||
| 58 | + | ||
| 59 | + # Do plain and secret variables both reach getenv() and $_SERVER? | ||
| 60 | + environment_variables = { | ||
| 61 | + PROBE_PLAIN = "plain-value" | ||
| 62 | + } | ||
| 63 | + secret_environment_variables = { | ||
| 64 | + PROBE_SECRET = "secret-value" | ||
| 65 | + } | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +resource "scaleway_function_cron" "probe" { | ||
| 69 | + function_id = scaleway_function.probe.id | ||
| 70 | + name = "probe-cron" | ||
| 71 | + schedule = "*/5 * * * *" | ||
| 72 | + args = jsonencode({ probe = "cron", nested = { list = [1, 2] } }) | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +output "endpoint" { | ||
| 76 | + value = "https://${scaleway_function.probe.domain_name}" | ||
| 77 | +} | ||