|
| 1 | +// |
| 2 | +// Author:: Salim Afiune Maya (<[email protected]>) |
| 3 | +// Copyright:: Copyright 2020, Lacework Inc. |
| 4 | +// License:: Apache License, Version 2.0 |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +package cmd |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/pkg/errors" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + |
| 28 | + "github.com/lacework/go-sdk/api" |
| 29 | + "github.com/lacework/go-sdk/internal/array" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + // list of valid API methods |
| 34 | + validApiMethods = []string{"get", "post", "delete", "patch"} |
| 35 | + |
| 36 | + // data to send for POST/PATCH request |
| 37 | + apiData string |
| 38 | + |
| 39 | + // apiCmd represents the api command |
| 40 | + apiCmd = &cobra.Command{ |
| 41 | + Use: "api <method> <path>", |
| 42 | + Short: "Helper to call Lacework's ResfulAPI", |
| 43 | + Long: `Use this helper to call any available Lacework API endpoint. |
| 44 | +
|
| 45 | +An example, list all integrations configured in your account: |
| 46 | +
|
| 47 | + lacework-cli api get /external/integrations |
| 48 | +
|
| 49 | +For a complete list of available API endpoints visit: |
| 50 | +
|
| 51 | + https://<ACCOUNT>.lacework.net/api/v1/external/docs |
| 52 | +`, |
| 53 | + Args: argsApiValidator, |
| 54 | + RunE: runApiCommand, |
| 55 | + } |
| 56 | +) |
| 57 | + |
| 58 | +func init() { |
| 59 | + rootCmd.AddCommand(apiCmd) |
| 60 | + |
| 61 | + apiCmd.Flags().StringVarP(&apiData, |
| 62 | + "data", "d", "", |
| 63 | + "data to send only for post and patch requests", |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +func runApiCommand(cmd *cobra.Command, args []string) error { |
| 68 | + switch args[0] { |
| 69 | + case "post", "patch": |
| 70 | + if apiData == "" { |
| 71 | + return fmt.Errorf("missing '--data' parameter for post or patch requests") |
| 72 | + } |
| 73 | + case "delete", "get": |
| 74 | + if apiData != "" { |
| 75 | + return fmt.Errorf("use '--data' only for post and patch requests") |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + lacework, err := api.NewClient(cli.Account, |
| 80 | + api.WithApiKeys(cli.KeyID, cli.Secret), |
| 81 | + ) |
| 82 | + if err != nil { |
| 83 | + return errors.Wrap(err, "unable to generate Lacework API client") |
| 84 | + } |
| 85 | + |
| 86 | + cli.Log.Debugw("api client generated", |
| 87 | + "version", lacework.ApiVersion(), |
| 88 | + "base_url", lacework.URL(), |
| 89 | + ) |
| 90 | + |
| 91 | + response := new(map[string]interface{}) |
| 92 | + err = lacework.RequestDecoder( |
| 93 | + strings.ToUpper(args[0]), |
| 94 | + strings.TrimPrefix(args[1], "/"), |
| 95 | + strings.NewReader(apiData), |
| 96 | + response, |
| 97 | + ) |
| 98 | + if err != nil { |
| 99 | + return errors.Wrap(err, "unable to send the request") |
| 100 | + } |
| 101 | + |
| 102 | + pretty, err := cli.JsonF.Marshal(*response) |
| 103 | + if err != nil { |
| 104 | + cli.Log.Debugw("api response", "raw", response) |
| 105 | + return errors.Wrap(err, "unable to format json response") |
| 106 | + } |
| 107 | + |
| 108 | + fmt.Println(string(pretty)) |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func argsApiValidator(_ *cobra.Command, args []string) error { |
| 113 | + if len(args) != 2 { |
| 114 | + return errors.New("requires 2 argument. (method and path)") |
| 115 | + } |
| 116 | + if !array.ContainsStr(validApiMethods, args[0]) { |
| 117 | + return fmt.Errorf( |
| 118 | + "invalid method specified: '%s' (valid methods are %s)", |
| 119 | + args[0], validApiMethods, |
| 120 | + ) |
| 121 | + } |
| 122 | + return nil |
| 123 | +} |
0 commit comments