gsce 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python2
  2. import sys,os
  3. import subprocess
  4. import re
  5. def gen_dockerfile( image_name, app_name, bin_name, proj_dir) :
  6. if not os.path.exists(proj_dir + "/Tools/build") :
  7. os.makedirs(proj_dir + "/Tools/build")
  8. df =open(proj_dir + "/Tools/build/Dockerfile" + '.' + app_name, 'w')
  9. df.write('# This file is auto-generated, any edits will be overwritten\n')
  10. df.write('\n')
  11. # Choose the base image from the user input
  12. df.write('FROM '+ image_name + '\n')
  13. df.write('\n')
  14. # SWITCH to ROOT
  15. df.write('# SWITCH to root \n')
  16. df.write('USER root \n')
  17. df.write('\n')
  18. # DOWNLOAD dependencies
  19. df.write('# Download dependencies\n')
  20. df.write('RUN apt-get update \\\n')
  21. df.write(' && apt-get install -y openssl libjemalloc-dev python python-pip python-dev \\\n')
  22. df.write(' && pip install protobuf \\\n')
  23. df.write(' && pip install pycrypto \n')
  24. df.write('# Temporal fixes for Dependencies Issue #1: libcrypto.so.1.0.0 and libssl.so.1.0.0 have different locations \n')
  25. df.write('RUN ln -s /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 \\\n')
  26. df.write(' && ln -s /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 /lib/x86_64-linux-gnu/libssl.so.1.0.0 \n')
  27. # SETUP Directory Structure
  28. print "cwd: "+ proj_dir
  29. df.write('# Setup Directory Structure \n')
  30. df.write('RUN mkdir -p ' + proj_dir + '/LibOS/shim/test/apps/' + app_name + ' \\\n')
  31. df.write(' && mkdir -p ' + proj_dir + '/Pal/src/host/Linux-SGX/signer \\\n')
  32. df.write(' && mkdir -p ' + proj_dir + '/Runtime \\\n')
  33. df.write(' && mkdir /gbin \n')
  34. # COPY system files
  35. df.write('# Copy system files \n')
  36. df.write('COPY Runtime/* ' + proj_dir + '/Runtime/ \n')
  37. df.write('COPY Pal/src/Makefile.Host ' + proj_dir + '/Pal/src/Makefile.Host \n')
  38. df.write('COPY Pal/src/host/Linux-SGX/signer/* ' + proj_dir + '/Pal/src/host/Linux-SGX/signer/ \n')
  39. # COPY tools for building app instance
  40. df.write('# Copy tools for building app instance\n')
  41. df.write('COPY Tools/build/tools/* /gbin/ \n')
  42. df.write('COPY Tools/gen_manifest /gbin/ \n')
  43. # Generating manifest file for target app
  44. df.write('# Generating manifest for target app \n')
  45. df.write('RUN /gbin/gen_manifest ' + app_name + ' ' + bin_name + ' ' + proj_dir + '\n')
  46. # Sign Enclave
  47. df.write('# Signing Enclave \n')
  48. df.write('RUN cd ' + proj_dir + '/LibOS/shim/test/apps/' + app_name + ' && \ \n'
  49. ' '+ proj_dir + '/Pal/src/host/Linux-SGX/signer/pal-sgx-sign -libpal ' + proj_dir +
  50. '/Pal/src/host/Linux-SGX/../../../../Runtime/libpal-Linux-SGX.so -key ' + proj_dir +
  51. '/Pal/src/host/Linux-SGX/signer/enclave-key.pem -output ' + app_name + '.manifest.sgx ' +
  52. '-manifest ' + app_name + '.manifest \n')
  53. # Remove signing key
  54. df.write('# Removing key after signing \n')
  55. # TODO
  56. # Overwrite Entry Point
  57. df.write('ENTRYPOINT ["/bin/bash", "/gbin/app_exec"] \n')
  58. df.close()
  59. def make_exec(path) :
  60. mode = os.stat(path).st_mode
  61. mode |= (mode & 0o444) >> 2 # copy R bits to X
  62. os.chmod(path, mode)
  63. def gen_app_executor(app_name, bin_cmd, proj_dir) :
  64. if not os.path.exists(proj_dir + "/Tools/build/tools") :
  65. os.makedirs(proj_dir + "/Tools/build/tools")
  66. e_path = proj_dir + "/Tools/build/tools/app_exec"
  67. print "e_path: " + e_path
  68. ef = open(e_path, "w")
  69. make_exec(e_path)
  70. ef.write('#!/usr/bin/env bash\n\n')
  71. ef.write('cd ' + proj_dir + '/LibOS/shim/test/apps/' + app_name +'\n')
  72. ef.write('# Generate EINITOKEN \n')
  73. ef.write(proj_dir + '/Pal/src/host/Linux-SGX/signer/pal-sgx-get-token -output '
  74. + app_name + '.token -sig ' + app_name + '.sig \n')
  75. ef.write('# Run the application \n')
  76. ef.write('SGX=1 ./' + app_name + '.manifest.sgx ' + bin_cmd + '\n')
  77. ef.close()
  78. if __name__ == "__main__":
  79. if len(sys.argv) < 3:
  80. print "Usage: gsce run [Image name] "
  81. exit()
  82. image_name = sys.argv[-1]
  83. image_match = re.match(r'([^:]*)(:*)(.*)', image_name)
  84. if image_match :
  85. app_name = image_match.group(1)
  86. print "app_name: " + app_name
  87. inspect_cmd = "sudo docker inspect --format '{{.Config.Cmd}}' " + image_name
  88. res = subprocess.check_output(inspect_cmd, shell=True).strip()
  89. print res
  90. match = re.match(r'\[([^\s]*)\s*(.*)\]', res)
  91. bin_name = match.group(1)
  92. if match.group(2) :
  93. bin_cmd = match.group(2)
  94. else :
  95. bin_cmd = ""
  96. print "bin_name: " + bin_name + " bin_cmd: " + bin_cmd
  97. # Store the rest arguments as docker run arguments
  98. docker_str = " " + " ".join(sys.argv[2:-1])
  99. # print image_cmd
  100. proj_dir = os.path.abspath(os.getcwd() + "/../")
  101. # STEP 1: Generating Dockerfile
  102. gen_dockerfile(image_name, app_name, bin_name, proj_dir)
  103. # STEP 2: Generating entry point execute script
  104. gen_app_executor(app_name, bin_cmd, proj_dir)
  105. # STEP 3: Building new docker image with generated Dockerfile
  106. os.chdir('..')
  107. os.system("sudo docker build -f Tools/build/Dockerfile." + app_name + " -t gsc_" + app_name + " .\n")
  108. # STEP 4: Run GSC with the target app
  109. os.system("sudo docker run -i -t" + docker_str +" --device=/dev/gsgx --device=/dev/isgx -v /var/run/aesmd/aesm.socket:/var/run/aesmd/aesm.socket gsc_"+app_name+"\n")