Skip to content
Snippets Groups Projects
Commit 156103e2 authored by Lucas Roesler's avatar Lucas Roesler Committed by Alex Ellis
Browse files

Update the sample functions


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

Signed-off-by: default avatarLucas Roesler <roesler.lucas@gmail.com>
parent 1a055deb
No related branches found
No related tags found
No related merge requests found
ApiKeyProtected
FROM golang:1.7.5 as builder
RUN mkdir -p /go/src/app
COPY handler.go /go/src/app
WORKDIR /go/src/app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest
WORKDIR /root/
EXPOSE 8080
ENV http_proxy ""
ENV https_proxy ""
ADD https://github.com/alexellis/faas/releases/download/0.6.6d/fwatchdog /usr/bin
RUN chmod +x /usr/bin/fwatchdog
COPY --from=builder /go/src/app/app .
ENV fprocess="/root/app"
CMD ["fwatchdog"]
### Api-Key-Protected sample
See the [secure secret management guide](../guide/secure_secret_management.md) for instructions on how to use this function.
When calling via the gateway pass the additional header "X-Api-Key", if it matches the `secret_api_key` value then the function will give access, otherwise access denied.
File added
#!/bin/sh
echo Building functions/api-key-protected:latest
docker build --no-cache -t functions/api-key-protected:latest .
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
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 {
fmt.Println("Unlocked the function!")
} else {
fmt.Println("Access denied!")
}
}
func main() {
bytes, _ := ioutil.ReadAll(os.Stdin)
handle(bytes)
}
FROM golang:1.7.5
RUN mkdir -p /go/src/app
COPY handler.go /go/src/app
WORKDIR /go/src/app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
CMD ["echo"]
### Api-Key-Protected sample ### Api-Key-Protected sample
See the [secure secret management guide](../guide/secure_secret_management.md) for instructions on how to use this function. To use this sample provide an env variable for the container/service in `secret_api_key`.
When calling via the gateway pass the additional header "X-Api-Key", if it matches the `secret_api_key` value then the function will give access, otherwise access denied. Then when calling via the gateway pass the additional header "X-Api-Key", if it matches the `secret_api_key` value then the function will give access, otherwise access denied.
#!/bin/sh
echo Building functions/api-key-protected:build
docker build --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy \
-t functions/api-key-protected . -f Dockerfile.build
docker create --name render_extract functions/api-key-protected
docker cp render_extract:/go/src/app/app ./app
docker rm -f render_extract
echo Building functions/api-key-protected:latest
docker build --no-cache -t functions/api-key-protected:latest .
...@@ -4,21 +4,15 @@ import ( ...@@ -4,21 +4,15 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http"
"os" "os"
"strings"
)
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)) "github.com/openfaas/faas/watchdog/types"
)
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!") fmt.Println("Unlocked the function!")
} else { } else {
fmt.Println("Access denied!") fmt.Println("Access denied!")
...@@ -27,5 +21,9 @@ func handle(body []byte) { ...@@ -27,5 +21,9 @@ func handle(body []byte) {
func main() { func main() {
bytes, _ := ioutil.ReadAll(os.Stdin) bytes, _ := ioutil.ReadAll(os.Stdin)
handle(bytes) req, err := types.UnmarshalRequest(bytes)
if err != nil {
log.Fatal(err)
}
handle(req.Header, req.Body.Raw)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment