diff --git a/sample-functions/haveibeenpwned/handler.go b/sample-functions/haveibeenpwned/handler.go new file mode 100644 index 00000000..b97ae3ba --- /dev/null +++ b/sample-functions/haveibeenpwned/handler.go @@ -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 +} diff --git a/sample-functions/haveibeenpwned/handler_test.go b/sample-functions/haveibeenpwned/handler_test.go new file mode 100644 index 00000000..4a65f8df --- /dev/null +++ b/sample-functions/haveibeenpwned/handler_test.go @@ -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() + } +} diff --git a/sample-functions/stack.yml b/sample-functions/stack.yml index a7ae689a..3c6a4231 100644 --- a/sample-functions/stack.yml +++ b/sample-functions/stack.yml @@ -1,102 +1,81 @@ provider: name: faas - gateway: http://localhost:8080 # can be a remote server + gateway: http://localhost:8080 functions: alpinefunction: lang: dockerfile - handler: ./AlpineFunction + handler: ./alpinefunction image: functions/alpine:latest - environment: - fprocess: "cat" - - pwgen: - lang: dockerfile - handler: ./pwgen - image: functions/pwgen-sample:latest - apikey-secret: lang: go handler: ./apikey-secret image: functions/apikey-secret:0.1 - captainsintent: lang: dockerfile - handler: ./CaptainsIntent + handler: ./captainsintent 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: lang: dockerfile handler: ./echo 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: lang: dockerfile handler: ./gif-maker image: functions/gif-maker:latest - environment: - read_timeout: 60 - write_timeout: 60 - + haveibeenpwned: + lang: go + handler: ./haveibeenpwned + image: haveibeenpwned:latest hostnameintent: lang: dockerfile - handler: ./HostnameIntent + handler: ./hostnameintent image: functions/hostname-intent:latest - markdownrender: lang: dockerfile - handler: ./MarkdownRender + handler: ./markdownrender 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: lang: dockerfile - handler: ./Nmap + handler: ./nmap 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