Skip to content

Commit 401cdc2

Browse files
martindekovalexellis
authored andcommitted
Add small fixes to lab6 and lab11
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]>
1 parent 6cc6fa7 commit 401cdc2

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

hmac-protected/hmac-protected/handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
def validateHMAC(message, secret, hash):
44

55
# GitHub and the sign flag prefix the hash with "sha1="
6-
receivedHash = clearHash(hash)
6+
receivedHash = getHash(hash)
77

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

1212
return receivedHash == createdHash
1313

14-
def clearHash(hash):
14+
def getHash(hash):
1515
if "sha1=" in hash:
1616
hash=hash[5:]
1717
return hash

lab11.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
Before starting this lab create a new folder
66

7-
```
7+
```bash
88
mkdir -p lab11 \
99
&& cd lab11
1010
```
1111

12+
also make sure your `faas-cli` version is `0.7.4` or above with the following command:
13+
14+
```
15+
$ faas-cli version
16+
```
17+
1218
## What is HMAC
1319

1420
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.
@@ -26,7 +32,7 @@ We will use the `--sign` flag provided by faas-cli to send a header containing t
2632
2733
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:
2834

29-
```
35+
```bash
3036
$ faas-cli deploy --name env --fprocess="env" --image="functions/alpine:latest"
3137
```
3238

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

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

74-
```
80+
```bash
7581
$ faas-cli new --lang python3 hmac-protected --prefix="<your-docker-username>"
7682
```
7783

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

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

82-
```
88+
```bash
8389
$ echo -n "<your-secret>" | docker secret create payload-secret -
8490
```
8591

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

90-
```
96+
```yml
9197
provider:
9298
name: faas
9399
gateway: http://127.0.0.1:8080
@@ -103,23 +109,21 @@ functions:
103109
104110
Replace the content of the `handler.py` with the following code:
105111

106-
```
112+
```python
107113
import os, hmac, hashlib
108114
109115
def validateHMAC(message, secret, hash):
110116
111117
# GitHub and the sign flag prefix the hash with "sha1="
112-
receivedHash = clearHash(hash)
113-
encodedSecret = secret.encode()
114-
encodedMessage = message.encode()
118+
receivedHash = getHash(hash)
115119
116120
# Hash message with secret
117-
expectedMAC = hmac.new(encodedSecret,encodedMessage,hashlib.sha1)
121+
expectedMAC = hmac.new(secret.encode(), message.encode(), hashlib.sha1)
118122
createdHash = expectedMAC.hexdigest()
119123
120124
return receivedHash == createdHash
121125
122-
def clearHash(hash):
126+
def getHash(hash):
123127
if "sha1=" in hash:
124128
hash=hash[5:]
125129
return hash
@@ -158,7 +162,7 @@ On receipt of the request, the function will use `payload-secret` to sign the re
158162
159163
Here we compare the generated and received hashes:
160164
161-
```
165+
```python
162166
...
163167
if hmacDigest == cleanHash:
164168
return True
@@ -168,7 +172,7 @@ Here we compare the generated and received hashes:
168172

169173
* Invoke the function with the flag:
170174

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

lab6.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ Open your browser and access http://127.0.0.1:8080/function/show-html. You shoul
125125

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

128+
Inside `html` folder add new `list.html` file with this content:
129+
130+
```html
131+
<!DOCTYPE html>
132+
<html lang='en'>
133+
<head>
134+
<meta charset='UTF-8'>
135+
<title>OpenFaaS</title>
136+
</head>
137+
<body>
138+
<h2>This is a list!</h2>
139+
<ul>
140+
<li>One</li>
141+
<li>Two</li>
142+
<li>Three</li>
143+
</ul>
144+
</body>
145+
</html>
146+
```
147+
128148
Edit your `handler.py` to the following:
129149

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

175195
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.
176196

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

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

190-
Edit `list.html`:
191210

192-
```html
193-
<!DOCTYPE html>
194-
<html lang='en'>
195-
<head>
196-
<meta charset='UTF-8'>
197-
<title>OpenFaaS</title>
198-
</head>
199-
<body>
200-
<h2>This is a list!</h2>
201-
<ul>
202-
<li>One</li>
203-
<li>Two</li>
204-
<li>Three</li>
205-
</ul>
206-
</body>
207-
</html>
208-
```
209211

210212
Change your `handler.py`:
211213

0 commit comments

Comments
 (0)