From 3f05bd1526fc1326245fa78bb2f89fd795b8bc3c Mon Sep 17 00:00:00 2001 From: Carson Long <12767276+ctlong@users.noreply.github.com> Date: Fri, 2 Feb 2024 10:33:22 -0800 Subject: [PATCH 1/2] Breaking: Remove LOG_CACHE_ADDR env var option Removes the LOG_CACHE_ADDR env var as an option for telling the plugin where to look for Log Cache. This is a relic of when the plugin had a dual function as a standalone CLI, and is no longer necessary since the plugin can always fetch the Log Cache address from the targeted CF API. --- README.md | 3 -- internal/command/meta.go | 6 --- internal/command/meta_test.go | 30 -------------- internal/command/query.go | 27 ++++++------- internal/command/tail.go | 75 +++++++++++++++++------------------ internal/command/tail_test.go | 44 -------------------- internal/logcache/plugin.go | 3 -- 7 files changed, 48 insertions(+), 140 deletions(-) diff --git a/README.md b/README.md index a48eeb93..63893cc7 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ USAGE: tail [options] ENVIRONMENT VARIABLES: - LOG_CACHE_ADDR Overrides the default location of log-cache. LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication. OPTIONS: @@ -81,7 +80,6 @@ USAGE: log-meta [options] ENVIRONMENT VARIABLES: - LOG_CACHE_ADDR Overrides the default location of log-cache. LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication. OPTIONS: @@ -102,7 +100,6 @@ USAGE: query [options] ENVIRONMENT VARIABLES: - LOG_CACHE_ADDR Overrides the default location of log-cache. LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication. OPTIONS: diff --git a/internal/command/meta.go b/internal/command/meta.go index 2ae9e7ef..992b22bd 100644 --- a/internal/command/meta.go +++ b/internal/command/meta.go @@ -506,12 +506,6 @@ func maxDuration(a, b time.Duration) time.Duration { } func logCacheEndpoint(cli plugin.CliConnection) (string, error) { - logCacheAddr := os.Getenv("LOG_CACHE_ADDR") - - if logCacheAddr != "" { - return logCacheAddr, nil - } - apiEndpoint, err := cli.ApiEndpoint() if err != nil { return "", err diff --git a/internal/command/meta_test.go b/internal/command/meta_test.go index 004c9cbb..14f45990 100644 --- a/internal/command/meta_test.go +++ b/internal/command/meta_test.go @@ -1018,36 +1018,6 @@ var _ = Describe("Meta", func() { Expect(strings.Split(tableWriter.String(), "\n")).To(HaveLen(57)) }) - It("uses the LOG_CACHE_ADDR environment variable", func() { - _ = os.Setenv("LOG_CACHE_ADDR", "https://different-log-cache:8080") - defer func() { _ = os.Unsetenv("LOG_CACHE_ADDR") }() - - httpClient.responseBody = []string{ - metaResponseInfo("source-1"), - } - - cliConn.cliCommandResult = [][]string{ - { - capiAppsResponse(map[string]string{"source-1": "app-1"}), - }, - } - cliConn.cliCommandErr = nil - - command.Meta( - cliConn, - nil, - httpClient, - logger, - tableWriter, - ) - - Expect(httpClient.requestURLs).To(HaveLen(1)) - u, err := url.Parse(httpClient.requestURLs[0]) - Expect(err).ToNot(HaveOccurred()) - Expect(u.Scheme).To(Equal("https")) - Expect(u.Host).To(Equal("different-log-cache:8080")) - }) - It("does not send Authorization header with LOG_CACHE_SKIP_AUTH", func() { _ = os.Setenv("LOG_CACHE_SKIP_AUTH", "true") defer func() { _ = os.Unsetenv("LOG_CACHE_SKIP_AUTH") }() diff --git a/internal/command/query.go b/internal/command/query.go index 11bec224..fc725b75 100644 --- a/internal/command/query.go +++ b/internal/command/query.go @@ -54,25 +54,22 @@ func Query( }) } - logCacheAddr := os.Getenv("LOG_CACHE_ADDR") - if logCacheAddr == "" { - hasAPI, err := cli.HasAPIEndpoint() - if err != nil { - log.Fatalf("%s", err) - } - - if !hasAPI { - log.Fatalf("No API endpoint targeted.") - } + hasAPI, err := cli.HasAPIEndpoint() + if err != nil { + log.Fatalf("%s", err) + } - tokenURL, err := cli.ApiEndpoint() - if err != nil { - log.Fatalf("%s", err) - } + if !hasAPI { + log.Fatalf("No API endpoint targeted.") + } - logCacheAddr = strings.Replace(tokenURL, "api", "log-cache", 1) + tokenURL, err := cli.ApiEndpoint() + if err != nil { + log.Fatalf("%s", err) } + logCacheAddr := strings.Replace(tokenURL, "api", "log-cache", 1) + client := logcache.NewClient(logCacheAddr, logcache.WithHTTPClient(c)) var res *logcache.PromQLQueryResult diff --git a/internal/command/tail.go b/internal/command/tail.go index 9ab2635b..2d196e78 100644 --- a/internal/command/tail.go +++ b/internal/command/tail.go @@ -59,53 +59,50 @@ func Tail( } }() - logCacheAddr := os.Getenv("LOG_CACHE_ADDR") - if logCacheAddr == "" { - hasAPI, err := cli.HasAPIEndpoint() - if err != nil { - log.Fatalf("%s", err) - } + hasAPI, err := cli.HasAPIEndpoint() + if err != nil { + log.Fatalf("%s", err) + } - if !hasAPI { - log.Fatalf("No API endpoint targeted.") - } + if !hasAPI { + log.Fatalf("No API endpoint targeted.") + } - tokenURL, err := cli.ApiEndpoint() - if err != nil { - log.Fatalf("%s", err) - } + tokenURL, err := cli.ApiEndpoint() + if err != nil { + log.Fatalf("%s", err) + } - user, err := cli.Username() - if err != nil { - log.Fatalf("%s", err) - } + user, err := cli.Username() + if err != nil { + log.Fatalf("%s", err) + } - org, err := cli.GetCurrentOrg() - if err != nil { - log.Fatalf("%s", err) - } + org, err := cli.GetCurrentOrg() + if err != nil { + log.Fatalf("%s", err) + } - space, err := cli.GetCurrentSpace() - if err != nil { - log.Fatalf("%s", err) - } + space, err := cli.GetCurrentSpace() + if err != nil { + log.Fatalf("%s", err) + } - logCacheAddr = strings.Replace(tokenURL, "api", "log-cache", 1) + logCacheAddr := strings.Replace(tokenURL, "api", "log-cache", 1) - headerPrinter := formatter.sourceHeader - switch o.source.Type { - case _application: - headerPrinter = formatter.appHeader - case _service: - headerPrinter = formatter.serviceHeader - } + headerPrinter := formatter.sourceHeader + switch o.source.Type { + case _application: + headerPrinter = formatter.appHeader + case _service: + headerPrinter = formatter.serviceHeader + } - if !o.noHeaders { - header, ok := headerPrinter(o.source.Name, org.Name, space.Name, user) - if ok { - lw.Write(header) - lw.Write("") - } + if !o.noHeaders { + header, ok := headerPrinter(o.source.Name, org.Name, space.Name, user) + if ok { + lw.Write(header) + lw.Write("") } } diff --git a/internal/command/tail_test.go b/internal/command/tail_test.go index 7a4d9337..2a1bf79a 100644 --- a/internal/command/tail_test.go +++ b/internal/command/tail_test.go @@ -826,26 +826,6 @@ var _ = Describe("LogCache", func() { Expect(wrapperFunc).To(Panic()) }) - It("uses the LOG_CACHE_ADDR environment variable", func() { - os.Setenv("LOG_CACHE_ADDR", "https://different-log-cache:8080") - defer os.Unsetenv("LOG_CACHE_ADDR") - - command.Tail( - context.Background(), - cliConn, - []string{"app-name"}, - httpClient, - logger, - writer, - ) - Expect(httpClient.requestURLs).To(HaveLen(1)) - - u, err := url.Parse(httpClient.requestURLs[0]) - Expect(err).ToNot(HaveOccurred()) - Expect(u.Scheme).To(Equal("https")) - Expect(u.Host).To(Equal("different-log-cache:8080")) - }) - It("does not send Authorization header with LOG_CACHE_SKIP_AUTH", func() { os.Setenv("LOG_CACHE_SKIP_AUTH", "true") defer os.Unsetenv("LOG_CACHE_SKIP_AUTH") @@ -1621,30 +1601,6 @@ var _ = Describe("LogCache", func() { Expect(logger.printfMessages).To(ContainElement("app not found")) Expect(logger.printfMessages).To(ContainElement("service not found")) }) - - It("uses the LOG_CACHE_ADDR environment variable", func() { - os.Setenv("LOG_CACHE_ADDR", "https://different-log-cache:8080") - defer os.Unsetenv("LOG_CACHE_ADDR") - - cliConn.cliCommandResult = [][]string{{""}, {""}} - cliConn.cliCommandErr = []error{errors.New("app not found"), errors.New("service not found")} - - command.Tail( - context.Background(), - cliConn, - []string{"app-name"}, - httpClient, - logger, - writer, - ) - Expect(httpClient.requestURLs).To(HaveLen(1)) - - u, err := url.Parse(httpClient.requestURLs[0]) - Expect(err).ToNot(HaveOccurred()) - Expect(u.Scheme).To(Equal("https")) - Expect(u.Host).To(Equal("different-log-cache:8080")) - Expect(u.Path).To(ContainSubstring("app-name")) - }) }) }) diff --git a/internal/logcache/plugin.go b/internal/logcache/plugin.go index bc800d62..9e8cd3e3 100644 --- a/internal/logcache/plugin.go +++ b/internal/logcache/plugin.go @@ -64,7 +64,6 @@ func (lc *LogCache) GetMetadata() plugin.PluginMetadata { Usage: `tail [options] ENVIRONMENT VARIABLES: - LOG_CACHE_ADDR Overrides the default location of log-cache. LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`, Options: map[string]string{ "-start-time": "Start of query range in UNIX nanoseconds.", @@ -86,7 +85,6 @@ ENVIRONMENT VARIABLES: Usage: `log-meta [options] ENVIRONMENT VARIABLES: - LOG_CACHE_ADDR Overrides the default location of log-cache. LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`, Options: map[string]string{ "-source-type": "Source type of information to show. Available: 'all', 'application', 'service', 'platform', and 'unknown'. Excludes unknown sources unless 'all' or 'unknown' is selected, or `--guid` is used. To receive information on platform or unknown source id's, you must have the doppler.firehose, or logs.admin scope.", @@ -103,7 +101,6 @@ ENVIRONMENT VARIABLES: Usage: `query [options] ENVIRONMENT VARIABLES: - LOG_CACHE_ADDR Overrides the default location of log-cache. LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`, Options: map[string]string{ "-time": "Effective time for query execution of an instant query. Cannont be used with --start, --end, or --step. Can be a unix timestamp or RFC3339.", From 42733f5aae29287613860f408245cc918357423f Mon Sep 17 00:00:00 2001 From: Carson Long <12767276+ctlong@users.noreply.github.com> Date: Fri, 2 Feb 2024 10:41:45 -0800 Subject: [PATCH 2/2] Breaking: Remove LOG_CACHE_SKIP_AUTH env var option Removes the LOG_CACHE_SKIP_AUTH env var as an option for telling the plugin not to use authentication headers in requests. This is a relic of when the plugin had a dual function as a standalone CLI, and is no longer necessary. Log Cache should always require authentication headers, which can be easily fetched with cf CLI connection methods. --- README.md | 9 --------- internal/command/meta.go | 17 +++++++---------- internal/command/meta_test.go | 27 --------------------------- internal/command/query.go | 17 +++++++---------- internal/command/tail.go | 17 +++++++---------- internal/command/tail_test.go | 16 ---------------- internal/logcache/plugin.go | 15 +++------------ 7 files changed, 24 insertions(+), 94 deletions(-) diff --git a/README.md b/README.md index 63893cc7..0523cae6 100644 --- a/README.md +++ b/README.md @@ -54,9 +54,6 @@ NAME: USAGE: tail [options] -ENVIRONMENT VARIABLES: - LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication. - OPTIONS: --start-time Start of query range in UNIX nanoseconds. --end-time End of query range in UNIX nanoseconds. @@ -79,9 +76,6 @@ NAME: USAGE: log-meta [options] -ENVIRONMENT VARIABLES: - LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication. - OPTIONS: --guid Display raw source GUIDs with no source Names. Incompativle with 'source' and 'source-type' for --sort-by. Incompatible with 'application' for --source-type --noise Fetch and display the rate of envelopes per minute for the last minute. WARNING: This is slow... @@ -99,9 +93,6 @@ NAME: USAGE: query [options] -ENVIRONMENT VARIABLES: - LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication. - OPTIONS: --end End time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339. --start Start time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339. diff --git a/internal/command/meta.go b/internal/command/meta.go index 992b22bd..02162f3c 100644 --- a/internal/command/meta.go +++ b/internal/command/meta.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "os" "regexp" "sort" "strings" @@ -221,15 +220,13 @@ func createLogCacheClient(c http.Client, log Logger, cli plugin.CliConnection) * log.Fatalf("Could not determine Log Cache endpoint: %s", err) } - if strings.ToLower(os.Getenv("LOG_CACHE_SKIP_AUTH")) != "true" { - c = http.NewTokenClient(c, func() string { - token, err := cli.AccessToken() - if err != nil { - log.Fatalf("Unable to get Access Token: %s", err) - } - return token - }) - } + c = http.NewTokenClient(c, func() string { + token, err := cli.AccessToken() + if err != nil { + log.Fatalf("Unable to get Access Token: %s", err) + } + return token + }) return logcache.NewClient( logCacheEndpoint, diff --git a/internal/command/meta_test.go b/internal/command/meta_test.go index 14f45990..6d8952aa 100644 --- a/internal/command/meta_test.go +++ b/internal/command/meta_test.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "net/url" - "os" "strings" "code.cloudfoundry.org/log-cache-cli/v4/internal/command" @@ -1018,32 +1017,6 @@ var _ = Describe("Meta", func() { Expect(strings.Split(tableWriter.String(), "\n")).To(HaveLen(57)) }) - It("does not send Authorization header with LOG_CACHE_SKIP_AUTH", func() { - _ = os.Setenv("LOG_CACHE_SKIP_AUTH", "true") - defer func() { _ = os.Unsetenv("LOG_CACHE_SKIP_AUTH") }() - - httpClient.responseBody = []string{ - metaResponseInfo("source-1"), - } - - cliConn.cliCommandResult = [][]string{ - { - capiAppsResponse(map[string]string{"source-1": "app-1"}), - }, - } - cliConn.cliCommandErr = nil - - command.Meta( - cliConn, - nil, - httpClient, - logger, - tableWriter, - ) - - Expect(httpClient.requestHeaders[0]).To(BeEmpty()) - }) - It("fatally logs when it receives too many arguments", func() { Expect(func() { command.Meta( diff --git a/internal/command/query.go b/internal/command/query.go index fc725b75..5c98f285 100644 --- a/internal/command/query.go +++ b/internal/command/query.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "os" "strconv" "strings" "time" @@ -44,15 +43,13 @@ func Query( lw := lineWriter{w: w} - if strings.ToLower(os.Getenv("LOG_CACHE_SKIP_AUTH")) != "true" { - c = http.NewTokenClient(c, func() string { - token, err := cli.AccessToken() - if err != nil { - log.Fatalf("Unable to get Access Token: %s", err) - } - return token - }) - } + c = http.NewTokenClient(c, func() string { + token, err := cli.AccessToken() + if err != nil { + log.Fatalf("Unable to get Access Token: %s", err) + } + return token + }) hasAPI, err := cli.HasAPIEndpoint() if err != nil { diff --git a/internal/command/tail.go b/internal/command/tail.go index 2d196e78..1a8a8228 100644 --- a/internal/command/tail.go +++ b/internal/command/tail.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "os" "regexp" "strings" "text/template" @@ -114,15 +113,13 @@ func Tail( return formatter.formatEnvelope(e) } - if strings.ToLower(os.Getenv("LOG_CACHE_SKIP_AUTH")) != "true" { - c = http.NewTokenClient(c, func() string { - token, err := cli.AccessToken() - if err != nil { - log.Fatalf("Unable to get Access Token: %s", err) - } - return token - }) - } + c = http.NewTokenClient(c, func() string { + token, err := cli.AccessToken() + if err != nil { + log.Fatalf("Unable to get Access Token: %s", err) + } + return token + }) client := logcache.NewClient(logCacheAddr, logcache.WithHTTPClient(c)) diff --git a/internal/command/tail_test.go b/internal/command/tail_test.go index 2a1bf79a..0bf42a9b 100644 --- a/internal/command/tail_test.go +++ b/internal/command/tail_test.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "net/url" - "os" "strconv" "time" @@ -826,21 +825,6 @@ var _ = Describe("LogCache", func() { Expect(wrapperFunc).To(Panic()) }) - It("does not send Authorization header with LOG_CACHE_SKIP_AUTH", func() { - os.Setenv("LOG_CACHE_SKIP_AUTH", "true") - defer os.Unsetenv("LOG_CACHE_SKIP_AUTH") - - command.Tail( - context.Background(), - cliConn, - []string{"app-name"}, - httpClient, - logger, - writer, - ) - Expect(httpClient.requestHeaders[0]).To(BeEmpty()) - }) - It("follow retries for empty responses", func() { httpClient.responseBody = []string{emptyResponseBody()} diff --git a/internal/logcache/plugin.go b/internal/logcache/plugin.go index 9e8cd3e3..1144d0c9 100644 --- a/internal/logcache/plugin.go +++ b/internal/logcache/plugin.go @@ -61,10 +61,7 @@ func (lc *LogCache) GetMetadata() plugin.PluginMetadata { Name: "tail", HelpText: "Output logs for a source-id/app", UsageDetails: plugin.Usage{ - Usage: `tail [options] - -ENVIRONMENT VARIABLES: - LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`, + Usage: `tail [options] `, Options: map[string]string{ "-start-time": "Start of query range in UNIX nanoseconds.", "-end-time": "End of query range in UNIX nanoseconds.", @@ -82,10 +79,7 @@ ENVIRONMENT VARIABLES: Name: "log-meta", HelpText: "Show all available meta information", UsageDetails: plugin.Usage{ - Usage: `log-meta [options] - -ENVIRONMENT VARIABLES: - LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`, + Usage: `log-meta [options]`, Options: map[string]string{ "-source-type": "Source type of information to show. Available: 'all', 'application', 'service', 'platform', and 'unknown'. Excludes unknown sources unless 'all' or 'unknown' is selected, or `--guid` is used. To receive information on platform or unknown source id's, you must have the doppler.firehose, or logs.admin scope.", "-sort-by": "Sort by specified column. Available: 'source-id', 'source', 'source-type', 'count', 'expired', 'cache-duration', and 'rate'.", @@ -98,10 +92,7 @@ ENVIRONMENT VARIABLES: Name: "query", HelpText: "Issues a PromQL query against Log Cache", UsageDetails: plugin.Usage{ - Usage: `query [options] - -ENVIRONMENT VARIABLES: - LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`, + Usage: `query [options]`, Options: map[string]string{ "-time": "Effective time for query execution of an instant query. Cannont be used with --start, --end, or --step. Can be a unix timestamp or RFC3339.", "-start": "Start time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339.",