Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion workspaces/backstage/e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"devDependencies": {
"@eslint/js": "10.0.1",
"@playwright/test": "1.59.1",
"@red-hat-developer-hub/e2e-test-utils": "2.1.6",
"@red-hat-developer-hub/e2e-test-utils": "2.1.7",
"@types/node": "25.5.2",
"eslint": "10.2.0",
"eslint-plugin-check-file": "3.3.1",
Expand Down
4 changes: 4 additions & 0 deletions workspaces/backstage/e2e-tests/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,9 @@ export default playwrightDefineConfig({
name: "backstage-auth",
testMatch: /tests\/specs\/auth\.spec\.ts/,
},
{
name: "backstage-gitlab-auth",
testMatch: /tests\/specs\/gitlab-auth\.spec\.ts/,
},
],
});
109 changes: 109 additions & 0 deletions workspaces/backstage/e2e-tests/support/api/gitlab-oauth-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
export interface GitLabOAuthApp {
id: number;
applicationId: string;
applicationName: string;
secret: string;
callbackUrl: string;
scopes: string[];
}

/**
* GitLab OAuth application helper for auth-provider e2e.
* Ported from RHDH core (gitlab-helper) — create/delete OAuth apps only.
*/
export class GitLabOAuthHelper {
private readonly personalAccessToken: string;
private readonly apiBaseUrl: string;

constructor(host: string, personalAccessToken: string) {
const cleanHost = host.replace(/^https?:\/\//, "");
this.apiBaseUrl = `https://${cleanHost}/api/v4`;
this.personalAccessToken = personalAccessToken;
}

async createOAuthApplication(
name: string,
redirectUri: string,
scopes = "api read_user write_repository sudo",
trusted = true,
): Promise<GitLabOAuthApp> {
const response = await fetch(`${this.apiBaseUrl}/applications`, {
method: "POST",
headers: {
// GitLab API header name
// eslint-disable-next-line @typescript-eslint/naming-convention
"PRIVATE-TOKEN": this.personalAccessToken,
"Content-Type": "application/json",
},
body: JSON.stringify({
name,
// eslint-disable-next-line @typescript-eslint/naming-convention
redirect_uri: redirectUri,
scopes,
trusted,
}),
});

if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Failed to create OAuth application: ${response.status} ${response.statusText} - ${errorText}`,
);
}

const app = (await response.json()) as Record<string, unknown>;
const id = app.id;
const applicationId = app.application_id;
const secret = app.secret;
if (
typeof id !== "number" ||
typeof applicationId !== "string" ||
typeof secret !== "string"
) {
throw new TypeError(
"GitLab API response missing required fields (id, application_id, or secret)",
);
}

const applicationName =
(typeof app.application_name === "string" && app.application_name) ||
(typeof app.name === "string" && app.name) ||
name;
const callbackUrl =
(typeof app.callback_url === "string" && app.callback_url) ||
(typeof app.redirect_uri === "string" && app.redirect_uri) ||
redirectUri;
const responseScopes = Array.isArray(app.scopes)
? app.scopes.filter((s): s is string => typeof s === "string")
: scopes.split(" ");

return {
id,
applicationId,
applicationName,
secret,
callbackUrl,
scopes: responseScopes,
};
}

async deleteOAuthApplication(applicationId: number): Promise<void> {
const response = await fetch(
`${this.apiBaseUrl}/applications/${applicationId}`,
{
method: "DELETE",
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
"PRIVATE-TOKEN": this.personalAccessToken,
},
},
);

if (!response.ok && response.status !== 404) {
const errorText = await response.text();
throw new Error(
`Failed to delete OAuth application: ${response.status} ${response.statusText} - ${errorText}`,
);
}
}
}
20 changes: 20 additions & 0 deletions workspaces/backstage/e2e-tests/support/constants/gitlab-auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** Static catalog token from tests/config/gitlab-auth/value-file.yaml */
export const GITLAB_AUTH_CATALOG_TOKEN = "gitlab-auth-e2e-token";

/** Display names expected after GitLab org ingestion (core auth-providers suite). */
export const GITLAB_INGESTED_USERS = [
"user1",
"user2",
"user3",
"Administrator",
] as const;

export const GITLAB_INGESTED_GROUPS = [
"my-org",
"group1",
"all",
"nested",
"nested_2",
] as const;

export const GITLAB_LOGIN_USER = "user1";
107 changes: 107 additions & 0 deletions workspaces/backstage/e2e-tests/support/gitlab/gitlab-login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { expect, type Locator, type Page } from "@playwright/test";
import type { UIhelper } from "@red-hat-developer-hub/e2e-test-utils/helpers";

/**
* GitLab OAuth popup login (ported from RHDH core Common.gitlabLogin).
* Not yet on LoginHelper in e2e-test-utils.
*/
export async function gitlabLogin(
page: Page,
uiHelper: UIhelper,
username: string,
password: string,
): Promise<string> {
await page.goto("/");
await page.waitForSelector('p:has-text("Sign in using GitLab")');

const [popup] = await Promise.all([
page.waitForEvent("popup"),
uiHelper.clickButton("Sign In"),
]);

await expect(async () => {
await popup.waitForLoadState("domcontentloaded");
expect(popup).toBeTruthy();
}).toPass({
intervals: [5_000, 10_000],
timeout: 20_000,
});

try {
await popup.waitForEvent("close", { timeout: 5000 });
return "Already logged in";
} catch {
// Popup stayed open — continue with credentials.
}

await popup.locator("#user_login").click({ timeout: 5000 });
await popup.locator("#user_login").fill(username, { timeout: 5000 });
await popup.locator("#user_password").click({ timeout: 5000 });
await popup.locator("#user_password").fill(password, { timeout: 5000 });
await popup.getByTestId("sign-in-button").click({ timeout: 5000 });

await popup
.waitForLoadState("domcontentloaded", { timeout: 10_000 })
.catch(() => undefined);

const twoFactorInput = popup.locator("#user_otp_attempt");
if (await twoFactorInput.isVisible({ timeout: 5000 }).catch(() => false)) {
await popup.waitForEvent("close", { timeout: 20_000 });
return "Login successful";
}

// GitLab EE button text is "Authorize <app-name>"; prefer role + testid.
const authorizeCandidates: Locator[] = [
popup.getByRole("button", { name: /Authorize/ }),
popup.getByTestId("authorize-button"),
popup.locator('button:has-text("Authorize")'),
];

await expect(async () => {
for (const candidate of authorizeCandidates) {
if (await candidate.isVisible({ timeout: 2000 }).catch(() => false)) {
return;
}
}
throw new Error("Authorization button not found");
}).toPass({
intervals: [1000, 2000],
timeout: 15_000,
});

let buttonToClick: Locator | undefined;
for (const candidate of authorizeCandidates) {
if (await candidate.isVisible().catch(() => false)) {
buttonToClick = candidate;
break;
}
}
if (!buttonToClick) {
throw new Error("Failed to find authorization button");
}

await popup
.getByRole("document")
.click({ timeout: 1000 })
.catch(() => undefined);

await buttonToClick.waitFor({ state: "visible", timeout: 5000 });
await expect(buttonToClick).toBeEnabled({ timeout: 10_000 });
await buttonToClick.scrollIntoViewIfNeeded({ timeout: 5000 });
await popup.waitForTimeout(1000);

try {
await buttonToClick.click({ timeout: 5000 });
} catch {
await buttonToClick.click({ force: true, timeout: 5000 });
}

try {
await popup.waitForEvent("close", { timeout: 20_000 });
} catch {
if (!popup.isClosed()) {
throw new Error("GitLab login popup did not close after sign-in");
}
}
return "Login successful";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
app:
baseUrl: ${RHDH_BASE_URL}
title: RHDH GitLab Auth E2E

backend:
baseUrl: ${RHDH_BASE_URL}
cors:
origin: ${RHDH_BASE_URL}

permission:
enabled: false

signInPage: gitlab

auth:
environment: production
providers:
guest:
dangerouslyAllowOutsideDevelopment: true
gitlab:
production:
audience: https://${AUTH_PROVIDERS_GITLAB_HOST}
clientId: ${AUTH_PROVIDERS_GITLAB_CLIENT_ID}
clientSecret: ${AUTH_PROVIDERS_GITLAB_CLIENT_SECRET}
callbackUrl: ${RHDH_BASE_URL}/api/auth/gitlab/handler/frame
# Dynamic gitlab auth provider has no implicit default (unlike core's
# static authProvidersModule). Match upstream default:
# usernameMatchingUserEntityName.
signIn:
resolvers:
- resolver: usernameMatchingUserEntityName

catalog:
rules:
- allow: [API, Component, Group, Location, Resource, System, Template, User]
providers:
gitlab:
orgProvider:
host: ${AUTH_PROVIDERS_GITLAB_HOST}
orgEnabled: true
group: ${AUTH_PROVIDERS_GITLAB_PARENT_ORG}
restrictUsersToGroup: true
includeUsersWithoutSeat: true
schedule:
initialDelay:
seconds: 0
frequency:
minutes: 1
timeout:
minutes: 1

integrations:
gitlab:
- host: ${AUTH_PROVIDERS_GITLAB_HOST}
token: ${AUTH_PROVIDERS_GITLAB_TOKEN}
apiBaseUrl: https://${AUTH_PROVIDERS_GITLAB_HOST}/api/v4
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
plugins:
- package: oci://quay.io/rhdh/red-hat-developer-hub-backstage-plugin-homepage:2.0.0--1.17.1!red-hat-developer-hub-backstage-plugin-homepage
disabled: true
- package: ./dynamic-plugins/dist/backstage-plugin-auth-backend-module-gitlab-provider-dynamic
disabled: false
- package: ./dynamic-plugins/dist/backstage-plugin-catalog-backend-module-gitlab-org-dynamic
disabled: false
pluginConfig:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] style/conventions

The catalog.providers.gitlab.orgProvider configuration block is duplicated identically between app-config-rhdh.yaml (lines 34-47) and dynamic-plugins.yaml pluginConfig (lines 8-24). This duplication increases maintenance burden without functional benefit since deep merge produces identical results.

Suggested fix: Keep the orgProvider config in one location only — either in dynamic-plugins.yaml pluginConfig (where the gitlab-org module reads it) or in app-config-rhdh.yaml, but not both.

catalog:
providers:
gitlab:
orgProvider:
host: ${AUTH_PROVIDERS_GITLAB_HOST}
orgEnabled: true
group: ${AUTH_PROVIDERS_GITLAB_PARENT_ORG}
restrictUsersToGroup: true
includeUsersWithoutSeat: true
schedule:
initialDelay:
seconds: 0
frequency:
minutes: 1
timeout:
minutes: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Secret
metadata:
name: rhdh-secrets
type: Opaque
stringData:
# Vault-synced (must be VAULT_* in CI / vault UI)
AUTH_PROVIDERS_GITLAB_HOST: $VAULT_AUTH_PROVIDERS_GITLAB_HOST
AUTH_PROVIDERS_GITLAB_TOKEN: $VAULT_AUTH_PROVIDERS_GITLAB_TOKEN
AUTH_PROVIDERS_GITLAB_PARENT_ORG: $VAULT_AUTH_PROVIDERS_GITLAB_PARENT_ORG
DEFAULT_USER_PASSWORD: $VAULT_DEFAULT_USER_PASSWORD
# Set dynamically in beforeAll after creating the ephemeral OAuth app
AUTH_PROVIDERS_GITLAB_CLIENT_ID: $AUTH_PROVIDERS_GITLAB_CLIENT_ID
AUTH_PROVIDERS_GITLAB_CLIENT_SECRET: $AUTH_PROVIDERS_GITLAB_CLIENT_SECRET
RHDH_BASE_URL: $RHDH_BASE_URL
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Static external-access token for catalog API calls (Helm-injected appConfig).
global:
lightspeed:
enabled: false

upstream:
backstage:
appConfig:
backend:
auth:
externalAccess:
- type: static
options:
token: gitlab-auth-e2e-token
subject: gitlab-auth-e2e
2 changes: 1 addition & 1 deletion workspaces/backstage/e2e-tests/tests/specs/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const isNightlyMode =
!!process.env.E2E_NIGHTLY_MODE ||
(process.env.JOB_NAME?.includes("periodic-") ?? false);

test.describe("Auth plugin", () => {
test.describe("Auth plugin", { tag: "@auth-tests" }, () => {
test.beforeAll(async ({ rhdh }) => {
await rhdh.configure({
auth: "guest",
Expand Down
Loading
Loading