Update the sample functions

**What**
- Move the new secrets sample function to ApiKeyProtected-Secrets
- Bring back the original ApiKeyProtected sample function

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
This commit is contained in:
Lucas Roesler
2017-11-09 10:12:12 +01:00
committed by Alex Ellis
parent 1a055deb49
commit 156103e2db
10 changed files with 99 additions and 15 deletions

View File

@ -4,21 +4,15 @@ import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/openfaas/faas/watchdog/types"
)
func handle(body []byte) {
key := os.Getenv("Http_X_Api_Key")
secretBytes, err := ioutil.ReadFile("/run/secrets/secret_api_key")
if err != nil {
log.Fatal(err)
}
secret := strings.TrimSpace(string(secretBytes))
if key == secret {
func handle(header http.Header, body []byte) {
key := header.Get("X-Api-Key")
if key == os.Getenv("secret_api_key") {
fmt.Println("Unlocked the function!")
} else {
fmt.Println("Access denied!")
@ -27,5 +21,9 @@ func handle(body []byte) {
func main() {
bytes, _ := ioutil.ReadAll(os.Stdin)
handle(bytes)
req, err := types.UnmarshalRequest(bytes)
if err != nil {
log.Fatal(err)
}
handle(req.Header, req.Body.Raw)
}