-
Notifications
You must be signed in to change notification settings - Fork 20
feat(php): Add PHP authentication token generation sample #628
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
Open
gxjx-x
wants to merge
8
commits into
main
Choose a base branch
from
add-php-authentication
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a89bf56
feat(php): Add PHP authentication token generation sample
gxjx-x 3d1f851
test(php): Add PHPUnit integration tests for PHP authentication sample
gxjx-x 68f9842
ci: trigger re-run
gxjx-x 31b1e8b
ci: Wire php-authentication into ci-gate.yml
gxjx-x ec17912
ci: Use PYTHON_IAM_ROLE for PHP authentication tests
gxjx-x d649234
ci: Remove shivammathur/setup-php, use runner built-in PHP
gxjx-x d4affb9
ci: Enable pdo_pgsql extension on runner
gxjx-x 60fc2ce
fix: Use sslrootcert=system for SSL verification in tests
gxjx-x 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
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 @@ | ||
| name: PHP authentication integration tests | ||
|
|
||
| permissions: {} | ||
|
|
||
| on: | ||
| workflow_call: {} | ||
| workflow_dispatch: | ||
| push: | ||
| branches: [ main ] | ||
|
|
||
| jobs: | ||
| create-cluster: | ||
| uses: ./.github/workflows/dsql-cluster-create.yml | ||
| with: | ||
| workflow_name: php-authentication | ||
| secrets: | ||
| AWS_IAM_ROLE: ${{ secrets.PYTHON_IAM_ROLE }} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably create a different role here and not use the python one? |
||
| permissions: | ||
| id-token: write | ||
|
|
||
| php-authentication-integ-test: | ||
| needs: create-cluster | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| id-token: write | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Enable pdo_pgsql extension | ||
| run: sudo phpenmod pdo_pgsql | ||
|
|
||
| - name: Configure AWS Credentials | ||
| uses: aws-actions/configure-aws-credentials@v6 | ||
| with: | ||
| role-to-assume: ${{ secrets.PYTHON_IAM_ROLE }} | ||
| aws-region: ${{ needs.create-cluster.outputs.region }} | ||
|
|
||
| - name: Install dependencies | ||
| working-directory: ./php/authentication | ||
| run: composer install --no-interaction | ||
|
|
||
| - name: Run integration tests | ||
| working-directory: ./php/authentication | ||
| env: | ||
| CLUSTER_ENDPOINT: ${{ needs.create-cluster.outputs.cluster-endpoint }} | ||
| REGION: ${{ needs.create-cluster.outputs.region }} | ||
| run: ./vendor/bin/phpunit | ||
|
|
||
| delete-cluster: | ||
| if: always() && needs.create-cluster.result == 'success' | ||
| needs: [create-cluster, php-authentication-integ-test] | ||
| uses: ./.github/workflows/dsql-cluster-delete.yml | ||
| with: | ||
| cluster-id: ${{ needs.create-cluster.outputs.cluster-id }} | ||
| region: ${{ needs.create-cluster.outputs.region }} | ||
| secrets: | ||
| AWS_IAM_ROLE: ${{ secrets.PYTHON_IAM_ROLE }} | ||
| permissions: | ||
| id-token: write | ||
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,13 @@ | ||
| { | ||
| "require": { | ||
| "aws/aws-sdk-php": "^3.0" | ||
| }, | ||
| "require-dev": { | ||
| "phpunit/phpunit": "^11.0" | ||
| }, | ||
| "autoload": { | ||
| "psr-4": { | ||
| "Dsql\\": "src/" | ||
| } | ||
| } | ||
| } |
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,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
| bootstrap="vendor/autoload.php" | ||
| colors="true"> | ||
| <testsuites> | ||
| <testsuite name="Integration Tests"> | ||
| <directory>test</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
| </phpunit> |
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,23 @@ | ||
| <?php | ||
| // PHP SDK examples for generating Aurora DSQL authentication tokens | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| require 'vendor/autoload.php'; | ||
|
|
||
| // --8<-- [start:php-generate-token] | ||
| use Aws\DSQL\AuthTokenGenerator; | ||
| use Aws\Credentials\CredentialProvider; | ||
|
|
||
| function generateToken(string $yourClusterEndpoint, string $region): string | ||
| { | ||
| $provider = CredentialProvider::defaultProvider(); | ||
| $generator = new AuthTokenGenerator($provider); | ||
|
|
||
| // Use generateDbConnectAuthToken if you are not connecting as admin | ||
| $token = $generator->generateDbConnectAdminAuthToken($yourClusterEndpoint, $region); | ||
|
|
||
| echo $token . PHP_EOL; | ||
| return $token; | ||
| } | ||
| // --8<-- [end:php-generate-token] |
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,42 @@ | ||
| <?php | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| require __DIR__ . '/../vendor/autoload.php'; | ||
| require __DIR__ . '/../src/generate_token.php'; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class GenerateTokenTest extends TestCase | ||
| { | ||
| public function testGenerateTokenReturnsNonEmptyString(): void | ||
| { | ||
| $endpoint = getenv('CLUSTER_ENDPOINT'); | ||
| $region = getenv('REGION') ?: 'us-east-1'; | ||
|
|
||
| $this->assertNotEmpty($endpoint, 'CLUSTER_ENDPOINT environment variable must be set'); | ||
|
|
||
| $token = generateToken($endpoint, $region); | ||
|
|
||
| $this->assertIsString($token); | ||
| $this->assertNotEmpty($token); | ||
| } | ||
|
|
||
| public function testTokenCanConnectToCluster(): void | ||
| { | ||
| $endpoint = getenv('CLUSTER_ENDPOINT'); | ||
| $region = getenv('REGION') ?: 'us-east-1'; | ||
|
|
||
| $this->assertNotEmpty($endpoint, 'CLUSTER_ENDPOINT environment variable must be set'); | ||
|
|
||
| $token = generateToken($endpoint, $region); | ||
|
|
||
| $dsn = "pgsql:host={$endpoint};port=5432;dbname=postgres;sslmode=verify-full;sslrootcert=system"; | ||
| $pdo = new PDO($dsn, 'admin', $token); | ||
|
|
||
| $stmt = $pdo->query('SELECT 1 AS result'); | ||
| $result = $stmt->fetchColumn(); | ||
|
|
||
| $this->assertEquals(1, (int) $result); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, php is the only example with an auth-token CI. I guess we can add it to a backlog to add this for the rest of the samples.