Skip to content

Commit

Permalink
Added offchain-python
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyjams committed Jun 8, 2020
1 parent a319c0a commit bd995c8
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 15 deletions.
20 changes: 20 additions & 0 deletions v5/offchain-python-hello-world/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


* Basic

Build:
`./basic/build`

Run locally:
`./basic/run`
`./basic/run Alice`


* Tee

Build:
`./tee/build`

Run locally:
`./tee/run`
`./tee/run Alice`
8 changes: 8 additions & 0 deletions v5/offchain-python-hello-world/basic/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:3.7.3

### install some python3 dependencies
RUN pip3 install eth_abi

COPY ./src /app

ENTRYPOINT ["python", "/app/app.py"]
4 changes: 4 additions & 0 deletions v5/offchain-python-hello-world/basic/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
cd $(dirname $0)

docker image build -f ../basic/Dockerfile -t offchain-python-hello-world:1.0.0 .. $@
12 changes: 12 additions & 0 deletions v5/offchain-python-hello-world/basic/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
cd $(dirname $0)

IEXEC_OUT=/tmp/iexec_out

rm -rf $IEXEC_OUT
mkdir -p $IEXEC_OUT

docker run --rm -e IEXEC_OUT=/iexec_out -e IEXEC_IN=/iexec_in -v $IEXEC_OUT:/iexec_out offchain-python-hello-world:1.0.0 $@

echo
find $IEXEC_OUT
31 changes: 31 additions & 0 deletions v5/offchain-python-hello-world/src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import sys
import json
import eth_abi

iexec_out = os.environ['IEXEC_OUT']
iexec_in = os.environ['IEXEC_IN']

# Do whatever you want
data = "Hello, World!"
if len(sys.argv) > 1:
data = 'Hello, {}!'.format(sys.argv[1])

# Eventually use some confidential assets
if os.path.exists(iexec_in + '/dataset.txt'):
with open(iexec_in + '/dataset.txt', 'r') as dataset:
print('Confidential dataset: ' + dataset.read())

# Send callback data to smart-contract
callback_data = eth_abi.encode_abi([ 'string'], [ data ]).hex()
print('Offchain computing for Smart-Contracts [data:{}, callback_data:{}]'.format(data, callback_data))
with open(iexec_out + '/computed.json', 'w+') as f:
json.dump({ "callback-data" : callback_data}, f)


## Try:
# Basic:
# mkdir -p /tmp/iexec_out && IEXEC_OUT=/tmp/iexec_out IEXEC_IN=/tmp/iexec_in python3 app.py Alice
#
# Tee:
# mkdir -p /tmp/iexec_out && IEXEC_OUT=/tmp/iexec_out IEXEC_IN=../tee/confidential-assets python3 app.py Alice
14 changes: 14 additions & 0 deletions v5/offchain-python-hello-world/tee/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM sconecuratedimages/apps:python-3.7.3-alpine3.10-scone3.0

### install some python3 dependencies
RUN apk add gcc
RUN SCONE_MODE=sim pip3 install eth_abi

### copy the code inside the image
COPY ./src /app

### protect file system with Scone
COPY ./tee/protect-fs.sh ./tee/Dockerfile /build/
RUN sh /build/protect-fs.sh /app

ENTRYPOINT ["python", "/app/app.py"]
3 changes: 3 additions & 0 deletions v5/offchain-python-hello-world/tee/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
cd $(dirname $0)
docker image build -f ../tee/Dockerfile -t offchain-tee-python-hello-world:1.0.0 .. $@
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy dataset file
65 changes: 65 additions & 0 deletions v5/offchain-python-hello-world/tee/protect-fs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/sh

cd $(dirname $0)

if [ ! -e Dockerfile ]
then
printf "\nFailed to parse Dockerfile ENTRYPOINT\n"
printf "Did you forget to add your Dockerfile in your build?\n"
printf "COPY ./tee/Dockerfile /build/\n\n"
exit 1
fi

