Skip to content

Commit e65051d

Browse files
authored
export dashboards: add option to skip TLS verification (#468)
* export dashboards: add option to skip TLS verification * Address PR comments * Fix
1 parent fffc3e3 commit e65051d

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

cmd/export.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func setupExportCommand() *cobraext.Command {
3131
RunE: exportDashboardsCmd,
3232
}
3333
exportDashboardCmd.Flags().StringSliceP(cobraext.DashboardIDsFlagName, "d", nil, cobraext.DashboardIDsFlagDescription)
34+
exportDashboardCmd.Flags().Bool(cobraext.TLSSkipVerifyFlagName, false, cobraext.TLSSkipVerifyFlagDescription)
3435

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

5354
common.TrimStringSlice(dashboardIDs)
5455

55-
kibanaClient, err := kibana.NewClient()
56+
var opts []kibana.ClientOption
57+
tlsSkipVerify, _ := cmd.Flags().GetBool(cobraext.TLSSkipVerifyFlagName)
58+
if tlsSkipVerify {
59+
opts = append(opts, kibana.TLSSkipVerify())
60+
}
61+
62+
kibanaClient, err := kibana.NewClient(opts...)
5663
if err != nil {
5764
return errors.Wrap(err, "can't create Kibana client")
5865
}

internal/cobraext/const.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ const (
6363
SkipPullRequestFlagName = "skip-pull-request"
6464
SkipPullRequestFlagDescription = "skip opening a new pull request"
6565

66+
TLSSkipVerifyFlagName = "tls-skip-verify"
67+
TLSSkipVerifyFlagDescription = "skip TLS verify"
68+
6669
StackServicesFlagName = "services"
6770
StackServicesFlagDescription = "component services (comma-separated values: \"%s\")"
6871

internal/kibana/client.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ package kibana
66

77
import (
88
"bytes"
9+
"crypto/tls"
910
"io"
1011
"net/http"
1112
"net/url"
1213
"os"
1314

14-
"github.com/elastic/elastic-package/internal/install"
15-
1615
"github.com/pkg/errors"
1716

17+
"github.com/elastic/elastic-package/internal/install"
1818
"github.com/elastic/elastic-package/internal/logger"
1919
"github.com/elastic/elastic-package/internal/stack"
2020
)
@@ -24,10 +24,15 @@ type Client struct {
2424
host string
2525
username string
2626
password string
27+
28+
tlSkipVerify bool
2729
}
2830

31+
// ClientOption is functional option modifying Kibana client.
32+
type ClientOption func(*Client)
33+
2934
// NewClient creates a new instance of the client.
30-
func NewClient() (*Client, error) {
35+
func NewClient(opts ...ClientOption) (*Client, error) {
3136
host := os.Getenv(stack.KibanaHostEnv)
3237
if host == "" {
3338
return nil, stack.UndefinedEnvError(stack.KibanaHostEnv)
@@ -36,11 +41,23 @@ func NewClient() (*Client, error) {
3641
username := os.Getenv(stack.ElasticsearchUsernameEnv)
3742
password := os.Getenv(stack.ElasticsearchPasswordEnv)
3843

39-
return &Client{
44+
c := &Client{
4045
host: host,
4146
username: username,
4247
password: password,
43-
}, nil
48+
}
49+
50+
for _, opt := range opts {
51+
opt(c)
52+
}
53+
return c, nil
54+
}
55+
56+
// TLSSkipVerify option disables TLS verification.
57+
func TLSSkipVerify() ClientOption {
58+
return func(c *Client) {
59+
c.tlSkipVerify = true
60+
}
4461
}
4562

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

87104
client := http.Client{}
105+
if c.tlSkipVerify {
106+
client.Transport = &http.Transport{
107+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
108+
}
109+
}
110+
88111
resp, err := client.Do(req)
89112
if err != nil {
90113
return 0, nil, errors.Wrap(err, "could not send request to Kibana API")

0 commit comments

Comments
 (0)