Skip to content
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

feat: add validation for /groups endpoints #234

Merged
merged 16 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/getkin/kin-openapi v0.123.0
github.com/go-chi/chi/v5 v5.0.10
github.com/go-chi/cors v1.2.1
github.com/go-playground/validator/v10 v10.19.0
github.com/google/uuid v1.5.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/oapi-codegen/runtime v1.1.1
Expand Down Expand Up @@ -49,11 +50,14 @@ require (
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
Expand All @@ -69,6 +73,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -86,12 +91,13 @@ require (
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
26 changes: 20 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBd
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/getkin/kin-openapi v0.123.0 h1:zIik0mRwFNLyvtXK274Q6ut+dPh6nlxBp0x7mNrPhs8=
github.com/getkin/kin-openapi v0.123.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
Expand All @@ -43,6 +45,14 @@ github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw=
github.com/go-openapi/swag v0.22.8/go.mod h1:6QT22icPLEqAM/z/TChgb4WAveCHF92+2gF0CNjHpPI=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4=
github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
Expand Down Expand Up @@ -96,6 +106,8 @@ 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=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
Expand Down Expand Up @@ -197,6 +209,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand All @@ -208,8 +222,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/oauth2 v0.13.0 h1:jDDenyj+WgFtmV3zYVoi8aE2BwtXFLWOA67ZfNWftiY=
golang.org/x/oauth2 v0.13.0/go.mod h1:/JMhi4ZRXAf4HG9LiNmxvk+45+96RUlVThiH8FzNBn0=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -226,12 +240,12 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down
3 changes: 2 additions & 1 deletion rebac-admin-backend/v1/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func newReBACAdminBackendWithService(params ReBACAdminBackendParams, handler res
// Handler returns HTTP handlers implementing the ReBAC Admin OpenAPI spec.
func (b *ReBACAdminBackend) Handler(baseURL string) http.Handler {
baseURL, _ = strings.CutSuffix(baseURL, "/")
return resources.HandlerWithOptions(b.handler, resources.ChiServerOptions{
h := newHandlerWithValidation(b.handler)
return resources.HandlerWithOptions(h, resources.ChiServerOptions{
BaseURL: baseURL + "/v1",
ErrorHandlerFunc: func(w http.ResponseWriter, _ *http.Request, err error) {
writeErrorResponse(w, err)
Expand Down
16 changes: 16 additions & 0 deletions rebac-admin-backend/v1/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func NewNotFoundError(message string) error {
}
}

// NewMissingRequestBodyError returns an error instance that represents a missing request body error.
func NewMissingRequestBodyError(message string) error {
return &errorWithStatus{
status: http.StatusBadRequest,
message: fmt.Sprintf("missing request body: %s", message),
}
}

// NewValidationError returns an error instance that represents an input validation error.
func NewValidationError(message string) error {
return &errorWithStatus{
Expand All @@ -54,6 +62,14 @@ func NewValidationError(message string) error {
}
}

// NewRequestBodyValidationError returns an error instance that represents a request body validation error.
func NewRequestBodyValidationError(message string) error {
return &errorWithStatus{
status: http.StatusBadRequest,
message: fmt.Sprintf("invalid request body: %s", message),
}
}

// NewUnknownError returns an error instance that represents an unknown internal error.
func NewUnknownError(message string) error {
return &errorWithStatus{
Expand Down
76 changes: 45 additions & 31 deletions rebac-admin-backend/v1/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package v1

import (
"encoding/json"
"net/http"

"github.com/canonical/identity-platform-admin-ui/rebac-admin-backend/v1/resources"
Expand Down Expand Up @@ -36,21 +35,25 @@ func (h handler) GetGroups(w http.ResponseWriter, req *http.Request, params reso
func (h handler) PostGroups(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()

group := new(resources.Group)
defer req.Body.Close()
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if err := json.NewDecoder(req.Body).Decode(group); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
group, ok := body.(*resources.Group)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

group, err := h.Groups.CreateGroup(ctx, group)
result, err := h.Groups.CreateGroup(ctx, group)
if err != nil {
writeServiceErrorResponse(w, h.GroupsErrorMapper, err)
return
}

writeResponse(w, http.StatusCreated, group)
writeResponse(w, http.StatusCreated, result)
}

// DeleteGroupsItem deletes the specified group identified by the provided ID.
Expand Down Expand Up @@ -86,26 +89,25 @@ func (h handler) GetGroupsItem(w http.ResponseWriter, req *http.Request, id stri
func (h handler) PutGroupsItem(w http.ResponseWriter, req *http.Request, id string) {
ctx := req.Context()

group := new(resources.Group)
defer req.Body.Close()

if err := json.NewDecoder(req.Body).Decode(group); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if id != *group.Id {
writeErrorResponse(w, NewValidationError("group ID from path does not match the Group object"))
group, ok := body.(*resources.Group)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

group, err := h.Groups.UpdateGroup(ctx, group)
result, err := h.Groups.UpdateGroup(ctx, group)
if err != nil {
writeServiceErrorResponse(w, h.GroupsErrorMapper, err)
return
}

writeResponse(w, http.StatusOK, group)
writeResponse(w, http.StatusOK, result)
}

// GetGroupsItemEntitlements returns the list of entitlements for a group identified by the provided ID.
Expand Down Expand Up @@ -134,15 +136,19 @@ func (h handler) GetGroupsItemEntitlements(w http.ResponseWriter, req *http.Requ
func (h handler) PatchGroupsItemEntitlements(w http.ResponseWriter, req *http.Request, id string) {
ctx := req.Context()

patchesRequest := new(resources.GroupEntitlementsPatchRequestBody)
defer req.Body.Close()
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if err := json.NewDecoder(req.Body).Decode(patchesRequest); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
groupEntitlements, ok := body.(*resources.GroupEntitlementsPatchRequestBody)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

_, err := h.Groups.PatchGroupEntitlements(ctx, id, patchesRequest.Patches)
_, err = h.Groups.PatchGroupEntitlements(ctx, id, groupEntitlements.Patches)
if err != nil {
writeServiceErrorResponse(w, h.GroupsErrorMapper, err)
return
Expand Down Expand Up @@ -177,15 +183,19 @@ func (h handler) GetGroupsItemIdentities(w http.ResponseWriter, req *http.Reques
func (h handler) PatchGroupsItemIdentities(w http.ResponseWriter, req *http.Request, id string) {
ctx := req.Context()

patchesRequest := new(resources.GroupIdentitiesPatchRequestBody)
defer req.Body.Close()
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if err := json.NewDecoder(req.Body).Decode(patchesRequest); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
groupIdentities, ok := body.(*resources.GroupIdentitiesPatchRequestBody)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

_, err := h.Groups.PatchGroupIdentities(ctx, id, patchesRequest.Patches)
_, err = h.Groups.PatchGroupIdentities(ctx, id, groupIdentities.Patches)
if err != nil {
writeServiceErrorResponse(w, h.GroupsErrorMapper, err)
return
Expand Down Expand Up @@ -220,15 +230,19 @@ func (h handler) GetGroupsItemRoles(w http.ResponseWriter, req *http.Request, id
func (h handler) PatchGroupsItemRoles(w http.ResponseWriter, req *http.Request, id string) {
ctx := req.Context()

patchesRequest := new(resources.GroupRolesPatchRequestBody)
defer req.Body.Close()
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if err := json.NewDecoder(req.Body).Decode(patchesRequest); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
groupRoles, ok := body.(*resources.GroupRolesPatchRequestBody)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

_, err := h.Groups.PatchGroupRoles(ctx, id, patchesRequest.Patches)
_, err = h.Groups.PatchGroupRoles(ctx, id, groupRoles.Patches)
if err != nil {
writeServiceErrorResponse(w, h.GroupsErrorMapper, err)
return
Expand Down
Loading
Loading