Skip to content

Commit

Permalink
Convert integrations to use slog
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy committed Jan 9, 2025
1 parent 5eee08d commit cbc8fd5
Show file tree
Hide file tree
Showing 65 changed files with 648 additions and 647 deletions.
49 changes: 34 additions & 15 deletions integrations/access/accesslist/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/gravitational/teleport/integrations/lib"
"github.com/gravitational/teleport/integrations/lib/logger"
pd "github.com/gravitational/teleport/integrations/lib/plugindata"
logutils "github.com/gravitational/teleport/lib/utils/log"
)

const (
Expand Down Expand Up @@ -118,7 +119,7 @@ func (a *App) run(ctx context.Context) error {

log := logger.Get(ctx)

log.Info("Access list monitor is running")
log.InfoContext(ctx, "Access list monitor is running")

a.job.SetReady(true)

Expand All @@ -134,7 +135,7 @@ func (a *App) run(ctx context.Context) error {
}
timer.Reset(jitter(reminderInterval))
case <-ctx.Done():
log.Info("Access list monitor is finished")
log.InfoContext(ctx, "Access list monitor is finished")
return nil
}
}
Expand All @@ -146,7 +147,7 @@ func (a *App) run(ctx context.Context) error {
func (a *App) remindIfNecessary(ctx context.Context) error {
log := logger.Get(ctx)

log.Info("Looking for Access List Review reminders")
log.InfoContext(ctx, "Looking for Access List Review reminders")

var nextToken string
var err error
Expand All @@ -156,21 +157,25 @@ func (a *App) remindIfNecessary(ctx context.Context) error {
accessLists, nextToken, err = a.apiClient.ListAccessLists(ctx, 0 /* default page size */, nextToken)
if err != nil {
if trace.IsNotImplemented(err) {
log.Errorf("access list endpoint is not implemented on this auth server, so the access list app is ceasing to run.")
log.ErrorContext(ctx, "access list endpoint is not implemented on this auth server, so the access list app is ceasing to run")
return trace.Wrap(err)
} else if trace.IsAccessDenied(err) {
log.Warnf("Slack bot does not have permissions to list access lists. Please add access_list read and list permissions " +
"to the role associated with the Slack bot.")
const msg = "Slack bot does not have permissions to list access lists. Please add access_list read and list permissions " +
"to the role associated with the Slack bot."
log.WarnContext(ctx, msg)
} else {
log.Errorf("error listing access lists: %v", err)
log.ErrorContext(ctx, "error listing access lists", "error", err)
}
break
}

for _, accessList := range accessLists {
recipients, err := a.getRecipientsRequiringReminders(ctx, accessList)
if err != nil {
log.WithError(err).Warnf("Error getting recipients to notify for review due for access list %q", accessList.Spec.Title)
log.WarnContext(ctx, "Error getting recipients to notify for review due for access list",
"error", err,
"access_list", accessList.Spec.Title,
)
continue
}

Expand All @@ -195,7 +200,7 @@ func (a *App) remindIfNecessary(ctx context.Context) error {
}

if len(errs) > 0 {
log.WithError(trace.NewAggregate(errs...)).Warn("Error notifying for access list reviews")
log.WarnContext(ctx, "Error notifying for access list reviews", "error", trace.NewAggregate(errs...))
}

return nil
Expand All @@ -213,7 +218,10 @@ func (a *App) getRecipientsRequiringReminders(ctx context.Context, accessList *a

// If the current time before the notification start time, skip notifications.
if now.Before(notificationStart) {
log.Debugf("Access list %s is not ready for notifications, notifications start at %s", accessList.GetName(), notificationStart.Format(time.RFC3339))
log.DebugContext(ctx, "Access list is not ready for notifications",
"access_list", accessList.GetName(),
"notification_start_time", notificationStart.Format(time.RFC3339),
)
return nil, nil
}

Expand Down Expand Up @@ -255,12 +263,17 @@ func (a *App) fetchRecipients(ctx context.Context, accessList *accesslist.Access
if err != nil {
// TODO(kiosion): Remove in v18; protecting against server not having `GetAccessListOwners` func.
if trace.IsNotImplemented(err) {
log.WithError(err).Warnf("Error getting nested owners for access list '%v', continuing with only explicit owners", accessList.GetName())
log.WarnContext(ctx, "Error getting nested owners for access list, continuing with only explicit owners",
"error", err,
"access_list", accessList.GetName(),
)
for _, owner := range accessList.Spec.Owners {
allOwners = append(allOwners, &owner)
}
} else {
log.WithError(err).Errorf("Error getting owners for access list '%v'", accessList.GetName())
log.ErrorContext(ctx, "Error getting owners for access list",
"error", err,
"access_list", accessList.GetName())
}
}

Expand All @@ -270,7 +283,7 @@ func (a *App) fetchRecipients(ctx context.Context, accessList *accesslist.Access
for _, owner := range allOwners {
recipient, err := a.bot.FetchRecipient(ctx, owner.Name)
if err != nil {
log.Debugf("error getting recipient %s", owner.Name)
log.DebugContext(ctx, "error getting recipient", "recipient", owner.Name)
continue
}
allRecipients[owner.Name] = *recipient
Expand All @@ -293,7 +306,10 @@ func (a *App) updatePluginDataAndGetRecipientsRequiringReminders(ctx context.Con
// Calculate days from start.
daysFromStart := now.Sub(notificationStart) / oneDay
windowStart = notificationStart.Add(daysFromStart * oneDay)
log.Infof("windowStart: %s, now: %s", windowStart.String(), now.String())
log.InfoContext(ctx, "calculating window start",
"window_start", logutils.StringerAttr(windowStart),
"now", logutils.StringerAttr(now),
)
}

recipients := []common.Recipient{}
Expand All @@ -304,7 +320,10 @@ func (a *App) updatePluginDataAndGetRecipientsRequiringReminders(ctx context.Con

// If the notification window is before the last notification date, then this user doesn't need a notification.
if !windowStart.After(lastNotification) {
log.Debugf("User %s has already been notified for access list %s", recipient.Name, accessList.GetName())
log.DebugContext(ctx, "User has already been notified for access list",
"user", recipient.Name,
"access_list", accessList.GetName(),
)
userNotifications[recipient.Name] = lastNotification
continue
}
Expand Down
14 changes: 9 additions & 5 deletions integrations/access/accessmonitoring/access_monitoring_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,18 @@ func (amrh *RuleHandler) RecipientsFromAccessMonitoringRules(ctx context.Context
for _, rule := range amrh.getAccessMonitoringRules() {
match, err := MatchAccessRequest(rule.Spec.Condition, req)
if err != nil {
log.WithError(err).WithField("rule", rule.Metadata.Name).
Warn("Failed to parse access monitoring notification rule")
log.WarnContext(ctx, "Failed to parse access monitoring notification rule",
"error", err,
"rule", rule.Metadata.Name,
)
}
if !match {
continue
}
for _, recipient := range rule.Spec.Notification.Recipients {
rec, err := amrh.fetchRecipientCallback(ctx, recipient)
if err != nil {
log.WithError(err).Warn("Failed to fetch plugin recipients based on Access monitoring rule recipients")
log.WarnContext(ctx, "Failed to fetch plugin recipients based on Access monitoring rule recipients", "error", err)
continue
}
recipientSet.Add(*rec)
Expand All @@ -176,8 +178,10 @@ func (amrh *RuleHandler) RawRecipientsFromAccessMonitoringRules(ctx context.Cont
for _, rule := range amrh.getAccessMonitoringRules() {
match, err := MatchAccessRequest(rule.Spec.Condition, req)
if err != nil {
log.WithError(err).WithField("rule", rule.Metadata.Name).
Warn("Failed to parse access monitoring notification rule")
log.WarnContext(ctx, "Failed to parse access monitoring notification rule",
"error", err,
"rule", rule.Metadata.Name,
)
}
if !match {
continue
Expand Down
Loading

0 comments on commit cbc8fd5

Please sign in to comment.