Skip to content

Commit

Permalink
Add small fixes to lab6 and lab11
Browse files Browse the repository at this point in the history
Adressing issues from @padiazg also adding small changes
to the lab11 like changing name from `clearHash` to `getHash`
and adding `python` `yml` and `bash` to the code snippets

Signed-off-by: Martin Dekov (VMware) <[email protected]>
  • Loading branch information
martindekov authored and alexellis committed Nov 10, 2018
1 parent 6cc6fa7 commit 401cdc2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
4 changes: 2 additions & 2 deletions hmac-protected/hmac-protected/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
def validateHMAC(message, secret, hash):

# GitHub and the sign flag prefix the hash with "sha1="
receivedHash = clearHash(hash)
receivedHash = getHash(hash)

# Hash message with secret
expectedMAC = hmac.new(secret.encode(), message.encode(), hashlib.sha1)
createdHash = expectedMAC.hexdigest()

return receivedHash == createdHash

def clearHash(hash):
def getHash(hash):
if "sha1=" in hash:
hash=hash[5:]
return hash
Expand Down
30 changes: 17 additions & 13 deletions lab11.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

Before starting this lab create a new folder

```
```bash
mkdir -p lab11 \
&& cd lab11
```

also make sure your `faas-cli` version is `0.7.4` or above with the following command:

```
$ faas-cli version
```

## What is HMAC

Without any form of authentication or trust our functions may be exposed to anyone who can guess their URL. If our functions are accessible on the Internet or the local network then they could be invoked by a bad actor. By default functions respond to any request. However, if we want to control access to functions we can use Hash-based Message Authentication Code (HMAC) to validate the source of information.
Expand All @@ -26,7 +32,7 @@ We will use the `--sign` flag provided by faas-cli to send a header containing t
Let's first inspect what the flag does by deploying the `env` function which will print all of the environmental variables accessible inside the function:

```
```bash
$ faas-cli deploy --name env --fprocess="env" --image="functions/alpine:latest"
```

Expand Down Expand Up @@ -71,23 +77,23 @@ We see the `HMAC` being provided as the environmental variable `Http_Hmac`. The

For our purpose we are going to create a new Python 3 function. Let’s call it `hmac-protected`:

```
```bash
$ faas-cli new --lang python3 hmac-protected --prefix="<your-docker-username>"
```

Add `payload-secret` which will serve as the key that will hash the payload.

Create `payload-secret` like we did in [lab10](https://github.com/openfaas/workshop/blob/master/lab10.md):

```
```bash
$ echo -n "<your-secret>" | docker secret create payload-secret -
```

> Note: Remember the string you put in place of `<your-secret>`
Our `hmac-protected.yml` should look like:

```
```yml
provider:
name: faas
gateway: http://127.0.0.1:8080
Expand All @@ -103,23 +109,21 @@ functions:
Replace the content of the `handler.py` with the following code:

```
```python
import os, hmac, hashlib
def validateHMAC(message, secret, hash):
# GitHub and the sign flag prefix the hash with "sha1="
receivedHash = clearHash(hash)
encodedSecret = secret.encode()
encodedMessage = message.encode()
receivedHash = getHash(hash)
# Hash message with secret
expectedMAC = hmac.new(encodedSecret,encodedMessage,hashlib.sha1)
expectedMAC = hmac.new(secret.encode(), message.encode(), hashlib.sha1)
createdHash = expectedMAC.hexdigest()
return receivedHash == createdHash
def clearHash(hash):
def getHash(hash):
if "sha1=" in hash:
hash=hash[5:]
return hash
Expand Down Expand Up @@ -158,7 +162,7 @@ On receipt of the request, the function will use `payload-secret` to sign the re
Here we compare the generated and received hashes:
```
```python
...
if hmacDigest == cleanHash:
return True
Expand All @@ -168,7 +172,7 @@ Here we compare the generated and received hashes:

* Invoke the function with the flag:

```
```bash
$ echo -n "This is a message" | faas-cli invoke hmac-protected --sign hmac --key=<your-secret>
```

Expand Down
40 changes: 21 additions & 19 deletions lab6.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ Open your browser and access http://127.0.0.1:8080/function/show-html. You shoul

Now we're going to add a path to the function URL.

Inside `html` folder add new `list.html` file with this content:

```html
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>OpenFaaS</title>
</head>
<body>
<h2>This is a list!</h2>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</body>
</html>
```

Edit your `handler.py` to the following:

```python
Expand Down Expand Up @@ -174,7 +194,7 @@ Now that we've understood how to serve html via functions, let's dynamically cha

The query string is `action=new`, hence the value of `Http_Query` would be `action=new`. We can also use the `parse_qs` function from the `urllib.parse` package and easily parse this query string.

First of all, let's create a new HTML file inside called `list.html`. So the structure should look like the following now:
The structure of the directory of our function looks like this:

```
├── show-html
Expand All @@ -187,25 +207,7 @@ First of all, let's create a new HTML file inside called `list.html`. So the str
└── show-html.yml
```

Edit `list.html`:

```html
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>OpenFaaS</title>
</head>
<body>
<h2>This is a list!</h2>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</body>
</html>
```

Change your `handler.py`:

Expand Down

0 comments on commit 401cdc2

Please sign in to comment.