Skip to content

Commit

Permalink
feat: Take bearer token from kube config by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanconway committed Jul 10, 2024
1 parent 8d12103 commit c08ccc6
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 17 deletions.
48 changes: 36 additions & 12 deletions cmd/korrel8rcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
package main

import (
"errors"
"fmt"
"log"
"net/url"
"path/filepath"

"os"
"path/filepath"

httptransport "github.com/go-openapi/runtime/client"
"github.com/korrel8r/client/pkg/build"
"github.com/korrel8r/client/pkg/swagger/client"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

const (
envURL = "KORREL8RCLI_URL"
envBearerToken = "KORREL8RCLI_BEARER_TOKEN"
)

var (
Expand All @@ -25,17 +29,37 @@ var (
}

// Global Flags
output = EnumFlag("yaml", "json-pretty", "json")
korrel8rURL = rootCmd.PersistentFlags().StringP("url", "u", os.Getenv("KORREL8RCLI_URL"), "URL of remote korrel8r service, environment KORREL8RCLI_URL")
insecure = rootCmd.PersistentFlags().BoolP("insecure", "k", false, "Insecure connection, skip TLS verification.")
bearerTokenFlag = rootCmd.PersistentFlags().String("bearer-token", "", "Bearer token for Authorization, environment KORREL8RCLI_BEARER_TOKEN")
output = EnumFlag("yaml", "json-pretty", "json")
korrel8rURL = rootCmd.PersistentFlags().StringP("url", "u", urlDefault(),
fmt.Sprintf("URL of remote korrel8r. Default from env %v", envURL))
insecure = rootCmd.PersistentFlags().BoolP("insecure", "k", false, "Insecure connection, skip TLS verification.")
// NOTE don't show the bearer token default for security reasons.
bearerTokenFlag = rootCmd.PersistentFlags().StringP("bearer-token", "t", "",
fmt.Sprintf("Authhorization token. Default from %v or kube config.", envBearerToken))
)

func urlDefault() string {
if u := os.Getenv(envURL); u != "" {
return u
}
return "https://localhost:8080"
}
func bearerToken() string {
if *bearerTokenFlag != "" {
if *bearerTokenFlag != "" { // Flag first
return *bearerTokenFlag
}
return os.Getenv("KORREL8RCLI_BEARER_TOKEN")
if token := os.Getenv(envBearerToken); token != "" { // Env next
return token
}
if cfg, err := config.GetConfig(); err == nil { // Kube config last
if cfg.BearerTokenFile != "" { // Try the file first
if b, err := os.ReadFile(cfg.BearerTokenFile); err == nil {
return string(b)
}
}
return cfg.BearerToken
}
return ""
}

func main() {
Expand All @@ -57,7 +81,7 @@ func init() {

func newClient() *client.RESTAPI {
if *korrel8rURL == "" {
check(errors.New("Either command line flag --url or environment variable KORREL8R_URL must be set. "))
check(fmt.Errorf("Either command line flag --url or environment variable %v be set.", envURL))
}
u, err := url.Parse(*korrel8rURL)
check(err)
Expand All @@ -72,8 +96,8 @@ func newClient() *client.RESTAPI {
} else {
transport = httptransport.New(u.Host, u.Path, []string{u.Scheme})
}
if bearerToken() != "" {
transport.DefaultAuthentication = httptransport.BearerToken(bearerToken())
if token := bearerToken(); token != "" {
transport.DefaultAuthentication = httptransport.BearerToken(token)
}
return client.New(transport, nil)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/korrel8rcli/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

var webCmd = &cobra.Command{
Use: "web REMOTE-URL [LISTEN-ADDR]",
Short: "Connect to REMOTE-URL and run an HTTP server listening on LISTEN-ADDR (default :8080)",
Args: cobra.RangeArgs(1, 2),
Use: "web [FLAGS]",
Short: "Connect to remote korrel8r, show graphs in local HTTP server.",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, args []string) error {
gin.DefaultWriter = log.Writer()
gin.SetMode(gin.ReleaseMode)
Expand Down Expand Up @@ -43,5 +43,5 @@ var (

func init() {
rootCmd.AddCommand(webCmd)
addr = webCmd.Flags().StringP("addr", "a", ":8080", "Listeing address for web server")
addr = webCmd.Flags().StringP("addr", "a", ":8081", "Listening address for web server")
}
23 changes: 22 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/korrel8r/client

go 1.21
go 1.22.0

toolchain go1.22.4

require (
github.com/gin-gonic/gin v1.10.0
Expand All @@ -15,6 +17,24 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
k8s.io/apimachinery v0.30.1 // indirect
k8s.io/client-go v0.30.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
Expand Down Expand Up @@ -69,4 +89,5 @@ require (
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/controller-runtime v0.18.4
)
Loading

0 comments on commit c08ccc6

Please sign in to comment.