Skip to content

Commit ac4d723

Browse files
authored
fix: add validation to cron (#159)
1 parent 9495777 commit ac4d723

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ require (
4646
github.com/mitchellh/reflectwalk v1.0.2 // indirect
4747
github.com/oklog/run v1.0.0 // indirect
4848
github.com/pmezard/go-difflib v1.0.0 // indirect
49+
github.com/robfig/cron/v3 v3.0.1 // indirect
4950
github.com/rogpeppe/go-internal v1.8.0 // indirect
5051
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
5152
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
143143
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
144144
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
145145
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
146+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
147+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
146148
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
147149
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
148150
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=

provider/script.go

+15
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ package provider
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/google/uuid"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11+
"github.com/robfig/cron/v3"
1012
)
1113

14+
var ScriptCRONParser = cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.DowOptional | cron.Descriptor)
15+
1216
func scriptResource() *schema.Resource {
1317
return &schema.Resource{
1418
Description: "Use this resource to run a script from an agent.",
@@ -56,6 +60,17 @@ func scriptResource() *schema.Resource {
5660
Type: schema.TypeString,
5761
Optional: true,
5862
Description: "The cron schedule to run the script on. This is a cron expression.",
63+
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
64+
v, ok := i.(string)
65+
if !ok {
66+
return []string{}, []error{fmt.Errorf("got type %T instead of string", i)}
67+
}
68+
_, err := ScriptCRONParser.Parse(v)
69+
if err != nil {
70+
return []string{}, []error{fmt.Errorf("%s is not a valid cron expression: %w", v, err)}
71+
}
72+
return nil, nil
73+
},
5974
},
6075
"start_blocks_login": {
6176
Type: schema.TypeBool,

0 commit comments

Comments
 (0)