-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change BigQueryCredentail to common function: GCPCredential
- Loading branch information
1 parent
0eeeb72
commit 6c43ecc
Showing
4 changed files
with
58 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
metadata-ingestion/src/datahub/ingestion/source/common/credentials.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import json | ||
import tempfile | ||
from typing import Any, Dict, Optional | ||
|
||
from pydantic import Field, root_validator | ||
|
||
from datahub.configuration import ConfigModel | ||
from datahub.configuration.validate_multiline_string import pydantic_multiline_string | ||
|
||
|
||
class GCPCredential(ConfigModel): | ||
project_id: Optional[str] = Field(description="Project id to set the credentials") | ||
private_key_id: str = Field(description="Private key id") | ||
private_key: str = Field( | ||
description="Private key in a form of '-----BEGIN PRIVATE KEY-----\\nprivate-key\\n-----END PRIVATE KEY-----\\n'" | ||
) | ||
client_email: str = Field(description="Client email") | ||
client_id: str = Field(description="Client Id") | ||
auth_uri: str = Field( | ||
default="https://accounts.google.com/o/oauth2/auth", | ||
description="Authentication uri", | ||
) | ||
token_uri: str = Field( | ||
default="https://oauth2.googleapis.com/token", description="Token uri" | ||
) | ||
auth_provider_x509_cert_url: str = Field( | ||
default="https://www.googleapis.com/oauth2/v1/certs", | ||
description="Auth provider x509 certificate url", | ||
) | ||
type: str = Field(default="service_account", description="Authentication type") | ||
client_x509_cert_url: Optional[str] = Field( | ||
default=None, | ||
description="If not set it will be default to https://www.googleapis.com/robot/v1/metadata/x509/client_email", | ||
) | ||
|
||
_fix_private_key_newlines = pydantic_multiline_string("private_key") | ||
|
||
@root_validator(skip_on_failure=True) | ||
def validate_config(cls, values: Dict[str, Any]) -> Dict[str, Any]: | ||
if values.get("client_x509_cert_url") is None: | ||
values["client_x509_cert_url"] = ( | ||
f"https://www.googleapis.com/robot/v1/metadata/x509/{values['client_email']}" | ||
) | ||
return values | ||
|
||
def create_credential_temp_file(self, project_id: Optional[str] = None) -> str: | ||
configs = self.dict() | ||
if project_id: | ||
configs["project_id"] = project_id | ||
with tempfile.NamedTemporaryFile(delete=False) as fp: | ||
cred_json = json.dumps(self.dict(), indent=4, separators=(",", ": ")) | ||
fp.write(cred_json.encode()) | ||
return fp.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters