From 78e76d3eb55fd30a338476ca7d78f9d935d09790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ahl?= Date: Sun, 3 Mar 2019 10:25:56 +0100 Subject: [PATCH] V0.0.1 rc2 (#3) fix: generate s3 path correct without prefix test: Update tests for AccesslogPath(s3path to access log) fix: Adding correct default value for start-time and end-time --- cmd/cat.go | 2 +- cmd/root.go | 11 +++++++++-- logworker/logworker.go | 15 +++++++++++---- logworker/logworker_test.go | 24 ++++++++++++++++++------ 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/cmd/cat.go b/cmd/cat.go index 78f2bd0..be0f4bb 100644 --- a/cmd/cat.go +++ b/cmd/cat.go @@ -37,7 +37,7 @@ possible user these filter for _, v := range client.List() { buff := &aws.WriteAtBuffer{} - key := fmt.Sprintf("%s%s%s", configuration.Prefix, accessLogFilter.AccesslogPath(), v) + key := fmt.Sprintf("%s%s", accessLogFilter.AccesslogPath(configuration.Prefix), v) _, err := client.S3Downloader.Download(buff, &s3.GetObjectInput{ Bucket: aws.String(viper.GetString("s3-bucket")), Key: aws.String(key), diff --git a/cmd/root.go b/cmd/root.go index 685e3e1..0eac364 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "time" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" @@ -58,9 +59,9 @@ func init() { viper.BindPFlag("s3-bucket", rootCmd.PersistentFlags().Lookup("s3-bucket")) rootCmd.PersistentFlags().StringP("s3-prefix", "p", ".*", "The prefix (logical hierarchy) in the bucket. If you don't specify a prefix, the logs are placed at the root level of the bucket.") viper.BindPFlag("s3-prefix", rootCmd.PersistentFlags().Lookup("s3-prefix")) - rootCmd.PersistentFlags().StringP("start-time", "", ".*", "") + rootCmd.PersistentFlags().StringP("start-time", "", defaultStartTime().Format("2006-01-02 15:04:05"), "") viper.BindPFlag("start-time", rootCmd.PersistentFlags().Lookup("start-time")) - rootCmd.PersistentFlags().StringP("end-time", "", ".*", "") + rootCmd.PersistentFlags().StringP("end-time", "", time.Now().Format("2006-01-02 15:04:05"), "") viper.BindPFlag("end-time", rootCmd.PersistentFlags().Lookup("end-time")) // Cobra also supports local flags, which will only run @@ -93,3 +94,9 @@ func initConfig() { fmt.Println("Using config file:", viper.ConfigFileUsed()) } } + +func defaultStartTime() time.Time { + t := time.Now().UTC() + d := 24 * time.Hour + return t.Truncate(d) +} diff --git a/logworker/logworker.go b/logworker/logworker.go index f85ed71..5dbdb34 100644 --- a/logworker/logworker.go +++ b/logworker/logworker.go @@ -3,6 +3,7 @@ package logworker import ( "fmt" "os" + "path/filepath" "regexp" "strings" "time" @@ -117,7 +118,7 @@ func (l *LogWorker) List() []string { var accessLogs []string input := &s3.ListObjectsV2Input{ Bucket: aws.String(l.Configuration.Bucket), - Prefix: aws.String(fmt.Sprintf("%s%s", l.Configuration.Prefix, l.AccessLogFilter.AccesslogPath())), + Prefix: aws.String(l.AccessLogFilter.AccesslogPath(l.Configuration.Prefix)), Delimiter: aws.String("/"), MaxKeys: aws.Int64(200), } @@ -137,8 +138,9 @@ func (l *LogWorker) List() []string { return accessLogs } -func (a *AccessLogFilter) AccesslogPath() string { - return fmt.Sprintf("/AWSLogs/%s/elasticloadbalancing/%s/%s/", a.AwsAccountID, a.Region, a.StartTime.Format("2006/01/02")) +func (a *AccessLogFilter) AccesslogPath(prefix string) string { + return filepath.Join(prefix, fmt.Sprintf("AWSLogs/%s/elasticloadbalancing/%s/%s/", a.AwsAccountID, a.Region, a.StartTime.Format("2006/01/02"))) + "/" + } func (a *AccessLogFilter) beforeEndTime(accessLog string) bool { @@ -176,11 +178,16 @@ func NewAccessLogFilter() AccessLogFilter { Logger.Fatalf("Failed to parse start time. Gott error: %v", err) fmt.Println("failed to parse starttime") } - + endTime, err := time.Parse("2006-01-02 15:04:05", viper.GetString("end-time")) + if err != nil { + Logger.Fatalf("Failed to parse end time. Gott error: %v", err) + fmt.Println("failed to parse endtime") + } accessLogFilter := AccessLogFilter{} accessLogFilter.AwsAccountID = viper.GetString("aws-account-id") accessLogFilter.Region = viper.GetString("region") accessLogFilter.StartTime = startTime // time.Now() + accessLogFilter.EndTime = endTime accessLogFilter.LoadBalancerID = viper.GetString("load-balancer-id") accessLogFilter.IPaddress = viper.GetString("ip-address") accessLogFilter.RandomString = viper.GetString("random-string") diff --git a/logworker/logworker_test.go b/logworker/logworker_test.go index fa13ac1..76d3b18 100644 --- a/logworker/logworker_test.go +++ b/logworker/logworker_test.go @@ -7,28 +7,40 @@ import ( "time" ) -func TestAccessLogFilter(t *testing.T) { +func TestAccessLogFilterAccesslogPath(t *testing.T) { tt := []struct { name string accessLogFilter AccessLogFilter - accessLogPath string + prefix string + out string }{ - {"test1", + {"WithoutPrefix", AccessLogFilter{ AwsAccountID: "00000000111111", Region: "eu-west-1", StartTime: time.Now(), }, - fmt.Sprintf("/AWSLogs/00000000111111/elasticloadbalancing/eu-west-1/%s/", time.Now().Format("2006/01/02")), + "", + fmt.Sprintf("AWSLogs/00000000111111/elasticloadbalancing/eu-west-1/%s/", time.Now().Format("2006/01/02")), + }, + {"WithPrefix", + AccessLogFilter{ + + AwsAccountID: "00000000111111", + Region: "eu-west-1", + StartTime: time.Now(), + }, + "team-xxx", + fmt.Sprintf("team-xxx/AWSLogs/00000000111111/elasticloadbalancing/eu-west-1/%s/", time.Now().Format("2006/01/02")), }, } for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - if tc.accessLogFilter.AccesslogPath() != tc.accessLogPath { - t.Fatalf("accespath test %v should be %v; got %v", tc.name, tc.accessLogFilter.AccesslogPath(), tc.accessLogPath) + if tc.accessLogFilter.AccesslogPath(tc.prefix) != tc.out { + t.Fatalf("accespath test %v should be %v; got %v", tc.name, tc.out, tc.accessLogFilter.AccesslogPath(tc.prefix)) } }) }