Skip to content
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

feat: add support for GCP postgres instance authentication using IAM with service account keyfile/keyfile json #3162

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/integrations/engines/gcp-postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ pip install "sqlmesh[gcppostgres]"
| `user` | The username (posgres or IAM) to use for authentication | string | Y |
| `password` | The password to use for authentication. Required when connecting as a Postgres user | string | N |
| `enable_iam_auth` | Enables IAM authentication. Required when connecting as an IAM user | boolean | N |
| `keyfile` | Path to the keyfile to be used with enable_iam_auth instead of ADC | string | N |
| `keyfile_json` | Keyfile information provided inline (not recommended) | dict | N |
| `db` | The name of the database instance to connect to | string | Y |
20 changes: 18 additions & 2 deletions sqlmesh/core/config/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,8 @@ class GCPPostgresConnectionConfig(ConnectionConfig):
password: The postgres user's password. Only needed when the user is a postgres user.
enable_iam_auth: Set to True when user is an IAM user.
db: Name of the db to connect to.
keyfile: string path to json service account credentials file
keyfile_json: dict service account credentials info
pre_ping: Whether or not to pre-ping the connection before starting a new transaction to ensure it is still alive.
"""

Expand All @@ -856,8 +858,11 @@ class GCPPostgresConnectionConfig(ConnectionConfig):
password: t.Optional[str] = None
enable_iam_auth: t.Optional[bool] = None
db: str
# Keyfile Auth
keyfile: t.Optional[str] = None
keyfile_json: t.Optional[t.Dict[str, t.Any]] = None
timeout: t.Optional[int] = None

scopes: t.Tuple[str, ...] = ("https://www.googleapis.com/auth/sqlservice.admin",)
driver: str = "pg8000"
type_: Literal["gcp_postgres"] = Field(alias="type", default="gcp_postgres")
concurrent_tasks: int = 4
Expand Down Expand Up @@ -904,8 +909,19 @@ def _engine_adapter(self) -> t.Type[EngineAdapter]:
@property
def _connection_factory(self) -> t.Callable:
from google.cloud.sql.connector import Connector
from google.oauth2 import service_account

creds = None
if self.keyfile:
creds = service_account.Credentials.from_service_account_file(
self.keyfile, scopes=self.scopes
)
elif self.keyfile_json:
creds = service_account.Credentials.from_service_account_info(
self.keyfile_json, scopes=self.scopes
)

return Connector().connect
return Connector(credentials=creds).connect


class RedshiftConnectionConfig(ConnectionConfig):
Expand Down