Add haveibeenpwned sample function

Idea from Matthew Holt, using the haveibeenpwned API to query
whether a password has been found in a data-breach.

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (VMware)
2018-08-13 10:20:13 +01:00
parent 87cfa097bc
commit 44ceb8da22
3 changed files with 150 additions and 71 deletions

View File

@ -0,0 +1,78 @@
// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// API source - https://haveibeenpwned.com/API/v2#SearchingPwnedPasswordsByRange
// Idea from Matthew Holt (@mholt6)
package function
import (
"crypto/sha1"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
// Handle a serverless request
func Handle(payload []byte) string {
if len(payload) == 0 {
return "Enter a number of characters."
}
hashed := fmt.Sprintf("%X", sha1.Sum(payload))
prefix := hashed[:5]
c := http.Client{}
req, _ := http.NewRequest(http.MethodGet,
fmt.Sprintf("https://api.pwnedpasswords.com/range/%s", prefix), nil)
res, err := c.Do(req)
if err != nil {
return err.Error()
}
var bytesOut []byte
if res.Body != nil {
defer res.Body.Close()
bytesOut, _ = ioutil.ReadAll(res.Body)
}
passwords := string(bytesOut)
foundTimes, findErr := findPassword(passwords, prefix, hashed)
if findErr != nil {
return findErr.Error()
}
result := result{Found: foundTimes}
output, _ := json.Marshal(result)
return string(output)
}
type result struct {
Found int `json:"found"`
}
func findPassword(passwords string, prefix string, hashed string) (int, error) {
foundTimes := 0
for _, passwordLine := range strings.Split(passwords, "\r\n") {
if passwordLine != "" {
parts := strings.Split(passwordLine, ":")
if fmt.Sprintf("%s%s", prefix, parts[0]) == hashed {
var convErr error
foundTimes, convErr = strconv.Atoi(parts[1])
if convErr != nil {
return 0, fmt.Errorf(`Cannot convert value: "%s", error: "%s"\n`, parts[1], convErr)
}
break
}
}
}
return foundTimes, nil
}

View File

@ -0,0 +1,22 @@
package function
import (
"encoding/json"
"testing"
)
func Test_Handle(t *testing.T) {
res := Handle([]byte("test1234"))
result := result{}
err := json.Unmarshal([]byte(res), &result)
if err != nil {
t.Errorf("unable to unmarshal response, error: %s", err)
t.Fail()
}
if result.Found == 0 {
t.Errorf("expected test1234 to be found several times")
t.Fail()
}
}

View File

@ -1,102 +1,81 @@
provider: provider:
name: faas name: faas
gateway: http://localhost:8080 # can be a remote server gateway: http://localhost:8080
functions: functions:
alpinefunction: alpinefunction:
lang: dockerfile lang: dockerfile
handler: ./AlpineFunction handler: ./alpinefunction
image: functions/alpine:latest image: functions/alpine:latest
environment:
fprocess: "cat"
pwgen:
lang: dockerfile
handler: ./pwgen
image: functions/pwgen-sample:latest
apikey-secret: apikey-secret:
lang: go lang: go
handler: ./apikey-secret handler: ./apikey-secret
image: functions/apikey-secret:0.1 image: functions/apikey-secret:0.1
captainsintent: captainsintent:
lang: dockerfile lang: dockerfile
handler: ./CaptainsIntent handler: ./captainsintent
image: functions/captainsintent:latest image: functions/captainsintent:latest
changecolorintent:
lang: dockerfile
handler: ./changecolorintent
image: functions/alexa-leds:latest
chelloworld:
lang: dockerfile
handler: ./chelloworld
image: functions/helloc:latest
dockerhubstats:
lang: dockerfile
handler: ./dockerhubstats
image: functions/hubstats:latest
echo: echo:
lang: dockerfile lang: dockerfile
handler: ./echo handler: ./echo
image: functions/faas-echo:latest image: functions/faas-echo:latest
changecolorintent:
lang: dockerfile
handler: ./ChangeColorIntent
image: functions/alexa-leds:latest
chelloworld:
lang: dockerfile
handler: ./CHelloWorld/src
image: functions/helloc:latest
dockerhubstats:
lang: dockerfile
handler: ./DockerHubStats
image: functions/hubstats:latest
gif-maker: gif-maker:
lang: dockerfile lang: dockerfile
handler: ./gif-maker handler: ./gif-maker
image: functions/gif-maker:latest image: functions/gif-maker:latest
environment: haveibeenpwned:
read_timeout: 60 lang: go
write_timeout: 60 handler: ./haveibeenpwned
image: haveibeenpwned:latest
hostnameintent: hostnameintent:
lang: dockerfile lang: dockerfile
handler: ./HostnameIntent handler: ./hostnameintent
image: functions/hostname-intent:latest image: functions/hostname-intent:latest
markdownrender: markdownrender:
lang: dockerfile lang: dockerfile
handler: ./MarkdownRender handler: ./markdownrender
image: functions/markdown-render:latest image: functions/markdown-render:latest
nodeinfo:
lang: dockerfile
handler: ./NodeInfo
image: functions/node-info:latest
phantomjs:
lang: dockerfile
handler: ./Phantomjs
image: functions/phantomjs:latest
resizeimagemagick:
lang: dockerfile
handler: ./ResizeImageMagick
image: functions/resizer:latest
sentimentanalysis:
lang: dockerfile
handler: ./SentimentAnalysis
image: functions/sentimentanalysis:latest
webhookstash:
lang: dockerfile
handler: ./WebhookStash
image: functions/webhookstash:latest
wordcountfunction:
lang: dockerfile
handler: ./WordCountFunction
image: functions/wordcount:latest
environment:
fprocess: "wc"
nmap: nmap:
lang: dockerfile lang: dockerfile
handler: ./Nmap handler: ./nmap
image: functions/nmap:0.1 image: functions/nmap:0.1
nodeinfo:
lang: dockerfile
handler: ./nodeinfo
image: functions/node-info:latest
phantomjs:
lang: dockerfile
handler: ./phantomjs
image: functions/phantomjs:latest
pwgen:
lang: dockerfile
handler: ./pwgen
image: functions/pwgen-sample:latest
resizeimagemagick:
lang: dockerfile
handler: ./resizeimagemagick
image: functions/resizer:latest
sentimentanalysis:
lang: dockerfile
handler: ./sentimentanalysis
image: functions/sentimentanalysis:latest
webhookstash:
lang: dockerfile
handler: ./webhookstash
image: functions/webhookstash:latest
wordcountfunction:
lang: dockerfile
handler: ./wordcountfunction
image: functions/wordcount:latest