Skip to content

Commit

Permalink
refactor logger and fix History tag expiration (WIP)
Browse files Browse the repository at this point in the history
- Use an instance of logrus instead of the module
- Add a ton of unit tests for expiring history
- Fix "too few fields for role History Tag: ""???

TL;DR: not sure I've actually found the bug yet?

Refs: #305
  • Loading branch information
synfinatic committed Mar 23, 2022
1 parent 9f5bb3e commit 3e68bf0
Show file tree
Hide file tree
Showing 29 changed files with 391 additions and 54 deletions.
2 changes: 0 additions & 2 deletions cmd/cache_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package main

import (
"fmt"

log "github.com/sirupsen/logrus"
)

type CacheCmd struct{}
Expand Down
1 change: 0 additions & 1 deletion cmd/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
// "github.com/davecgh/go-spew/spew"
"github.com/goccy/go-yaml"
"github.com/posener/complete"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/sso"
"github.com/synfinatic/aws-sso-cli/utils"
)
Expand Down
1 change: 0 additions & 1 deletion cmd/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/utils"
)

Expand Down
1 change: 0 additions & 1 deletion cmd/console_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/c-bata/go-prompt"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/sso"
"github.com/synfinatic/aws-sso-cli/storage"
"github.com/synfinatic/aws-sso-cli/utils"
Expand Down
1 change: 0 additions & 1 deletion cmd/exec_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"runtime"

"github.com/c-bata/go-prompt"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/sso"
"github.com/synfinatic/aws-sso-cli/utils"
)
Expand Down
1 change: 0 additions & 1 deletion cmd/flush_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/sso"
)

Expand Down
1 change: 0 additions & 1 deletion cmd/list_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"reflect"
"sort"

log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/utils"
"github.com/synfinatic/gotable"
)
Expand Down
35 changes: 35 additions & 0 deletions cmd/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

/*
* AWS SSO CLI
* Copyright (c) 2021-2022 Aaron Turner <synfinatic at gmail dot com>
*
* This program is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or with the authors permission any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import (
"github.com/sirupsen/logrus"
)

var log *logrus.Logger

/*
func SetLogger(l *logrus.Logger) {
log = l
}
func GetLogger() *logrus.Logger {
return log
}
*/
11 changes: 8 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/alecthomas/kong"
"github.com/posener/complete"
// "github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/sso"
"github.com/synfinatic/aws-sso-cli/storage"
"github.com/synfinatic/aws-sso-cli/utils"
Expand Down Expand Up @@ -113,9 +113,14 @@ type CLI struct {

func main() {
cli := CLI{}
ctx, override := parseArgs(&cli)
var err error

log = logrus.New()
ctx, override := parseArgs(&cli)
sso.SetLogger(log)
storage.SetLogger(log)
utils.SetLogger(log)

if err := logLevelValidate(cli.LogLevel); err != nil {
log.Fatalf("%s", err.Error())
}
Expand Down Expand Up @@ -217,7 +222,7 @@ func parseArgs(cli *CLI) (*kong.Context, sso.OverrideSettings) {
LogLines: cli.Lines,
}

log.SetFormatter(&log.TextFormatter{
logrus.SetFormatter(&logrus.TextFormatter{
DisableLevelTruncation: true,
PadLevelText: true,
DisableTimestamp: true,
Expand Down
1 change: 0 additions & 1 deletion cmd/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/c-bata/go-prompt"
// "github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/sso"
"github.com/synfinatic/aws-sso-cli/utils"
)
Expand Down
1 change: 0 additions & 1 deletion cmd/setup_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strings"

"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/sso"
"github.com/synfinatic/aws-sso-cli/utils"
)
Expand Down
1 change: 0 additions & 1 deletion cmd/tags_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"sort"

log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/sso"
)

Expand Down
1 change: 0 additions & 1 deletion sso/awssso.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ssooidc"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/storage"
"github.com/synfinatic/aws-sso-cli/utils"
"github.com/synfinatic/gotable"
Expand Down
1 change: 0 additions & 1 deletion sso/awssso_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ssooidc"
oidctypes "github.com/aws/aws-sdk-go-v2/service/ssooidc/types"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/storage"
"github.com/synfinatic/aws-sso-cli/utils"
)
Expand Down
27 changes: 21 additions & 6 deletions sso/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"time"

// "github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/aws-sso-cli/utils"
)

Expand Down Expand Up @@ -195,24 +194,34 @@ func (c *Cache) deleteOldHistory() {
cache := c.GetSSO()

newHistoryItems := []string{}

// iteratate over each ARN in our History list
for _, arn := range cache.History {
id, role, err := utils.ParseRoleARN(arn)
if err != nil {
log.Errorf("Unable to parse History ARN %s: %s", arn, err.Error())
continue
}

// for the given ARN, lookup the History tag
if a, ok := cache.Roles.Accounts[id]; ok {
if r, ok := a.Roles[role]; ok {
// figure out if this history item has expired
values := strings.SplitN(r.Tags["History"], ",", 2)
history, ok := r.Tags["History"]
if !ok || history == "" {
// doesn't have anything to expires
log.Debugf("%s is in history list without a History tag in cache?", arn)
continue
}

values := strings.SplitN(history, ",", 2)
if len(values) != 2 {
log.Errorf("Too few fields for %s History Tag: '%s'", r.Arn, r.Tags["History"])
log.Errorf("Too few fields for %s History Tag: '%s'", r.Arn, history)
continue
}
lastTime, err := strconv.ParseInt(values[1], 10, 64)
if err != nil {
log.Errorf("Unable to parse %s History Tag '%s': %s", r.Arn, r.Tags["History"], err.Error())
log.Errorf("Unable to parse %s History Tag '%s': %s", r.Arn, history, err.Error())
continue
}

Expand All @@ -221,14 +230,20 @@ func (c *Cache) deleteOldHistory() {
// keep current entries in our list
newHistoryItems = append(newHistoryItems, arn)
} else {
// else, delete the tag
// else, delete the tag and remove the item from History by
// not appending it to newHistoryItems
delete(r.Tags, "History")
log.Debugf("Removed expired history role: %s", r.Arn)
}
} else {
log.Errorf("History contains %s, but no role by that name", arn)
}
} else {
log.Errorf("History contains %s, but no account by that name", arn)
}
}

cache.History = newHistoryItems
c.GetSSO().History = newHistoryItems
}

// Refresh updates our cached Roles based on AWS SSO & our Config
Expand Down
Loading

0 comments on commit 3e68bf0

Please sign in to comment.