Skip to content

Commit 670bf36

Browse files
authored
fix: make org and project name case insensitive (#867)
* fix: make org and project name case insensitive All org and project slugs will be converted to lower case before they are created. Existing org and projects will continue to work as usual. Signed-off-by: Kush Sharma <thekushsharma@gmail.com> * add additional latency metrics for few service calls Signed-off-by: Kush Sharma <thekushsharma@gmail.com> --------- Signed-off-by: Kush Sharma <thekushsharma@gmail.com>
1 parent 366c9f4 commit 670bf36

File tree

8 files changed

+49
-0
lines changed

8 files changed

+49
-0
lines changed

core/authenticate/service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
frontiersession "github.com/raystack/frontier/core/authenticate/session"
2424
"github.com/raystack/frontier/core/serviceuser"
2525
"github.com/raystack/frontier/internal/bootstrap/schema"
26+
"github.com/raystack/frontier/internal/metrics"
2627
"github.com/raystack/frontier/pkg/errors"
2728

2829
"github.com/lestrrat-go/jwx/v2/jwk"
@@ -739,6 +740,11 @@ func (s Service) getOrCreateUser(ctx context.Context, email, title string) (user
739740
}
740741

741742
func (s Service) GetPrincipal(ctx context.Context, assertions ...ClientAssertion) (Principal, error) {
743+
if metrics.ServiceOprLatency != nil {
744+
promCollect := metrics.ServiceOprLatency("authenticate", "GetPrincipal")
745+
defer promCollect()
746+
}
747+
742748
var currentPrincipal Principal
743749
if len(assertions) == 0 {
744750
// check all assertions

core/organization/service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/raystack/frontier/core/relation"
1919
"github.com/raystack/frontier/core/user"
2020
"github.com/raystack/frontier/internal/bootstrap/schema"
21+
"github.com/raystack/frontier/internal/metrics"
2122
)
2223

2324
type Repository interface {
@@ -229,6 +230,11 @@ func (s Service) Update(ctx context.Context, org Organization) (Organization, er
229230
}
230231

231232
func (s Service) ListByUser(ctx context.Context, principal authenticate.Principal, filter Filter) ([]Organization, error) {
233+
if metrics.ServiceOprLatency != nil {
234+
promCollect := metrics.ServiceOprLatency("organization", "ListByUser")
235+
defer promCollect()
236+
}
237+
232238
subjectIDs, err := s.relationService.LookupResources(ctx, relation.Relation{
233239
Object: relation.Object{
234240
Namespace: schema.OrganizationNamespace,

internal/metrics/metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
func Init() {
1010
initStripe()
1111
initDB()
12+
initService()
1213
}
1314

1415
type HistogramFunc func(labelValue ...string) func()

internal/metrics/service.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package metrics
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
"github.com/prometheus/client_golang/prometheus/promauto"
6+
)
7+
8+
var ServiceOprLatency HistogramFunc
9+
10+
func initService() {
11+
ServiceOprLatency = createMeasureTime(promauto.NewHistogramVec(prometheus.HistogramOpts{
12+
Name: "service_operation_latency",
13+
Help: "Time taken for service operation to complete",
14+
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 30, 60},
15+
}, []string{"service", "operation"}))
16+
}

internal/store/postgres/organization_repository.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (r OrganizationRepository) Create(ctx context.Context, org organization.Org
146146
if strings.TrimSpace(org.Name) == "" {
147147
return organization.Organization{}, organization.ErrInvalidDetail
148148
}
149+
org.Name = strings.ToLower(org.Name)
149150

150151
marshaledMetadata, err := json.Marshal(org.Metadata)
151152
if err != nil {

internal/store/postgres/organization_repository_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ func (s *OrganizationRepositoryTestSuite) TestCreate() {
236236
},
237237
ErrString: organization.ErrConflict.Error(),
238238
},
239+
{
240+
Description: "should return error if organization name already exist case sensitive",
241+
OrganizationToCreate: organization.Organization{
242+
Name: "ORG-1",
243+
Metadata: metadata.Metadata{},
244+
},
245+
ErrString: organization.ErrConflict.Error(),
246+
},
239247
{
240248
Description: "should return error if organization name is empty",
241249
OrganizationToCreate: organization.Organization{

internal/store/postgres/project_repository.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (r ProjectRepository) Create(ctx context.Context, prj project.Project) (pro
108108
if strings.TrimSpace(prj.Name) == "" {
109109
return project.Project{}, project.ErrInvalidDetail
110110
}
111+
prj.Name = strings.ToLower(prj.Name)
111112

112113
marshaledMetadata, err := json.Marshal(prj.Metadata)
113114
if err != nil {

internal/store/postgres/project_repository_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ func (s *ProjectRepositoryTestSuite) TestCreate() {
250250
},
251251
ErrString: project.ErrConflict.Error(),
252252
},
253+
{
254+
Description: "should return error if project slug already exist case sensitive",
255+
ProjectToCreate: project.Project{
256+
Name: "PROJECT-2",
257+
Organization: organization.Organization{
258+
ID: s.orgs[0].ID,
259+
},
260+
},
261+
ErrString: project.ErrConflict.Error(),
262+
},
253263
{
254264
Description: "should return error if org id not an uuid",
255265
ProjectToCreate: project.Project{

0 commit comments

Comments
 (0)