Repository navigation
GitHub sign-in for Google Workspace via Cloudflare Access #230
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { execFileSync } from 'node:child_process'; | ||
| import { test } from 'node:test'; | ||
| import jsonata from 'jsonata'; | ||
| import { ROLE_IDS } from '../src/config/roleIds'; | ||
| import { MEMBERS } from '../src/config/users'; | ||
| import type { Member } from '../src/config/utils'; | ||
| import { | ||
| buildNameIdTransform, | ||
| getWorkspaceSsoTable, | ||
| googleSamlProfile, | ||
| UNMAPPED_NAME_ID, | ||
| } from '../src/config/workspaceSso'; | ||
|
|
||
| const pilot: Member = { | ||
| github: 'octo', | ||
| githubUserId: 42, | ||
| googleEmailPrefix: 'octo', | ||
| memberOf: [ROLE_IDS.WORKSPACE_SSO, ROLE_IDS.GO_SDK], | ||
| }; | ||
| const email = 'octo@modelcontextprotocol.io'; | ||
| const identity = { id: 42, idp: { id: 'gh' }, groups: [{ name: 'modelcontextprotocol' }] }; | ||
|
|
||
| test('the pilot team requires a GitHub identity and a current provisioning role', () => { | ||
| assert.deepEqual(getWorkspaceSsoTable([pilot, { ...pilot, memberOf: [] }]), { 42: email }); | ||
| for (const change of [ | ||
| { github: undefined }, | ||
| { githubUserId: undefined }, | ||
| { githubUserId: -1 }, | ||
| { googleEmailPrefix: 'x@evil.com' }, | ||
| { googleEmailPrefix: undefined }, | ||
| { existingGWSUser: true, memberOf: [ROLE_IDS.WORKSPACE_SSO] }, | ||
| { skipGoogleUserProvisioning: true }, | ||
| ]) | ||
| assert.throws(() => getWorkspaceSsoTable([{ ...pilot, ...change }])); | ||
| assert.throws(() => getWorkspaceSsoTable([pilot, { ...pilot, googleEmailPrefix: 'cat' }])); | ||
| }); | ||
|
|
||
| test('NameID uses the numeric ID, GitHub IdP and org', async () => { | ||
| const transform = jsonata(buildNameIdTransform({ 42: email }, 'gh')); | ||
| for (const id of [42, '42']) assert.equal(await transform.evaluate({ ...identity, id }), email); | ||
| for (const change of [ | ||
| { id: 43 }, | ||
| { id: undefined }, | ||
| { id: [42] }, | ||
| { id: '__proto__' }, | ||
| { idp: { id: 'otp' } }, | ||
| { idp: undefined }, | ||
| { groups: undefined }, | ||
| { groups: [{ name: 'other' }] }, | ||
| ]) | ||
| assert.equal(await transform.evaluate({ ...identity, ...change }), UNMAPPED_NAME_ID); | ||
| assert.equal(await jsonata(buildNameIdTransform({}, 'gh')).evaluate(identity), UNMAPPED_NAME_ID); | ||
| }); | ||
|
|
||
| test('Google requires both profile values after inert bootstrap', () => { | ||
| const issuer = 'https://accounts.google.com/samlrp/metadata?rpid=example'; | ||
| const acs = 'https://accounts.google.com/samlrp/example/acs'; | ||
| assert.deepEqual(googleSamlProfile(), { | ||
| spEntityId: 'https://access.invalid/sp', | ||
| consumerServiceUrl: 'https://access.invalid/acs', | ||
| }); | ||
| assert.deepEqual(googleSamlProfile(issuer, acs), { spEntityId: issuer, consumerServiceUrl: acs }); | ||
| for (const id of [undefined, '', 'google.com', 'google.com/a/example.org']) | ||
| assert.throws(() => googleSamlProfile(id, acs)); | ||
| for (const url of [ | ||
| undefined, | ||
| 'https://accounts.google.com/a/example.org/acs', | ||
| 'https://www.google.com/a/example.org/acs', | ||
| ]) | ||
| assert.throws(() => googleSamlProfile(issuer, url)); | ||
| }); | ||
|
|
||
| test( | ||
| 'GitHub IDs resolve to their configured logins', | ||
| { skip: !process.argv.includes('--verify-github-ids') }, | ||
| () => { | ||
| for (const m of MEMBERS.filter((member) => member.memberOf.includes(ROLE_IDS.WORKSPACE_SSO))) { | ||
| const login = execFileSync('gh', ['api', `/user/${m.githubUserId}`, '--jq', '.login'], { | ||
| encoding: 'utf8', | ||
| }).trim(); | ||
| assert.equal( | ||
| login.toLowerCase(), | ||
| m.github?.toLowerCase(), | ||
| `GitHub user ID ${m.githubUserId} resolves to ${login}` | ||
| ); | ||
| } | ||
| } | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,12 @@ | ||
| import * as pulumi from '@pulumi/pulumi'; | ||
| import * as cloudflare from '@pulumi/cloudflare'; | ||
| import { ACCESS_POLICIES, GITHUB_ORG, getAccessPolicyTeams } from './config/accessPolicies'; | ||
| import { ROLE_IDS } from './config/roleIds'; | ||
| import { | ||
| buildNameIdTransform, | ||
| getWorkspaceSsoTable, | ||
| googleSamlProfile, | ||
| } from './config/workspaceSso'; | ||
|
|
||
| const config = new pulumi.Config('cloudflare'); | ||
| // Cloudflare integration is optional - only enabled if roleManagementToken is configured, | ||
|
|
@@ -65,6 +71,51 @@ if (CLOUDFLARE_ENABLED) { | |
| } | ||
| ); | ||
| }); | ||
|
|
||
| // GitHub sign-in for Google Workspace: Access is the SAML IdP for Google's SSO | ||
| // profile. Workspace SSO team only, GitHub only; the NameID transform picks the account. | ||
| const workspaceSsoPolicy = new cloudflare.ZeroTrustAccessPolicy( | ||
| 'access-policy-workspace-sso', | ||
| { | ||
| accountId, | ||
| name: 'Google Workspace sign-in', | ||
| decision: 'allow', | ||
| sessionDuration: '1h', | ||
| includes: [ | ||
| { | ||
| githubOrganization: { | ||
| identityProviderId: githubIdentityProviderId, | ||
| name: GITHUB_ORG, | ||
| team: ROLE_IDS.WORKSPACE_SSO, | ||
|
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. The policy names the team as a string, so it doesn't wait for Pulumi to create it. Can we pass the name from the team resource in Generated by Claude Code |
||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { provider, ignoreChanges: ['connectionRules'] } | ||
| ); | ||
| new cloudflare.ZeroTrustAccessApplication( | ||
| 'access-app-workspace-sso', | ||
| { | ||
| accountId, | ||
| name: 'Google Workspace', | ||
| type: 'saas', | ||
| allowedIdps: [githubIdentityProviderId], | ||
| autoRedirectToIdentity: true, | ||
| appLauncherVisible: false, | ||
| sessionDuration: '1h', | ||
| policies: [{ id: workspaceSsoPolicy.id, precedence: 1 }], | ||
| saasApp: { | ||
| authType: 'saml', | ||
| ...googleSamlProfile(config.get('googleSamlEntityId'), config.get('googleSamlAcsUrl')), | ||
| nameIdFormat: 'email', | ||
| nameIdTransformJsonata: buildNameIdTransform( | ||
| getWorkspaceSsoTable(), | ||
| githubIdentityProviderId | ||
| ), | ||
| }, | ||
|
Comment on lines
+105
to
+115
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. Two small additions:
Generated by Claude Code |
||
| }, | ||
| { provider } | ||
| ); | ||
| } | ||
|
|
||
| export const cloudflareAccessPolicyIds = Object.fromEntries( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // GitHub sign-in for Google Workspace via a Cloudflare Access SAML app (src/cloudflare.ts). | ||
| // See README "GitHub sign-in for Google Workspace". | ||
| import { GITHUB_ORG } from './accessPolicies'; | ||
| import { ROLE_IDS } from './roleIds'; | ||
| import { buildRoleLookup } from './roles'; | ||
| import { MEMBERS } from './users'; | ||
| import { hasProvisionUserRole, resolveGoogleMemberEmail, type Member } from './utils'; | ||
|
|
||
| /** NameID for anyone not mapped; `.invalid` (RFC 2606) is never a Workspace account. */ | ||
| export const UNMAPPED_NAME_ID = 'unmapped@access.invalid'; | ||
|
|
||
| /** `githubUserId -> Workspace email` for members of the Workspace SSO team; throws on bad config. */ | ||
| export function getWorkspaceSsoTable(members: readonly Member[] = MEMBERS): Record<string, string> { | ||
| const table: Record<string, string> = {}; | ||
| const roles = buildRoleLookup(); | ||
| for (const m of members.filter((m) => m.memberOf.includes(ROLE_IDS.WORKSPACE_SSO))) { | ||
| if (!m.github || !hasProvisionUserRole(m.memberOf, roles) || m.skipGoogleUserProvisioning) { | ||
| throw new Error( | ||
| `Workspace SSO: "${m.googleEmailPrefix}" needs a GitHub login and a provisioning role` | ||
| ); | ||
| } | ||
| const id = m.githubUserId ?? 0; | ||
| if ( | ||
| !Number.isSafeInteger(id) || | ||
| id <= 0 || | ||
| String(id) in table || | ||
| !/^[a-z0-9][a-z0-9._-]*$/.test(m.googleEmailPrefix ?? '') | ||
| ) { | ||
| throw new Error( | ||
| `Workspace SSO: "${m.github}" needs a unique githubUserId and googleEmailPrefix` | ||
| ); | ||
| } | ||
| table[String(id)] = resolveGoogleMemberEmail(m)!; | ||
| } | ||
| return table; | ||
| } | ||
|
|
||
| /** | ||
| * JSONata NameID transform, evaluated against the Access identity: `id` (numeric | ||
| * GitHub user ID), `idp.id` (login method), `groups[].name` (GitHub orgs). | ||
| * Unmapped inputs evaluate to UNMAPPED_NAME_ID. Literals are JSON-encoded, so config can't inject JSONata. | ||
| */ | ||
| export function buildNameIdTransform(table: Record<string, string>, githubIdpId: string): string { | ||
| return `( | ||
|
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. The description says an engine difference fails closed. The expression does, but Cloudflare doesn't document what it sends when a transform errors or returns nothing, and the default NameID is the user's email. If it falls back to that, the allowlist is skipped for any org member whose GitHub email is on our domain. Before the Google profile is assigned, can you capture the SAML response for an unlisted org member and confirm the NameID is Generated by Claude Code
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. Both checks use a throwaway app, so they don't need this merged, and this thread has to be resolved before merge anyway. Can you run them first and post the results here? While the app is up:
Generated by Claude Code |
||
| $allowed := idp.id = ${JSON.stringify(githubIdpId)} and ${JSON.stringify(GITHUB_ORG)} in groups.name and $type(id) in ["number", "string"]; | ||
| $email := $allowed ? $lookup(${JSON.stringify(table)}, $string(id)); | ||
| $type($email) = "string" ? $email : ${JSON.stringify(UNMAPPED_NAME_ID)} | ||
| )`; | ||
| } | ||
|
|
||
| /** Bootstrap without a Google destination until the modern profile supplies both SP values. */ | ||
| export function googleSamlProfile(spEntityId?: string, consumerServiceUrl?: string) { | ||
| if (spEntityId === undefined && consumerServiceUrl === undefined) { | ||
| return { | ||
| spEntityId: 'https://access.invalid/sp', | ||
| consumerServiceUrl: 'https://access.invalid/acs', | ||
| }; | ||
| } | ||
| const acs = consumerServiceUrl && new URL(consumerServiceUrl); | ||
| if ( | ||
| !spEntityId?.trim() || | ||
| /^google\.com(?:\/|$)/i.test(spEntityId.trim()) || | ||
| !acs || | ||
| acs.origin !== 'https://accounts.google.com' || | ||
| !acs.pathname.startsWith('/samlrp/') | ||
| ) { | ||
| throw new Error('Set both SP values from the modern Google SAML profile in the Admin console'); | ||
| } | ||
| return { spEntityId, consumerServiceUrl: acs.href }; | ||
| } | ||
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.
Step 3 says to check the 2-Step Verification settings but not what passes. Can we state it: post-SSO verification applies to SSO profile users, 2SV is enforced and the account is enrolled, and the account has no admin role?
Generated by Claude Code