|
1 | 1 | exports.onExecutePostLogin = async (event, api) => { |
2 | 2 | console.log("Running action:", "gheGroups"); |
3 | 3 |
|
| 4 | + // There's a way for us to store these configs in a "friendlier" way, but the |
| 5 | + // method we use may change. |
| 6 | + // |
| 7 | + // For now it's easier to hard-code these values until we resolve IAM-1499, |
| 8 | + // "Change the way apps.yaml is accessed". |
| 9 | + // |
| 10 | + // TODO(bhee) If in a ~1yr (2026-04-07) we haven't made progress on IAM-1499 |
| 11 | + // or there's too much toil, we should consider stashing a yml file somewhere |
| 12 | + // and `fetch`ing it here. |
| 13 | + const GHE_ADMINS_GROUP = "mozilliansorg_ghe_admins"; |
| 14 | + |
| 15 | + // Used in tandem with `securityManagersAllowedApplications`. |
| 16 | + const GHE_SECURITY_MANAGERS_GROUP = "mozilliansorg_ghe_security-managers"; |
| 17 | + |
4 | 18 | // Object of applications with a 1:1 mapping of clientID and related group |
5 | 19 | const applicationGroupMapping = { |
6 | 20 | // Dev applications |
@@ -80,6 +94,15 @@ exports.onExecutePostLogin = async (event, api) => { |
80 | 94 | JDiNCQVrXzw2ILureegz1T8c3OrUZCUb: "mozilliansorg_ghe_mozilla-firefox_users", |
81 | 95 | }; |
82 | 96 |
|
| 97 | + // The applications to allow a member of `GHE_SECURITY_MANAGERS_GROUP` access |
| 98 | + // to. |
| 99 | + // |
| 100 | + // Right now, defined as all applications. This may change at some point in |
| 101 | + // the future. |
| 102 | + const securityManagersAllowedApplications = Object.keys( |
| 103 | + applicationGroupMapping |
| 104 | + ); |
| 105 | + |
83 | 106 | // ClientID isn't mapped here, return callback() and proceed rules processing |
84 | 107 | if (applicationGroupMapping[event.client.client_id] === undefined) { |
85 | 108 | console.log("Not mapped"); |
@@ -169,52 +192,71 @@ exports.onExecutePostLogin = async (event, api) => { |
169 | 192 | } |
170 | 193 | }; |
171 | 194 |
|
172 | | - const processProfile = (profile) => { |
173 | | - // Create a new URL object |
| 195 | + const bail = (errorCode) => { |
174 | 196 | const gheWikiUrl = new URL("https://wiki.mozilla.org/GitHub/SAML_issues"); |
175 | | - |
176 | | - // Set the tenant searchParam |
177 | 197 | gheWikiUrl.searchParams.set("auth", event.tenant.id); |
| 198 | + gheWikiUrl.searchParams.set("dbg", errorCode); |
| 199 | + api.redirect.sendUserTo(gheWikiUrl.href); |
| 200 | + return api.access.deny(`Access denied: See ${gheWikiUrl.href}`); |
| 201 | + }; |
178 | 202 |
|
179 | | - let errorCode; |
180 | | - |
181 | | - // Confirm the user has the group defined from mozillians matching the application id |
| 203 | + // Confirm the user has a githubUsername stored in mozillians and they have |
| 204 | + // the correct access groups. |
| 205 | + const processProfile = (profile) => { |
| 206 | + // Get githubUsername from person api, otherwise we'll redirect |
| 207 | + let githubUsername; |
| 208 | + try { |
| 209 | + githubUsername = profile.usernames.values["HACK#GITHUB"]; |
| 210 | + // If profile does not hold key/value for githubUsername |
| 211 | + if (githubUsername === undefined) { |
| 212 | + console.log("githubUsername is undefined"); |
| 213 | + return bail("ghnd"); |
| 214 | + } else if (githubUsername.length === 0) { |
| 215 | + // If somehow dinopark allows a user to store an empty value |
| 216 | + console.log("empty HACK#GITHUB"); |
| 217 | + return bail("ghnd"); |
| 218 | + } |
| 219 | + } catch (err) { |
| 220 | + console.log("Unable to do the githubUsername lookup: " + err); |
| 221 | + return bail("ghul"); |
| 222 | + } |
| 223 | + // Confirm the user has the group defined from mozillians matching the |
| 224 | + // application's client id. |
182 | 225 | if ( |
183 | | - !event.user.app_metadata.groups?.includes( |
| 226 | + event.user.app_metadata.groups?.includes( |
184 | 227 | applicationGroupMapping[event.client.client_id] |
185 | 228 | ) |
186 | 229 | ) { |
187 | | - errorCode = "ghgr"; |
188 | | - } else { |
189 | | - // Get githubUsername from person api, otherwise we'll redirect |
190 | | - let githubUsername; |
191 | | - |
192 | | - try { |
193 | | - githubUsername = profile.usernames.values["HACK#GITHUB"]; |
194 | | - // If profile does not hold key/value for githubUsername |
195 | | - if (githubUsername === undefined) { |
196 | | - console.log("githubUsername is undefined"); |
197 | | - errorCode = "ghnd"; |
198 | | - } else if (githubUsername.length === 0) { |
199 | | - // If somehow dinopark allows a user to store an empty value |
200 | | - console.log("empty HACK#GITHUB"); |
201 | | - errorCode = "ghnd"; |
202 | | - } |
203 | | - } catch (err) { |
204 | | - console.log("Unable to do the githubUsername lookup: " + err); |
205 | | - errorCode = "ghul"; |
206 | | - } |
| 230 | + console.log( |
| 231 | + `Granting access for ${githubUsername} (member of the mozillians GHE group)` |
| 232 | + ); |
| 233 | + return; |
207 | 234 | } |
208 | | - |
209 | | - // confirm the user has a githubUsername stored in mozillians, otherwise redirect |
210 | | - if (errorCode) { |
211 | | - // Set the search parameter error code |
212 | | - gheWikiUrl.searchParams.set("dbg", errorCode); |
213 | | - // Redirect the user |
214 | | - api.redirect.sendUserTo(gheWikiUrl.href); |
215 | | - return api.access.deny(`Access denied: See ${gheWikiUrl.href}`); |
| 235 | + // Or, the member is a part of the security managers group, with specific |
| 236 | + // access to certain groups. |
| 237 | + // |
| 238 | + // See comments for `GHE_SECURITY_MANAGERS_GROUP` and |
| 239 | + // `securityManagersAllowedApplications`. |
| 240 | + if ( |
| 241 | + event.user.app_metadata.groups?.includes(GHE_SECURITY_MANAGERS_GROUP) && |
| 242 | + securityManagersAllowedApplications.includes(event.client.client_id) |
| 243 | + ) { |
| 244 | + console.log( |
| 245 | + `Granting access for ${githubUsername} (member of the security managers group)` |
| 246 | + ); |
| 247 | + return; |
216 | 248 | } |
217 | | - return; |
| 249 | + // Or, the member is a part of the admins group. |
| 250 | + if (event.user.app_metadata.groups?.includes(GHE_ADMINS_GROUP)) { |
| 251 | + console.log( |
| 252 | + `Granting access for ${githubUsername} (member of the admins group)` |
| 253 | + ); |
| 254 | + return; |
| 255 | + } |
| 256 | + console.log( |
| 257 | + `Denying access to GHE, not a member; not in security; and not an admin` |
| 258 | + ); |
| 259 | + return bail("ghgr"); |
218 | 260 | }; |
219 | 261 |
|
220 | 262 | // Main |
|
0 commit comments