ENTRYPOINT_ARSG=$(grep ENTRYPOINT ./Dockerfile | tail -1 | grep -o '"[^"]\+"' | tr -d '"')
echo $ENTRYPOINT_ARSG > ./entrypoint

if [ -z "$ENTRYPOINT_ARSG" ]
then
printf "\nFailed to parse Dockerfile ENTRYPOINT\n"
printf "Did you forget to add an ENTRYPOINT to your Dockerfile?\n"
printf "ENTRYPOINT [\"executable\", \"param1\", \"param2\"]\n\n"
exit 1
fi

INTERPRETER=$(awk '{print $1}' ./entrypoint) # python
ENTRYPOINT=$(cat ./entrypoint) # /python /app/app.py

export SCONE_MODE=sim
export SCONE_HEAP=1G

APP_FOLDER=$1

printf "\n### Starting file system protection ...\n\n"

scone fspf create /fspf.pb
scone fspf addr /fspf.pb / --not-protected --kernel /
scone fspf addr /fspf.pb /usr --authenticated --kernel /usr
scone fspf addf /fspf.pb /usr /usr
scone fspf addr /fspf.pb /bin --authenticated --kernel /bin
scone fspf addf /fspf.pb /bin /bin
scone fspf addr /fspf.pb /lib --authenticated --kernel /lib
scone fspf addf /fspf.pb /lib /lib
scone fspf addr /fspf.pb /etc/ssl --authenticated --kernel /etc/ssl
scone fspf addf /fspf.pb /etc/ssl /etc/ssl
scone fspf addr /fspf.pb /sbin --authenticated --kernel /sbin
scone fspf addf /fspf.pb /sbin /sbin
printf "\n### Protecting code found in folder \"$APP_FOLDER\"\n\n"
scone fspf addr /fspf.pb $APP_FOLDER --authenticated --kernel $APP_FOLDER
scone fspf addf /fspf.pb $APP_FOLDER $APP_FOLDER

scone fspf encrypt /fspf.pb > ./keytag

MRENCLAVE="$(SCONE_HASH=1 $INTERPRETER)"
FSPF_TAG=$(cat ./keytag | awk '{print $9}')
FSPF_KEY=$(cat ./keytag | awk '{print $11}')
FINGERPRINT="$FSPF_KEY|$FSPF_TAG|$MRENCLAVE|$ENTRYPOINT"
echo $FINGERPRINT > ./fingerprint

printf "\n\n"
printf "Your application fingerprint (mrenclave) is ready:\n"
printf "#####################################################################\n"
printf "iexec.json:\n\n"
printf "%s\n" "\"app\": { " " \"owner\" : ... " " \"name\": ... " " ..." " \"mrenclave\": \"$FINGERPRINT\"" "}"
printf "#####################################################################\n"
printf "Hint: Replace 'mrenclave' before doing 'iexec app deploy' step.\n"
printf "\n\n"
12 changes: 12 additions & 0 deletions v5/offchain-python-hello-world/tee/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
cd $(dirname $0)

IEXEC_OUT=/tmp/iexec_out

rm -rf $IEXEC_OUT
mkdir -p $IEXEC_OUT

docker run --rm -e IEXEC_OUT=/iexec_out -e IEXEC_IN=/iexec_in -v $IEXEC_OUT:/iexec_out -v $(pwd)/confidential-assets:/iexec_in --device /dev/isgx offchain-tee-python-hello-world:1.0.0 $@

echo
find $IEXEC_OUT
2 changes: 1 addition & 1 deletion v5/python-hello-world/basic/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.7.3

### install some python3 dependencies
### install python3 dependencies you need
RUN pip3 install pyfiglet

COPY ./src /app
Expand Down
2 changes: 1 addition & 1 deletion v5/python-hello-world/basic/build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
cd $(dirname $0)

docker image build -f ../basic/Dockerfile -t python-hello-world:4.0.0 .. $@
docker image build -f ../basic/Dockerfile -t python-hello-world:1.0.0 .. $@
10 changes: 6 additions & 4 deletions v5/python-hello-world/basic/run
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/bin/sh
cd $(dirname $0)

