-
-
Notifications
You must be signed in to change notification settings - Fork 174
ci: add manual workflow to validate DOCKERHUB_TOKEN before a build #716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kojiromike
merged 7 commits into
openemr:master
from
kojiromike:dockerhub-credential-check
May 12, 2026
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b912a19
ci: add manual workflow to validate DOCKERHUB_TOKEN before a build
kojiromike 9efb487
ci: probe write scope via no-op PATCH, not just GET
kojiromike 6830046
ci: classify Docker Hub responses precisely; tolerate transport/parse…
kojiromike a5b9bb9
ci: treat missing description fields as a parse failure
kojiromike fbf58a6
ci: prevent workflow-command injection via repository / detail strings
kojiromike 1e288b6
ci: pin arduino/setup-task to 3.x
kojiromike fae2ea2
ci: distinguish 401 (invalid auth) from 403 (insufficient scope)
kojiromike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| name: Docker Hub Credential Check | ||
|
|
||
| # Manual workflow that proves DOCKERHUB_USERNAME / DOCKERHUB_TOKEN are valid | ||
| # for the readme-push API path before triggering an expensive build. Issue #714 | ||
| # rotated the secret because the readme push was returning Forbidden; this lets | ||
| # whoever rotates next confirm the new value works in seconds, without pushing | ||
| # an image. | ||
| # | ||
| # Validates two paths: registry login (used by docker/login-action in the | ||
| # build workflows) and the Docker Hub API (used by peter-evans/dockerhub-description | ||
| # to PATCH the repo description). A token can pass the first and fail the | ||
| # second, which is exactly the failure mode that prompted #714. | ||
| # | ||
| # The API check writes the current description back to itself via a no-op PATCH | ||
| # so it actually exercises the write/delete scope peter-evans uses; a read-only | ||
| # token would 200 on a GET but 403 here. Side effect: bumps Docker Hub's | ||
| # last-modified timestamp on the repo (the same side effect the real readme | ||
| # push already produces every time it runs, so nothing novel). | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| check: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
|
|
||
| - name: Validate registry login | ||
| uses: docker/login-action@v4 | ||
| with: | ||
| username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
| password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
|
|
||
| - name: Setup PHP | ||
| uses: shivammathur/setup-php@v2 | ||
| with: | ||
| php-version: '8.5' | ||
|
|
||
| - name: Install Task | ||
| uses: arduino/setup-task@v2 | ||
|
kojiromike marked this conversation as resolved.
|
||
|
|
||
| - name: Validate Docker Hub API auth | ||
| env: | ||
| DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | ||
| DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} | ||
| working-directory: tools/release | ||
| run: task ci:check-dockerhub-credential | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| /** | ||
| * Validate DOCKERHUB_USERNAME / DOCKERHUB_TOKEN against the Docker Hub API | ||
| * path that peter-evans/dockerhub-description uses. | ||
| * | ||
| * Manual smoke test invoked from the dockerhub-credential-check workflow. | ||
| * Exits non-zero on failure with a `::error::` line; on success emits a | ||
| * `::notice::` line. | ||
| * | ||
| * @package openemr-devops | ||
| * @link https://www.open-emr.org | ||
| * @author Michael A. Smith <michael@opencoreemr.com> | ||
| * @copyright Copyright (c) 2026 OpenCoreEMR Inc. | ||
| * @license https://github.com/openemr/openemr-devops/blob/master/LICENSE GNU General Public License 3 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
|
||
| use OpenEMR\Release\DockerHubCredentialChecker; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\SingleCommandApplication; | ||
|
|
||
| (new SingleCommandApplication()) | ||
| ->setName('check-dockerhub-credential') | ||
| ->setDescription('Validate DOCKERHUB_USERNAME / DOCKERHUB_TOKEN for the readme-push API path') | ||
| ->addOption( | ||
| 'repository', | ||
| null, | ||
| InputOption::VALUE_REQUIRED, | ||
| 'Docker Hub repository (owner/name)', | ||
| 'openemr/openemr', | ||
| ) | ||
| ->setCode(function (InputInterface $input, OutputInterface $output): int { | ||
| $username = getenv('DOCKERHUB_USERNAME'); | ||
| $token = getenv('DOCKERHUB_TOKEN'); | ||
| if (!is_string($username) || $username === '') { | ||
| $output->writeln('::error::DOCKERHUB_USERNAME env var is required'); | ||
| return 1; | ||
| } | ||
| if (!is_string($token) || $token === '') { | ||
| $output->writeln('::error::DOCKERHUB_TOKEN env var is required'); | ||
| return 1; | ||
| } | ||
| /** @var string $repository */ | ||
| $repository = $input->getOption('repository'); | ||
| if (preg_match('#^[A-Za-z0-9][A-Za-z0-9._-]*/[A-Za-z0-9._-]+$#', $repository) !== 1) { | ||
| $output->writeln('::error::--repository must match owner/name (alphanumeric, dot, underscore, dash).'); | ||
| return 1; | ||
| } | ||
|
|
||
| $result = (new DockerHubCredentialChecker())->check($username, $token, $repository); | ||
| $output->writeln($result->toGithubActionsLine()); | ||
| return $result->isOk() ? 0 : 1; | ||
| }) | ||
| ->run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Outcome of a Docker Hub credential check. | ||
| * | ||
| * @package openemr-devops | ||
| * @link https://www.open-emr.org | ||
| * @author Michael A. Smith <michael@opencoreemr.com> | ||
| * @copyright Copyright (c) 2026 OpenCoreEMR Inc. | ||
| * @license https://github.com/openemr/openemr-devops/blob/master/LICENSE GNU General Public License 3 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenEMR\Release; | ||
|
|
||
| final readonly class DockerHubCredentialCheckResult | ||
| { | ||
| public DockerHubCredentialCheckStatus $status; | ||
| public string $repository; | ||
| public ?int $httpStatus; | ||
| public ?string $detail; | ||
|
|
||
| public function __construct( | ||
| DockerHubCredentialCheckStatus $status, | ||
| string $repository, | ||
| ?int $httpStatus = null, | ||
| ?string $detail = null, | ||
| ) { | ||
| // Defensively scrub CR/LF from caller-controlled strings before they | ||
| // ever get formatted into a `::error::` / `::notice::` line. The | ||
| // workflow-command syntax is line-based; an embedded newline could | ||
| // inject a second command. Belt-and-braces — the bin layer also | ||
| // validates repository against an owner/name pattern up front. | ||
| $this->status = $status; | ||
| $this->repository = $this->scrubLineBreaks($repository); | ||
| $this->httpStatus = $httpStatus; | ||
| $this->detail = $detail !== null ? $this->scrubLineBreaks($detail) : null; | ||
| } | ||
|
|
||
| private function scrubLineBreaks(string $value): string | ||
| { | ||
| return strtr($value, ["\r" => ' ', "\n" => ' ']); | ||
| } | ||
|
|
||
| /** | ||
| * Map raw HTTP statuses from Docker Hub's login + repository read + | ||
| * repository write probes to a result. Pure: no network. Tested directly. | ||
| * | ||
| * - $loginStatus is the POST /v2/users/login/ HTTP status (null if the | ||
| * request itself failed at the transport layer) | ||
| * - $jwt is the token extracted from the login response (null if the | ||
| * response was unparseable JSON or missing the token field) | ||
| * - $readStatus is the GET /v2/repositories/<repo>/ status (null if the | ||
| * step was not reached) | ||
| * - $descriptionsParsed is whether the GET response body was usable JSON | ||
| * with the expected fields (null if read step not reached) | ||
| * - $writeStatus is the no-op PATCH /v2/repositories/<repo>/ status | ||
| * (null if the step was not reached) | ||
| */ | ||
| public static function interpret( | ||
| string $repository, | ||
| ?int $loginStatus, | ||
| ?string $jwt, | ||
| ?int $readStatus, | ||
| ?bool $descriptionsParsed, | ||
| ?int $writeStatus, | ||
| ): self { | ||
| if ($loginStatus === null) { | ||
| return new self(DockerHubCredentialCheckStatus::NETWORK_ERROR, $repository); | ||
| } | ||
| if (in_array($jwt, [null, '', 'null'], true)) { | ||
| return self::fromAuthFailure($repository, $loginStatus); | ||
| } | ||
| if ($readStatus === null) { | ||
| return new self(DockerHubCredentialCheckStatus::NETWORK_ERROR, $repository); | ||
| } | ||
| if ($readStatus !== 200 || $descriptionsParsed !== true) { | ||
| return self::fromAccessFailure($repository, $readStatus); | ||
| } | ||
| if ($writeStatus === null) { | ||
| return new self(DockerHubCredentialCheckStatus::NETWORK_ERROR, $repository); | ||
| } | ||
| return self::fromWriteStatus($repository, $writeStatus); | ||
| } | ||
|
|
||
| public function isOk(): bool | ||
| { | ||
| return $this->status === DockerHubCredentialCheckStatus::OK; | ||
| } | ||
|
|
||
| /** | ||
| * Format as a single GitHub-Actions workflow command line | ||
| * (`::error::…` or `::notice::…`). | ||
| */ | ||
| public function toGithubActionsLine(): string | ||
| { | ||
| return match ($this->status) { | ||
| DockerHubCredentialCheckStatus::OK => sprintf( | ||
| '::notice::Credential is valid for %s (read + no-op write confirmed).', | ||
| $this->repository, | ||
| ), | ||
| DockerHubCredentialCheckStatus::INVALID_CREDENTIAL => | ||
| '::error::Login failed (HTTP ' . $this->httpStatusOrUnknown() | ||
| . ') — DOCKERHUB_USERNAME / DOCKERHUB_TOKEN appear invalid.', | ||
| DockerHubCredentialCheckStatus::INSUFFICIENT_SCOPE => sprintf( | ||
| '::error::Login succeeded but the token lacks required scope on %s (HTTP %s). ' | ||
| . 'Verify R/W/D scope on this repository.', | ||
| $this->repository, | ||
| $this->httpStatusOrUnknown(), | ||
| ), | ||
| DockerHubCredentialCheckStatus::UNEXPECTED_RESPONSE => sprintf( | ||
| '::error::Unexpected response from Docker Hub API for %s (HTTP %s). %s', | ||
| $this->repository, | ||
| $this->httpStatusOrUnknown(), | ||
| $this->detail ?? 'Re-run, check status.docker.com, then re-evaluate.', | ||
| ), | ||
| DockerHubCredentialCheckStatus::NETWORK_ERROR => sprintf( | ||
| '::error::Could not reach Docker Hub API for %s. %s', | ||
| $this->repository, | ||
| $this->detail ?? 'Re-run, check status.docker.com, then re-evaluate.', | ||
| ), | ||
|
kojiromike marked this conversation as resolved.
|
||
| }; | ||
| } | ||
|
|
||
| private static function fromAuthFailure(string $repository, int $loginStatus): self | ||
| { | ||
| return match (true) { | ||
| in_array($loginStatus, [401, 403], true) => | ||
| new self(DockerHubCredentialCheckStatus::INVALID_CREDENTIAL, $repository, $loginStatus), | ||
| default => | ||
| new self(DockerHubCredentialCheckStatus::UNEXPECTED_RESPONSE, $repository, $loginStatus), | ||
| }; | ||
| } | ||
|
|
||
| private static function fromAccessFailure(string $repository, int $readStatus): self | ||
| { | ||
| return match (true) { | ||
| in_array($readStatus, [401, 403], true) => | ||
| new self(DockerHubCredentialCheckStatus::INSUFFICIENT_SCOPE, $repository, $readStatus), | ||
| default => | ||
| new self(DockerHubCredentialCheckStatus::UNEXPECTED_RESPONSE, $repository, $readStatus), | ||
| }; | ||
|
kojiromike marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static function fromWriteStatus(string $repository, int $writeStatus): self | ||
| { | ||
| return match (true) { | ||
| $writeStatus === 200 => | ||
| new self(DockerHubCredentialCheckStatus::OK, $repository, 200), | ||
| in_array($writeStatus, [401, 403], true) => | ||
| new self(DockerHubCredentialCheckStatus::INSUFFICIENT_SCOPE, $repository, $writeStatus), | ||
| default => | ||
| new self(DockerHubCredentialCheckStatus::UNEXPECTED_RESPONSE, $repository, $writeStatus), | ||
| }; | ||
|
kojiromike marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private function httpStatusOrUnknown(): string | ||
| { | ||
| return $this->httpStatus !== null ? (string) $this->httpStatus : '(unknown)'; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @package openemr-devops | ||
| * @link https://www.open-emr.org | ||
| * @author Michael A. Smith <michael@opencoreemr.com> | ||
| * @copyright Copyright (c) 2026 OpenCoreEMR Inc. | ||
| * @license https://github.com/openemr/openemr-devops/blob/master/LICENSE GNU General Public License 3 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenEMR\Release; | ||
|
|
||
| enum DockerHubCredentialCheckStatus: string | ||
| { | ||
| case OK = 'ok'; | ||
| case INVALID_CREDENTIAL = 'invalid_credential'; | ||
| case INSUFFICIENT_SCOPE = 'insufficient_scope'; | ||
| case UNEXPECTED_RESPONSE = 'unexpected_response'; | ||
| case NETWORK_ERROR = 'network_error'; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.