Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
Added secret handler
Browse files Browse the repository at this point in the history
Signed-off-by: Bart Smykla <bsmykla@vmware.com>
  • Loading branch information
Bart Smykla committed Jan 8, 2019
1 parent 013a759 commit 948f29a
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 9 deletions.
12 changes: 6 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Expand Up @@ -12,11 +12,11 @@

[[constraint]]
name = "github.com/openfaas/faas"
version = "0.8.8"
version = "0.9.14"

[[constraint]]
name = "github.com/openfaas/faas-provider"
version = "0.8.0"
version = "0.8.1"

# match docker/distribution revision with moby
[[override]]
Expand Down
244 changes: 244 additions & 0 deletions handlers/secrets.go
Expand Up @@ -2,13 +2,24 @@ package handlers

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/docker/cli/opts"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"

"github.com/openfaas/faas/gateway/requests"
)

var (
ownerLabel = "com.openfaas.owner"
ownerLabelValue = "openfaas"
)

func makeSecretsArray(c *client.Client, secretNames []string) ([]*swarm.SecretReference, error) {
Expand Down Expand Up @@ -75,3 +86,236 @@ func makeSecretsArray(c *client.Client, secretNames []string) ([]*swarm.SecretRe

return values, nil
}

func getSecretsWithLabel(c *client.Client, labelName string, labelValue string) ([]swarm.Secret, error) {
secrets, secretListErr := c.SecretList(context.Background(), types.SecretListOptions{})
if secretListErr != nil {
return nil, secretListErr
}

var filteredSecrets []swarm.Secret

for _, secret := range secrets {
if secret.Spec.Labels[labelName] == labelValue {
filteredSecrets = append(filteredSecrets, secret)
}
}

return filteredSecrets, nil
}

func getSecretWithName(c *client.Client, name string) (secret *swarm.Secret, err error, status int) {
secrets, secretListErr := c.SecretList(context.Background(), types.SecretListOptions{})
if secretListErr != nil {
return nil, secretListErr, http.StatusInternalServerError
}

for _, secret := range secrets {
if secret.Spec.Name == name {
if secret.Spec.Labels[ownerLabel] == ownerLabelValue {
return &secret, nil, http.StatusOK
}

return nil, fmt.Errorf(
"found secret with name: %s, but it doesn't have label: %s == %s",
name,
ownerLabel,
ownerLabelValue,
), http.StatusInternalServerError
}
}

return nil, fmt.Errorf("not found secret with name: %s", name), http.StatusNotFound
}

func getSecrets(c *client.Client, _ []byte) (responseStatus int, responseBody []byte, err error) {
secrets, err := getSecretsWithLabel(c, ownerLabel, ownerLabelValue)
if err != nil {
return http.StatusInternalServerError, nil, fmt.Errorf(
"cannot get secrets with label: %s == %s in secretGetHandler: %s",
ownerLabel,
ownerLabelValue,
err,
)
}

var results []requests.Secret

for _, s := range secrets {
results = append(results, requests.Secret{Name: s.Spec.Name})
}

resultsJson, marshalErr := json.Marshal(results)
if marshalErr != nil {
return http.StatusInternalServerError,
nil,
fmt.Errorf("error marshalling secrets to json: %s", marshalErr)

}

return http.StatusOK, resultsJson, nil
}

func createNewSecret(c *client.Client, body []byte) (responseStatus int, err error) {
var secret requests.Secret

unmarshalErr := json.Unmarshal(body, &secret)
if unmarshalErr != nil {
return http.StatusBadRequest,
fmt.Errorf("error unmarshalling body to json in secretPostHandler: %s", unmarshalErr)
}

_, createSecretErr := c.SecretCreate(context.Background(), swarm.SecretSpec{
Annotations: swarm.Annotations{
Name: secret.Name,
Labels: map[string]string{
ownerLabel: ownerLabelValue,
},
},
Data: []byte(secret.Value),
})
if createSecretErr != nil {
return http.StatusInternalServerError, fmt.Errorf(
"error creating secret in secretPostHandler: %s",
createSecretErr,
)
}

return http.StatusCreated, nil
}

func updateSecret(c *client.Client, body []byte) (responseStatus int, err error) {
var secret requests.Secret

unmarshalErr := json.Unmarshal(body, &secret)
if unmarshalErr != nil {
return http.StatusBadRequest, fmt.Errorf(
"error unmarshaling secret in secretPutHandler: %s",
unmarshalErr,
)
}

foundSecret, getSecretErr, status := getSecretWithName(c, secret.Name)
if getSecretErr != nil {
return status, fmt.Errorf(
"cannot get secret with name: %s, which you want to remove. Error: %s",
secret.Name,
getSecretErr,
)
}

nextVersion := swarm.Version{
Index: foundSecret.Version.Index + 1,
}

updateSecretErr := c.SecretUpdate(context.Background(), foundSecret.ID, nextVersion, swarm.SecretSpec{
Annotations: swarm.Annotations{
Name: secret.Name,
},
Data: []byte(secret.Value),
})
if updateSecretErr != nil {
return http.StatusInternalServerError, fmt.Errorf(
"couldn't update secret (name: %s, ID: %s) because of error: %s",
secret.Name,
foundSecret.ID,
getSecretErr,
)
}

return http.StatusOK, nil
}

func deleteSecret(c *client.Client, body []byte) (responseStatus int, err error) {
var secret requests.Secret

unmarshalErr := json.Unmarshal(body, &secret)
if unmarshalErr != nil {
return http.StatusBadRequest, fmt.Errorf(
"error unmarshaling secret in secretDeleteHandler: %s",
unmarshalErr,
)
}

foundSecret, getSecretErr, status := getSecretWithName(c, secret.Name)
if getSecretErr != nil {
return status, fmt.Errorf(
"cannot get secret with name: %s, which you want to remove. Error: %s",
secret.Name,
getSecretErr,
)
}

removeSecretErr := c.SecretRemove(context.Background(), foundSecret.ID)
if removeSecretErr != nil {
return http.StatusInternalServerError, fmt.Errorf(
"error trying to remove secret (name: `%s`, ID: `%s`): %s",
secret.Name,
foundSecret.ID,
removeSecretErr,
)
}

return http.StatusOK, nil
}

func MakeSecretsHandler(c *client.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, readBodyErr := ioutil.ReadAll(r.Body)
if readBodyErr != nil {
log.Printf("couldn't read body of a request: %s", readBodyErr)

w.WriteHeader(http.StatusInternalServerError)

return
}

if r.Body != nil {
defer r.Body.Close()
}

var (
responseStatus int
responseBody []byte
responseErr error
)

switch r.Method {
case http.MethodGet:
responseStatus, responseBody, responseErr = getSecrets(c, body)

if responseErr == nil && responseBody != nil {
_, writeErr := w.Write(responseBody)

if writeErr != nil {
log.Println("cannot write body of a response")

w.WriteHeader(http.StatusInternalServerError)

return
}
}

break
case http.MethodPost:
responseStatus, responseErr = createNewSecret(c, body)
break
case http.MethodPut:
responseStatus, responseErr = updateSecret(c, body)
break
case http.MethodDelete:
responseStatus, responseErr = deleteSecret(c, body)
break
}

if responseErr != nil {
log.Println(responseErr)

w.WriteHeader(responseStatus)

return
}

w.WriteHeader(responseStatus)
}
}
1 change: 1 addition & 0 deletions server.go
Expand Up @@ -58,6 +58,7 @@ func main() {
UpdateHandler: handlers.UpdateHandler(dockerClient, maxRestarts, restartDelay),
Health: handlers.Health(),
InfoHandler: handlers.MakeInfoHandler(version.BuildVersion(), version.GitCommit),
SecretHandler: handlers.MakeSecretsHandler(dockerClient),
}

bootstrapConfig := bootTypes.FaaSConfig{
Expand Down
3 changes: 3 additions & 0 deletions vendor/github.com/openfaas/faas-provider/serve.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/openfaas/faas-provider/types/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vendor/github.com/openfaas/faas/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/openfaas/faas/gateway/requests/requests.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 948f29a

Please sign in to comment.