Skip to content

Commit c8f1fff

Browse files
committed
linting
1 parent d446d91 commit c8f1fff

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

metadata-ingestion/src/datahub/ingestion/source/aws/aws_common.py

+29-25
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from mypy_boto3_sagemaker import SageMakerClient
2828
from mypy_boto3_sts import STSClient
2929

30+
3031
class AwsEnvironment(Enum):
3132
EC2 = "EC2"
3233
ECS = "ECS"
@@ -37,6 +38,7 @@ class AwsEnvironment(Enum):
3738
CLOUD_FORMATION = "CLOUD_FORMATION"
3839
UNKNOWN = "UNKNOWN"
3940

41+
4042
class AwsAssumeRoleConfig(PermissiveConfigModel):
4143
# Using the PermissiveConfigModel to allow the user to pass additional arguments.
4244

@@ -55,7 +57,7 @@ def get_instance_metadata_token() -> Optional[str]:
5557
response = requests.put(
5658
"http://169.254.169.254/latest/api/token",
5759
headers={"X-aws-ec2-metadata-token-ttl-seconds": "21600"},
58-
timeout=1
60+
timeout=1,
5961
)
6062
if response.status_code == 200:
6163
return response.text
@@ -74,7 +76,7 @@ def is_running_on_ec2() -> bool:
7476
response = requests.get(
7577
"http://169.254.169.254/latest/meta-data/instance-id",
7678
headers={"X-aws-ec2-metadata-token": token},
77-
timeout=1
79+
timeout=1,
7880
)
7981
return response.status_code == 200
8082
except requests.exceptions.RequestException:
@@ -87,25 +89,27 @@ def detect_aws_environment() -> AwsEnvironment:
8789
Order matters as some environments may have multiple indicators.
8890
"""
8991
# Check Lambda first as it's most specific
90-
if os.getenv('AWS_LAMBDA_FUNCTION_NAME'):
91-
if os.getenv('AWS_EXECUTION_ENV', '').startswith('CloudFormation'):
92+
if os.getenv("AWS_LAMBDA_FUNCTION_NAME"):
93+
if os.getenv("AWS_EXECUTION_ENV", "").startswith("CloudFormation"):
9294
return AwsEnvironment.CLOUD_FORMATION
9395
return AwsEnvironment.LAMBDA
9496

9597
# Check EKS (IRSA)
96-
if os.getenv('AWS_WEB_IDENTITY_TOKEN_FILE') and os.getenv('AWS_ROLE_ARN'):
98+
if os.getenv("AWS_WEB_IDENTITY_TOKEN_FILE") and os.getenv("AWS_ROLE_ARN"):
9799
return AwsEnvironment.EKS
98100

99101
# Check App Runner
100-
if os.getenv('AWS_APP_RUNNER_SERVICE_ID'):
102+
if os.getenv("AWS_APP_RUNNER_SERVICE_ID"):
101103
return AwsEnvironment.APP_RUNNER
102104

103105
# Check ECS
104-
if os.getenv('ECS_CONTAINER_METADATA_URI_V4') or os.getenv('ECS_CONTAINER_METADATA_URI'):
106+
if os.getenv("ECS_CONTAINER_METADATA_URI_V4") or os.getenv(
107+
"ECS_CONTAINER_METADATA_URI"
108+
):
105109
return AwsEnvironment.ECS
106110

107111
# Check Elastic Beanstalk
108-
if os.getenv('ELASTIC_BEANSTALK_ENVIRONMENT_NAME'):
112+
if os.getenv("ELASTIC_BEANSTALK_ENVIRONMENT_NAME"):
109113
return AwsEnvironment.BEANSTALK
110114

111115
if is_running_on_ec2():
@@ -124,14 +128,14 @@ def get_instance_role_arn() -> Optional[str]:
124128
response = requests.get(
125129
"http://169.254.169.254/latest/meta-data/iam/security-credentials/",
126130
headers={"X-aws-ec2-metadata-token": token},
127-
timeout=1
131+
timeout=1,
128132
)
129133
if response.status_code == 200:
130134
role_name = response.text.strip()
131135
if role_name:
132-
sts = boto3.client('sts')
136+
sts = boto3.client("sts")
133137
identity = sts.get_caller_identity()
134-
return identity['Arn']
138+
return identity.get("Arn")
135139
except Exception as e:
136140
logger.debug(f"Failed to get instance role ARN: {e}")
137141
return None
@@ -140,15 +144,15 @@ def get_instance_role_arn() -> Optional[str]:
140144
def get_lambda_role_arn() -> Optional[str]:
141145
"""Get the Lambda function's role ARN"""
142146
try:
143-
function_name = os.getenv('AWS_LAMBDA_FUNCTION_NAME')
147+
function_name = os.getenv("AWS_LAMBDA_FUNCTION_NAME")
144148
if not function_name:
145149
return None
146150

147-
lambda_client = boto3.client('lambda')
151+
lambda_client = boto3.client("lambda")
148152
function_config = lambda_client.get_function_configuration(
149153
FunctionName=function_name
150154
)
151-
return function_config.get('Role')
155+
return function_config.get("Role")
152156
except Exception as e:
153157
logger.debug(f"Failed to get Lambda role ARN: {e}")
154158
return None
@@ -166,26 +170,28 @@ def get_current_identity() -> Tuple[Optional[str], Optional[str]]:
166170
return role_arn, "lambda.amazonaws.com"
167171

168172
elif env == AwsEnvironment.EKS:
169-
role_arn = os.getenv('AWS_ROLE_ARN')
173+
role_arn = os.getenv("AWS_ROLE_ARN")
170174
return role_arn, "eks.amazonaws.com"
171175

172176
elif env == AwsEnvironment.APP_RUNNER:
173177
try:
174-
sts = boto3.client('sts')
178+
sts = boto3.client("sts")
175179
identity = sts.get_caller_identity()
176-
return identity['Arn'], "apprunner.amazonaws.com"
180+
return identity.get("Arn"), "apprunner.amazonaws.com"
177181
except Exception as e:
178182
logger.debug(f"Failed to get App Runner role: {e}")
179183

180184
elif env == AwsEnvironment.ECS:
181185
try:
182-
metadata_uri = os.getenv('ECS_CONTAINER_METADATA_URI_V4') or os.getenv('ECS_CONTAINER_METADATA_URI')
186+
metadata_uri = os.getenv("ECS_CONTAINER_METADATA_URI_V4") or os.getenv(
187+
"ECS_CONTAINER_METADATA_URI"
188+
)
183189
if metadata_uri:
184190
response = requests.get(f"{metadata_uri}/task", timeout=1)
185191
if response.status_code == 200:
186192
task_metadata = response.json()
187-
if 'TaskARN' in task_metadata:
188-
return task_metadata.get('TaskARN'), "ecs.amazonaws.com"
193+
if "TaskARN" in task_metadata:
194+
return task_metadata.get("TaskARN"), "ecs.amazonaws.com"
189195
except Exception as e:
190196
logger.debug(f"Failed to get ECS task role: {e}")
191197

@@ -320,8 +326,7 @@ def get_session(self) -> Session:
320326
elif self.aws_profile:
321327
# Named profile is second priority
322328
session = Session(
323-
region_name=self.aws_region,
324-
profile_name=self.aws_profile
329+
region_name=self.aws_region, profile_name=self.aws_profile
325330
)
326331
else:
327332
# Use boto3's credential autodetection
@@ -334,9 +339,8 @@ def get_session(self) -> Session:
334339
# Only assume role if:
335340
# 1. We're not in a known AWS environment with a role, or
336341
# 2. We need to assume a different role than our current one
337-
should_assume_role = (
338-
current_role_arn is None or
339-
any(role.RoleArn != current_role_arn for role in target_roles)
342+
should_assume_role = current_role_arn is None or any(
343+
role.RoleArn != current_role_arn for role in target_roles
340344
)
341345

342346
if should_assume_role:

0 commit comments

Comments
 (0)