mik-laj commented on a change in pull request #9879: URL: https://github.com/apache/airflow/pull/9879#discussion_r456886345
########## File path: airflow/providers/google/cloud/hooks/compute_engine_ssh.py ########## @@ -0,0 +1,211 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import subprocess +import time +import uuid +from typing import Any, List, Optional, Union + +import requests +from googleapiclient.discovery import build + +from airflow.providers.google.common.hooks.base_google import GoogleBaseHook + +SERVICE_ACCOUNT_METADATA_URL = ( + 'http://metadata.google.internal/computeMetadata/v1/instance/' + 'service-accounts/default/email') +HEADERS = {'Metadata-Flavor': 'Google'} + + +class ComputeEngineSSHHook(GoogleBaseHook): + """ + Hook to connect to a remote instance in compute engine + + :param api_version: + :param gcp_conn_id: + :param delegate_to: + :param oslogin: + :param account: + """ + + def __init__(self, + api_version: str = "v1", + gcp_conn_id: str = "google_cloud_default", + delegate_to: Optional[str] = None, + oslogin=None, + account=None + ) -> None: + super().__init__(gcp_conn_id, delegate_to) + self.api_version = api_version + self.oslogin = oslogin + self.account = account + + def get_oslogin(self) -> Any: + """ + Connects to Oslogin API + """ + if self.oslogin is None: + http_authorized = self._authorize() + self.oslogin = build("oslogin", self.api_version, + http=http_authorized, cache_discovery=False) + return self.oslogin + + def get_service_account(self) -> str: + """ + Get the service account id + :return: + """ + if self.account is None: + self.account = requests.get( + SERVICE_ACCOUNT_METADATA_URL, headers=HEADERS).text + if not self.account.startswith('users/'): Review comment: This will only work on GCE instances. Can you try to fetch email from credentials - `hook._get_credentials.service_account_email`? This still won't be perfect as there are edge cases that may not work properly, but we can deal with it later. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
