Skip to content

Commit

Permalink
vmsdk/python: improve sample cli to check euid
Browse files Browse the repository at this point in the history
The sample cli scripts required to be run as root.

Signed-off-by: zhongjie <zhongjie.shi@intel.com>
  • Loading branch information
intelzhongjie committed Jan 30, 2024
1 parent 76d8c1e commit 0ef0a74
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
7 changes: 6 additions & 1 deletion vmsdk/python/cc_event_log_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import logging
import argparse
import os
from cctrusted_vm import CCTrustedVmSdk


Expand All @@ -11,7 +12,11 @@
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 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
31 changes: 21 additions & 10 deletions vmsdk/python/cc_imr_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
Command line to dump the integrated measurement register
"""
import logging
import os
from cctrusted_vm 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 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()
5 changes: 5 additions & 0 deletions vmsdk/python/cc_quote_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import argparse
import base64
import logging
import os
import random
from cctrusted_vm import CCTrustedVmSdk

Expand Down Expand Up @@ -49,6 +50,10 @@ def make_userdata():

def main():
"""Example to call get_cc_report and dump the result to stdout."""
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
14 changes: 12 additions & 2 deletions vmsdk/python/td_report_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
Command line to dump the integrated measurement register
"""
import logging
import os
from cctrusted_vm 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 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()

0 comments on commit 0ef0a74

Please sign in to comment.