diff --git a/cmd/cape/cmd/delete.go b/cmd/cape/cmd/delete.go index f9df813..3e8c5ff 100644 --- a/cmd/cape/cmd/delete.go +++ b/cmd/cape/cmd/delete.go @@ -1,6 +1,7 @@ package cmd import ( + "encoding/json" "fmt" "net/http" @@ -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 { @@ -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) } @@ -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) } diff --git a/cmd/cape/cmd/deploy.go b/cmd/cape/cmd/deploy.go index 83f9fa5..daa32fc 100644 --- a/cmd/cape/cmd/deploy.go +++ b/cmd/cape/cmd/deploy.go @@ -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 } diff --git a/cmd/cape/cmd/key.go b/cmd/cape/cmd/key.go index a379e43..b39b29a 100644 --- a/cmd/cape/cmd/key.go +++ b/cmd/cape/cmd/key.go @@ -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 } @@ -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, diff --git a/cmd/cape/cmd/list.go b/cmd/cape/cmd/list.go index c91a00f..bb611e1 100644 --- a/cmd/cape/cmd/list.go +++ b/cmd/cape/cmd/list.go @@ -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 { @@ -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) diff --git a/cmd/cape/cmd/login.go b/cmd/cape/cmd/login.go index eca8264..4f988bc 100644 --- a/cmd/cape/cmd/login.go +++ b/cmd/cape/cmd/login.go @@ -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") diff --git a/cmd/cape/cmd/token.go b/cmd/cape/cmd/token.go index 97dfbd5..49920a7 100644 --- a/cmd/cape/cmd/token.go +++ b/cmd/cape/cmd/token.go @@ -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) } } @@ -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) } }