2021-12-25 16:29:10 -08:00
|
|
|
# Copyright 2021 Collate
|
|
|
|
# Licensed 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.
|
2022-10-11 17:15:02 +02:00
|
|
|
"""
|
|
|
|
Module containing AWS Client
|
|
|
|
"""
|
2022-04-22 11:25:05 +02:00
|
|
|
from typing import Any
|
2021-12-25 16:29:10 -08:00
|
|
|
|
2022-08-19 16:15:40 +02:00
|
|
|
import boto3
|
2021-12-25 16:29:10 -08:00
|
|
|
from boto3 import Session
|
|
|
|
|
2022-08-03 09:55:50 +02:00
|
|
|
from metadata.clients.connection_clients import (
|
2022-06-27 15:08:41 +05:30
|
|
|
DynamoClient,
|
|
|
|
GlueDBClient,
|
|
|
|
GluePipelineClient,
|
2022-11-02 16:12:45 +05:30
|
|
|
KinesisClient,
|
2022-11-08 06:24:49 -08:00
|
|
|
QuickSightClient,
|
2022-11-03 22:49:20 +05:30
|
|
|
SageMakerClient,
|
2022-06-27 15:08:41 +05:30
|
|
|
)
|
2022-08-19 16:15:40 +02:00
|
|
|
from metadata.utils.logger import utils_logger
|
|
|
|
|
|
|
|
logger = utils_logger()
|
2022-04-22 11:30:59 +05:30
|
|
|
|
|
|
|
|
2021-12-25 16:29:10 -08:00
|
|
|
class AWSClient:
|
|
|
|
"""
|
2022-04-22 11:25:05 +02:00
|
|
|
AWSClient creates a boto3 Session client based on AWSCredentials.
|
2021-12-25 16:29:10 -08:00
|
|
|
"""
|
|
|
|
|
2022-11-11 09:59:15 +01:00
|
|
|
def __init__(self, config: "AWSCredentials"):
|
|
|
|
# local import to avoid the creation of circular dependencies with CustomSecretStr
|
|
|
|
from metadata.generated.schema.security.credentials.awsCredentials import ( # pylint: disable=import-outside-toplevel
|
|
|
|
AWSCredentials,
|
|
|
|
)
|
2021-12-25 16:29:10 -08:00
|
|
|
|
2022-11-14 13:48:50 +01:00
|
|
|
self.config = (
|
|
|
|
config
|
|
|
|
if isinstance(config, AWSCredentials)
|
|
|
|
else (AWSCredentials.parse_obj(config) if config else config)
|
|
|
|
)
|
2021-12-25 16:29:10 -08:00
|
|
|
|
|
|
|
def _get_session(self) -> Session:
|
|
|
|
if (
|
2022-04-07 13:45:47 +05:30
|
|
|
self.config.awsAccessKeyId
|
|
|
|
and self.config.awsSecretAccessKey
|
|
|
|
and self.config.awsSessionToken
|
2021-12-25 16:29:10 -08:00
|
|
|
):
|
|
|
|
return Session(
|
2022-04-07 13:45:47 +05:30
|
|
|
aws_access_key_id=self.config.awsAccessKeyId,
|
|
|
|
aws_secret_access_key=self.config.awsSecretAccessKey.get_secret_value(),
|
|
|
|
aws_session_token=self.config.awsSessionToken,
|
|
|
|
region_name=self.config.awsRegion,
|
2021-12-25 16:29:10 -08:00
|
|
|
)
|
2022-04-07 13:45:47 +05:30
|
|
|
if self.config.awsAccessKeyId and self.config.awsSecretAccessKey:
|
2021-12-25 16:29:10 -08:00
|
|
|
return Session(
|
2022-04-07 13:45:47 +05:30
|
|
|
aws_access_key_id=self.config.awsAccessKeyId,
|
|
|
|
aws_secret_access_key=self.config.awsSecretAccessKey.get_secret_value(),
|
|
|
|
region_name=self.config.awsRegion,
|
2021-12-25 16:29:10 -08:00
|
|
|
)
|
2022-04-07 13:45:47 +05:30
|
|
|
if self.config.awsRegion:
|
|
|
|
return Session(region_name=self.config.awsRegion)
|
2021-12-25 16:29:10 -08:00
|
|
|
return Session()
|
|
|
|
|
|
|
|
def get_client(self, service_name: str) -> Any:
|
2022-08-19 16:15:40 +02:00
|
|
|
# initialize the client depending on the AWSCredentials passed
|
|
|
|
if self.config is not None:
|
|
|
|
logger.info(f"Getting AWS client for service [{service_name}]")
|
|
|
|
session = self._get_session()
|
|
|
|
return session.client(service_name=service_name)
|
2022-10-11 17:15:02 +02:00
|
|
|
|
|
|
|
logger.info(f"Getting AWS default client for service [{service_name}]")
|
|
|
|
# initialized with the credentials loaded from running machine
|
|
|
|
return boto3.client(service_name=service_name)
|
2022-02-08 00:22:11 +05:30
|
|
|
|
|
|
|
def get_resource(self, service_name: str) -> Any:
|
|
|
|
session = self._get_session()
|
|
|
|
return session.resource(service_name=service_name)
|
2022-04-22 11:30:59 +05:30
|
|
|
|
2022-11-03 22:49:20 +05:30
|
|
|
def get_dynamo_client(self) -> DynamoClient:
|
2022-04-22 11:30:59 +05:30
|
|
|
return DynamoClient(self.get_resource("dynamodb"))
|
|
|
|
|
2022-06-27 15:08:41 +05:30
|
|
|
def get_glue_db_client(self) -> GlueDBClient:
|
|
|
|
return GlueDBClient(self.get_client("glue"))
|
|
|
|
|
|
|
|
def get_glue_pipeline_client(self) -> GluePipelineClient:
|
|
|
|
return GluePipelineClient(self.get_client("glue"))
|
2022-11-02 16:12:45 +05:30
|
|
|
|
2022-11-03 22:49:20 +05:30
|
|
|
def get_sagemaker_client(self) -> SageMakerClient:
|
|
|
|
return SageMakerClient(self.get_client("sagemaker"))
|
|
|
|
|
2022-11-02 16:12:45 +05:30
|
|
|
def get_kinesis_client(self) -> KinesisClient:
|
|
|
|
return KinesisClient(self.get_client("kinesis"))
|
2022-11-08 06:24:49 -08:00
|
|
|
|
|
|
|
def get_quicksight_client(self) -> QuickSightClient:
|
|
|
|
return QuickSightClient(self.get_client("quicksight"))
|