Skip to content

Commit

Permalink
export dashboards: add option to skip TLS verification (#468)
Browse files Browse the repository at this point in the history
* export dashboards: add option to skip TLS verification

* Address PR comments

* Fix
  • Loading branch information
mtojek authored Aug 10, 2021
1 parent fffc3e3 commit e65051d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
9 changes: 8 additions & 1 deletion cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func setupExportCommand() *cobraext.Command {
RunE: exportDashboardsCmd,
}
exportDashboardCmd.Flags().StringSliceP(cobraext.DashboardIDsFlagName, "d", nil, cobraext.DashboardIDsFlagDescription)
exportDashboardCmd.Flags().Bool(cobraext.TLSSkipVerifyFlagName, false, cobraext.TLSSkipVerifyFlagDescription)

cmd := &cobra.Command{
Use: "export",
Expand All @@ -52,7 +53,13 @@ func exportDashboardsCmd(cmd *cobra.Command, args []string) error {

common.TrimStringSlice(dashboardIDs)

kibanaClient, err := kibana.NewClient()
var opts []kibana.ClientOption
tlsSkipVerify, _ := cmd.Flags().GetBool(cobraext.TLSSkipVerifyFlagName)
if tlsSkipVerify {
opts = append(opts, kibana.TLSSkipVerify())
}

kibanaClient, err := kibana.NewClient(opts...)
if err != nil {
return errors.Wrap(err, "can't create Kibana client")
}
Expand Down
3 changes: 3 additions & 0 deletions internal/cobraext/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const (
SkipPullRequestFlagName = "skip-pull-request"
SkipPullRequestFlagDescription = "skip opening a new pull request"

TLSSkipVerifyFlagName = "tls-skip-verify"
TLSSkipVerifyFlagDescription = "skip TLS verify"

StackServicesFlagName = "services"
StackServicesFlagDescription = "component services (comma-separated values: \"%s\")"

Expand Down
33 changes: 28 additions & 5 deletions internal/kibana/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ package kibana

import (
"bytes"
"crypto/tls"
"io"
"net/http"
"net/url"
"os"

"github.com/elastic/elastic-package/internal/install"

"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/stack"
)
Expand All @@ -24,10 +24,15 @@ type Client struct {
host string
username string
password string

tlSkipVerify bool
}

// ClientOption is functional option modifying Kibana client.
type ClientOption func(*Client)

// NewClient creates a new instance of the client.
func NewClient() (*Client, error) {
func NewClient(opts ...ClientOption) (*Client, error) {
host := os.Getenv(stack.KibanaHostEnv)
if host == "" {
return nil, stack.UndefinedEnvError(stack.KibanaHostEnv)
Expand All @@ -36,11 +41,23 @@ func NewClient() (*Client, error) {
username := os.Getenv(stack.ElasticsearchUsernameEnv)
password := os.Getenv(stack.ElasticsearchPasswordEnv)

return &Client{
c := &Client{
host: host,
username: username,
password: password,
}, nil
}

for _, opt := range opts {
opt(c)
}
return c, nil
}

// TLSSkipVerify option disables TLS verification.
func TLSSkipVerify() ClientOption {
return func(c *Client) {
c.tlSkipVerify = true
}
}

func (c *Client) get(resourcePath string) (int, []byte, error) {
Expand Down Expand Up @@ -85,6 +102,12 @@ func (c *Client) sendRequest(method, resourcePath string, body []byte) (int, []b
req.Header.Add("kbn-xsrf", install.DefaultStackVersion)

client := http.Client{}
if c.tlSkipVerify {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}

resp, err := client.Do(req)
if err != nil {
return 0, nil, errors.Wrap(err, "could not send request to Kibana API")
Expand Down

0 comments on commit e65051d

Please sign in to comment.