mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 13:14:18 +02:00
ask for pass if missing or use env pass, add token support (#60)
This commit is contained in:
@@ -17,27 +17,33 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package cfg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/elastic/elastic-transport-go/v8/elastictransport"
|
||||
"github.com/elastic/go-elasticsearch/v9"
|
||||
"golang.org/x/term"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// used in general config struct
|
||||
type Cluster struct {
|
||||
Uri, User, Pass string
|
||||
client *elasticsearch.TypedClient
|
||||
Default bool
|
||||
Name, Uri, User, Pass, Token string
|
||||
client *elasticsearch.TypedClient
|
||||
Default, DebugHTTP bool
|
||||
}
|
||||
|
||||
// used just for writing back to the config file
|
||||
type ClusterConfig struct {
|
||||
Uri, User, Pass string
|
||||
Default bool
|
||||
Uri, User, Pass, Token string
|
||||
Default bool
|
||||
}
|
||||
|
||||
// to write the config, we avoid all other config settings
|
||||
@@ -45,17 +51,97 @@ type WriteConfig struct {
|
||||
Clusters map[string]*ClusterConfig
|
||||
}
|
||||
|
||||
func (cluster *Cluster) SetClient(client *elasticsearch.TypedClient) {
|
||||
cluster.client = client
|
||||
}
|
||||
|
||||
func (cluster *Cluster) getTransport() elastictransport.Option {
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
|
||||
if cluster.DebugHTTP {
|
||||
return elastictransport.WithTransport(
|
||||
&DebugTransport{Transport: transport},
|
||||
)
|
||||
}
|
||||
|
||||
return elastictransport.WithTransport(transport)
|
||||
}
|
||||
|
||||
func (cluster *Cluster) getDefaultOptions() []elasticsearch.Option {
|
||||
// These headers are not needed with ES 9, but with ES 8, we set
|
||||
// them here so every API call uses it. The only exception being
|
||||
// the api repl, which does it on its own.
|
||||
headers := http.Header{}
|
||||
headers.Add("content-type", "application/json")
|
||||
headers.Add("Accept", "application/json")
|
||||
|
||||
return []elasticsearch.Option{
|
||||
elasticsearch.WithAddresses(cluster.Uri),
|
||||
elasticsearch.WithTransportOptions(
|
||||
cluster.getTransport(),
|
||||
elastictransport.WithHeader(headers),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// return the go-elasticsearch client object but before doing that,
|
||||
// check if we need to tune auth
|
||||
func (cluster *Cluster) ES() *elasticsearch.TypedClient {
|
||||
if cluster.client == nil {
|
||||
fmt.Println("no current cluster, use 'esctl cluster switch <name>' to set one")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := cluster.CheckAuth(); err != nil {
|
||||
fmt.Printf("Error: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return cluster.client
|
||||
}
|
||||
|
||||
func (cluster *Cluster) SetClient(client *elasticsearch.TypedClient) {
|
||||
cluster.client = client
|
||||
// add authentication to es client, if not yet done
|
||||
func (cluster *Cluster) CheckAuth() error {
|
||||
|
||||
if cluster.Pass == "" && cluster.User != "" && cluster.Token == "" && cluster.Default {
|
||||
// no token - user is set, but no password.
|
||||
|
||||
// check if the env var is set
|
||||
pass := os.Getenv("ES_PASS")
|
||||
if pass != "" {
|
||||
cluster.Pass = pass
|
||||
} else {
|
||||
// k, try interactively
|
||||
fmt.Printf("Enter password for elasticsearch user %s@%s: ", cluster.User, cluster.Name)
|
||||
pass, err := term.ReadPassword(int(syscall.Stdin))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
passwd := strings.TrimSpace(string(pass))
|
||||
if passwd == "" {
|
||||
return errors.New("password empty")
|
||||
}
|
||||
|
||||
cluster.Pass = string(pass)
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
opts := cluster.getDefaultOptions()
|
||||
opts = append(opts, elasticsearch.WithBasicAuth(cluster.User, cluster.Pass))
|
||||
|
||||
es, err := elasticsearch.NewTyped(opts...)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setup elasticsearch connection: %w", err)
|
||||
}
|
||||
|
||||
cluster.SetClient(es)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// set Default=true for the given cluster in the config (if exists)
|
||||
@@ -72,6 +158,7 @@ func (conf *Config) SwitchCluster(name string) error {
|
||||
Uri: cluster.Uri,
|
||||
User: cluster.User,
|
||||
Pass: cluster.Pass,
|
||||
Token: cluster.Token,
|
||||
Default: false,
|
||||
}
|
||||
|
||||
@@ -97,23 +184,61 @@ func (conf *Config) SwitchCluster(name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (conf *Config) SetupES() error {
|
||||
// These headers are not needed with ES 9, but with ES 8, we set
|
||||
// them here so every API call uses it. The only exception being
|
||||
// the api repl, which does it on its own.
|
||||
headers := http.Header{}
|
||||
headers.Add("content-type", "application/json")
|
||||
headers.Add("Accept", "application/json")
|
||||
// We do NOT use go-elasticsearch to check for cluster reachability,
|
||||
// because at this stage, auth may not have been configured. So
|
||||
// instead we just connect to the cluster using plan net/http, ignore
|
||||
// HTTP response status and return true if we could just reach ith
|
||||
func (cluster *Cluster) IsReachable() (bool, error) {
|
||||
ctx, cancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
time.Duration(500)*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
for _, cluster := range conf.Clusters {
|
||||
es, err := elasticsearch.NewTyped(
|
||||
elasticsearch.WithAddresses(cluster.Uri),
|
||||
elasticsearch.WithBasicAuth(cluster.User, cluster.Pass),
|
||||
elasticsearch.WithTransportOptions(
|
||||
conf.getTransport(),
|
||||
elastictransport.WithHeader(headers),
|
||||
),
|
||||
)
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
"GET",
|
||||
cluster.Uri,
|
||||
nil,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
client := &http.Client{Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if resp != nil {
|
||||
// at this stage we do not care if the elasticsearch cluster
|
||||
// accepts our request or if it's misconfigured in some way
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (conf *Config) SetupES() error {
|
||||
for name, cluster := range conf.Clusters {
|
||||
cluster.Name = name
|
||||
cluster.DebugHTTP = conf.DebugHTTP
|
||||
|
||||
opts := cluster.getDefaultOptions()
|
||||
|
||||
switch {
|
||||
case cluster.Pass != "" && cluster.User != "":
|
||||
opts = append(opts, elasticsearch.WithBasicAuth(cluster.User, cluster.Pass))
|
||||
case cluster.Token != "":
|
||||
opts = append(opts, elasticsearch.WithAPIKey(cluster.Token))
|
||||
}
|
||||
|
||||
es, err := elasticsearch.NewTyped(opts...)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setup elasticsearch connection: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user