Source code for airflow.providers.ydb.utils.credentials
# 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.from__future__importannotationsimportloggingfromtypingimportTYPE_CHECKING,Anyimportydbimportydb.iam.authasauthifTYPE_CHECKING:fromairflow.models.connectionimportConnection
[docs]defget_credentials_from_connection(endpoint:str,database:str,connection:Connection,connection_extra:dict[str,Any]|None=None)->Any:""" Return YDB credentials object for YDB SDK based on connection settings. Credentials will be used with this priority: * login * token * service_account_json_path * service_account_json * use_vm_metadata * anonymous :param endpoint: address of YDB cluster, e.g. ``grpcs://my-server.com:2135`` :param database: YDB database name, e.g. ``/local`` :param connection: connection object :param connection_extra: connection extra settings :return: YDB credentials object """ifconnection.login:driver_config=ydb.DriverConfig(endpoint=endpoint,database=database,)log.info("using login as credentials")returnydb.StaticCredentials(driver_config,user=connection.login,password=connection.password)connection_extra=connection_extraor{}token=connection_extra.get("token")iftoken:log.info("using token as credentials")returnydb.AccessTokenCredentials(token)service_account_json_path=connection_extra.get("service_account_json_path")ifservice_account_json_path:log.info("using service_account_json_path as credentials")returnauth.ServiceAccountCredentials.from_file(service_account_json_path)service_account_json=connection_extra.get("service_account_json")ifservice_account_json:log.info("using service_account_json as credentials")returnauth.ServiceAccountCredentials.from_content(service_account_json)use_vm_metadata=connection_extra.get("use_vm_metadata",False)ifuse_vm_metadata:log.info("using vm metadata as credentials")returnauth.MetadataUrlCredentials()log.info("using anonymous access")returnydb.AnonymousCredentials()