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

✨ Add auth service #234

Merged
merged 1 commit into from
Nov 22, 2023
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
7 changes: 2 additions & 5 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/go-sigma/sigma/pkg/auth"
"github.com/go-sigma/sigma/pkg/cmds/server"
"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/dal"
Expand Down Expand Up @@ -53,11 +54,7 @@ var serverCmd = &cobra.Command{
return
}

// err = daemon.InitializeClient()
// if err != nil {
// log.Error().Err(err).Msg("Initialize daemon client with error")
// return
// }
auth.Initialize()

err = server.Serve(server.ServerConfig{
WithoutDistribution: withoutDistribution,
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ require (
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible
github.com/anchore/syft v0.97.1
github.com/aquasecurity/trivy v0.47.0
github.com/aws/aws-sdk-go v1.48.1
github.com/aws/aws-sdk-go v1.48.2
github.com/bytedance/json v0.0.0-20190516032711-0d89175f1949
github.com/caarlos0/env/v9 v9.0.0
github.com/casbin/casbin/v2 v2.77.2
github.com/casbin/gorm-adapter/v3 v3.20.0
github.com/deckarep/golang-set/v2 v2.3.1
github.com/deckarep/golang-set/v2 v2.4.0
github.com/distribution/distribution/v3 v3.0.0-20231016181039-1d410148efe6
github.com/distribution/reference v0.5.0
github.com/docker/cli v24.0.7+incompatible
Expand Down Expand Up @@ -74,7 +74,7 @@ require (
gorm.io/driver/sqlite v1.5.4
gorm.io/gen v0.3.23
gorm.io/gorm v1.25.5
gorm.io/plugin/dbresolver v1.4.7
gorm.io/plugin/dbresolver v1.5.0
gorm.io/plugin/soft_delete v1.2.1
k8s.io/api v0.28.4
k8s.io/apimachinery v0.28.4
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/aws/aws-sdk-go v1.48.1 h1:OXPUVL4cLdsDsqkVIuhwY+D389tjI7e1xu0lsDYyeMk=
github.com/aws/aws-sdk-go v1.48.1/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go v1.48.2 h1:Lf7+Y4WmHB0AQLRQZA46diSwDa+LWbwY6IGaYoCVtTc=
github.com/aws/aws-sdk-go v1.48.2/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/becheran/wildmatch-go v1.0.0 h1:mE3dGGkTmpKtT4Z+88t8RStG40yN9T+kFEGj2PZFSzA=
github.com/becheran/wildmatch-go v1.0.0/go.mod h1:gbMvj0NtVdJ15Mg/mH9uxk2R1QCistMyU7d9KFzroX4=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -234,8 +234,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0=
github.com/davidmz/go-pageant v1.0.2/go.mod h1:P2EDDnMqIwG5Rrp05dTRITj9z2zpGcD9efWSkTNKLIE=
github.com/deckarep/golang-set/v2 v2.3.1 h1:vjmkvJt/IV27WXPyYQpAh4bRyWJc5Y435D17XQ9QU5A=
github.com/deckarep/golang-set/v2 v2.3.1/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/deckarep/golang-set/v2 v2.4.0 h1:DnfgWKdhvHM8Kihdw9fKWXd08EdsPiyoHsk5bfsmkNI=
github.com/deckarep/golang-set/v2 v2.4.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
Expand Down Expand Up @@ -1646,8 +1646,8 @@ gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/hints v1.1.2 h1:b5j0kwk5p4+3BtDtYqqfY+ATSxjj+6ptPgVveuynn9o=
gorm.io/hints v1.1.2/go.mod h1:/ARdpUHAtyEMCh5NNi3tI7FsGh+Cj/MIUlvNxCNCFWg=
gorm.io/plugin/dbresolver v1.4.7 h1:ZwtwmJQxTx9us7o6zEHFvH1q4OeEo1pooU7efmnunJA=
gorm.io/plugin/dbresolver v1.4.7/go.mod h1:l4Cn87EHLEYuqUncpEeTC2tTJQkjngPSD+lo8hIvcT0=
gorm.io/plugin/dbresolver v1.5.0 h1:XVHLxh775eP0CqVh3vcfJtYqja3uFl5Wr3cKlY8jgDY=
gorm.io/plugin/dbresolver v1.5.0/go.mod h1:l4Cn87EHLEYuqUncpEeTC2tTJQkjngPSD+lo8hIvcT0=
gorm.io/plugin/soft_delete v1.2.1 h1:qx9D/c4Xu6w5KT8LviX8DgLcB9hkKl6JC9f44Tj7cGU=
gorm.io/plugin/soft_delete v1.2.1/go.mod h1:Zv7vQctOJTGOsJ/bWgrN1n3od0GBAZgnLjEx+cApLGk=
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
Expand Down
75 changes: 75 additions & 0 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package auth

import (
"github.com/labstack/echo/v4"

"github.com/go-sigma/sigma/pkg/dal/dao"
"github.com/go-sigma/sigma/pkg/types/enums"
)

// Service is the interface for the auth service
type Service interface {
// Namespace ...
Namespace(c echo.Context, namespaceID int64, auth enums.Auth) bool
// Repository ...
Repository(c echo.Context, repositoryID int64, auth enums.Auth) bool
}

var _ Service = &service{}

type service struct {
roleServiceFactory dao.NamespaceMemberServiceFactory
namespaceServiceFactory dao.NamespaceServiceFactory
repositoryServiceFactory dao.RepositoryServiceFactory
}

type inject struct {
roleServiceFactory dao.NamespaceMemberServiceFactory
namespaceServiceFactory dao.NamespaceServiceFactory
repositoryServiceFactory dao.RepositoryServiceFactory
}

var s Service

// Initialize ...
func Initialize(injects ...inject) {
roleServiceFactory := dao.NewNamespaceMemberServiceFactory()
namespaceServiceFactory := dao.NewNamespaceServiceFactory()
repositoryServiceFactory := dao.NewRepositoryServiceFactory()
if len(injects) > 0 {
ij := injects[0]
if ij.roleServiceFactory != nil {
roleServiceFactory = ij.roleServiceFactory
}
if ij.namespaceServiceFactory != nil {
namespaceServiceFactory = ij.namespaceServiceFactory
}
if ij.repositoryServiceFactory != nil {
repositoryServiceFactory = ij.repositoryServiceFactory
}
}
s = &service{
roleServiceFactory: roleServiceFactory,
namespaceServiceFactory: namespaceServiceFactory,
repositoryServiceFactory: repositoryServiceFactory,
}
}

// GetInstance ...
func GetInstance() Service {
return s
}
76 changes: 76 additions & 0 deletions pkg/auth/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package auth

import (
"errors"

"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/types/enums"
)

// Namespace ...
func (s service) Namespace(c echo.Context, namespaceID int64, auth enums.Auth) bool {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return false
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return false
}
namespaceService := s.namespaceServiceFactory.New()
// 1. check namespace visibility
namespaceObj, err := namespaceService.Get(ctx, namespaceID)
if err != nil {
log.Error().Err(err).Msg("Get namespace by id failed")
return false
}
if namespaceObj.Visibility == enums.VisibilityPublic && auth == enums.AuthRead {
return true
}
// 2. check user is admin or not
if user.Role == enums.UserRoleAdmin || user.Role == enums.UserRoleRoot {
return true
}
// 3. check user is member of the namespace
roleService := s.roleServiceFactory.New()
namespaceMemberObj, err := roleService.GetNamespaceMember(ctx, namespaceID, user.ID)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
log.Error().Err(err).Msg("Get namespace member failed")
}
return false
}
if namespaceMemberObj.Role == enums.NamespaceRoleReader && auth == enums.AuthRead {
return true
}
if namespaceMemberObj.Role == enums.NamespaceRoleManager && (auth == enums.AuthManage || auth == enums.AuthRead) {
return true
}
if namespaceMemberObj.Role == enums.NamespaceRoleAdmin && (auth == enums.AuthAdmin || auth == enums.AuthManage || auth == enums.AuthRead) {
return true
}
return false
}
40 changes: 40 additions & 0 deletions pkg/auth/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package auth

import (
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"

"github.com/go-sigma/sigma/pkg/types/enums"
)

// Repository ...
func (s service) Repository(c echo.Context, repositoryID int64, auth enums.Auth) bool {
ctx := log.Logger.WithContext(c.Request().Context())

repositoryService := s.repositoryServiceFactory.New()
repositoryObj, err := repositoryService.Get(ctx, repositoryID)
if err != nil {
log.Error().Err(err).Msg("Get repository by id failed")
return false
}

if repositoryObj.Visibility == enums.VisibilityPublic && auth == enums.AuthRead {
return true
}

return s.Namespace(c, repositoryObj.NamespaceID, auth)
}
4 changes: 2 additions & 2 deletions pkg/dal/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func TestAuth(t *testing.T) {
assert.NoError(t, err)

ctx := log.Logger.WithContext(context.Background())
roleService := dao.NewRoleServiceFactory().New()
roleService := dao.NewNamespaceMemberServiceFactory().New()

added, _ := dal.AuthEnforcer.AddPolicy(enums.NamespaceRoleManager.String(), "library", "DS$*/**$manifests$*", "public", "(GET)|(HEAD)", "allow")
assert.True(t, added)

err = roleService.AddNamespaceRole(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
79 changes: 0 additions & 79 deletions pkg/dal/dao/auth.go

This file was deleted.

Loading
Loading