From 6f97e782a30c13a75a0c258d2d238f31ba26f159 Mon Sep 17 00:00:00 2001 From: Rishabh Gupta Date: Tue, 15 Jan 2019 21:25:36 +0530 Subject: [PATCH] Added vendor folder and suggested fixes Signed-off-by: Rishabh Gupta --- cmd/tester/Dockerfile | 27 +++-- cmd/tester/Dockerfile.armhf | 20 ++-- cmd/tester/Gopkg.lock | 33 +++++++ cmd/tester/Gopkg.toml | 34 +++++++ cmd/tester/README.md | 8 +- cmd/tester/main.go | 39 +++----- .../openfaas-incubator/connector-sdk/LICENSE | 22 +++++ .../connector-sdk/types/controller.go | 78 +++++++++++++++ .../connector-sdk/types/credentials.go | 29 ++++++ .../types/function_list_builder.go | 69 +++++++++++++ .../connector-sdk/types/invoker.go | 82 ++++++++++++++++ .../connector-sdk/types/make_client.go | 22 +++++ .../connector-sdk/types/topic_map.go | 42 ++++++++ .../github.com/openfaas/faas-provider/LICENSE | 21 ++++ .../openfaas/faas-provider/auth/basic_auth.go | 26 +++++ .../faas-provider/auth/credentials.go | 52 ++++++++++ .../vendor/github.com/openfaas/faas/LICENSE | 23 +++++ .../faas/gateway/requests/forward_request.go | 29 ++++++ .../faas/gateway/requests/prometheus.go | 23 +++++ .../faas/gateway/requests/requests.go | 98 +++++++++++++++++++ cmd/tester/yaml/docker-compose.yml | 4 +- cmd/tester/yaml/kubernetes/connector-dep.yml | 8 +- 22 files changed, 735 insertions(+), 54 deletions(-) create mode 100644 cmd/tester/Gopkg.lock create mode 100644 cmd/tester/Gopkg.toml create mode 100644 cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/LICENSE create mode 100644 cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/controller.go create mode 100644 cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/credentials.go create mode 100644 cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/function_list_builder.go create mode 100644 cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/invoker.go create mode 100644 cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/make_client.go create mode 100644 cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/topic_map.go create mode 100644 cmd/tester/vendor/github.com/openfaas/faas-provider/LICENSE create mode 100644 cmd/tester/vendor/github.com/openfaas/faas-provider/auth/basic_auth.go create mode 100644 cmd/tester/vendor/github.com/openfaas/faas-provider/auth/credentials.go create mode 100644 cmd/tester/vendor/github.com/openfaas/faas/LICENSE create mode 100644 cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/forward_request.go create mode 100644 cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/prometheus.go create mode 100644 cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/requests.go diff --git a/cmd/tester/Dockerfile b/cmd/tester/Dockerfile index 3a2c1a6..525a26b 100644 --- a/cmd/tester/Dockerfile +++ b/cmd/tester/Dockerfile @@ -1,16 +1,29 @@ -FROM golang:1.9.2 as builder -RUN mkdir -p /go/src/github.com/openfaas-incubator/connector -WORKDIR /go/src/github.com/openfaas-incubator/connector +FROM golang:1.10.4 as builder +RUN mkdir -p /go/src/github.com/openfaas-incubator/connector-sdk +WORKDIR /go/src/github.com/openfaas-incubator/connector-sdk +COPY vendor vendor COPY main.go . -RUN go get -d -v ./... +# Run a gofmt and exclude all vendored code. +RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" + +RUN go test -v ./... # Stripping via -ldflags "-s -w" RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-s -w" -installsuffix cgo -o ./connector -FROM alpine +FROM alpine:3.8 + +RUN addgroup -S app \ + && adduser -S -g app app + +WORKDIR /home/app + +COPY --from=builder /go/src/github.com/openfaas-incubator/connector-sdk/ . + +RUN chown -R app:app ./ -COPY --from=builder /go/src/github.com/openfaas-incubator/connector/connector /bin/connector +USER app -CMD ["/bin/connector"] \ No newline at end of file +CMD ["./connector"] \ No newline at end of file diff --git a/cmd/tester/Dockerfile.armhf b/cmd/tester/Dockerfile.armhf index 3a2c1a6..42ad4d0 100644 --- a/cmd/tester/Dockerfile.armhf +++ b/cmd/tester/Dockerfile.armhf @@ -1,16 +1,20 @@ -FROM golang:1.9.2 as builder -RUN mkdir -p /go/src/github.com/openfaas-incubator/connector -WORKDIR /go/src/github.com/openfaas-incubator/connector +FROM golang:1.10.4 as builder +RUN mkdir -p /go/src/github.com/openfaas-incubator/connector-sdk +WORKDIR /go/src/github.com/openfaas-incubator/connector-sdk +COPY vendor vendor COPY main.go . -RUN go get -d -v ./... +# Run a gofmt and exclude all vendored code. +RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" + +RUN go test -v ./... # Stripping via -ldflags "-s -w" -RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-s -w" -installsuffix cgo -o ./connector +RUN GOARM=7 CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-s -w" -installsuffix cgo -o ./connector -FROM alpine +FROM alpine:3.8 -COPY --from=builder /go/src/github.com/openfaas-incubator/connector/connector /bin/connector +COPY --from=builder /go/src/github.com/openfaas-incubator/connector-sdk/ . -CMD ["/bin/connector"] \ No newline at end of file +CMD ["./connector"] \ No newline at end of file diff --git a/cmd/tester/Gopkg.lock b/cmd/tester/Gopkg.lock new file mode 100644 index 0000000..543bf49 --- /dev/null +++ b/cmd/tester/Gopkg.lock @@ -0,0 +1,33 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + digest = "1:2a7750103f5ea9a84634dbb554672ce8b11c762281ec1318038cbec14a470241" + name = "github.com/openfaas-incubator/connector-sdk" + packages = ["types"] + pruneopts = "UT" + revision = "2db120a2ec4b544bae7ee306975b70c1dc9adcf4" + version = "0.2.0" + +[[projects]] + digest = "1:3f69624cb4ae8ab815a672f7d169d64786ee027a9d42eefab3c9d4e9debe0750" + name = "github.com/openfaas/faas" + packages = ["gateway/requests"] + pruneopts = "UT" + revision = "a65df4795bc66147c41161c48bfd4c72f60c7434" + version = "0.9.14" + +[[projects]] + digest = "1:deb76da5396c9f641ddea9ca79e31a14bdb09c787cdfda90488768b7539b1fd6" + name = "github.com/openfaas/faas-provider" + packages = ["auth"] + pruneopts = "UT" + revision = "220324e98f5db5aa61f02d1ab13f03e91310796c" + version = "0.8.1" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + input-imports = ["github.com/openfaas-incubator/connector-sdk/types"] + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/cmd/tester/Gopkg.toml b/cmd/tester/Gopkg.toml new file mode 100644 index 0000000..b3bdd28 --- /dev/null +++ b/cmd/tester/Gopkg.toml @@ -0,0 +1,34 @@ +# Gopkg.toml example +# +# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[[constraint]] + name = "github.com/openfaas-incubator/connector-sdk" + version = "0.2.0" + +[prune] + go-tests = true + unused-packages = true diff --git a/cmd/tester/README.md b/cmd/tester/README.md index 9982fdb..5b13907 100644 --- a/cmd/tester/README.md +++ b/cmd/tester/README.md @@ -1,14 +1,14 @@ -# Openfaas Sample Connector +# OpenFaas Sample Connector -This folder contains a sample openfaas connector. You can use this as a base for creating your own connectors. +This folder contains a sample OpenFaas connector. You can use this as a base for creating your own connectors. For a more complex example checkout [kafka-connector](https://github.com/openfaas-incubator/kafka-connector) ## How to Use 1. Clone this repository: `git clone https://github.com/openfaas-incubator/connector-sdk.git` 2. Go into the directory: `cd ./connector-sdk/cmd/tester/yaml` -3. For openfaas deployed on docker swarm do: `docker stack deploy func -c ./docker-compose.yml` -4. For openfaas deployed on kubernetes do: `kubectl create -f ./kubernetes --namespace openfaas` +3. For OpenFaas deployed on Docker Swarm do: `docker stack deploy func -c ./docker-compose.yml` +4. For OpenFaas deployed on kubernetes do: `kubectl create -f ./kubernetes --namespace openfaas` To check if it actually works and triggers a function, deploy any function with annotation `topic=faas-request`. You can also run this command to deploy a sample function and see `trigger-func` invocation count growing in ui. diff --git a/cmd/tester/main.go b/cmd/tester/main.go index 7ca56b1..61380f6 100644 --- a/cmd/tester/main.go +++ b/cmd/tester/main.go @@ -5,57 +5,44 @@ package main import ( - "fmt" - "log" + "errors" "os" - "strings" "time" "github.com/openfaas-incubator/connector-sdk/types" ) func main() { - creds := types.GetCredentials() // Get credentials for gateway login - config := getControllerConfig() + creds := types.GetCredentials() + config, err := getControllerConfig() + if err != nil { + panic(err) + } controller := types.NewController(creds, config) - fmt.Println(controller) controller.BeginMapBuilder() - topics := getTopics() + topic := "faas-request" + invokeTime := time.Second * 2 // Simulate events emitting from queue/pub-sub for { - time.Sleep(2 * time.Second) + time.Sleep(invokeTime) data := []byte("test " + time.Now().String()) - for _, topic := range topics { - controller.Invoke(topic, &data) - } + controller.Invoke(topic, &data) } } -func getControllerConfig() *types.ControllerConfig { +func getControllerConfig() (*types.ControllerConfig, error) { gURL, ok := os.LookupEnv("gateway_url") if !ok { - gURL = "http://127.0.0.1:8080/" + return nil, errors.New("Gateway URL not set") } - fmt.Println("Gateway Url: ", gURL) return &types.ControllerConfig{ RebuildInterval: time.Millisecond * 1000, GatewayURL: gURL, PrintResponse: true, - } -} - -func getTopics() []string { - topics := []string{} - t, ok := os.LookupEnv("topics") - if !ok { - log.Print("No topics given in environment variable") - } - - topics = strings.Split(t, ",") - return topics + }, nil } diff --git a/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/LICENSE b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/LICENSE new file mode 100644 index 0000000..08c5fb7 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2017-2018 Alex Ellis +Copyright (c) 2017-2018 OpenFaaS Project + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/controller.go b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/controller.go new file mode 100644 index 0000000..4b6fa96 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/controller.go @@ -0,0 +1,78 @@ +package types + +import ( + "log" + "time" + + "github.com/openfaas/faas-provider/auth" +) + +// ControllerConfig configures a connector SDK controller +type ControllerConfig struct { + UpstreamTimeout time.Duration + GatewayURL string + PrintResponse bool + RebuildInterval time.Duration +} + +// Controller for the connector SDK +type Controller struct { + Config *ControllerConfig + Invoker *Invoker + TopicMap *TopicMap + Credentials *auth.BasicAuthCredentials +} + +// NewController create a new connector SDK controller +func NewController(credentials *auth.BasicAuthCredentials, config *ControllerConfig) *Controller { + + invoker := Invoker{ + PrintResponse: config.PrintResponse, + Client: MakeClient(config.UpstreamTimeout), + GatewayURL: config.GatewayURL, + } + topicMap := NewTopicMap() + + return &Controller{ + Config: config, + Invoker: &invoker, + TopicMap: &topicMap, + Credentials: credentials, + } +} + +// Invoke attempts to invoke any functions which match the +// topic the incoming message was published on. +func (c *Controller) Invoke(topic string, message *[]byte) { + c.Invoker.Invoke(c.TopicMap, topic, message) +} + +// BeginMapBuilder begins to build a map of function->topic by +// querying the API gateway. +func (c *Controller) BeginMapBuilder() { + + lookupBuilder := FunctionLookupBuilder{ + GatewayURL: c.Config.GatewayURL, + Client: MakeClient(c.Config.UpstreamTimeout), + Credentials: c.Credentials, + } + + ticker := time.NewTicker(c.Config.RebuildInterval) + go synchronizeLookups(ticker, &lookupBuilder, c.TopicMap) +} + +func synchronizeLookups(ticker *time.Ticker, + lookupBuilder *FunctionLookupBuilder, + topicMap *TopicMap) { + + for { + <-ticker.C + lookups, err := lookupBuilder.Build() + if err != nil { + log.Fatalln(err) + } + + log.Println("Syncing topic map") + topicMap.Sync(&lookups) + } +} diff --git a/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/credentials.go b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/credentials.go new file mode 100644 index 0000000..bf4684d --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/credentials.go @@ -0,0 +1,29 @@ +package types + +import ( + "os" + + "github.com/openfaas/faas-provider/auth" +) + +func GetCredentials() *auth.BasicAuthCredentials { + var credentials *auth.BasicAuthCredentials + + if val, ok := os.LookupEnv("basic_auth"); ok && len(val) > 0 { + if val == "true" || val == "1" { + + reader := auth.ReadBasicAuthFromDisk{} + + if val, ok := os.LookupEnv("secret_mount_path"); ok && len(val) > 0 { + reader.SecretMountPath = os.Getenv("secret_mount_path") + } + + res, err := reader.Read() + if err != nil { + panic(err) + } + credentials = res + } + } + return credentials +} diff --git a/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/function_list_builder.go b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/function_list_builder.go new file mode 100644 index 0000000..0f4ffc1 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/function_list_builder.go @@ -0,0 +1,69 @@ +// Copyright (c) OpenFaaS Project 2018. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +package types + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + "github.com/openfaas/faas-provider/auth" + "github.com/openfaas/faas/gateway/requests" +) + +// FunctionLookupBuilder builds a list of OpenFaaS functions +type FunctionLookupBuilder struct { + GatewayURL string + Client *http.Client + Credentials *auth.BasicAuthCredentials +} + +// Build compiles a map of topic names and functions that have +// advertised to receive messages on said topic +func (s *FunctionLookupBuilder) Build() (map[string][]string, error) { + var err error + serviceMap := make(map[string][]string) + + req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/system/functions", s.GatewayURL), nil) + + if s.Credentials != nil { + req.SetBasicAuth(s.Credentials.User, s.Credentials.Password) + } + + res, reqErr := s.Client.Do(req) + + if reqErr != nil { + return serviceMap, reqErr + } + + if res.Body != nil { + defer res.Body.Close() + } + + bytesOut, _ := ioutil.ReadAll(res.Body) + + functions := []requests.Function{} + marshalErr := json.Unmarshal(bytesOut, &functions) + + if marshalErr != nil { + return serviceMap, marshalErr + } + + for _, function := range functions { + if function.Annotations != nil { + annotations := *function.Annotations + + if topic, pass := annotations["topic"]; pass { + + if serviceMap[topic] == nil { + serviceMap[topic] = []string{} + } + serviceMap[topic] = append(serviceMap[topic], function.Name) + } + } + } + + return serviceMap, err +} diff --git a/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/invoker.go b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/invoker.go new file mode 100644 index 0000000..1170972 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/invoker.go @@ -0,0 +1,82 @@ +package types + +import ( + "bytes" + "fmt" + "io" + "io/ioutil" + "log" + "net/http" +) + +type Invoker struct { + PrintResponse bool + Client *http.Client + GatewayURL string +} + +func (i *Invoker) Invoke(topicMap *TopicMap, topic string, message *[]byte) { + if len(*message) > 0 { + + matchedFunctions := topicMap.Match(topic) + for _, matchedFunction := range matchedFunctions { + + log.Printf("Invoke function: %s", matchedFunction) + + gwURL := fmt.Sprintf("%s/function/%s", i.GatewayURL, matchedFunction) + reader := bytes.NewReader(*message) + + body, statusCode, doErr := invokefunction(i.Client, gwURL, reader) + + if doErr != nil { + log.Printf("Unable to invoke from %s, error: %s\n", matchedFunction, doErr) + return + } + + printBody := false + stringOutput := "" + + if body != nil && i.PrintResponse { + stringOutput = string(*body) + printBody = true + } + + if printBody { + log.Printf("Response [%d] from %s %s", statusCode, matchedFunction, stringOutput) + + } else { + log.Printf("Response [%d] from %s", statusCode, matchedFunction) + } + } + } +} + +func invokefunction(c *http.Client, gwURL string, reader io.Reader) (*[]byte, int, error) { + + httpReq, _ := http.NewRequest(http.MethodPost, gwURL, reader) + + if httpReq.Body != nil { + defer httpReq.Body.Close() + } + + var body *[]byte + + res, doErr := c.Do(httpReq) + if doErr != nil { + return nil, http.StatusServiceUnavailable, doErr + } + + if res.Body != nil { + defer res.Body.Close() + + bytesOut, readErr := ioutil.ReadAll(res.Body) + if readErr != nil { + log.Printf("Error reading body") + return nil, http.StatusServiceUnavailable, doErr + + } + body = &bytesOut + } + + return body, res.StatusCode, doErr +} diff --git a/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/make_client.go b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/make_client.go new file mode 100644 index 0000000..a380f80 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/make_client.go @@ -0,0 +1,22 @@ +package types + +import ( + "net" + "net/http" + "time" +) + +func MakeClient(timeout time.Duration) *http.Client { + return &http.Client{ + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: timeout, + KeepAlive: 10 * time.Second, + }).DialContext, + MaxIdleConns: 100, + MaxIdleConnsPerHost: 100, + IdleConnTimeout: 120 * time.Millisecond, + }, + } +} diff --git a/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/topic_map.go b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/topic_map.go new file mode 100644 index 0000000..684d928 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas-incubator/connector-sdk/types/topic_map.go @@ -0,0 +1,42 @@ +// Copyright (c) OpenFaaS Project 2018. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +package types + +import "sync" + +func NewTopicMap() TopicMap { + lookup := make(map[string][]string) + return TopicMap{ + lookup: &lookup, + lock: sync.Mutex{}, + } +} + +type TopicMap struct { + lookup *map[string][]string + lock sync.Mutex +} + +func (t *TopicMap) Match(topicName string) []string { + t.lock.Lock() + defer t.lock.Unlock() + + var values []string + + for key, val := range *t.lookup { + if key == topicName { + values = val + break + } + } + + return values +} + +func (t *TopicMap) Sync(updated *map[string][]string) { + t.lock.Lock() + defer t.lock.Unlock() + + t.lookup = updated +} diff --git a/cmd/tester/vendor/github.com/openfaas/faas-provider/LICENSE b/cmd/tester/vendor/github.com/openfaas/faas-provider/LICENSE new file mode 100644 index 0000000..e547e74 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas/faas-provider/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Alex Ellis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/cmd/tester/vendor/github.com/openfaas/faas-provider/auth/basic_auth.go b/cmd/tester/vendor/github.com/openfaas/faas-provider/auth/basic_auth.go new file mode 100644 index 0000000..3fd7d75 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas/faas-provider/auth/basic_auth.go @@ -0,0 +1,26 @@ +// Copyright (c) OpenFaaS Author(s). All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +package auth + +import ( + "net/http" +) + +// DecorateWithBasicAuth enforces basic auth as a middleware with given credentials +func DecorateWithBasicAuth(next http.HandlerFunc, credentials *BasicAuthCredentials) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + user, password, ok := r.BasicAuth() + w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) + + if !ok || !(credentials.Password == password && user == credentials.User) { + + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte("invalid credentials")) + return + } + + next.ServeHTTP(w, r) + } +} diff --git a/cmd/tester/vendor/github.com/openfaas/faas-provider/auth/credentials.go b/cmd/tester/vendor/github.com/openfaas/faas-provider/auth/credentials.go new file mode 100644 index 0000000..4f2ca34 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas/faas-provider/auth/credentials.go @@ -0,0 +1,52 @@ +// Copyright (c) OpenFaaS Author(s). All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +package auth + +import ( + "fmt" + "io/ioutil" + "path" + "strings" +) + +// BasicAuthCredentials for credentials +type BasicAuthCredentials struct { + User string + Password string +} + +type ReadBasicAuth interface { + Read() (error, *BasicAuthCredentials) +} + +type ReadBasicAuthFromDisk struct { + SecretMountPath string +} + +func (r *ReadBasicAuthFromDisk) Read() (*BasicAuthCredentials, error) { + var credentials *BasicAuthCredentials + + if len(r.SecretMountPath) == 0 { + return nil, fmt.Errorf("invalid SecretMountPath specified for reading secrets") + } + + userPath := path.Join(r.SecretMountPath, "basic-auth-user") + user, userErr := ioutil.ReadFile(userPath) + if userErr != nil { + return nil, fmt.Errorf("unable to load %s", userPath) + } + + userPassword := path.Join(r.SecretMountPath, "basic-auth-password") + password, passErr := ioutil.ReadFile(userPassword) + if passErr != nil { + return nil, fmt.Errorf("Unable to load %s", userPassword) + } + + credentials = &BasicAuthCredentials{ + User: strings.TrimSpace(string(user)), + Password: strings.TrimSpace(string(password)), + } + + return credentials, nil +} diff --git a/cmd/tester/vendor/github.com/openfaas/faas/LICENSE b/cmd/tester/vendor/github.com/openfaas/faas/LICENSE new file mode 100644 index 0000000..fc538a1 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas/faas/LICENSE @@ -0,0 +1,23 @@ +MIT License + +Copyright (c) 2016-2018 Alex Ellis +Copyright (c) 2018 OpenFaaS Author(s) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/forward_request.go b/cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/forward_request.go new file mode 100644 index 0000000..adc573c --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/forward_request.go @@ -0,0 +1,29 @@ +package requests + +import "fmt" +import "net/url" + +// ForwardRequest for proxying incoming requests +type ForwardRequest struct { + RawPath string + RawQuery string + Method string +} + +// NewForwardRequest create a ForwardRequest +func NewForwardRequest(method string, url url.URL) ForwardRequest { + return ForwardRequest{ + Method: method, + RawQuery: url.RawQuery, + RawPath: url.Path, + } +} + +// ToURL create formatted URL +func (f *ForwardRequest) ToURL(addr string, watchdogPort int) string { + if len(f.RawQuery) > 0 { + return fmt.Sprintf("http://%s:%d%s?%s", addr, watchdogPort, f.RawPath, f.RawQuery) + } + return fmt.Sprintf("http://%s:%d%s", addr, watchdogPort, f.RawPath) + +} diff --git a/cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/prometheus.go b/cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/prometheus.go new file mode 100644 index 0000000..1bb41f7 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/prometheus.go @@ -0,0 +1,23 @@ +// Copyright (c) Alex Ellis 2017. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +package requests + +// PrometheusInnerAlertLabel PrometheusInnerAlertLabel +type PrometheusInnerAlertLabel struct { + AlertName string `json:"alertname"` + FunctionName string `json:"function_name"` +} + +// PrometheusInnerAlert PrometheusInnerAlert +type PrometheusInnerAlert struct { + Status string `json:"status"` + Labels PrometheusInnerAlertLabel `json:"labels"` +} + +// PrometheusAlert as produced by AlertManager +type PrometheusAlert struct { + Status string `json:"status"` + Receiver string `json:"receiver"` + Alerts []PrometheusInnerAlert `json:"alerts"` +} diff --git a/cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/requests.go b/cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/requests.go new file mode 100644 index 0000000..5c8f174 --- /dev/null +++ b/cmd/tester/vendor/github.com/openfaas/faas/gateway/requests/requests.go @@ -0,0 +1,98 @@ +// Copyright (c) Alex Ellis 2017. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +// Package requests package provides a client SDK or library for +// the OpenFaaS gateway REST API +package requests + +// CreateFunctionRequest create a function in the swarm. +type CreateFunctionRequest struct { + + // Service corresponds to a Docker Service + Service string `json:"service"` + + // Image corresponds to a Docker image + Image string `json:"image"` + + // Network is specific to Docker Swarm - default overlay network is: func_functions + Network string `json:"network"` + + // EnvProcess corresponds to the fprocess variable for your container watchdog. + EnvProcess string `json:"envProcess"` + + // EnvVars provides overrides for functions. + EnvVars map[string]string `json:"envVars"` + + // RegistryAuth is the registry authentication (optional) + // in the same encoded format as Docker native credentials + // (see ~/.docker/config.json) + RegistryAuth string `json:"registryAuth,omitempty"` + + // Constraints are specific to back-end orchestration platform + Constraints []string `json:"constraints"` + + // Secrets list of secrets to be made available to function + Secrets []string `json:"secrets"` + + // Labels are metadata for functions which may be used by the + // back-end for making scheduling or routing decisions + Labels *map[string]string `json:"labels"` + + // Annotations are metadata for functions which may be used by the + // back-end for management, orchestration, events and build tasks + Annotations *map[string]string `json:"annotations"` + + // Limits for function + Limits *FunctionResources `json:"limits"` + + // Requests of resources requested by function + Requests *FunctionResources `json:"requests"` + + // ReadOnlyRootFilesystem removes write-access from the root filesystem + // mount-point. + ReadOnlyRootFilesystem bool `json:"readOnlyRootFilesystem"` +} + +// FunctionResources Memory and CPU +type FunctionResources struct { + Memory string `json:"memory"` + CPU string `json:"cpu"` +} + +// Function exported for system/functions endpoint +type Function struct { + Name string `json:"name"` + Image string `json:"image"` + InvocationCount float64 `json:"invocationCount"` // TODO: shouldn't this be int64? + Replicas uint64 `json:"replicas"` + EnvProcess string `json:"envProcess"` + + // AvailableReplicas is the count of replicas ready to receive invocations as reported by the back-end + AvailableReplicas uint64 `json:"availableReplicas"` + + // Labels are metadata for functions which may be used by the + // back-end for making scheduling or routing decisions + Labels *map[string]string `json:"labels"` + + // Annotations are metadata for functions which may be used by the + // back-end for management, orchestration, events and build tasks + Annotations *map[string]string `json:"annotations"` +} + +// AsyncReport is the report from a function executed on a queue worker. +type AsyncReport struct { + FunctionName string `json:"name"` + StatusCode int `json:"statusCode"` + TimeTaken float64 `json:"timeTaken"` +} + +// DeleteFunctionRequest delete a deployed function +type DeleteFunctionRequest struct { + FunctionName string `json:"functionName"` +} + +// Secret for underlying orchestrator +type Secret struct { + Name string `json:"name"` + Value string `json:"value,omitempty"` +} diff --git a/cmd/tester/yaml/docker-compose.yml b/cmd/tester/yaml/docker-compose.yml index 7afd1c7..4ab6a50 100644 --- a/cmd/tester/yaml/docker-compose.yml +++ b/cmd/tester/yaml/docker-compose.yml @@ -1,12 +1,10 @@ version: '3.2' services: connector: - image: zeerorg/connector-sample + image: zeerorg/connector-sample:1.0 hostname: sample-connector environment: gateway_url: http://gateway:8080 - topics: "faas-request" - print_response: "true" basic_auth: "true" secret_mount_path: "/run/secrets/" secrets: diff --git a/cmd/tester/yaml/kubernetes/connector-dep.yml b/cmd/tester/yaml/kubernetes/connector-dep.yml index 6bb8513..adaf450 100644 --- a/cmd/tester/yaml/kubernetes/connector-dep.yml +++ b/cmd/tester/yaml/kubernetes/connector-dep.yml @@ -17,16 +17,12 @@ spec: spec: containers: - name: sample-connector - image: zeerorg/connector-sample + image: zeerorg/connector-sample:1.0 env: - name: gateway_url value: "http://gateway.openfaas:8080" - - name: topics - value: "faas-request" - - name: print_response - value: "true" - name: basic_auth - value: "false" + value: "true" - name: secret_mount_path value: "/var/secrets/" volumeMounts: