From f839e75757b61053568705f26338919d57eae7ed Mon Sep 17 00:00:00 2001 From: Andy Chao Date: Mon, 9 Sep 2024 11:21:45 +0800 Subject: [PATCH] MG-2296 - Domain creation allowed with nil alias (#2297) --- auth/api/http/domains/endpoint_test.go | 14 ++++++++++++++ auth/api/http/domains/requests.go | 7 ++++++- auth/postgres/domains_test.go | 18 ++++++++++++++++++ auth/postgres/init.go | 6 ++++++ internal/api/common.go | 1 + 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/auth/api/http/domains/endpoint_test.go b/auth/api/http/domains/endpoint_test.go index bed4dd25d8..4117775cab 100644 --- a/auth/api/http/domains/endpoint_test.go +++ b/auth/api/http/domains/endpoint_test.go @@ -165,6 +165,20 @@ func TestCreateDomain(t *testing.T) { status: http.StatusBadRequest, err: apiutil.ErrMissingName, }, + { + desc: "register a new domain with an empty alias", + domain: auth.Domain{ + ID: ID, + Name: "test", + Metadata: mgclients.Metadata{"role": "domain"}, + Tags: []string{"tag1", "tag2"}, + Alias: "", + }, + token: validToken, + contentType: contentType, + status: http.StatusBadRequest, + err: apiutil.ErrMissingAlias, + }, { desc: "register a new domain with invalid content type", domain: auth.Domain{ diff --git a/auth/api/http/domains/requests.go b/auth/api/http/domains/requests.go index 179bcd163d..0d804700a3 100644 --- a/auth/api/http/domains/requests.go +++ b/auth/api/http/domains/requests.go @@ -25,7 +25,7 @@ type createDomainReq struct { Name string `json:"name"` Metadata map[string]interface{} `json:"metadata,omitempty"` Tags []string `json:"tags,omitempty"` - Alias string `json:"alias,omitempty"` + Alias string `json:"alias"` } func (req createDomainReq) validate() error { @@ -36,6 +36,11 @@ func (req createDomainReq) validate() error { if req.Name == "" { return apiutil.ErrMissingName } + + if req.Alias == "" { + return apiutil.ErrMissingAlias + } + return nil } diff --git a/auth/postgres/domains_test.go b/auth/postgres/domains_test.go index fb80d9cd38..a48ffa9ba4 100644 --- a/auth/postgres/domains_test.go +++ b/auth/postgres/domains_test.go @@ -169,6 +169,24 @@ func TestSave(t *testing.T) { }, err: nil, }, + { + desc: "add domain with empty alias", + domain: auth.Domain{ + ID: testsutil.GenerateUUID(&testing.T{}), + Name: "test1", + Alias: "", + Tags: []string{"test"}, + Metadata: map[string]interface{}{ + "test": "test", + }, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + CreatedBy: userID, + UpdatedBy: userID, + Status: auth.EnabledStatus, + }, + err: repoerr.ErrCreateEntity, + }, { desc: "add domain with malformed metadata", domain: auth.Domain{ diff --git a/auth/postgres/init.go b/auth/postgres/init.go index bae8674b36..ae69c3a0ca 100644 --- a/auth/postgres/init.go +++ b/auth/postgres/init.go @@ -51,6 +51,12 @@ func Migration() *migrate.MemoryMigrationSource { `DROP TABLE IF EXISTS keys`, }, }, + { + Id: "auth_2", + Up: []string{ + `ALTER TABLE domains ALTER COLUMN alias SET NOT NULL`, + }, + }, }, } } diff --git a/internal/api/common.go b/internal/api/common.go index 34ea7f0fdb..0a3eb2e068 100644 --- a/internal/api/common.go +++ b/internal/api/common.go @@ -124,6 +124,7 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) { errors.Contains(err, errors.ErrMalformedEntity), errors.Contains(err, apiutil.ErrMissingID), errors.Contains(err, apiutil.ErrMissingName), + errors.Contains(err, apiutil.ErrMissingAlias), errors.Contains(err, apiutil.ErrMissingEmail), errors.Contains(err, apiutil.ErrMissingHost), errors.Contains(err, apiutil.ErrInvalidResetPass),