-
Notifications
You must be signed in to change notification settings - Fork 2k
access_monitoring_rules: Implement schedules conditions #59044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef4e98a
Implement schedules conditions
bernardjkim 5b0bd0d
Fix lint
bernardjkim c606b56
Remove schedules predicate expression
bernardjkim b5afa73
Support schedules within notification rules
bernardjkim 5dcb19d
Merge branch 'master' into bernard/condition-schedules
bernardjkim 46b4f85
Use timezone during evaluation
bernardjkim 4e7e313
Add debug logs
bernardjkim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ package accessmonitoring | |
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
|
|
@@ -52,7 +53,7 @@ func mockFetchRecipient(ctx context.Context, recipient string) (*common.Recipien | |
| return nil, nil | ||
| } | ||
|
|
||
| func TestRecipeints(t *testing.T) { | ||
| func TestRecipients(t *testing.T) { | ||
| const ( | ||
| pluginName = "fakePluginName" | ||
| pluginType = "fakePluginType" | ||
|
|
@@ -133,7 +134,7 @@ func TestRecipeints(t *testing.T) { | |
| require.ElementsMatch(t, []string{}, rawRecipients) | ||
| } | ||
|
|
||
| func TestRecipeintsWithResources(t *testing.T) { | ||
| func TestRecipientsWithResources(t *testing.T) { | ||
| const ( | ||
| pluginName = "fakePluginName" | ||
| pluginType = "fakePluginType" | ||
|
|
@@ -205,6 +206,85 @@ func TestRecipeintsWithResources(t *testing.T) { | |
| require.ElementsMatch(t, []string{recipient}, rawRecipients) | ||
| } | ||
|
|
||
| func TestRecipientsWithSchedules(t *testing.T) { | ||
| const ( | ||
| pluginName = "fakePluginName" | ||
| pluginType = "fakePluginType" | ||
| recipient = "[email protected]" | ||
| ) | ||
|
|
||
| teleportClient := &mockTeleportClient{} | ||
| teleportClient. | ||
| On("GetUser", mock.Anything, mock.Anything, mock.Anything). | ||
| Return(&types.UserV2{}, nil) | ||
|
|
||
| amrh := NewRuleHandler(RuleHandlerConfig{ | ||
| Client: teleportClient, | ||
| PluginType: pluginType, | ||
| PluginName: pluginName, | ||
| FetchRecipientCallback: func(ctx context.Context, recipient string) (*common.Recipient, error) { | ||
| return emailRecipient(recipient), nil | ||
| }, | ||
| }) | ||
|
|
||
| rule1, err := services.NewAccessMonitoringRuleWithLabels("rule1", nil, &pb.AccessMonitoringRuleSpec{ | ||
| Subjects: []string{types.KindAccessRequest}, | ||
| Schedules: map[string]*pb.Schedule{ | ||
| "default": { | ||
| Time: &pb.TimeSchedule{ | ||
| Shifts: []*pb.TimeSchedule_Shift{ | ||
| { | ||
| Weekday: time.Monday.String(), | ||
| Start: "14:00", | ||
| End: "15:00", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| Condition: `true`, | ||
| Notification: &pb.Notification{ | ||
| Name: pluginName, | ||
| Recipients: []string{recipient}, | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
| err = amrh.HandleAccessMonitoringRule(context.Background(), types.Event{ | ||
| Type: types.OpPut, | ||
| Resource: types.Resource153ToLegacy(rule1), | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Len(t, amrh.getAccessMonitoringRules(), 1) | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| // Expect recipient from matching rule. | ||
| req := &types.AccessRequestV3{ | ||
| Spec: types.AccessRequestSpecV3{ | ||
| Created: time.Date(2025, time.August, 11, 14, 30, 0, 0, time.UTC), | ||
| }, | ||
| } | ||
|
|
||
| recipients := amrh.RecipientsFromAccessMonitoringRules(ctx, req) | ||
| require.ElementsMatch(t, []common.Recipient{*emailRecipient(recipient)}, recipients.ToSlice()) | ||
|
|
||
| rawRecipients := amrh.RawRecipientsFromAccessMonitoringRules(ctx, req) | ||
| require.ElementsMatch(t, []string{recipient}, rawRecipients) | ||
|
|
||
| // Expect no recipient when not in schedule. | ||
| req = &types.AccessRequestV3{ | ||
| Spec: types.AccessRequestSpecV3{ | ||
| Created: time.Date(2025, time.August, 11, 15, 30, 0, 0, time.UTC), | ||
| }, | ||
| } | ||
|
|
||
| recipients = amrh.RecipientsFromAccessMonitoringRules(ctx, req) | ||
| require.ElementsMatch(t, []common.Recipient{}, recipients.ToSlice()) | ||
|
|
||
| rawRecipients = amrh.RawRecipientsFromAccessMonitoringRules(ctx, req) | ||
| require.ElementsMatch(t, []string{}, rawRecipients) | ||
| } | ||
|
|
||
| func emailRecipient(recipient string) *common.Recipient { | ||
| return &common.Recipient{ | ||
| Name: recipient, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| Copyright 2025 Gravitational, Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package accessmonitoring | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/gravitational/trace" | ||
|
|
||
| accessmonitoringrulesv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/accessmonitoringrules/v1" | ||
| ) | ||
|
|
||
| // ClockTime returns a new time value overriding the hour and minute. | ||
| func ClockTime(timestamp time.Time, hourMinute string) (time.Time, error) { | ||
| const hourMinuteFormat = "15:04" // 24-hour HH:MM format | ||
|
|
||
| parsed, err := time.ParseInLocation(hourMinuteFormat, hourMinute, timestamp.Location()) | ||
| if err != nil { | ||
| return time.Time{}, trace.Wrap(err) | ||
| } | ||
|
|
||
| return time.Date(timestamp.Year(), timestamp.Month(), timestamp.Day(), | ||
| parsed.Hour(), parsed.Minute(), 0, 0, timestamp.Location()), nil | ||
| } | ||
|
|
||
| // inSchedule returns true if the timestamp is within the schedule. | ||
| func inSchedule(schedule *accessmonitoringrulesv1.Schedule, timestamp time.Time) (bool, error) { | ||
| if schedule.GetTime() == nil { | ||
| return false, nil | ||
| } | ||
|
|
||
| if len(schedule.GetTime().GetShifts()) == 0 { | ||
| return false, nil | ||
| } | ||
|
|
||
| loc, err := time.LoadLocation(schedule.GetTime().GetTimezone()) | ||
| if err != nil { | ||
| return false, trace.Wrap(err) | ||
| } | ||
|
|
||
| timestamp = timestamp.In(loc) | ||
| weekday := timestamp.Weekday().String() | ||
|
|
||
| for _, shift := range schedule.GetTime().GetShifts() { | ||
| if weekday != shift.Weekday { | ||
| continue | ||
| } | ||
|
|
||
| startTime, err := ClockTime(timestamp, shift.Start) | ||
| if err != nil { | ||
| return false, trace.Wrap(err, "invalid start time: %q", shift.Start) | ||
| } | ||
|
|
||
| endTime, err := ClockTime(timestamp, shift.End) | ||
| if err != nil { | ||
| return false, trace.Wrap(err, "invalid end time: %q", shift.End) | ||
| } | ||
|
|
||
| if !timestamp.Before(startTime) && !timestamp.After(endTime) { | ||
| return true, nil | ||
| } | ||
| } | ||
| return false, nil | ||
| } | ||
|
|
||
| // InSchedules returns true if the provided timestamp is within an of the provided | ||
| // schedules. Returns false if schedules is empty. | ||
| func InSchedules(schedules map[string]*accessmonitoringrulesv1.Schedule, timestamp time.Time) (bool, error) { | ||
| for _, schedule := range schedules { | ||
| isInSchedule, err := inSchedule(schedule, timestamp) | ||
| if err != nil { | ||
| return false, trace.Wrap(err) | ||
| } | ||
| if isInSchedule { | ||
| return true, nil | ||
| } | ||
| } | ||
| return false, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and below, should we log that we're skipping the request because the schedule doesn't match?