-
Notifications
You must be signed in to change notification settings - Fork 36
tests: Allow e2e to use injected token (cli) for UI access #1010
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
queria
wants to merge
1
commit into
guacsec:release/0.4.z
Choose a base branch
from
queria:e2e-entra-id
base: release/0.4.z
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
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
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 |
|---|---|---|
| @@ -1,23 +1,142 @@ | ||
| import { expect, type Page } from "@playwright/test"; | ||
| import axios from "axios"; | ||
| import https from "node:https"; | ||
|
|
||
| import { | ||
| AUTH_CLIENT_ID, | ||
| AUTH_CLIENT_SECRET, | ||
| AUTH_PASSWORD, | ||
| AUTH_REQUIRED, | ||
| AUTH_SCOPE, | ||
| AUTH_URL, | ||
| AUTH_USER, | ||
| TRUSTIFY_UI_URL, | ||
| UI_AUTH_MODE, | ||
| logger, | ||
| } from "../../common/constants"; | ||
|
|
||
| export const login = async (page: Page) => { | ||
| if (AUTH_REQUIRED === "true") { | ||
| const userName = AUTH_USER; | ||
| const userPassword = AUTH_PASSWORD; | ||
| const httpsAgent = new https.Agent({ rejectUnauthorized: false }); | ||
|
|
||
| const discoverFrontendOidcConfig = async (baseURL: string) => { | ||
| const response = await axios.get<string>(baseURL, { | ||
| maxRedirects: 0, | ||
| httpsAgent, | ||
| timeout: 5000, | ||
| timeoutErrorMessage: `Could not fetch index.html from ${baseURL}`, | ||
| }); | ||
|
|
||
| const matcher = response.data.match(/window._env\s*=\s*"([^"]+)"/); | ||
| const serverConfig = matcher?.[1]; | ||
| if (!serverConfig) { | ||
| throw new Error( | ||
| "Could not discover OIDC config from frontend index.html. " + | ||
| "Set PLAYWRIGHT_AUTH_URL explicitly.", | ||
| ); | ||
| } | ||
|
|
||
| const envInfo: Record<string, string> = JSON.parse(atob(serverConfig)); | ||
| return { | ||
| oidcServerUrl: envInfo.OIDC_SERVER_URL, | ||
| oidcClientId: envInfo.OIDC_CLIENT_ID || "frontend", | ||
| }; | ||
| }; | ||
|
|
||
| const getClientCredentialsToken = async (authUrl: string) => { | ||
| const oidcConfigResponse = await axios.get( | ||
| `${authUrl}/.well-known/openid-configuration`, | ||
| { httpsAgent }, | ||
| ); | ||
| const tokenEndpoint = oidcConfigResponse.data.token_endpoint; | ||
| expect( | ||
| tokenEndpoint, | ||
| "Could not discover token_endpoint from OIDC configuration", | ||
| ).toBeTruthy(); | ||
|
|
||
| await page.goto("/importers"); | ||
| const data = new URLSearchParams(); | ||
| data.append("grant_type", "client_credentials"); | ||
| data.append("client_id", AUTH_CLIENT_ID); | ||
| data.append("client_secret", AUTH_CLIENT_SECRET); | ||
| if (AUTH_SCOPE) { | ||
| data.append("scope", AUTH_SCOPE); | ||
| } | ||
|
|
||
| const response = await axios.post(tokenEndpoint, data, { | ||
| headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
| httpsAgent, | ||
| }); | ||
|
|
||
| return response.data as { | ||
| access_token: string; | ||
| expires_in: number; | ||
| token_type: string; | ||
| }; | ||
| }; | ||
|
|
||
| const loginWithTokenInjection = async (page: Page) => { | ||
| const { oidcServerUrl, oidcClientId } = | ||
| await discoverFrontendOidcConfig(TRUSTIFY_UI_URL); | ||
| logger.info( | ||
| `Discovered frontend OIDC config: server=${oidcServerUrl}, clientId=${oidcClientId}`, | ||
| ); | ||
|
|
||
| const authUrl = AUTH_URL || oidcServerUrl; | ||
| if (!authUrl) { | ||
| throw new Error( | ||
| "Auth URL not available. Set PLAYWRIGHT_AUTH_URL or ensure the frontend exposes OIDC_SERVER_URL.", | ||
| ); | ||
| } | ||
|
|
||
| await page.fill('input[name="username"]:visible', userName); | ||
| await page.fill('input[name="password"]:visible', userPassword); | ||
| await page.keyboard.press("Enter"); | ||
| const tokenResponse = await getClientCredentialsToken(authUrl); | ||
| logger.info("Obtained access token via client_credentials"); | ||
|
|
||
| const expiresAt = | ||
| Math.floor(Date.now() / 1000) + (tokenResponse.expires_in || 3600); | ||
|
|
||
| const storageKey = `oidc.user:${oidcServerUrl}:${oidcClientId}`; | ||
| const userObject = JSON.stringify({ | ||
| access_token: tokenResponse.access_token, | ||
| token_type: "Bearer", | ||
| scope: AUTH_SCOPE || "openid", | ||
| expires_at: expiresAt, | ||
| profile: { | ||
| sub: AUTH_CLIENT_ID, | ||
| }, | ||
| }); | ||
|
|
||
| logger.debug(`Injecting token into sessionStorage key: ${storageKey}`); | ||
|
|
||
| await page.addInitScript( | ||
| ({ key, value }: { key: string; value: string }) => { | ||
| sessionStorage.setItem(key, value); | ||
| }, | ||
| { key: storageKey, value: userObject }, | ||
| ); | ||
|
|
||
| await page.goto("/importers"); | ||
| await expect(page.getByRole("heading", { name: "Importers" })).toHaveCount(1); | ||
| }; | ||
|
|
||
| const loginWithForm = async (page: Page) => { | ||
| const userName = AUTH_USER; | ||
| const userPassword = AUTH_PASSWORD; | ||
|
|
||
| await page.goto("/importers"); | ||
|
|
||
| await page.fill('input[name="username"]:visible', userName); | ||
| await page.fill('input[name="password"]:visible', userPassword); | ||
| await page.keyboard.press("Enter"); | ||
|
|
||
| await expect(page.getByRole("heading", { name: "Importers" })).toHaveCount(1); | ||
| }; | ||
|
|
||
| export const login = async (page: Page) => { | ||
| if (AUTH_REQUIRED !== "true") { | ||
| return; | ||
| } | ||
|
|
||
| await expect(page.getByRole("heading", { name: "Importers" })).toHaveCount( | ||
| 1, | ||
| ); // Ensure login was successful | ||
| if (UI_AUTH_MODE === "token_injection") { | ||
| await loginWithTokenInjection(page); | ||
| } else { | ||
| await loginWithForm(page); | ||
| } | ||
| }; | ||
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.
issue (testing): Add an e2e scenario that explicitly exercises
token_injectionauth modeCurrently
loginWithTokenInjectionis only reachable viaUI_AUTH_MODE, but there’s no e2e coverage to verify it actually works or fixes the SSO/MFA scenario described in the PR. Please add a smoke test (or a small Playwright project) that runs withPLAYWRIGHT_UI_AUTH_MODE=token_injectionandAUTH_REQUIRED=true, uses a known-good Entra ID client, and asserts the UI is authenticated without using the login form. This will both prevent regressions and prove the injected token is sufficient for UI access.