JWT로 인증

BigQuery API는 JSON 웹 토큰(JWT)을 수락하여 요청을 인증합니다.

애플리케이션 기본 사용자 인증 정보(ADC)를 사용하여 BigQuery에 인증을 사용하는 것이 좋습니다. ADC를 사용할 수 없고 인증에 서비스 계정을 사용하는 경우 대신 서명된 JWT를 사용할 수 있습니다. JWT를 사용하면 Google의 승인 서버에 대한 네트워크 요청 없이 API를 호출할 수 있습니다.

JWT를 사용하여 다음과 같은 방법으로 인증할 수 있습니다.

범위 및 대상

가능하면 서비스 계정과 함께 범위를 사용하세요. 불가능하다면 대상 클레임을 사용할 수 있습니다. BigQuery API의 경우 대상 값을 https://bigquery.googleapis.com/으로 설정합니다.

클라이언트 라이브러리로 JWT 만들기

Google Cloud 콘솔에서 또는 gcloud CLI를 사용하여 생성된 서비스 계정 키의 경우 JWT 서명을 제공하는 클라이언트 라이브러리를 사용합니다. 다음 목록은 많이 사용되는 프로그래밍 언어에 적합한 몇 가지 옵션을 제공합니다.

자바 예시

다음 예시에서는 자바용 BigQuery 클라이언트 라이브러리를 사용하여 JWT를 만들고 서명합니다. BigQuery API의 기본 범위는 클라이언트 라이브러리에서 https://www.googleapis.com/auth/bigquery로 설정됩니다.

import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryOptions; import com.google.common.collect.ImmutableList;  import java.io.FileInputStream; import java.io.IOException; import java.net.URI;  public class Example {     public static void main(String... args) throws IOException {         String projectId = "myproject";         // Load JSON file that contains service account keys and create ServiceAccountCredentials object.         String credentialsPath = "/path/to/key.json";         ServiceAccountCredentials credentials = null;         try (FileInputStream is = new FileInputStream(credentialsPath)) {           credentials =  ServiceAccountCredentials.fromStream(is);           // The default scope for BigQuery is used.            // Alternatively, use `.setScopes()` to set custom scopes.           credentials = credentials.toBuilder()               .setUseJwtAccessWithScope(true)               .build();         }         // Instantiate BigQuery client with the credentials object.         BigQuery bigquery =                 BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();         // Use the client to list BigQuery datasets.         System.out.println("Datasets:");         bigquery             .listDatasets(projectId)             .iterateAll()             .forEach(dataset -> System.out.printf("%s%n", dataset.getDatasetId().getDataset()));     } } 

REST 또는 gcloud CLI로 JWT 만들기

시스템 관리 서비스 계정의 경우 JWT를 수동으로 조합한 다음 REST 메서드 projects.serviceAccounts.signJwt 또는 Google Cloud CLI 명령어 gcloud beta iam service-accounts sign-jwt를 사용하여 JWT에 서명합니다. 이러한 방법 중 하나를 사용하려면 서비스 계정 토큰 생성자 Identity and Access Management 역할의 구성원이어야 합니다.

gcloud CLI 예시

다음 예시에서는 JWT를 조합한 후 gcloud beta iam service-accounts sign-jwt 명령어를 사용하여 서명하는 bash 스크립트를 보여줍니다.

#!/bin/bash  SA_EMAIL_ADDRESS="[email protected]"  TMP_DIR=$(mktemp -d /tmp/sa_signed_jwt.XXXXX) trap "rm -rf ${TMP_DIR}" EXIT JWT_FILE="${TMP_DIR}/jwt-claim-set.json" SIGNED_JWT_FILE="${TMP_DIR}/output.jwt"  IAT=$(date '+%s') EXP=$((IAT+3600))  cat <<EOF > $JWT_FILE {   "aud": "https://bigquery.googleapis.com/",   "iat": $IAT,   "exp": $EXP,   "iss": "$SA_EMAIL_ADDRESS",   "sub": "$SA_EMAIL_ADDRESS" } EOF  gcloud beta iam service-accounts sign-jwt --iam-account $SA_EMAIL_ADDRESS $JWT_FILE $SIGNED_JWT_FILE  echo "Datasets:" curl -L -H "Authorization: Bearer $(cat $SIGNED_JWT_FILE)" \ -X GET \ "https://bigquery.googleapis.com/bigquery/v2/projects/myproject/datasets?alt=json" 

다음 단계