Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vmsdk/python: refine sample cli #87

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions vmsdk/python/cc_event_log_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@
"""
import logging
import argparse
from cctrusted_vm import CCTrustedVmSdk
import os
from cctrusted_base.api import CCTrustedApi
from cctrusted_vm.cvm import ConfidentialVM
from cctrusted_vm.sdk import CCTrustedVmSdk


LOG = logging.getLogger(__name__)

logging.basicConfig(level=logging.NOTSET, format='%(name)s %(levelname)-8s %(message)s')

def main():
"""example cc event log fetching utility"""
"""Example cc event log fetching utility."""
if ConfidentialVM.detect_cc_type() == CCTrustedApi.TYPE_CC_NONE:
LOG.error("This is not a confidential VM!")
return
if os.geteuid() != 0:
LOG.error("Please run as root which is required for this example!")
return

parser = argparse.ArgumentParser(
description="The example utility to fetch CC event logs")
parser.add_argument('-s', type=int,
Expand Down
38 changes: 27 additions & 11 deletions vmsdk/python/cc_imr_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,37 @@
Command line to dump the integrated measurement register
"""
import logging
from cctrusted_vm import CCTrustedVmSdk
import os
from cctrusted_base.api import CCTrustedApi
from cctrusted_vm.cvm import ConfidentialVM
from cctrusted_vm.sdk import CCTrustedVmSdk

LOG = logging.getLogger(__name__)

logging.basicConfig(level=logging.NOTSET, format='%(name)s %(levelname)-8s %(message)s')

count = CCTrustedVmSdk.inst().get_measurement_count()
for index in range(CCTrustedVmSdk.inst().get_measurement_count()):
alg = CCTrustedVmSdk.inst().get_default_algorithms()
imr = CCTrustedVmSdk.inst().get_cc_measurement([index, alg.alg_id])
digest_obj = imr.digest(alg.alg_id)
def main():
"""Example to call get_cc_measurement and dump the result to stdout."""
if ConfidentialVM.detect_cc_type() == CCTrustedApi.TYPE_CC_NONE:
LOG.error("This is not a confidential VM!")
return
if os.geteuid() != 0:
LOG.error("Please run as root which is required for this example!")
return

hash_str = ""
for hash_item in digest_obj.hash:
hash_str += "".join([f"{hash_item:02x}", " "])
count = CCTrustedVmSdk.inst().get_measurement_count()
LOG.info("Measurement Count: %d", count)
for index in range(CCTrustedVmSdk.inst().get_measurement_count()):
alg = CCTrustedVmSdk.inst().get_default_algorithms()
imr = CCTrustedVmSdk.inst().get_cc_measurement([index, alg.alg_id])
digest_obj = imr.digest(alg.alg_id)

LOG.info("Algorithms: %s", str(alg))
LOG.info("HASH: %s", hash_str)
hash_str = ""
for hash_item in digest_obj.hash:
hash_str += "".join([f"{hash_item:02x}", " "])

LOG.info("Algorithms: %s", str(alg))
LOG.info("HASH: %s", hash_str)

if __name__ == "__main__":
main()
12 changes: 11 additions & 1 deletion vmsdk/python/cc_quote_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import argparse
import base64
import logging
import os
import random
from cctrusted_vm import CCTrustedVmSdk
from cctrusted_base.api import CCTrustedApi
from cctrusted_vm.cvm import ConfidentialVM
from cctrusted_vm.sdk import CCTrustedVmSdk

LOG = logging.getLogger(__name__)
OUT_FORMAT_RAW = "raw"
Expand Down Expand Up @@ -49,6 +52,13 @@ def make_userdata():

def main():
"""Example to call get_cc_report and dump the result to stdout."""
if ConfidentialVM.detect_cc_type() == CCTrustedApi.TYPE_CC_NONE:
LOG.error("This is not a confidential VM!")
return
if os.geteuid() != 0:
LOG.error("Please run as root which is required for this example!")
return

parser = argparse.ArgumentParser()
parser.add_argument(
"--out-format",
Expand Down
21 changes: 18 additions & 3 deletions vmsdk/python/td_report_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@
Command line to dump the integrated measurement register
"""
import logging
from cctrusted_vm import CCTrustedTdvmSdk
import os
from cctrusted_base.api import CCTrustedApi
from cctrusted_vm.cvm import ConfidentialVM
from cctrusted_vm.tdx import CCTrustedTdvmSdk

LOG = logging.getLogger(__name__)

logging.basicConfig(level=logging.NOTSET, format='%(message)s')

tdreport = CCTrustedTdvmSdk.inst().get_tdreport()
tdreport.dump()
def main():
"""Example to call get_tdreport and dump the result to stdout."""
if ConfidentialVM.detect_cc_type() != CCTrustedApi.TYPE_CC_TDX:
LOG.error("This is not a TD VM!")
return
if os.geteuid() != 0:
LOG.error("Please run as root which is required for this example!")
return

tdreport = CCTrustedTdvmSdk.inst().get_tdreport()
tdreport.dump()

if __name__ == "__main__":
main()
Loading