rm -rf /tmp/iexec_out
mkdir -p /tmp/iexec_out
IEXEC_OUT=/tmp/iexec_out

docker run --rm -e IEXEC_OUT=/iexec_out -v /tmp/iexec_out:/iexec_out python-hello-world:4.0.0 $@
rm -rf $IEXEC_OUT
mkdir -p $IEXEC_OUT

docker run --rm -e IEXEC_OUT=/iexec_out -e IEXEC_IN=/iexec_in -v /tmp/iexec_out:/iexec_out python-hello-world:1.0.0 $@

echo
find /tmp/iexec_out/
find $IEXEC_OUT
15 changes: 13 additions & 2 deletions v5/python-hello-world/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
from pyfiglet import Figlet

iexec_out = os.environ['IEXEC_OUT']
iexec_in = os.environ['IEXEC_IN']

# Do whatever you want
text = "Hello, World!"
if len(sys.argv) > 1:
text = 'Hello, {}!'.format(sys.argv[1])
text = Figlet().renderText(text) + text # Let's add some art for e.g.

text = Figlet().renderText(text) + text # Let's add some art
# Eventually use some confidential assets
if os.path.exists(iexec_in + '/dataset.txt'):
with open(iexec_in + '/dataset.txt', 'r') as dataset:
text = text + '\nConfidential dataset: ' + dataset.read()

# Append some results
with open(iexec_out + '/result.txt', 'w+') as fout:
fout.write(text)
print(text)
Expand All @@ -20,4 +26,9 @@
with open(iexec_out + '/computed.json', 'w+') as f:
json.dump({ "deterministic-output-path" : iexec_out + '/result.txt' }, f)

# Try: mkdir -p /tmp/iexec_out && IEXEC_OUT=/tmp/iexec_out python3 app.py Alice
## Try:
# Basic:
# mkdir -p /tmp/iexec_out && IEXEC_OUT=/tmp/iexec_out IEXEC_IN=/tmp/iexec_in python3 app.py Alice
#
# Tee:
# mkdir -p /tmp/iexec_out && IEXEC_OUT=/tmp/iexec_out IEXEC_IN=../tee/confidential-assets python3 app.py Alice
3 changes: 1 addition & 2 deletions v5/python-hello-world/tee/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
FROM sconecuratedimages/apps:python-3.7.3-alpine3.10-scone3.0

### install some python3 dependencies
### install python3 dependencies you need
RUN SCONE_MODE=sim pip3 install pyfiglet

### copy the code inside the image
COPY ./src /app

### protect file system with Scone
Expand Down
2 changes: 1 addition & 1 deletion v5/python-hello-world/tee/build
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
cd $(dirname $0)
docker image build -f ../tee/Dockerfile -t tee-python-hello-world:4.0.0 .. $@
docker image build -f ../tee/Dockerfile -t tee-python-hello-world:1.0.0 .. $@
1 change: 1 addition & 0 deletions v5/python-hello-world/tee/confidential-assets/dataset.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy dataset file
10 changes: 6 additions & 4 deletions v5/python-hello-world/tee/run
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/bin/sh
cd $(dirname $0)

rm -rf /tmp/iexec_out
mkdir -p /tmp/iexec_out
IEXEC_OUT=/tmp/iexec_out

docker run --rm -e IEXEC_OUT=/iexec_out -v /tmp/iexec_out:/iexec_out --device /dev/isgx tee-python-hello-world:4.0.0 $@
rm -rf $IEXEC_OUT
mkdir -p $IEXEC_OUT

docker run --rm -e IEXEC_OUT=/iexec_out -e IEXEC_IN=/iexec_in -v $IEXEC_OUT:/iexec_out -v $(pwd)/confidential-assets:/iexec_in --device /dev/isgx tee-python-hello-world:1.0.0 $@

echo
find /tmp/iexec_out/
find $IEXEC_OUT

0 comments on commit bd995c8

Please sign in to comment.