Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add faas secret remove command #579

Merged
merged 1 commit into from Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 4 additions & 14 deletions commands/secret_list.go
@@ -1,4 +1,4 @@
// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved.
// Copyright (c) OpenFaaS Author(s) 2019. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package commands
Expand Down Expand Up @@ -28,20 +28,9 @@ faas-cli secret list --gateway=http://127.0.0.1:8080`,

func init() {
secretListCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://")
// secretListCmd.Flags().BoolVarP(&verboseList, "verbose", "v", false, "Verbose output for the function list")
secretListCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation")

secretCmd.AddCommand(secretListCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// secretCreateCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// secretCreateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func preRunSecretListCmd(cmd *cobra.Command, args []string) error {
Expand All @@ -58,10 +47,11 @@ func runSecretList(cmd *cobra.Command, args []string) error {
}

if len(secrets) == 0 {
fmt.Println("No secret found.")
fmt.Printf("No secrets found.\n")
return nil
}
fmt.Print(renderSecretList(secrets))

fmt.Printf("%s", renderSecretList(secrets))

return nil
}
Expand Down
60 changes: 60 additions & 0 deletions commands/secret_remove.go
@@ -0,0 +1,60 @@
// Copyright (c) OpenFaaS Author(s) 2019. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package commands

import (
"fmt"
"os"

"github.com/openfaas/faas-cli/proxy"
"github.com/openfaas/faas-cli/schema"
"github.com/spf13/cobra"
)

var secretRemoveCmd = &cobra.Command{
Use: "remove",
Aliases: []string{"rm"},
Short: "remove a secret",
Long: `Remove a secret by name`,
Example: `faas-cli secret remove NAME
faas-cli secret remove NAME --gateway=http://127.0.0.1:8080`,
RunE: runSecretRemove,
PreRunE: preRunSecretRemoveCmd,
}

func init() {
secretRemoveCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://")
secretRemoveCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation")

secretCmd.AddCommand(secretRemoveCmd)
}

func preRunSecretRemoveCmd(cmd *cobra.Command, args []string) error {

if len(args) == 0 {
return fmt.Errorf("give a name of a secret")
}

if len(args) > 1 {
return fmt.Errorf("give ONLY the name of a single secret")
}
return nil
}

func runSecretRemove(cmd *cobra.Command, args []string) error {
var gatewayAddress string
gatewayAddress = getGatewayURL(gateway, defaultGateway, "", os.Getenv(openFaaSURLEnvironment))

secret := schema.Secret{
Name: args[0],
}
err := proxy.RemoveSecret(gatewayAddress, secret, tlsInsecure)
if err != nil {
return err
}

fmt.Print("Removed.. OK.\n")

return nil
}
42 changes: 42 additions & 0 deletions commands/secret_remove_test.go
@@ -0,0 +1,42 @@
// Copyright (c) OpenFaaS Author(s) 2019. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package commands

import (
"testing"
)

func Test_preRunSecretRemoveCmd_NoArgs_Fails(t *testing.T) {

res := preRunSecretRemoveCmd(nil, []string{})

want := "give a name of a secret"
if res.Error() != want {
t.Errorf("want %q, got %q", want, res.Error())
}
}

func Test_preRunSecretRemoveCmd_MoreThan1Arg_Fails(t *testing.T) {

res := preRunSecretRemoveCmd(nil, []string{
"secret1",
"secret2",
})

want := "give ONLY the name of a single secret"
if res.Error() != want {
t.Errorf("want %q, got %q", want, res.Error())
}
}

func Test_preRunSecretRemoveCmd_ExtactlyOneArgIsFine(t *testing.T) {

res := preRunSecretRemoveCmd(nil, []string{
"secret1",
})

if res != nil {
t.Errorf("expected no validation error, but got %q", res.Error())
}
}
49 changes: 49 additions & 0 deletions proxy/secret.go
@@ -1,6 +1,7 @@
package proxy

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -64,3 +65,51 @@ func GetSecretList(gateway string, tlsInsecure bool) ([]schema.Secret, error) {

return results, nil
}

// RemoveSecret remove a secret via the OpenFaaS API by name
func RemoveSecret(gateway string, secret schema.Secret, tlsInsecure bool) error {

if !tlsInsecure {
if !strings.HasPrefix(gateway, "https") {
fmt.Println("WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.")
}
}

gateway = strings.TrimRight(gateway, "/")
client := MakeHTTPClient(&defaultCommandTimeout, tlsInsecure)

body, _ := json.Marshal(secret)

getRequest, err := http.NewRequest(http.MethodDelete, gateway+"/system/secrets", bytes.NewBuffer(body))
SetAuth(getRequest, gateway)

if err != nil {
return fmt.Errorf("cannot connect to OpenFaaS on URL: %s", gateway)
}

res, err := client.Do(getRequest)
if err != nil {
return fmt.Errorf("cannot connect to OpenFaaS on URL: %s", gateway)
}

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

switch res.StatusCode {
case http.StatusOK, http.StatusAccepted:
break
case http.StatusNotFound:
return fmt.Errorf("unable to find secret: %s", secret.Name)
case http.StatusUnauthorized:
return fmt.Errorf("unauthorized access, run \"faas-cli login\" to setup authentication for this server")

default:
bytesOut, err := ioutil.ReadAll(res.Body)
if err == nil {
return fmt.Errorf("server returned unexpected status code: %d - %s", res.StatusCode, string(bytesOut))
}
}

return nil
}