Skip to content

Commit

Permalink
✨ Add auth and audit for namespac (#236)
Browse files Browse the repository at this point in the history
* ✨ Add auth and audit for namespac

* ✨ Update namespace member database name
  • Loading branch information
tosone authored Nov 24, 2023
1 parent f811171 commit 5eb77de
Show file tree
Hide file tree
Showing 50 changed files with 2,240 additions and 2,454 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,5 @@ pkg/**/*.html

.vscode/

package/
*.tar.gz
web/.yarn/
137 changes: 0 additions & 137 deletions docker-compose-dev.yml

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/dal/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestAuth(t *testing.T) {
added, _ := dal.AuthEnforcer.AddPolicy(enums.NamespaceRoleManager.String(), "library", "DS$*/**$manifests$*", "public", "(GET)|(HEAD)", "allow")
assert.True(t, added)

err = roleService.AddNamespaceMember(ctx, 1, models.Namespace{ID: 1, Name: "library"}, enums.NamespaceRoleManager)
_, err = roleService.AddNamespaceMember(ctx, 1, models.Namespace{ID: 1, Name: "library"}, enums.NamespaceRoleManager)
assert.NoError(t, err)
// added, _ = dal.AuthEnforcer.AddRoleForUser("1", enums.NamespaceRoleManager.String(), "library")
// assert.True(t, added)
Expand Down
2 changes: 1 addition & 1 deletion pkg/dal/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {
models.DaemonGcBlobRule{},
models.DaemonGcBlobRunner{},
models.DaemonGcBlobRecord{},
models.NamespaceRole{},
models.NamespaceMember{},
)

g.ApplyInterface(func(models.CacheQuery) {}, models.Cache{})
Expand Down
15 changes: 8 additions & 7 deletions pkg/dal/dao/mocks/namespace_member.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/dal/dao/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func (s *namespaceService) ListNamespaceWithAuth(ctx context.Context, userID int
return nil, 0, err
}
if !(userObj.Role == enums.UserRoleAdmin || userObj.Role == enums.UserRoleRoot) {
q = q.LeftJoin(s.tx.NamespaceRole, s.tx.Namespace.ID.EqCol(s.tx.NamespaceRole.NamespaceID), s.tx.NamespaceRole.UserID.Eq(userID)).
Where(s.tx.NamespaceRole.ID.IsNotNull()).Or(s.tx.Namespace.Visibility.Eq(enums.VisibilityPublic))
q = q.LeftJoin(s.tx.NamespaceMember, s.tx.Namespace.ID.EqCol(s.tx.NamespaceMember.NamespaceID), s.tx.NamespaceMember.UserID.Eq(userID)).
Where(s.tx.NamespaceMember.ID.IsNotNull()).Or(s.tx.Namespace.Visibility.Eq(enums.VisibilityPublic))
}
}
field, ok := s.tx.Namespace.GetFieldByName(ptr.To(sort.Sort))
Expand Down
59 changes: 32 additions & 27 deletions pkg/dal/dao/namespace_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ import (
// NamespaceMemberService is the interface that provides methods to operate on role model
type NamespaceMemberService interface {
// AddNamespaceMember ...
AddNamespaceMember(ctx context.Context, userID int64, namespaceObj models.Namespace, role enums.NamespaceRole) error
AddNamespaceMember(ctx context.Context, userID int64, namespaceObj models.Namespace, role enums.NamespaceRole) (*models.NamespaceMember, error)
// UpdateNamespaceMember ...
UpdateNamespaceMember(ctx context.Context, userID int64, namespaceObj models.Namespace, role enums.NamespaceRole) error
// DeleteNamespaceMember ...
DeleteNamespaceMember(ctx context.Context, userID int64, namespaceObj models.Namespace) error
// ListNamespaceMembers ...
ListNamespaceMembers(ctx context.Context, namespaceID int64, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.NamespaceRole, int64, error)
ListNamespaceMembers(ctx context.Context, namespaceID int64, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.NamespaceMember, int64, error)
// GetNamespaceMember ...
GetNamespaceMember(ctx context.Context, namespaceID int64, userID int64) (*models.NamespaceRole, error)
GetNamespaceMember(ctx context.Context, namespaceID int64, userID int64) (*models.NamespaceMember, error)
// CountNamespaceMember ...
CountNamespaceMember(ctx context.Context, userID int64, namespaceID int64) (int64, error)
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (s *namespaceMemberServiceFactory) New(txs ...*query.Query) NamespaceMember
}

// AddNamespaceMember ...
func (s namespaceMemberService) AddNamespaceMember(ctx context.Context, userID int64, namespaceObj models.Namespace, role enums.NamespaceRole) error {
func (s namespaceMemberService) AddNamespaceMember(ctx context.Context, userID int64, namespaceObj models.Namespace, role enums.NamespaceRole) (*models.NamespaceMember, error) {
err := s.tx.CasbinRule.WithContext(ctx).Create(&models.CasbinRule{
PType: ptr.Of("g"),
V0: ptr.Of(fmt.Sprintf("%d", userID)),
Expand All @@ -86,9 +86,14 @@ func (s namespaceMemberService) AddNamespaceMember(ctx context.Context, userID i
V5: ptr.Of(""),
})
if err != nil {
return err
return nil, err
}
namespaceMember := &models.NamespaceMember{UserID: userID, NamespaceID: namespaceObj.ID, Role: role}
err = s.tx.NamespaceMember.WithContext(ctx).Create(namespaceMember)
if err != nil {
return nil, err
}
return s.tx.NamespaceRole.WithContext(ctx).Create(&models.NamespaceRole{UserID: userID, NamespaceID: namespaceObj.ID, Role: role})
return namespaceMember, nil
}

// UpdateNamespaceMember ...
Expand All @@ -102,11 +107,11 @@ func (s namespaceMemberService) UpdateNamespaceMember(ctx context.Context, userI
if err != nil {
return err
}
_, err = s.tx.NamespaceRole.WithContext(ctx).Where(
s.tx.NamespaceRole.UserID.Eq(userID),
s.tx.NamespaceRole.NamespaceID.Eq(namespaceObj.ID),
_, err = s.tx.NamespaceMember.WithContext(ctx).Where(
s.tx.NamespaceMember.UserID.Eq(userID),
s.tx.NamespaceMember.NamespaceID.Eq(namespaceObj.ID),
).Updates(map[string]any{
query.NamespaceRole.Role.ColumnName().String(): role,
query.NamespaceMember.Role.ColumnName().String(): role,
})
return err
}
Expand All @@ -120,49 +125,49 @@ func (s namespaceMemberService) DeleteNamespaceMember(ctx context.Context, userI
if err != nil {
return err
}
_, err = s.tx.NamespaceRole.WithContext(ctx).Where(
s.tx.NamespaceRole.UserID.Eq(userID),
s.tx.NamespaceRole.NamespaceID.Eq(namespaceObj.ID),
_, err = s.tx.NamespaceMember.WithContext(ctx).Where(
s.tx.NamespaceMember.UserID.Eq(userID),
s.tx.NamespaceMember.NamespaceID.Eq(namespaceObj.ID),
).Delete()
return err
}

// ListNamespaceMembers ...
func (s namespaceMemberService) ListNamespaceMembers(ctx context.Context, namespaceID int64, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.NamespaceRole, int64, error) {
func (s namespaceMemberService) ListNamespaceMembers(ctx context.Context, namespaceID int64, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.NamespaceMember, int64, error) {
pagination = utils.NormalizePagination(pagination)
q := s.tx.NamespaceRole.WithContext(ctx).Where(s.tx.NamespaceRole.NamespaceID.Eq(namespaceID))
q := s.tx.NamespaceMember.WithContext(ctx).Where(s.tx.NamespaceMember.NamespaceID.Eq(namespaceID))
if name != nil {
q = q.RightJoin(s.tx.User, s.tx.NamespaceRole.UserID.EqCol(s.tx.User.ID), s.tx.User.Username.Like(fmt.Sprintf("%s%%", ptr.To(name))))
q = q.RightJoin(s.tx.User, s.tx.NamespaceMember.UserID.EqCol(s.tx.User.ID), s.tx.User.Username.Like(fmt.Sprintf("%s%%", ptr.To(name))))
}
q = q.Preload(s.tx.NamespaceRole.User)
field, ok := s.tx.NamespaceRole.GetFieldByName(ptr.To(sort.Sort))
q = q.Preload(s.tx.NamespaceMember.User)
field, ok := s.tx.NamespaceMember.GetFieldByName(ptr.To(sort.Sort))
if ok {
switch ptr.To(sort.Method) {
case enums.SortMethodDesc:
q = q.Order(field.Desc())
case enums.SortMethodAsc:
q = q.Order(field)
default:
q = q.Order(s.tx.NamespaceRole.UpdatedAt.Desc())
q = q.Order(s.tx.NamespaceMember.UpdatedAt.Desc())
}
} else {
q = q.Order(s.tx.NamespaceRole.UpdatedAt.Desc())
q = q.Order(s.tx.NamespaceMember.UpdatedAt.Desc())
}
return q.FindByPage(ptr.To(pagination.Limit)*(ptr.To(pagination.Page)-1), ptr.To(pagination.Limit))
}

// GetNamespaceMember ...
func (s namespaceMemberService) GetNamespaceMember(ctx context.Context, namespaceID int64, userID int64) (*models.NamespaceRole, error) {
return s.tx.NamespaceRole.WithContext(ctx).Where(
s.tx.NamespaceRole.UserID.Eq(userID),
s.tx.NamespaceRole.NamespaceID.Eq(namespaceID),
func (s namespaceMemberService) GetNamespaceMember(ctx context.Context, namespaceID int64, userID int64) (*models.NamespaceMember, error) {
return s.tx.NamespaceMember.WithContext(ctx).Where(
s.tx.NamespaceMember.UserID.Eq(userID),
s.tx.NamespaceMember.NamespaceID.Eq(namespaceID),
).First()
}

// CountNamespaceMember ...
func (s namespaceMemberService) CountNamespaceMember(ctx context.Context, userID int64, namespaceID int64) (int64, error) {
return s.tx.NamespaceRole.WithContext(ctx).Where(
s.tx.NamespaceRole.UserID.Eq(userID),
s.tx.NamespaceRole.NamespaceID.Eq(namespaceID),
return s.tx.NamespaceMember.WithContext(ctx).Where(
s.tx.NamespaceMember.UserID.Eq(userID),
s.tx.NamespaceMember.NamespaceID.Eq(namespaceID),
).Count()
}
10 changes: 5 additions & 5 deletions pkg/dal/dao/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (s *repositoryService) ListRepositoryWithAuth(ctx context.Context, namespac
pagination = utils.NormalizePagination(pagination)
q := s.tx.Repository.WithContext(ctx).Where(s.tx.Repository.NamespaceID.Eq(namespaceID))
if name != nil {
q = q.Where(s.tx.Repository.Name.Like(fmt.Sprintf("%%%s%%", ptr.To(name))))
q = q.Where(s.tx.Repository.Name.Like(fmt.Sprintf("%s%%", ptr.To(name))))
}
if userID == 0 { // find the public namespace
q = q.Where(s.tx.Repository.Visibility.Eq(enums.VisibilityPublic))
Expand All @@ -204,8 +204,8 @@ func (s *repositoryService) ListRepositoryWithAuth(ctx context.Context, namespac
return nil, 0, err
}
if !(userObj.Role == enums.UserRoleAdmin || userObj.Role == enums.UserRoleRoot) {
q = q.LeftJoin(s.tx.NamespaceRole, s.tx.Repository.NamespaceID.EqCol(s.tx.NamespaceRole.NamespaceID), s.tx.NamespaceRole.UserID.Eq(userID)).
Where(s.tx.NamespaceRole.ID.IsNotNull()).Or(s.tx.Repository.Visibility.Eq(enums.VisibilityPublic))
q = q.LeftJoin(s.tx.NamespaceMember, s.tx.Repository.NamespaceID.EqCol(s.tx.NamespaceMember.NamespaceID), s.tx.NamespaceMember.UserID.Eq(userID)).
Where(s.tx.NamespaceMember.ID.IsNotNull()).Or(s.tx.Repository.Visibility.Eq(enums.VisibilityPublic))
}
}
field, ok := s.tx.Repository.GetFieldByName(ptr.To(sort.Sort))
Expand All @@ -229,7 +229,7 @@ func (s *repositoryService) ListRepository(ctx context.Context, namespaceID int6
pagination = utils.NormalizePagination(pagination)
q := s.tx.Repository.WithContext(ctx).Where(s.tx.Repository.NamespaceID.Eq(namespaceID))
if name != nil {
q = q.Where(s.tx.Repository.Name.Like(fmt.Sprintf("%%%s%%", ptr.To(name))))
q = q.Where(s.tx.Repository.Name.Like(fmt.Sprintf("%s%%", ptr.To(name))))
}
field, ok := s.tx.Repository.GetFieldByName(ptr.To(sort.Sort))
if ok {
Expand Down Expand Up @@ -266,7 +266,7 @@ func (s *repositoryService) UpdateRepository(ctx context.Context, id int64, upda
func (s *repositoryService) CountRepository(ctx context.Context, namespaceID int64, name *string) (int64, error) {
q := s.tx.Repository.WithContext(ctx).Where(s.tx.Repository.NamespaceID.Eq(namespaceID))
if name != nil {
q = q.Where(s.tx.Repository.Name.Like(fmt.Sprintf("%%%s%%", ptr.To(name))))
q = q.Where(s.tx.Repository.Name.Like(fmt.Sprintf("%s%%", ptr.To(name))))
}
return q.Count()
}
Expand Down
Loading

0 comments on commit 5eb77de

Please sign in to comment.