Skip to content

Commit

Permalink
Merge pull request #263 from capeprivacy/justin/delete-token-flag
Browse files Browse the repository at this point in the history
allow tokens for the delete cmd
  • Loading branch information
justin1121 authored Mar 29, 2023
2 parents 9408526 + 3a77c13 commit 0d36eb2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
29 changes: 24 additions & 5 deletions cmd/cape/cmd/delete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"fmt"
"net/http"

Expand All @@ -26,6 +27,8 @@ var deleteCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(deleteCmd)

deleteCmd.PersistentFlags().StringP("token", "t", "", "authorization token to use")
}

func delete(cmd *cobra.Command, args []string) error {
Expand All @@ -38,12 +41,18 @@ func delete(cmd *cobra.Command, args []string) error {

functionID := args[0]

t, err := authToken()
if err != nil {
return err
token, _ := cmd.Flags().GetString("token")
if token == "" {
t, err := authToken()
if err != nil {
return err
}

token = t
}
auth := entities.FunctionAuth{Type: entities.AuthenticationTypeUserToken, Token: t}
err = doDelete(u, functionID, insecure, auth)

auth := entities.FunctionAuth{Type: entities.AuthenticationTypeUserToken, Token: token}
err := doDelete(u, functionID, insecure, auth)
if err != nil {
return fmt.Errorf("delete failed: %w", err)
}
Expand Down Expand Up @@ -71,6 +80,16 @@ func doDelete(url string, functionID string, insecure bool, auth entities.Functi
if res.StatusCode == 404 {
return fmt.Errorf("function ID not found: %s", functionID)
}

var errMsg ErrorMsg
if err := json.NewDecoder(res.Body).Decode(&errMsg); err != nil {
return fmt.Errorf("expected 200, got server response code %d and could not decode err msg", res.StatusCode)
}

if errMsg.Error() != "" {
return errMsg
}

return fmt.Errorf("expected 200, got server response code %d", res.StatusCode)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cape/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func doDeploy(url string, token string, functionInput string, functionName strin
reader = bytes.NewBuffer(buf)
}

keyReq, err := GetKeyRequest(pcrSlice)
keyReq, err := GetKeyRequest(pcrSlice, token)
if err != nil {
return "", nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/cape/cmd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ func key(cmd *cobra.Command, args []string) error {
return UserError{Msg: "error retrieving pcr flags", Err: err}
}

keyReq, err := GetKeyRequest(pcrSlice)
token, err := getAuthToken()
if err != nil {
return err
}

keyReq, err := GetKeyRequest(pcrSlice, token)
if err != nil {
return err
}
Expand Down Expand Up @@ -68,16 +73,11 @@ func key(cmd *cobra.Command, args []string) error {
return nil
}

func GetKeyRequest(pcrSlice []string) (sdk.KeyRequest, error) {
t, err := getAuthToken()
if err != nil {
return sdk.KeyRequest{}, err
}

func GetKeyRequest(pcrSlice []string, token string) (sdk.KeyRequest, error) {
return sdk.KeyRequest{
URL: C.EnclaveHost,
Insecure: C.Insecure,
FunctionAuth: entities.FunctionAuth{Type: entities.AuthenticationTypeUserToken, Token: t},
FunctionAuth: entities.FunctionAuth{Type: entities.AuthenticationTypeUserToken, Token: token},
ConfigDir: C.LocalConfigDir,
CapeKeyFile: C.LocalCapeKeyFileName,
PcrSlice: pcrSlice,
Expand Down
8 changes: 6 additions & 2 deletions cmd/cape/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import (
)

type ErrorMsg struct {
Error string `json:"error"`
ErrorMsg string `json:"error"`
}

func (e ErrorMsg) Error() string {
return e.ErrorMsg
}

type ErrServerForList struct {
Expand Down Expand Up @@ -116,7 +120,7 @@ func doList(url string, insecure bool, auth entities.FunctionAuth, limit int, of
return ErrUnauthorized
}

return ErrServerForList{res.StatusCode, e.Error}
return ErrServerForList{res.StatusCode, e.ErrorMsg}
}

body, err := io.ReadAll(res.Body)
Expand Down
10 changes: 5 additions & 5 deletions cmd/cape/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ func login(cmd *cobra.Command, args []string) error {
return err
}

keyReq, err := GetKeyRequest([]string{})
authToken, err := getAuthToken()
if err != nil {
return err
}

_, err = sdk.Key(keyReq)
keyReq, err := GetKeyRequest([]string{}, authToken)
if err != nil {
fmt.Println("Unable to fetch cape key, try running 'cape key' after login")
return err
}

authToken, err := getAuthToken()
_, err = sdk.Key(keyReq)
if err != nil {
return err
fmt.Println("Unable to fetch cape key, try running 'cape key' after login")
}

customerID, _ := cmd.Flags().GetString("link-aws-account")
Expand Down
4 changes: 2 additions & 2 deletions cmd/cape/cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ You can set an expiry of the token with the --expiry flag.
if err := json.NewDecoder(resp.Body).Decode(&errMsg); err != nil {
fmt.Println("could not decode response body")
} else {
fmt.Println("received server error: ", errMsg.Error)
fmt.Printf("received server error: %s", errMsg)
}
}

Expand Down Expand Up @@ -276,7 +276,7 @@ var deleteTokenCmd = &cobra.Command{
if err := json.NewDecoder(resp.Body).Decode(&errMsg); err != nil {
fmt.Println("could not decode response body")
} else {
fmt.Println("received server error: ", errMsg.Error)
fmt.Printf("received server error: %s\n", errMsg)
}
}

Expand Down

0 comments on commit 0d36eb2

Please sign in to comment.