-
Notifications
You must be signed in to change notification settings - Fork 434
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
How to use a service account for CI deployments #225
Comments
Note I tried the directions at https://developers.google.com/identity/protocols/OAuth2ServiceAccount and using the resultant JSON key in place of the |
This seems to be related to #28 - but although it doesn't require a browser, it still requires interaction on the command line. |
Pinging @grant on this one (as a recent committer to https://github.com/google/clasp/blob/master/src/auth.ts). I looked at the oauth2 client used here, and it seems there is a way to set creds in an env var: https://www.npmjs.com/package/google-auth-library#loading-credentials-from-environment-variables It even mentions the deployment use case. Also - over in gapps, looks like there was a PR for such a request by @gunar https://github.com/danthareja/node-google-apps-script/pull/46/files |
This request is for using a service account rather than a user account. I think using |
@grant thanks for looking into this issue. Any kind of auto refresh would then need to persist - in the context of CI then, one would have to check if the local
|
To expand, the code snippet from google-auth-library is:
And then:
So maybe either just implicitly be able to read JWT tokens of this sort in a |
Hey @marcosscriven does this PR #223 solve your issue? It removes This changes However, it still requires you to select the Gmail account you're authorizing the app for. I can take a look at that sample code there and see if we can get it to work. Looks like
may be where it authorizes without opening up in the browser. I hope this helps! Also feel free to open your own PR, or ask more questions. |
@campionfellin - it certainly looks close. It doesn't have to be from env vars - I'm chiefly thinking about Bitbucket Cloud Pipelines https://confluence.atlassian.com/bitbucket/environment-variables-794502608.html One could easily generate a JSON file at build time, populating the secrets from the env vars. It would be handy to avoid that step though, as show in the snippet. |
Also @campionfellin - I don't think the snippet you highlighted is anything to do with the auth - it's just an example of going on to use any of the APIs (dns in this instance). The bit that enables it is simply |
Hey @marcosscriven it looks like rather than using However, I don't think this would solve the issue of getting the |
Hey @marcosscriven , so it does look like using what's in the sample will work. |
Question for @grant : is this what we want to do by default, or add a flag for it? I am afraid of taking away the user's ability to see what scopes they are authorizing. |
@campionfellin - The scopes would have be chosen by the user while generating the service account key, so I think just working by default would be fine (so long as it was documented how to use this rather than a token). |
My question is more for users who don't generate service accounts. What about a flag like |
Another flag sounds OK. I'm getting a bit confused by all the discussion here, but it seems like this is just a FR for adding another flag like |
@grant - I'm not clear of the purpose of 'logging in' with a service account? Logging in at the moment is just about getting a token into To be clear, I would expect to be able to provide service account credentials (created according to https://developers.google.com/identity/protocols/OAuth2ServiceAccount), in either a file or env var, and for all remaining API actions to simply use I think it should work fine by simply inferring behaviour from the contents of |
Hey @marcosscriven and @grant I've done a bit of investigation today, so here's a follow up, please correct me on any things I am misunderstanding:
It goes on to explain that this is "2-legged OAuth", as compared to "3-legged OAuth" which can act on behalf of the user but needs user permission (like the pop-up that we currently have). My understanding of what you want is essentially for your Bitbucket pipeline to interact with Google Services, without you having to open a page to login. I don't think that with a Service Account or JWT this will be possible, for most Anyway, here's how I tested it: In GCP, I made a service account, with full "owner" access to the entire project. I downloaded those credentials (in the same format as you have above and same as the documentation) and used them to authenticate my API calls, like here: Line 41 in d807a6c
But instead of the oauth2client I used the one I created as a JWT Client. Now the first command I tried was
Since most of the However, if all you really need is for your pipeline to work, there is a fairly simple solution, which I'll explain in my next comment. |
So this is how I would solve your CI problem specifically, though we use Travis instead of BitBucket, it should be simple to translate. On your local machine with some real user account (yours), use If you find that BitBucket doesn't allow that with files, but rather environmental variables, it would be a pretty simple change here: Line 61 in d807a6c
To either read from a file or from ENV. However, Service Accounts and JWT will still not work. Let me know if this at least unblocks you, or if you have further questions or can help me understand your situation better. |
@campionfellin Thanks for looking into this - as it happens, I'm not looking for impersonation in my case. There's two flavours of that in OAuth - there's the 3LO (Three-legged Oauth), which allows impersonation with a pre-shared key, but still requires user interaction for the user to accept. There's a much lesser known 2LOi (Two-legged Oauth with Impersonation) - but I don't see that mentioned anywhere in Google's docs. Anyway - for me, I do have an account I setup just for services, and I give that account the rights to run scripts and access drives that way. All I need here is for the OAuth client clasp uses to be setup with the service account json key (as in your penultimate post), and that'll work for me. Regardless though - the method you specify for working around it (if one needed to) is what I considered to start with, but noted the token there has about a week's validity. At which point clasp can refresh the token with the URL it has in So this can't be one way in CI - it too would have to store (securely) any changes to the token that clasp made right? |
@campionfellin - I just wasted some hours on using the service account (which should work). The crucial step (even for a service account with 'Domain-wide delegation'), was that I still had to go to the script project and 'share' it with the service user email (of the form [email protected]). I'm pretty sure that last step is not meant to be necessary. Maybe related to https://issuetracker.google.com/issues/36763096? @grant - As you're a member of the Google team on Github, is there any chance you can investigate this please? There's a lot of confusion around service accounts and the App Scripts API. |
To clarify, using the Python Google OAuth 2 client, this works - so long as I've 'shared' the script with the service account email:
EDIT - So while this works, trying:
Suddenly gives me a 403:
Which is very peculiar given the the API is clearly being used during get operation... |
Trying to use delegation-wide service account the right way (E.g. without the 'sharing' hack I mentioned), even just reading the project fails:
Fails with:
Despite ensuring those API scopes have been authorized for that client ID as per https://developers.google.com/api-client-library/python/auth/service-accounts. |
Note I asked about this on Stack Overflow too https://stackoverflow.com/questions/51049548/how-can-i-publish-a-google-app-script-using-a-domain-wide-delegation-service-acc |
Hey @marcosscriven, thanks for all the investigation. Please upvote the linked bug and this issue so I can ask the Apps Script team to prioritize this. |
@grant - I don't see any voting options there, but I've commented on it. It says it's 'blocked by' https://issuetracker.google.com/issues/26400743, but I don't have view permissions on that. |
@marcosscriven It looks like this issue is being triaged by the Apps Script team. I've asked the team for an update on the issue. Unfortunately, it's a lot easier to change features in |
Any update on this? Is it possible to authenticate clasp using a service account credential? |
for reference, this is how it works in gcloud auth activate-service-account --key-file service-account-credentials.json |
Out of curiosity, if I were to copy the |
What I'm basically asking is, once a user does a |
I pinged again. You can read more about the token here: For example, |
What is the summary of the workaround or fix for this? |
|
Affected too. Upvoted the bug. |
Say I store a token and refresh token as part of a CI pipeline. This works for a while, but eventually the refresh token is used to create a new token. But the CI pipeline is ephemeral. This new refresh token is not persisted to the next CI run. So what happens when the CI process runs again and tries to use the original token/request token? |
Hi guys, I want to use Github Actions to do
how do I make it login without a need to click on URL? |
Hi all, I am also trying to set this up, but then by using CloudBuild. After running |
@sativ01
where |
@grant Any news on service account support within App Script API (and later in |
No news here |
+1 |
I also am waiting to have a solution for CI |
Any update here?
Following this looks like an endless journey.. |
FYI I've developed a CI/CD process for Google Apps Script using GitHub Actions. See my comment here: #707 (comment) |
@ericanastas |
Did you look at the script? It's run by a cron trigger every week and stores.classprc.json if it is updated. |
is there a proper way to setup auth for service account yet? one more team impacted here :/ |
Admin token required? No thanks, lol |
Which "admin" token are you referring to? |
Hello , everybody, i succed to make a clasp push from my github worflow with the help of namaggarwal/clasp-token-action . Does someone succeed to do that ? |
@fletort i think the answer is that you need to generate credentials on your local computer (the global .clasprc.json generated in your /users/yourUser directory when you do a
I attach to this response also this other comment in a related post where i explain the workaround that worked for what i was trying to do in this link |
Running
clasp login
sets up a.clasprc
file with a token that seems to last about a week.Is there any way to get some kind of authentication working that could work in a headless setup like CI (E.g. GitHub Travis or Bitbucket Pipelines) please?
I looked at https://script.google.com/home/usersettings which has a switch for the API, but nothing about service tokens.
Note from @grant, please upvote this bug!
https://issuetracker.google.com/issues/36763096
The text was updated successfully, but these errors were encountered: