mirror of
https://github.com/openfaas/faas.git
synced 2025-06-20 21:26:31 +00:00
Add key-protected sample
This commit is contained in:
sample-functions/ApiKeyProtected
2
sample-functions/ApiKeyProtected/.gitignore
vendored
Normal file
2
sample-functions/ApiKeyProtected/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
ApiKeyProtected
|
||||||
|
|
16
sample-functions/ApiKeyProtected/Dockerfile
Normal file
16
sample-functions/ApiKeyProtected/Dockerfile
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
FROM alpine:latest
|
||||||
|
|
||||||
|
WORKDIR /root/
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
ENV http_proxy ""
|
||||||
|
ENV https_proxy ""
|
||||||
|
|
||||||
|
# ADD https://github.com/alexellis/faas/releases/download/v0.5-alpha/fwatchdog /usr/bin
|
||||||
|
COPY fwatchdog /usr/bin/
|
||||||
|
RUN chmod +x /usr/bin/fwatchdog
|
||||||
|
|
||||||
|
COPY app .
|
||||||
|
|
||||||
|
ENV fprocess="/root/app"
|
||||||
|
CMD ["fwatchdog"]
|
9
sample-functions/ApiKeyProtected/Dockerfile.build
Normal file
9
sample-functions/ApiKeyProtected/Dockerfile.build
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
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"]
|
||||||
|
|
6
sample-functions/ApiKeyProtected/README.md
Normal file
6
sample-functions/ApiKeyProtected/README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
### Api-Key-Protected sample
|
||||||
|
|
||||||
|
To use this sample provide an env variable for the container/service in `secret_api_key`.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
12
sample-functions/ApiKeyProtected/build.sh
Executable file
12
sample-functions/ApiKeyProtected/build.sh
Executable file
@ -0,0 +1,12 @@
|
|||||||
|
#!/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 .
|
29
sample-functions/ApiKeyProtected/handler.go
Normal file
29
sample-functions/ApiKeyProtected/handler.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/alexellis/faas/watchdog/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
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!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
bytes, _ := ioutil.ReadAll(os.Stdin)
|
||||||
|
req, err := types.UnmarshalRequest(bytes)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
handle(req.Header, req.Body.Raw)
|
||||||
|
}
|
Reference in New Issue
Block a user