Skip to content

Commit 588bfc1

Browse files
committed
fix codeql security error
1 parent b834ef0 commit 588bfc1

2 files changed

Lines changed: 23 additions & 33 deletions

File tree

examples/oidc_aws_e2e.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,9 @@
6565

6666
from __future__ import annotations
6767

68-
import hashlib
6968
import io
7069
import json
7170
import os
72-
import socket
73-
import ssl
7471
import sys
7572
import tarfile
7673
import time
@@ -105,6 +102,12 @@
105102
OIDC_PROVIDER_URL = "app.terraform.io"
106103
OIDC_AUDIENCE = "aws.workload.identity"
107104

105+
# Placeholder thumbprint for IAM's CreateOpenIDConnectProvider. AWS no
106+
# longer validates this field for providers backed by Amazon Trust Services
107+
# CAs (app.terraform.io is one) — any 40-char hex string is accepted.
108+
# See: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc_verify-thumbprint.html
109+
OIDC_PROVIDER_THUMBPRINT_PLACEHOLDER = "0" * 40
110+
108111
# Tag applied to the test instance so we can find/verify it later.
109112
INSTANCE_NAME_TAG = WORKSPACE_NAME
110113

@@ -176,20 +179,6 @@ def banner(s: str) -> None:
176179
print("=" * 72)
177180

178181

179-
def get_app_terraform_thumbprint() -> str:
180-
"""Fetch the leaf cert SHA1 thumbprint for app.terraform.io.
181-
182-
AWS no longer strictly enforces this thumbprint for IdPs backed by
183-
Amazon Trust Services CAs (since July 2023), but the API still
184-
requires the field. We pass the real leaf thumbprint for correctness.
185-
"""
186-
ctx = ssl.create_default_context()
187-
with socket.create_connection((OIDC_PROVIDER_URL, 443), timeout=10) as sock:
188-
with ctx.wrap_socket(sock, server_hostname=OIDC_PROVIDER_URL) as ssock:
189-
cert = ssock.getpeercert(binary_form=True)
190-
return hashlib.sha1(cert).hexdigest() # noqa: S324 (intentional: AWS API expects SHA1)
191-
192-
193182
def ensure_oidc_provider(iam) -> str:
194183
"""Create or reuse the app.terraform.io OIDC provider. Returns ARN."""
195184
expected_url = f"https://{OIDC_PROVIDER_URL}"
@@ -199,11 +188,10 @@ def ensure_oidc_provider(iam) -> str:
199188
print(f" reusing OIDC provider: {p['Arn']}")
200189
return p["Arn"]
201190

202-
thumbprint = get_app_terraform_thumbprint()
203191
resp = iam.create_open_id_connect_provider(
204192
Url=expected_url,
205193
ClientIDList=[OIDC_AUDIENCE],
206-
ThumbprintList=[thumbprint],
194+
ThumbprintList=[OIDC_PROVIDER_THUMBPRINT_PLACEHOLDER],
207195
)
208196
arn = resp["OpenIDConnectProviderArn"]
209197
print(f" created OIDC provider: {arn}")

examples/oidc_setup.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,8 @@
5959
from __future__ import annotations
6060

6161
import argparse
62-
import hashlib
6362
import json
6463
import os
65-
import socket
66-
import ssl
6764
import sys
6865
from dataclasses import dataclass, field
6966
from pathlib import Path
@@ -84,6 +81,21 @@
8481
OIDC_AUDIENCE = "aws.workload.identity"
8582
STATIC_AWS_VARS = ("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN")
8683

84+
# Placeholder thumbprint sent to AWS when creating the OIDC provider. AWS's
85+
# `CreateOpenIDConnectProvider` accepts a `ThumbprintList` parameter that
86+
# was historically expected to be the SHA1 hash of the provider's TLS
87+
# certificate. Since July 2023 AWS no longer validates this value for
88+
# providers backed by Amazon Trust Services CAs (which app.terraform.io
89+
# is) — the cert chain is validated at runtime against the ATS root CAs.
90+
# Any 40-char hex string is accepted in the field.
91+
# See: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc_verify-thumbprint.html
92+
#
93+
# Sending a placeholder removes the need for this script to make a TLS
94+
# connection to app.terraform.io and SHA1-hash the leaf cert — both of
95+
# which trip CodeQL's py/insecure-protocol and py/weak-sensitive-data-hashing
96+
# rules even though neither is a security concern in this context.
97+
OIDC_PROVIDER_THUMBPRINT_PLACEHOLDER = "0" * 40
98+
8799

88100
# ---------------------------------------------------------------------------
89101
# CLI
@@ -258,16 +270,6 @@ def parse_args(argv: list[str] | None = None) -> Args:
258270
# ---------------------------------------------------------------------------
259271

260272

261-
def _terraform_thumbprint() -> str:
262-
ctx = ssl.create_default_context()
263-
with socket.create_connection((OIDC_PROVIDER_URL, 443), timeout=10) as sock:
264-
with ctx.wrap_socket(sock, server_hostname=OIDC_PROVIDER_URL) as ssock:
265-
cert = ssock.getpeercert(binary_form=True)
266-
# AWS API expects SHA1; this is not used as a security primitive (since
267-
# July 2023 AWS validates the certificate chain, not the thumbprint).
268-
return hashlib.sha1(cert).hexdigest() # noqa: S324
269-
270-
271273
def ensure_identity_provider(iam) -> tuple[str, bool]:
272274
"""Return (OIDC provider ARN, was_created). Idempotent: reuses any
273275
existing provider for the same URL rather than recreating it.
@@ -285,7 +287,7 @@ def ensure_identity_provider(iam) -> tuple[str, bool]:
285287
resp = iam.create_open_id_connect_provider(
286288
Url=expected_url,
287289
ClientIDList=[OIDC_AUDIENCE],
288-
ThumbprintList=[_terraform_thumbprint()],
290+
ThumbprintList=[OIDC_PROVIDER_THUMBPRINT_PLACEHOLDER],
289291
)
290292
return resp["OpenIDConnectProviderArn"], True
291293

0 commit comments

Comments
 (0)