From e2ae2b3862abdca821b6047494a8cfebc7321bd7 Mon Sep 17 00:00:00 2001 From: Ivan Savciuc Date: Thu, 16 Nov 2023 15:35:28 +0200 Subject: [PATCH 1/2] feat: make sure emails are lowercase and valid --- CHANGELOG.md | 1 + docs/data-sources/account_team_member.md | 2 +- docs/data-sources/project_user.md | 2 +- docs/resources/account_team_member.md | 2 +- docs/resources/project_user.md | 2 +- go.mod | 2 +- go.sum | 12 ++++++++++++ internal/schemautil/schemautil.go | 17 +++++++++++++++++ internal/service/account/account_team_member.go | 10 ++++++---- internal/service/project/project_user.go | 9 +++++---- 10 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5142ab2b..2fdc1a7e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ nav_order: 1 ## [X.Y.Z] - YYYY-MM-DD - Fix incorrect read context in MySQL user resource +- Add validation for email fields in `account_team_member`, `organization_user` and `project_user` resources to check if email is lowercase and valid ## [3.13.4] - 2023-09-28 diff --git a/docs/data-sources/account_team_member.md b/docs/data-sources/account_team_member.md index cf6ba4121..5f07979ac 100644 --- a/docs/data-sources/account_team_member.md +++ b/docs/data-sources/account_team_member.md @@ -27,7 +27,7 @@ data "aiven_account_team_member" "foo" { - `account_id` (String) The unique account id This property cannot be changed, doing so forces recreation of the resource. - `team_id` (String) An account team id This property cannot be changed, doing so forces recreation of the resource. -- `user_email` (String) Is a user email address that first will be invited, and after accepting an invitation, he or she becomes a member of a team. This property cannot be changed, doing so forces recreation of the resource. +- `user_email` (String) Is a user email address that first will be invited, and after accepting an invitation, he or she becomes a member of a team. Should be lowercase. ### Read-Only diff --git a/docs/data-sources/project_user.md b/docs/data-sources/project_user.md index f706c30e6..70326172a 100644 --- a/docs/data-sources/project_user.md +++ b/docs/data-sources/project_user.md @@ -24,7 +24,7 @@ data "aiven_project_user" "mytestuser" { ### Required -- `email` (String) Email address of the user. This property cannot be changed, doing so forces recreation of the resource. +- `email` (String) Email address of the user. Should be lowercase - `project` (String) Identifies the project this resource belongs to. To set up proper dependencies please refer to this variable as a reference. This property cannot be changed, doing so forces recreation of the resource. ### Read-Only diff --git a/docs/resources/account_team_member.md b/docs/resources/account_team_member.md index 87a493ab9..8784f5077 100644 --- a/docs/resources/account_team_member.md +++ b/docs/resources/account_team_member.md @@ -38,7 +38,7 @@ resource "aiven_account_team_member" "foo" { - `account_id` (String) The unique account id This property cannot be changed, doing so forces recreation of the resource. - `team_id` (String) An account team id This property cannot be changed, doing so forces recreation of the resource. -- `user_email` (String) Is a user email address that first will be invited, and after accepting an invitation, he or she becomes a member of a team. This property cannot be changed, doing so forces recreation of the resource. +- `user_email` (String) Is a user email address that first will be invited, and after accepting an invitation, he or she becomes a member of a team. Should be lowercase. ### Optional diff --git a/docs/resources/project_user.md b/docs/resources/project_user.md index b3ab8e863..2fa6d986b 100644 --- a/docs/resources/project_user.md +++ b/docs/resources/project_user.md @@ -25,7 +25,7 @@ resource "aiven_project_user" "mytestuser" { ### Required -- `email` (String) Email address of the user. This property cannot be changed, doing so forces recreation of the resource. +- `email` (String) Email address of the user. Should be lowercase - `member_type` (String) Project membership type. The possible values are `admin`, `developer` and `operator`. - `project` (String) Identifies the project this resource belongs to. To set up proper dependencies please refer to this variable as a reference. This property cannot be changed, doing so forces recreation of the resource. diff --git a/go.mod b/go.mod index 33b05442c..fa521e68e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/aiven/terraform-provider-aiven -go 1.18 +go 1.21 require ( github.com/aiven/aiven-go-client v1.7.1-0.20230208122034-270adff994ec diff --git a/go.sum b/go.sum index a32aa8f0f..0d1e43121 100644 --- a/go.sum +++ b/go.sum @@ -60,6 +60,7 @@ github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4t github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc= github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM= github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0 h1:MzVXffFUye+ZcSR6opIgz9Co7WcDx6ZcY+RjfFHoA0I= +github.com/apparentlymart/go-dump v0.0.0-20190214190832-042adf3cf4a0/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM= github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk= github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec= github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw= @@ -105,6 +106,7 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= +github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E= @@ -258,6 +260,7 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE= +github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74= github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a h1:zPPuIq2jAWWPTrGt70eK/BSch+gFAGrNzecsoENgu2o= github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a/go.mod h1:yL958EeXv8Ylng6IfnvG4oflryUi3vgA3xPs9hmII1s= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= @@ -280,6 +283,7 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -316,11 +320,15 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758= +github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA= github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= +github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -333,6 +341,7 @@ github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdk github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= +github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -556,6 +565,7 @@ golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 h1:fqTvyMIIj+HRzMmnzr9NtpHP6 golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -745,6 +755,7 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= @@ -752,6 +763,7 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/schemautil/schemautil.go b/internal/schemautil/schemautil.go index 9f3ae313f..56c34a639 100644 --- a/internal/schemautil/schemautil.go +++ b/internal/schemautil/schemautil.go @@ -2,6 +2,7 @@ package schemautil import ( "fmt" + "net/mail" "net/url" "regexp" "strconv" @@ -349,3 +350,19 @@ func unmarshalUserConfig(src interface{}) ([]map[string]interface{}, error) { return []map[string]interface{}{config}, nil } + +// ValidateEmailAddress is a ValidateFunc that ensures a string is a valid email address +func ValidateEmailAddress(v any, k string) (ws []string, errors []error) { + addr, err := mail.ParseAddress(v.(string)) + if err != nil { + errors = append(errors, err) + + return + } + + if strings.ToLower(addr.Address) != addr.Address { + errors = append(errors, fmt.Errorf("%q: invalid email address", k)) + } + + return +} diff --git a/internal/service/account/account_team_member.go b/internal/service/account/account_team_member.go index c04d5f95f..4d04807af 100644 --- a/internal/service/account/account_team_member.go +++ b/internal/service/account/account_team_member.go @@ -25,10 +25,12 @@ var aivenAccountTeamMemberSchema = map[string]*schema.Schema{ Description: schemautil.Complex("An account team id").ForceNew().Build(), }, "user_email": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - Description: schemautil.Complex("Is a user email address that first will be invited, and after accepting an invitation, he or she becomes a member of a team.").ForceNew().Build(), + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Is a user email address that first will be invited, and after accepting an invitation, he " + + "or she becomes a member of a team. Should be lowercase.", + ValidateFunc: schemautil.ValidateEmailAddress, }, "invited_by_user_email": { Type: schema.TypeString, diff --git a/internal/service/project/project_user.go b/internal/service/project/project_user.go index 0168ccae3..e1554a987 100644 --- a/internal/service/project/project_user.go +++ b/internal/service/project/project_user.go @@ -14,10 +14,11 @@ import ( var aivenProjectUserSchema = map[string]*schema.Schema{ "project": schemautil.CommonSchemaProjectReference, "email": { - ForceNew: true, - Required: true, - Type: schema.TypeString, - Description: schemautil.Complex("Email address of the user.").ForceNew().Build(), + ForceNew: true, + Required: true, + Type: schema.TypeString, + Description: "Email address of the user. Should be lowercase", + ValidateFunc: schemautil.ValidateEmailAddress, }, "member_type": { Required: true, From 016085e5007d3173260715e947cc6ec56b4d35e7 Mon Sep 17 00:00:00 2001 From: Ivan Savciuc Date: Thu, 16 Nov 2023 18:11:07 +0200 Subject: [PATCH 2/2] fix(ci): semgrep job --- .github/workflows/lint.yml | 8 ++++++-- CHANGELOG.md | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f8fd0b4e9..ab679613d 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -31,9 +31,13 @@ jobs: semgrep: runs-on: ubuntu-latest if: (github.event_name == 'pull_request' && !contains(github.event.pull_request.labels.*.name, 'skip workflows')) || github.event_name == 'push' + container: + image: returntocorp/semgrep steps: - - uses: actions/checkout@v3 - - uses: returntocorp/semgrep-action@v1 + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - run: semgrep ci --config auto --no-suppress-errors make_lint: runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fdc1a7e0..34aa4aff2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ nav_order: 1 ## [X.Y.Z] - YYYY-MM-DD - Fix incorrect read context in MySQL user resource -- Add validation for email fields in `account_team_member`, `organization_user` and `project_user` resources to check if email is lowercase and valid +- Add validation for email fields in `account_team_member` and `project_user` resources to check if email is lowercase and valid ## [3.13.4] - 2023-09-28