简体   繁体   English

如何配置 Google BigQuery 命令行工具以使用服务帐户?

[英]How do I configure Google BigQuery command line tool to use a Service Account?

I've created a service account using the Google API Console and wish to use this service account with the Google BigQuery CLI (bq) tool.我使用 Google API 控制台创建了一个服务帐户,并希望将此服务帐户与Google BigQuery CLI (bq) 工具一起使用。

I've been using the command line tool to successfully access the BigQuery service using my valid OAuth2 credentials in ~/.bigquery.v2.token, however I can't seem to find any documentation on how to modify this file (or otherwise configure the tool) to use a service account instead.我一直在使用命令行工具使用我在 ~/.bigquery.v2.token 中的有效 OAuth2 凭据成功访问 BigQuery 服务,但是我似乎找不到任何关于如何修改此文件(或以其他方式配置)的文档该工具)改为使用服务帐户。

Here is my current.bigquery.v2.token file这是我的 current.bigquery.v2.token 文件

{
    "_module": "oauth2client.client",
    "_class": "OAuth2Credentials",
    "access_token": "--my-access-token--",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "invalid": false,
    "client_id": "--my-client-id--.apps.googleusercontent.com",
    "id_token": null,
    "client_secret": "--my-client-secret--",
    "token_expiry": "2012-11-06T15:57:12Z",
    "refresh_token": "--my-refresh-token--",
    "user_agent": "bq/2.0"
}

My other file: ~/.bigqueryrc generally looks like this:我的另一个文件:~/.bigqueryrc 通常看起来像这样:

project_id = --my-project-id--
credential_file = ~/.bigquery.v2.token

I've tried setting the credential_file paramater to the.p12 private key file for my service account but with no luck, it gives me back the following error我已经尝试将 credential_file 参数设置为我的服务帐户的 .p12 私钥文件,但没有成功,它返回以下错误

******************************************************************
** No OAuth2 credentials found, beginning authorization process **
******************************************************************

And asks me to go to a link in my browser to set up my OAuth2 credentials again.并要求我将 go 转到浏览器中的链接以再次设置我的 OAuth2 凭据。

The command line tools' initial configuration option "init":命令行工具的初始配置选项“init”:

bq help init

displays no helpful information about how to set up this tool to use a service account.没有显示有关如何设置此工具以使用服务帐户的有用信息。

I ended up finding some documentation on how to set this up 我最终找到了一些有关如何进行设置的文档

$ bq --help

....

--service_account: Use this service account email address for authorization. For example, 1234567890@developer.gserviceaccount.com.
(default: '')

--service_account_credential_file: File to be used as a credential store for service accounts. Must be set if using a service account.

--service_account_private_key_file: Filename that contains the service account private key. Required if --service_account is specified.
(default: '')

--service_account_private_key_password: Password for private key. This password must match the password you set on the key when you created it in the Google APIs Console. Defaults to the default Google APIs Console private key password.
(default: 'notasecret')

....

You can either set these specifically on each bq (bigquery commandline client) request, ie: 您可以在每个bq(bigquery命令行客户端)请求中专门设置这些设置,即:

$ bq --service_account --my-client-id--.apps.googleusercontent.com -- service_account_private_key_file ~/.bigquery.v2.p12 ... [command]

Or you can set up defaults in your ~/.bigqueryrc file like so 或者您可以在〜/ .bigqueryrc文件中设置默认值,如下所示

project_id = --my-project-id--
service_account = --my-client-id--@developer.gserviceaccount.com
service_account_credential_file = /home/james/.bigquery.v2.cred
service_account_private_key_file = /home/james/.bigquery.v2.p12

The service account can be found in the Google API Console, and you set up service_account_private_key_password when you created your service account (this defaults to "notasecret"). 您可以在Google API控制台中找到该服务帐户,并在创建服务帐户时设置了service_account_private_key_password(默认为“ notasecret”)。

note: file paths in .bigqueryrc had to be the full path, I was unable to use ~/.bigquery... 注意: .bigqueryrc中的文件路径必须是完整路径,我无法使用〜/ .bigquery ...

Some additional dependencies were required, you will need to install openssl via yum/apt-get 需要一些其他依赖项,您将需要通过yum / apt-get安装openssl

--yum--
$ yum install openssl-devel libssl-devel

--or apt-get--
$ apt-get install libssl-dev

and pyopenssl via easy install/pip 和pyopenssl通过易于安装/ pip

--easy install--
$ easy_install pyopenssl

--or pip--
$ pip install pyopenssl

The bq tool requires two configuration files, controlled by the --bigqueryrc and the --credential_file flag. bq工具需要两个配置文件,这些文件由--bigqueryrc和--credential_file标志控制。 If neither one is found, bq will attempt to automatically initialize during start up. 如果没有找到,bq将在启动过程中尝试自动初始化。

To avoid this for the --bigqueryrc file, you can place a ".bigqueryrc" file in the default location, or override it with --bigqueryrc to some writeable file path. 为避免--bigqueryrc文件出现此问题,您可以将“ .bigqueryrc”文件放在默认位置,或使用--bigqueryrc覆盖该文件到某些可写文件路径。

The bq authorization flags are now deprecated 现在不建议使用bq授权标志

bq documentation bq文档

For anyone else who comes along struggling to use bq with a service account... I had a seriously hard time getting this to work inside of a CI/CD pipeline using the Google Cloud SDK docker images on gitlab-ci.对于那些努力将 bq 与服务帐户一起使用的其他人...我很难使用 gitlab-ci 上的Google Cloud SDK docker 图像让它在 CI/CD 管道内部工作。 Turns out the missing bit for me was making sure to set the default project.原来我缺少的一点是确保设置默认项目。 On my laptop gcloud was happy inferring the default project from the service account, but for some reason the version within the docker image was defaulting to a public free project.在我的笔记本电脑上,gcloud 很乐意从服务帐户推断默认项目,但出于某种原因,docker 图像中的版本默认为公共免费项目。

- gcloud auth activate-service-account --key-file=${PATH_TO_SVC_ACCT_JSON};
- gcloud config set project ${GOOGLE_BIGQUERY_PROJECT}

after this I was able to use the bq utility as the service account.在此之后,我可以使用bq实用程序作为服务帐户。 I imagine setting the default project in the.bigqueryrc file does the trick too, which is why the OP didn't run into this issue.我想在 .bigqueryrc 文件中设置默认项目也可以解决问题,这就是 OP 没有遇到此问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM