Skip to content
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

New system test creating an API Key Role for Buildkite #5434

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yob
Copy link
Contributor

@yob yob commented Feb 5, 2025

Note: depends on #5416. Once that merges, I can rebase this and the first commit will fall away

At the moment creating an API Key Role for Buildkite on rubygems.org results in an Access Policy that requires a GitHub Actions principal: https://token.actions.githubusercontent.com. To fix it, I have to inspect the DOM on the new form and make the principal input visible, then change it to https://agent.buildkite.com.

This PR has two commits:

  1. Add a failing system test that demonstrates the issue
  2. A possible fix

Currently the fix is to change a conditional to use == instead of =. However, it's likely that will break some other tests. A possible alternative fix is:

diff --git a/app/controllers/oidc/api_key_roles_controller.rb b/app/controllers/oidc/api_key_roles_controller.rb
index 29d065b07..791b05fac 100644
--- a/app/controllers/oidc/api_key_roles_controller.rb
+++ b/app/controllers/oidc/api_key_roles_controller.rb
@@ -136,8 +136,6 @@ class OIDC::ApiKeyRolesController < ApplicationController
     return unless (gh = helpers.link_to_github(rubygem)).presence
     return unless (@api_key_role.provider == OIDC::Provider.github_actions)
 
-    statement.principal = { oidc: @api_key_role.provider.issuer }
-
     repo_condition = OIDC::AccessPolicy::Statement::Condition.new(
       claim: "repository",
       operator: "string_equals",

The principal input is hidden on the form anyway, and will be set to a default value in the OIDC::ApiKeyRole#set_statement_principals model callback

Fixes #5376

@@ -134,7 +134,7 @@ def add_default_params(rubygem, statement, condition)

return unless rubygem
return unless (gh = helpers.link_to_github(rubygem)).presence
return unless (@api_key_role.provider = OIDC::Provider.github_actions)
return unless (@api_key_role.provider == OIDC::Provider.github_actions)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is the conditional with assignment instead of equality intentional or accidental? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

it intentionally was an assignment, this is where the provider would get assigned (as a default, I think)

@@ -134,7 +134,7 @@ def add_default_params(rubygem, statement, condition)

return unless rubygem
return unless (gh = helpers.link_to_github(rubygem)).presence
return unless (@api_key_role.provider = OIDC::Provider.github_actions)
return unless (@api_key_role.provider == OIDC::Provider.github_actions)
Copy link
Member

Choose a reason for hiding this comment

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

it intentionally was an assignment, this is where the provider would get assigned (as a default, I think)

@yob
Copy link
Contributor Author

yob commented Feb 13, 2025

ah, fair enough then! From the outside it looked like the expression would never fail so might not have been intentional.

Would you prefer the alternative, where we just remove the hard coded principal assignment, and let than happen on form submit instead?

yob added 2 commits February 13, 2025 13:55
The form and POSTing to create works, but the resulting API Key Role has
a condition that expects a principal of "https://token.actions.githubusercontent.com"
The conditional in add_default_params is always try (it's a single =) so
statement.principal is set to the GitHub Actions principal
(https://token.actions.githubusercontent.com) every time the new API Key
Role form is loaded.

That field is hidden on the form so the user doesn't have a chance to
edit it, and after saving the created role has a provider of Buildkite
with an expected principal for GitHub Actions.

An alternative solution would be to remove the statement.principal
assignment completely. It's not required - when the form is submitted
the OIDC::ApiKeyRole#set_statement_principals callback will set the
correct principal for both GitHub Actionas *and* Buildkite.
@yob yob force-pushed the non-github-actions-api-key-role branch from 62605db to d810930 Compare February 13, 2025 02:55
@yob
Copy link
Contributor Author

yob commented Feb 13, 2025

I've rebased onto master and force pushed, to remove the commit from #5416

@segiddins
Copy link
Member

Would you prefer the alternative, where we just remove the hard coded principal assignment, and let than happen on form submit instead?

I think that's also OK!

Copy link

codecov bot commented Feb 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.17%. Comparing base (763630d) to head (d810930).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5434      +/-   ##
==========================================
- Coverage   97.06%   94.17%   -2.89%     
==========================================
  Files         451      451              
  Lines        9391     9450      +59     
==========================================
- Hits         9115     8900     -215     
- Misses        276      550     +274     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Creating an API Key Role for Buildkite results in an access policy that requires Github Actions
2 participants