Skip to content

Conversation

@derwasp
Copy link
Contributor

@derwasp derwasp commented Sep 5, 2022

Disclaimer
This the first PR in GO for me. I don't know how things are usually done.

Background

The actions-sync app seems to also be used for the GHAE instances sometimes. The issue there was that the default admin user doesn't have permission to the actions and the github orgs. The previous workaround was to temporarily add the admin user to the required orgs and then run the app, which is not really convenient for customers who don't have access to the instance console and therefore can't run ghe-org-admin-promote.

We already have an app which impersonates the actions-admin user, and this is how it was implemented: github/codeql-action-sync-tool#48
This PR does something similar.

Proposed changes

  • Add a new flag: --actions-admin-user which would accept the actions user. By default the flag is not set. To use the default value, a user has to pass actions-admin to it. The logic is based on this. This flag is used to impersonate the actions admin user before attempting to push the repository. Note that this logic requires the site_admin scope to be available in the token.
  • Fixed the tests to run with the new logic which impersonates the user before the push call: I just stubbed the endpoints.
  • Added the new flags to the readme.md: 👉 rendered

Unrelated changes

  • Added the artifact binary to the .gitignore file

Notes

To manually test this, do the following:

  1. Create a new GHAE instance (you can use a chat op).
  2. Create a new personal access token for the ghae-admin user.
  3. Spin up a new codespaces instance in this repository and checkout this PR.
  4. In your codespaces build the project: go build.
  5. Run the actions-sync command in your codespaces like so, where destination-token is the token for the ghae-admin user:
      mkdir /tmp/cache
      actions-sync sync \
        --cache-dir "/tmp/cache" \
        --destination-token "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
        --destination-url "https://ghaebuildXXXXXXXXXXXX.XXXXXX.net/" \
        --repo-name actions/setup-node \
        --actions-admin-user actions-admin
  1. The output should look something like this:
    @derwasp ➜ /workspaces/actions-sync $ go build
    @derwasp ➜ /workspaces/actions-sync $ actions-sync sync \
    >     --cache-dir "/tmp/cache" \
    >     --destination-token "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
    >     --destination-url "https://ghaebuildXXXXXXXXXXXX.XXXXXXXX.net/" \
    >     --repo-name actions/setup-node
    fetching * refs for actions/setup-node ...
    Getting an impersonation token for `actions-admin`..
    Running against GitHub AE, changing the repository scope to 'repo'..
    Got the impersonation token for `actions-admin`..
    syncing `actions/setup-node`
    successfully synced `actions/setup-node`

Previously the output would look something like this:

    fetching * refs for actions/setup-node ...
    syncing `actions/setup-node`
    error creating github repository `actions/setup-node`: error creating repository actions/setup-node: POST https://ghaebuildXXXXXXXXXXXX.XXXXXXXX.net/api/v3/orgs/actions/repos: 403 You need admin access to the organization before adding a repository to it. []

@derwasp derwasp changed the title Add a --actions-admin-user flag Add actions admin user impersonation via a --actions-admin-user flag Sep 6, 2022
@derwasp derwasp marked this pull request as ready for review September 6, 2022 11:19
@derwasp derwasp requested a review from a team as a code owner September 6, 2022 11:19
@derwasp
Copy link
Contributor Author

derwasp commented Sep 6, 2022

I can't request a review, since I don't have permissions in the repo, so I am going to tag people in the message instead.

Tagging
@TingluoHuang you created the original issue

@talktopri you reopened the original issue

@ajaykn you seem to be the maintainer

@chrisgavin as you implemented the original logic

@zarenner since you participated in the discussions about this long ago

Team
@kgrzebien @JacobMillward @cwille97 @jidicula @easyt

Copy link

@JacobMillward JacobMillward left a comment

Choose a reason for hiding this comment

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

Just a few really minor suggestions. Otherwise, looks great!

JacobMillward
JacobMillward previously approved these changes Sep 6, 2022
Copy link

@jidicula jidicula left a comment

Choose a reason for hiding this comment

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

Small change for return type idiom

README.md Outdated
**Arguments:**

- `actions-admin-user` _(optional)_
The name of the Actions admin user, which will be used for updating the chosen action. If not specified `actions-admin` will be used.
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a basic question:

When creating a personal access token include the repo and workflow scopes. 
Include the site_admin scope (optional) if you want organizations to be created as necessary.

With this impersonation to work, what kind of access scope is needed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I tested this manually, I used the same scopes as I would normally would for the token: public_repo (or repo for the AE instance) and workflow and it worked for me.
So it seems like nothing is changing there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested it with GHES today and for whatever reason, I needed to have a site_admin scope in my token. I will investigate further.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe impersonation should require the site_admin scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chrisgavin should this also be true for the AE instances?

Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK, yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just verified it. Here are the example runs with the recent code change:

GHES
A failed request with no site_admin permission:

