Skip to content

Kibana api key #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Optional:

Optional:

- `api_key` (String, Sensitive) API Key to use for authentication to Kibana
- `endpoints` (List of String, Sensitive) A list of endpoints where the terraform provider will point to, this must include the http(s) schema and port number.
- `insecure` (Boolean) Disable TLS certificate validation
- `password` (String, Sensitive) Password to use for API authentication to Kibana.
Expand Down
5 changes: 5 additions & 0 deletions generated/alerting/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ require (
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/disaster37/go-kibana-rest/v8 => ./libs/go-kibana-rest
21 changes: 17 additions & 4 deletions internal/clients/api_client.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API key support also needs to be added to connectors, as defaults for the fleet client and the SLO auth context

That's too many places :( There must be a better way.

Copy link
Contributor

@jloleysens jloleysens Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it would be nice refactor some of this functionality. I'm hopeful we'll have a single client for the entire Kibana surface area soon so that we don't have to implement glue code around all the existing clients.

Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,16 @@ func (a *ApiClient) GetFleetClient() (*fleet.Client, error) {
}

func (a *ApiClient) SetAlertingAuthContext(ctx context.Context) context.Context {
return context.WithValue(ctx, alerting.ContextBasicAuth, alerting.BasicAuth{
UserName: a.kibanaConfig.Username,
Password: a.kibanaConfig.Password,
})
if a.kibanaConfig.ApiKey != "" {
return context.WithValue(ctx, alerting.ContextAPIKeys, alerting.APIKey{
Key: a.kibanaConfig.ApiKey,
})
} else {
return context.WithValue(ctx, alerting.ContextBasicAuth, alerting.BasicAuth{
UserName: a.kibanaConfig.Username,
Password: a.kibanaConfig.Password,
})
}
}

func (a *ApiClient) ID(ctx context.Context, resourceId string) (*CompositeId, diag.Diagnostics) {
Expand Down Expand Up @@ -508,6 +514,9 @@ func buildKibanaConfig(d *schema.ResourceData, baseConfig BaseConfig) (kibana.Co
if password := os.Getenv("KIBANA_PASSWORD"); password != "" {
config.Password = strings.TrimSpace(password)
}
if api_key := os.Getenv("KIBANA_API_KEY"); api_key != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make sure this is added to the provider docs as well.

config.ApiKey = strings.TrimSpace(api_key)
}
if endpoint := os.Getenv("KIBANA_ENDPOINT"); endpoint != "" {
config.Address = endpoint
}
Expand All @@ -524,6 +533,10 @@ func buildKibanaConfig(d *schema.ResourceData, baseConfig BaseConfig) (kibana.Co
config.Password = password.(string)
}

if api_key, ok := kibConfig["api_key"]; ok && api_key != "" {
config.ApiKey = api_key.(string)
}

if endpoints, ok := kibConfig["endpoints"]; ok && len(endpoints.([]interface{})) > 0 {
// We're curently limited by the API to a single endpoint
if endpoint := endpoints.([]interface{})[0]; endpoint != nil {
Expand Down
9 changes: 9 additions & 0 deletions internal/schema/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,22 @@ func GetEsConnectionSchema(keyName string, isProviderConfiguration bool) *schema
}

func GetKibanaConnectionSchema() *schema.Schema {
withEnvDefault := func(key string, dv interface{}) schema.SchemaDefaultFunc { return nil }
return &schema.Schema{
Description: "Kibana connection configuration block.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"api_key": {
Description: "API Key to use for authentication to Kibana",
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: withEnvDefault("KIBANA_API_KEY", nil),
ConflictsWith: []string{"kibana.0.password", "kibana.0.username"},
},
"username": {
Description: "Username to use for API authentication to Kibana.",
Type: schema.TypeString,
Expand Down
51 changes: 51 additions & 0 deletions libs/go-kibana-rest/.github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: "build"

on:
push:
branches:
- main
- 7.x
- 8.x
tags:
- "*"
pull_request:
branches:
- main
- 7.x
- 8.x

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
- name: Setup Elasticsearch / Kibana
run: |
set -e
docker-compose up elasticsearch &
echo "Waiting for Elasticsearch availability"
until curl -s http://localhost:9200 | grep -q 'missing authentication credentials'; do sleep 30; done;
echo "Setting kibana_system password"
until curl -s -X POST -u elastic:changeme -H "Content-Type: application/json" http://localhost:9200/_security/user/kibana_system/_password -d "{\"password\":\"changeme\"}" | grep -q "^{}"; do sleep 10; done
curl -XPOST -u elastic:changeme http://localhost:9200/_license/start_trial?acknowledge=true
docker-compose up kibana &
until $(curl --output /dev/null --silent --head --fail -u elastic:changeme http://localhost:5601); do sleep 5; done
sleep 10
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout 600s
- name: Run build
run: go build
- name: Run test
run: make test
- uses: codecov/codecov-action@v2
with:
files: coverage.out
flags: unittests
fail_ci_if_error: true
12 changes: 12 additions & 0 deletions libs/go-kibana-rest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
12 changes: 12 additions & 0 deletions libs/go-kibana-rest/.theia/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug current file",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
21 changes: 21 additions & 0 deletions libs/go-kibana-rest/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 disaster37

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions libs/go-kibana-rest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
TEST?=./...
PKG_NAME=kbapi
KIBANA_URL ?= http://127.0.0.1:5601
KIBANA_USERNAME ?= elastic
KIBANA_PASSWORD ?= changeme

all: help


test: fmt
KIBANA_URL=${KIBANA_URL} KIBANA_USERNAME=${KIBANA_USERNAME} KIBANA_PASSWORD=${KIBANA_PASSWORD} go test $(TEST) -v -count 1 -parallel 1 -race -coverprofile=coverage.out -covermode=atomic $(TESTARGS) -timeout 120m

fmt:
@echo "==> Fixing source code with gofmt..."
gofmt -s -w ./



Loading