第4章 Enabling user authentication with GitHub
To authenticate users with GitHub, configure the GitHub authentication provider in Red Hat Developer Hub and provision the users and groups from GitHub to the Developer Hub software catalog.
Prerequisites
- You added a custom Developer Hub application configuration, and have sufficient permissions to modify it.
- You have sufficient permissions in GitHub to create and manage a GitHub App. Alternatively, you can ask your GitHub administrator to prepare the required GitHub App.
Procedure
To allow Developer Hub to authenticate with GitHub, create a GitHub App. Opt for a GitHub App instead of an OAuth app to use fine-grained permissions and use short-lived tokens.
Register a GitHub App with the following configuration:
- GitHub App name
-
Enter a unique name identifying your GitHub App, such as
authenticating-with-rhdh-<GUID>. - Homepage URL
-
Enter your Developer Hub URL:
https://<my_developer_hub_domain>. - Authorization callback URL
-
Enter your Developer Hub authentication backend URL:
https://<my_developer_hub_domain>/api/auth/github/handler/frame. - Webhook
- Clear "Active", as this is not needed for authentication and catalog providers.
- Organization permissions
-
Enable
Read-onlyaccess to Members. - Where can this GitHub App be installed?
-
Select
Only on this account.
-
In the General
Clients secrets section, click Generate a new client secret. - In the Install App tab, choose an account to install your GitHub App on.
Save the following values for the next step:
- Client ID
- Client secret
To add your GitHub credentials to Developer Hub, add the following key/value pairs to your Developer Hub secrets. You can use these secrets in the Developer Hub configuration files by using their respective environment variable name.
AUTHENTICATION_GITHUB_CLIENT_ID- Enter the saved Client ID.
AUTHENTICATION_GITHUB_CLIENT_SECRET- Enter the saved Client Secret.
AUTHENTICATION_GITHUB_HOST_DOMAIN-
Enter the GitHub host domain:
github.com. AUTHENTICATION_GITHUB_ORGANIZATION-
Enter your GitHub organization name, such as
<your_github_organization_name>.
Enable the GitHub organization provisioning plugin (
backstage-plugin-catalog-backend-module-github-org-dynamic). This plugin ingests GitHub users and groups to the Developer Hub software catalog.dynamic-plugins.yamlfile fragmentplugins: - package: './dynamic-plugins/dist/backstage-plugin-catalog-backend-module-github-org-dynamic' disabled: falseTo provision GitHub users and groups to the Developer Hub software catalog, add the
catalog.providers.githubOrgsection to your custom Developer Hubapp-config.yamlconfiguration file:app-config.yamlfragment with mandatorycatalog.providers.githubOrgfieldscatalog: providers: githubOrg: id: githuborg githubUrl: "${AUTHENTICATION_GITHUB_HOST_DOMAIN}" orgs: [ "${AUTHENTICATION_GITHUB_ORGANIZATION}" ] schedule: frequency: minutes: 30 initialDelay: seconds: 15 timeout: minutes: 15id-
Enter a stable identifier for this provider, such as
githuborg. Entities from this provider are associated with this identifier, therefore you must take care not to change it over time since that might lead to orphaned entities and/or conflicts. githubUrl-
Enter the configured secret variable name:
${AUTHENTICATION_GITHUB_HOST_DOMAIN}. orgs-
Enter the configured secret variable name:
${AUTHENTICATION_GITHUB_ORGANIZATION}. schedule.frequency- Enter your schedule frequency, in the cron, ISO duration, or "human duration" format.
schedule.timeout- Enter your schedule timeout, in the ISO duration or "human duration" format.
schedule.initialDelay- Enter your schedule initial delay, in the ISO duration or "human duration" format.
To set up the GitHub authentication provider, add the
auth.providers.githubsection to theapp-config.yamlfile content:app-config.yamlfile fragment with mandatory fields to enable authentication with GitHubauth: environment: production providers: github: production: clientId: ${AUTHENTICATION_GITHUB_CLIENT_ID} clientSecret: ${AUTHENTICATION_GITHUB_CLIENT_SECRET} signInPage: githubenvironment-
Enter
productionto disable the Guest login option in the Developer Hub login page. clientId-
Enter the configured secret variable name:
${AUTHENTICATION_GITHUB_CLIENT_ID}. clientSecret-
Enter the configured secret variable name:
${AUTHENTICATION_GITHUB_CLIENT_SECRET}. signInPage-
Enter
githubto enable the GitHub provider as your Developer Hub sign-in provider.
Optional: Consider adding the following optional fields:
app-config.yamlfile fragment including optional fields to enable authentication with GitHubauth: environment: production providers: github: production: clientId: ${AUTHENTICATION_GITHUB_CLIENT_ID} clientSecret: ${AUTHENTICATION_GITHUB_CLIENT_SECRET} callbackUrl: <your_intermediate_service_url/handler> sessionDuration: { hours: 24 } signIn: resolvers: - resolver: usernameMatchingUserEntityName dangerouslyAllowSignInWithoutUserInCatalog: true signInPage: githubcallbackUrl- Enter the callback URL that GitHub uses when initiating an OAuth flow, such as: <your_intermediate_service_url/handler>. Define it when Developer Hub is not the immediate receiver, such as in cases when you use one OAuth app for many Developer Hub instances.
sessionDuration-
Enter the user session lifespan, in
mslibrary format (such as '24h', '2 days'), ISO duration, or "human duration". signInresolvers- After successful authentication, Developer Hub resolves the user signing in to an existing user in the Developer Hub catalog. To best match users securely for your use case, consider configuring a specific resolver.
Enter the resolver list to override the default resolver:
usernameMatchingUserEntityName.The authentication provider tries each sign-in resolver in order until it succeeds, and fails if none succeed.
警告In production mode, only configure one resolver to ensure users are securely matched.
resolverEnter the sign-in resolver name. Available resolvers:
-
usernameMatchingUserEntityName -
preferredUsernameMatchingUserEntityName -
emailMatchingUserEntityProfileEmail
-
dangerouslyAllowSignInWithoutUserInCatalog: trueConfigure the sign-in resolver to bypass the user provisioning requirement in the Developer Hub software catalog.
警告Use
dangerouslyAllowSignInWithoutUserInCatalogto explore Developer Hub features, but do not use it in production.
Verification
To verify user and group provisioning, check the console logs.
Successful synchronization example:
{"class":"GithubMultiOrgEntityProvider","level":"info","message":"Reading GitHub users and teams for org: rhdh-dast","plugin":"catalog","service":"backstage","target":"https://github.com","taskId":"GithubMultiOrgEntityProvider:githuborg:refresh","taskInstanceId":"801b3c6c-167f-473b-b43e-e0b4b780c384","timestamp":"2024-09-09 23:55:58"} {"class":"GithubMultiOrgEntityProvider","level":"info","message":"Read 7 GitHub users and 2 GitHub groups in 0.4 seconds. Committing...","plugin":"catalog","service":"backstage","target":"https://github.com","taskId":"GithubMultiOrgEntityProvider:githuborg:refresh","taskInstanceId":"801b3c6c-167f-473b-b43e-e0b4b780c384","timestamp":"2024-09-09 23:55:59"}To verify GitHub authentication:
- Go to the Developer Hub login page.
- Your Developer Hub sign-in page displays Sign in using GitHub and the Guest user sign-in is disabled.
- Log in with a GitHub account.
Additional resources