> actions-sync sync \
>     --cache-dir "/tmp/cache" \
>     --destination-token "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
>     --destination-url "https://ghaebuildXXXXXXXXXXXX.XXXXXXXX.net/" \
>     --repo-name actions/setup-node
fetching * refs for actions/setup-node ...
Getting an impersonation token for `actions-admin`..
These are the scopes we have for the current token `repo, workflow`..
The current token doesn't have the `site_admin` scope. The impersonation request for GHES requres the `site_admin` permission to be able to impersonate. For GitHub AE it's not required.error obtaining the impersonation token: Failed to impersonate Actions admin user.: POST https://derwasp-03fbba5321ba8c306.qaboot.net/api/v3/admin/users/actions-admin/authorizations: 404 Not Found []

A successful request with the site_admin permission:

> actions-sync sync \
>     --cache-dir "/tmp/cache" \
>     --destination-token "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
>     --destination-url "https://derwasp-XXXXXXXXXXXX.XXXXXXXX.net/" \
>     --repo-name actions/setup-node
fetching * refs for actions/setup-node ...
Getting an impersonation token for `actions-admin`..
These are the scopes we have for the current token `repo, site_admin, workflow`..
Got the impersonation token for `actions-admin`..
syncing `actions/setup-node`
successfully synced `actions/setup-node`

GHAE

Note that there's no site_admin in the scopes, yet it works just fine.

> actions-sync sync \
>     --cache-dir "/tmp/cache" \
>     --destination-token "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
>     --destination-url "https://ghaebuildXXXXXXXXXXXX.XXXXXXXX.net/" \
>     --repo-name actions/setup-node
fetching * refs for actions/setup-node ...
Getting an impersonation token for `actions-admin`..
These are the scopes we have for the current token `repo, workflow`..
Running against GitHub AE, changing the repository scope to 'repo'..
Got the impersonation token for `actions-admin`..
syncing `actions/setup-node`
successfully synced `actions/setup-node`

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @derwasp , @chrisgavin for responses.

I think for GHAE as well, not sure why the behaviour is different, but i agree with @chrisgavin that GHAE instance also should have site_admin permissions
Otherwise, normal customers with less priviliges also be able to sync via impersonation ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ajaykn
Who can we verify this behavior with? Unless I am missing something, right now the GHAE instances clearly are not expecting the site_admin permission for the impersonation requests as I verified with the logs above.
I don't mind just asking for the site_admin permission in both cases, but I wanted to minimize the required permissions unless really necessary.

Copy link
Contributor

@ajaykn ajaykn Sep 21, 2022

Choose a reason for hiding this comment

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

@derwasp It seems, we are checking for site_admin even in codeql-sync tool.
https://github.com/github/codeql-action-sync-tool/blob/main/internal/push/push.go#L107

Can we do the same regardless of how it works in ghae.
Making sure site_admin permissions needed for impersonation for ghae as well.

And we can start conversation in slack channels like actions-on-ghae or ghae-engineering to see, why the impersonation logic working regardless of site_admin.

@derwasp derwasp requested review from chrisgavin and jidicula and removed request for chrisgavin and jidicula September 8, 2022 09:07
@derwasp
Copy link
Contributor Author

derwasp commented Sep 8, 2022

for whatever reason clicking "request review" from one person removed the review request from the other person 🤔

jidicula
jidicula previously approved these changes Sep 8, 2022
Copy link

@jidicula jidicula left a comment

Choose a reason for hiding this comment

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

Tiniest of nits but otherwise LGTM

@derwasp derwasp requested review from chrisgavin and jidicula and removed request for chrisgavin and jidicula September 9, 2022 09:17
@derwasp derwasp requested review from JacobMillward and kgrzebien and removed request for JacobMillward and chrisgavin September 13, 2022 12:58
JacobMillward
JacobMillward previously approved these changes Sep 13, 2022
@derwasp derwasp requested review from chrisgavin and removed request for kgrzebien September 14, 2022 11:06
* Require the impersonation to be explicit with the --actions-admin-user flag
* Reword the log messages to look the same way the existing messages are
JacobMillward
JacobMillward previously approved these changes Sep 20, 2022
kgrzebien
kgrzebien previously approved these changes Sep 20, 2022
Copy link

@kgrzebien kgrzebien left a comment

Choose a reason for hiding this comment

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

LGTM.
Nice review conversation, covering multiple topics 🚀

jidicula
jidicula previously approved these changes Sep 20, 2022
Copy link

@jidicula jidicula left a comment

Choose a reason for hiding this comment

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

LGTM 🎸

Also, fixed casing in the text literals
@derwasp derwasp dismissed stale reviews from jidicula, kgrzebien, and JacobMillward via 37946df September 21, 2022 11:59
* add a note to the readme
* add a note to the autogenerated cli help
Copy link
Contributor

@ajaykn ajaykn left a comment

Choose a reason for hiding this comment

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

Looks good to me.
Thanks @derwasp for adding this enhancement.

I was wondering if we can add tests here
https://github.com/actions/actions-sync/blob/main/script/test-build#L129
Not sure if the current code design allows.
We can track separately.

@ajaykn ajaykn merged commit 3a24d8e into actions:main Sep 21, 2022
@derwasp derwasp deleted the derwasp/add-actions-admin-user-switch branch September 21, 2022 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants