Skip to content

Commit 6407dd8

Browse files
Add arg to enable tpm (#310)
* Refactor out ec2.register_image arguments * Add command line arg to enable TPM
1 parent f41ba96 commit 6407dd8

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

upload-ami/src/upload_ami/upload_ami.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from mypy_boto3_ec2.client import EC2Client
1414
from mypy_boto3_ec2.literals import BootModeValuesType
15-
from mypy_boto3_ec2.type_defs import RegionTypeDef
15+
from mypy_boto3_ec2.type_defs import RegionTypeDef, RegisterImageRequestTypeDef
1616
from mypy_boto3_s3.client import S3Client
1717

1818
from concurrent.futures import ThreadPoolExecutor
@@ -127,6 +127,7 @@ def register_image_if_not_exists(
127127
image_info: ImageInfo,
128128
snapshot_id: str,
129129
public: bool,
130+
enable_tpm: bool,
130131
) -> str:
131132
"""
132133
Register image if it doesn't exist yet
@@ -150,19 +151,11 @@ def register_image_if_not_exists(
150151
else:
151152
raise Exception("Unknown system: " + image_info["system"])
152153

153-
logging.info(f"Registering image {image_name} with snapshot {snapshot_id}")
154-
155-
# TODO(arianvp): Not all instance types support TPM 2.0 yet. We should
156-
# upload two images, one with and one without TPM 2.0 support.
157-
158-
# if architecture == "x86_64" and image_info["boot_mode"] == "uefi":
159-
# tpmsupport['TpmSupport'] = "v2.0"
160-
161-
register_image = ec2.register_image(
162-
Name=image_name,
163-
Architecture=architecture,
164-
BootMode=image_info["boot_mode"],
165-
BlockDeviceMappings=[
154+
register_image_kwargs: RegisterImageRequestTypeDef = {
155+
"Name": image_name,
156+
"Architecture": architecture,
157+
"BootMode": image_info["boot_mode"],
158+
"BlockDeviceMappings": [
166159
{
167160
"DeviceName": "/dev/xvda",
168161
"Ebs": {
@@ -171,12 +164,12 @@ def register_image_if_not_exists(
171164
},
172165
}
173166
],
174-
RootDeviceName="/dev/xvda",
175-
VirtualizationType="hvm",
176-
EnaSupport=True,
177-
ImdsSupport="v2.0",
178-
SriovNetSupport="simple",
179-
TagSpecifications=[
167+
"RootDeviceName": "/dev/xvda",
168+
"VirtualizationType": "hvm",
169+
"EnaSupport": True,
170+
"ImdsSupport": "v2.0",
171+
"SriovNetSupport": "simple",
172+
"TagSpecifications": [
180173
{
181174
"ResourceType": "image",
182175
"Tags": [
@@ -185,7 +178,18 @@ def register_image_if_not_exists(
185178
],
186179
}
187180
],
188-
)
181+
}
182+
183+
if (
184+
enable_tpm
185+
and architecture == "x86_64"
186+
and image_info["boot_mode"] == "uefi"
187+
):
188+
register_image_kwargs["TpmSupport"] = "v2.0"
189+
190+
logging.info(f"Registering image {image_name} with snapshot {snapshot_id}")
191+
192+
register_image = ec2.register_image(**register_image_kwargs)
189193
image_id = register_image["ImageId"]
190194

191195
ec2.get_waiter("image_available").wait(ImageIds=[image_id])
@@ -303,6 +307,7 @@ def upload_ami(
303307
run_id: str,
304308
public: bool,
305309
dest_regions: list[str],
310+
enable_tpm: bool,
306311
) -> dict[str, str]:
307312
"""
308313
Upload NixOS AMI to AWS and return the image ids for each region
@@ -324,7 +329,7 @@ def upload_ami(
324329
)
325330

326331
image_id = register_image_if_not_exists(
327-
ec2, image_name, image_info, snapshot_id, public
332+
ec2, image_name, image_info, snapshot_id, public, enable_tpm
328333
)
329334

330335
regions = filter(
@@ -366,6 +371,12 @@ def main() -> None:
366371
action="append",
367372
default=[],
368373
)
374+
parser.add_argument(
375+
"--enable-tpm",
376+
action="store_true",
377+
default=False,
378+
help="Enable TPM 2.0 support for UEFI x86_64 images",
379+
)
369380

370381
args = parser.parse_args()
371382

@@ -384,6 +395,7 @@ def main() -> None:
384395
args.run_id,
385396
args.public,
386397
args.dest_region,
398+
args.enable_tpm,
387399
)
388400
print(json.dumps(image_ids))
389401

0 commit comments

Comments
 (0)