Skip to content

Commit 10095eb

Browse files
authored
access_monitoring_rules: Add timezone to schedules spec (#60067)
* Add timezone to AMR schedules * Update documentation with accepted timezone values * Address feedback - Allow empty timezone value - Reference IANA in error message - Add additional timeonze test cases * Fix test case
1 parent 7ffa26d commit 10095eb

File tree

7 files changed

+194
-28
lines changed

7 files changed

+194
-28
lines changed

api/gen/proto/go/teleport/accessmonitoringrules/v1/access_monitoring_rules.pb.go

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/proto/teleport/accessmonitoringrules/v1/access_monitoring_rules.proto

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,16 @@ message Schedule {
100100
// TimeSchedule specifies an in-line schedule.
101101
message TimeSchedule {
102102
// Shifts contains a set of shifts that make up the schedule.
103-
// Shifts are configured in UTC.
104103
repeated Shift shifts = 1;
105104

105+
// Timezone specifies the schedule timezone. This field is optional and defaults
106+
// to "UTC". Accepted values use timezone locations as defined in the IANA
107+
// Time Zone Database, such as "America/Los_Angeles", "Europe/Lisbon", or
108+
// "Asia/Singapore".
109+
//
110+
// See https://data.iana.org/time-zones/tzdb/zone1970.tab for a list of supported values.
111+
string timezone = 2;
112+
106113
// Shift contains the weekday, start time, and end time of a shift.
107114
message Shift {
108115
// Weekday specifies the day of the week, e.g., "Sunday", "Monday", "Tuesday".

docs/pages/reference/infrastructure-as-code/terraform-provider/data-sources/access_monitoring_rule.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ Optional:
7676

7777
Optional:
7878

79-
- `shifts` (Attributes List) Shifts contains a set of shifts that make up the schedule. Shifts are configured in UTC. (see [below for nested schema](#nested-schema-for-specschedulestimeshifts))
79+
- `shifts` (Attributes List) Shifts contains a set of shifts that make up the schedule. (see [below for nested schema](#nested-schema-for-specschedulestimeshifts))
80+
- `timezone` (String) Timezone specifies the schedule timezone. This field is optional and defaults to "UTC". Accepted values use timezone locations as defined in the IANA Time Zone Database, such as "America/Los_Angeles", "Europe/Lisbon", or "Asia/Singapore". See https://data.iana.org/time-zones/tzdb/zone1970.tab for a list of supported values.
8081

8182
### Nested Schema for `spec.schedules.time.shifts`
8283

docs/pages/reference/infrastructure-as-code/terraform-provider/resources/access_monitoring_rule.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ Optional:
9898

9999
Optional:
100100

101-
- `shifts` (Attributes List) Shifts contains a set of shifts that make up the schedule. Shifts are configured in UTC. (see [below for nested schema](#nested-schema-for-specschedulestimeshifts))
101+
- `shifts` (Attributes List) Shifts contains a set of shifts that make up the schedule. (see [below for nested schema](#nested-schema-for-specschedulestimeshifts))
102+
- `timezone` (String) Timezone specifies the schedule timezone. This field is optional and defaults to "UTC". Accepted values use timezone locations as defined in the IANA Time Zone Database, such as "America/Los_Angeles", "Europe/Lisbon", or "Asia/Singapore". See https://data.iana.org/time-zones/tzdb/zone1970.tab for a list of supported values.
102103

103104
### Nested Schema for `spec.schedules.time.shifts`
104105

integrations/terraform/tfschema/accessmonitoringrules/v1/access_monitoring_rules_terraform.go

Lines changed: 67 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/services/access_monitoring_rules.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"context"
2323
"slices"
2424
"time"
25+
_ "time/tzdata"
2526

2627
"github.com/gravitational/trace"
2728

@@ -156,6 +157,10 @@ func validateSchedules(schedules map[string]*accessmonitoringrulesv1.Schedule) e
156157
}
157158

158159
func validateTimeSchedule(schedule *accessmonitoringrulesv1.TimeSchedule) error {
160+
if _, err := time.LoadLocation(schedule.GetTimezone()); err != nil {
161+
return trace.Wrap(err, "invalid timezone: refer to the IANA Time Zone Database for valid options")
162+
}
163+
159164
if len(schedule.GetShifts()) == 0 {
160165
return trace.BadParameter("at least one shift is required")
161166
}

lib/services/access_monitoring_rules_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,98 @@ func TestValidateSchedules(t *testing.T) {
249249
require.ErrorContains(t, err, "at least one shift is require")
250250
},
251251
},
252+
{
253+
description: "valid timezone (UTC)",
254+
schedules: map[string]*accessmonitoringrulesv1.Schedule{
255+
"default": {
256+
Time: &accessmonitoringrulesv1.TimeSchedule{
257+
Timezone: "UTC",
258+
Shifts: []*accessmonitoringrulesv1.TimeSchedule_Shift{
259+
{
260+
Weekday: time.Monday.String(),
261+
Start: "00:00",
262+
End: "23:59",
263+
},
264+
},
265+
},
266+
},
267+
},
268+
assertErr: require.NoError,
269+
},
270+
{
271+
description: "valid timezone (America/Los_Angeles)",
272+
schedules: map[string]*accessmonitoringrulesv1.Schedule{
273+
"default": {
274+
Time: &accessmonitoringrulesv1.TimeSchedule{
275+
Timezone: "America/Los_Angeles",
276+
Shifts: []*accessmonitoringrulesv1.TimeSchedule_Shift{
277+
{
278+
Weekday: time.Monday.String(),
279+
Start: "00:00",
280+
End: "23:59",
281+
},
282+
},
283+
},
284+
},
285+
},
286+
assertErr: require.NoError,
287+
},
288+
{
289+
description: "valid timezone (Europe/Lisbon)",
290+
schedules: map[string]*accessmonitoringrulesv1.Schedule{
291+
"default": {
292+
Time: &accessmonitoringrulesv1.TimeSchedule{
293+
Timezone: "Europe/Lisbon",
294+
Shifts: []*accessmonitoringrulesv1.TimeSchedule_Shift{
295+
{
296+
Weekday: time.Monday.String(),
297+
Start: "00:00",
298+
End: "23:59",
299+
},
300+
},
301+
},
302+
},
303+
},
304+
assertErr: require.NoError,
305+
},
306+
{
307+
description: "valid timezone (Asia/Singapore)",
308+
schedules: map[string]*accessmonitoringrulesv1.Schedule{
309+
"default": {
310+
Time: &accessmonitoringrulesv1.TimeSchedule{
311+
Timezone: "Asia/Singapore",
312+
Shifts: []*accessmonitoringrulesv1.TimeSchedule_Shift{
313+
{
314+
Weekday: time.Monday.String(),
315+
Start: "00:00",
316+
End: "23:59",
317+
},
318+
},
319+
},
320+
},
321+
},
322+
assertErr: require.NoError,
323+
},
324+
{
325+
description: "invalid timezone",
326+
schedules: map[string]*accessmonitoringrulesv1.Schedule{
327+
"default": {
328+
Time: &accessmonitoringrulesv1.TimeSchedule{
329+
Timezone: "invalid",
330+
Shifts: []*accessmonitoringrulesv1.TimeSchedule_Shift{
331+
{
332+
Weekday: time.Monday.String(),
333+
Start: "00:00",
334+
End: "23:59",
335+
},
336+
},
337+
},
338+
},
339+
},
340+
assertErr: func(t require.TestingT, err error, _ ...interface{}) {
341+
require.ErrorContains(t, err, "invalid timezone")
342+
},
343+
},
252344
{
253345
description: "start time is not before end time",
254346
schedules: map[string]*accessmonitoringrulesv1.Schedule{

0 commit comments

Comments
 (0)