Skip to content

Commit

Permalink
✨ Change distribution user get func
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone committed Apr 11, 2024
1 parent 733ea83 commit 9729129
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 87 deletions.
19 changes: 14 additions & 5 deletions .github/workflows/image-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,21 @@ jobs:
with:
username: tosone
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Install latest Skopeo # GitHub's ubuntu 22 uses Skopeo 1.4 but we need 1.14
- name: Build latest skopeo
run: |
echo 'deb http://download.opensuse.org/repositories/home:/alvistack/xUbuntu_22.04/ /' | sudo tee /etc/apt/sources.list.d/home:alvistack.list
curl -fsSL https://download.opensuse.org/repositories/home:alvistack/xUbuntu_22.04/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_alvistack.gpg > /dev/null
sudo apt update
sudo apt -o Dpkg::Options::="--force-overwrite" install skopeo
git clone --depth 1 --branch v1.15.0 https://github.com/containers/skopeo.git
cd skopeo
DISABLE_CGO=1 make bin/skopeo
sudo cp ./bin/skopeo /usr/bin/skopeo
cd ..
rm -rf skopeo
skopeo --version
# - name: Install latest Skopeo # GitHub's ubuntu 22 uses Skopeo 1.4 but we need 1.14
# run: |
# echo 'deb http://download.opensuse.org/repositories/home:/alvistack/xUbuntu_22.04/ /' | sudo tee /etc/apt/sources.list.d/home:alvistack.list
# curl -fsSL https://download.opensuse.org/repositories/home:alvistack/xUbuntu_22.04/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_alvistack.gpg > /dev/null
# sudo apt update
# sudo apt -o Dpkg::Options::="--force-overwrite" install skopeo
- name: Save dockerfile to local
run: |
make dockerfile-local
Expand Down
16 changes: 6 additions & 10 deletions pkg/handlers/distribution/base/repository_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,22 @@ import (
"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/utils"
"github.com/go-sigma/sigma/pkg/xerrors"
)

// ListRepositories handles the list repositories request
func (h *handler) ListRepositories(c echo.Context) error {
iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, needRet, err := utils.GetUserFromCtxForDs(c)
if err != nil {
return err

Check warning on line 37 in pkg/handlers/distribution/base/repository_list.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/base/repository_list.go#L37

Added line #L37 was not covered by tests
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
if needRet {
return nil

Check warning on line 40 in pkg/handlers/distribution/base/repository_list.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/base/repository_list.go#L40

Added line #L40 was not covered by tests
}

var n = 1000
var err error
var nStr = c.QueryParam("n")
if nStr != "" {
n, err = strconv.Atoi(nStr)
Expand Down
15 changes: 5 additions & 10 deletions pkg/handlers/distribution/base/tags_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"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"
"github.com/go-sigma/sigma/pkg/utils"
Expand All @@ -41,15 +40,12 @@ var listTagsReg = regexp.MustCompile(fmt.Sprintf(`^/v2/%s/tags/list$`, reference

// ListTags handles the list tags request
func (h *handler) ListTags(c echo.Context) error {
iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, needRet, err := utils.GetUserFromCtxForDs(c)
if err != nil {
return err

Check warning on line 45 in pkg/handlers/distribution/base/tags_list.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/base/tags_list.go#L45

Added line #L45 was not covered by tests
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
if needRet {
return nil

Check warning on line 48 in pkg/handlers/distribution/base/tags_list.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/base/tags_list.go#L48

Added line #L48 was not covered by tests
}

var uri = c.Request().URL.Path
Expand All @@ -58,7 +54,6 @@ func (h *handler) ListTags(c echo.Context) error {
}

var n = 1000
var err error
var nStr = c.QueryParam("n")
if nStr != "" {
n, err = strconv.Atoi(nStr)
Expand Down
5 changes: 4 additions & 1 deletion pkg/handlers/distribution/blob/blob_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ import (
func (h *handler) DeleteBlob(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

user, err := utils.GetUserFromCtx(c)
user, needRet, err := utils.GetUserFromCtxForDs(c)

Check warning on line 40 in pkg/handlers/distribution/blob/blob_delete.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/blob/blob_delete.go#L40

Added line #L40 was not covered by tests
if err != nil {
return err
}
if needRet {
return nil
}

Check warning on line 46 in pkg/handlers/distribution/blob/blob_delete.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/blob/blob_delete.go#L44-L46

Added lines #L44 - L46 were not covered by tests

uri := c.Request().URL.Path

Expand Down
5 changes: 4 additions & 1 deletion pkg/handlers/distribution/blob/blob_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ import (
func (h *handler) GetBlob(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

user, err := utils.GetUserFromCtx(c)
user, needRet, err := utils.GetUserFromCtxForDs(c)

Check warning on line 50 in pkg/handlers/distribution/blob/blob_get.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/blob/blob_get.go#L50

Added line #L50 was not covered by tests
if err != nil {
return err
}
if needRet {
return nil
}

Check warning on line 56 in pkg/handlers/distribution/blob/blob_get.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/blob/blob_get.go#L54-L56

Added lines #L54 - L56 were not covered by tests

uri := c.Request().URL.Path

Expand Down
5 changes: 4 additions & 1 deletion pkg/handlers/distribution/blob/blob_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ import (
func (h *handler) HeadBlob(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

user, err := utils.GetUserFromCtx(c)
user, needRet, err := utils.GetUserFromCtxForDs(c)

Check warning on line 41 in pkg/handlers/distribution/blob/blob_head.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/blob/blob_head.go#L41

Added line #L41 was not covered by tests
if err != nil {
return err
}
if needRet {
return nil
}

Check warning on line 47 in pkg/handlers/distribution/blob/blob_head.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/blob/blob_head.go#L45-L47

Added lines #L45 - L47 were not covered by tests

uri := c.Request().URL.Path

Expand Down
14 changes: 5 additions & 9 deletions pkg/handlers/distribution/manifest/manifest_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
Expand All @@ -41,15 +40,12 @@ import (
func (h *handler) DeleteManifest(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, needRet, err := utils.GetUserFromCtxForDs(c)
if err != nil {
return err

Check warning on line 45 in pkg/handlers/distribution/manifest/manifest_delete.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/manifest/manifest_delete.go#L45

Added line #L45 was not covered by tests
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
if needRet {
return nil

Check warning on line 48 in pkg/handlers/distribution/manifest/manifest_delete.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/manifest/manifest_delete.go#L48

Added line #L48 was not covered by tests
}

uri := c.Request().URL.Path
Expand Down
14 changes: 5 additions & 9 deletions pkg/handlers/distribution/manifest/manifest_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"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"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/utils/imagerefs"
Expand All @@ -38,15 +37,12 @@ import (
func (h *handler) GetManifest(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, needRet, err := utils.GetUserFromCtxForDs(c)
if err != nil {
return err

Check warning on line 42 in pkg/handlers/distribution/manifest/manifest_get.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/manifest/manifest_get.go#L40-L42

Added lines #L40 - L42 were not covered by tests
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
if needRet {
return nil

Check warning on line 45 in pkg/handlers/distribution/manifest/manifest_get.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/manifest/manifest_get.go#L44-L45

Added lines #L44 - L45 were not covered by tests
}

uri := c.Request().URL.Path
Expand Down
14 changes: 5 additions & 9 deletions pkg/handlers/distribution/manifest/manifest_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"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"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/utils/imagerefs"
Expand All @@ -39,15 +38,12 @@ import (
func (h *handler) HeadManifest(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, needRet, err := utils.GetUserFromCtxForDs(c)
if err != nil {
return err

Check warning on line 43 in pkg/handlers/distribution/manifest/manifest_head.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/manifest/manifest_head.go#L43

Added line #L43 was not covered by tests
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
if needRet {
return nil

Check warning on line 46 in pkg/handlers/distribution/manifest/manifest_head.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/manifest/manifest_head.go#L46

Added line #L46 was not covered by tests
}

uri := c.Request().URL.Path
Expand Down
15 changes: 6 additions & 9 deletions pkg/handlers/distribution/manifest/manifest_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ const maxManifestBodySize = 4 << 20
func (h *handler) PutManifest(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, needRet, err := utils.GetUserFromCtxForDs(c)
if err != nil {
return err
}

Check warning on line 60 in pkg/handlers/distribution/manifest/manifest_put.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/manifest/manifest_put.go#L59-L60

Added lines #L59 - L60 were not covered by tests
if needRet {
return nil

Check warning on line 62 in pkg/handlers/distribution/manifest/manifest_put.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/manifest/manifest_put.go#L62

Added line #L62 was not covered by tests
}

uri := c.Request().URL.Path
Expand Down
13 changes: 5 additions & 8 deletions pkg/handlers/distribution/upload/upload_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ import (
func (h *handler) PostUpload(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, needRet, err := utils.GetUserFromCtxForDs(c)
if err != nil {
return err

Check warning on line 48 in pkg/handlers/distribution/upload/upload_post.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/upload/upload_post.go#L46-L48

Added lines #L46 - L48 were not covered by tests
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
if needRet {
return nil

Check warning on line 51 in pkg/handlers/distribution/upload/upload_post.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/upload/upload_post.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}

host := c.Request().Host
Expand Down
19 changes: 10 additions & 9 deletions pkg/handlers/distribution/upload/upload_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,22 @@ import (
func (h *handler) PutUpload(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
user, needRet, err := utils.GetUserFromCtxForDs(c)
if err != nil {
return err

Check warning on line 50 in pkg/handlers/distribution/upload/upload_put.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/upload/upload_put.go#L48-L50

Added lines #L48 - L50 were not covered by tests
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
if needRet {
return nil

Check warning on line 53 in pkg/handlers/distribution/upload/upload_put.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/upload/upload_put.go#L52-L53

Added lines #L52 - L53 were not covered by tests
}

uri := c.Request().URL.Path
uploadID := strings.TrimPrefix(uri[strings.LastIndex(uri, "/"):], "/")
c.Response().Header().Set("Location", fmt.Sprintf("%s://%s%s", c.Scheme(), c.Request().Host, uri))

uploadID := strings.TrimPrefix(uri[strings.LastIndex(uri, "/"):], "/")
if strings.Contains(uploadID, "/") {
return xerrors.NewDSError(c, xerrors.DSErrCodeBlobUploadInvalid)
}

Check warning on line 62 in pkg/handlers/distribution/upload/upload_put.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/distribution/upload/upload_put.go#L59-L62

Added lines #L59 - L62 were not covered by tests

repository := strings.TrimPrefix(strings.TrimSuffix(uri[:strings.LastIndex(uri, "/")], "/blobs"), "/v2/")
_, namespace, _, _, err := imagerefs.Parse(repository)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/handlers/users/users_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ import (
func (h *handler) Login(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

user, err := utils.GetUserFromCtx(c)
user, needRet, err := utils.GetUserFromCtx(c)

Check warning on line 46 in pkg/handlers/users/users_login.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/users/users_login.go#L46

Added line #L46 was not covered by tests
if err != nil {
return err
}
if needRet {
return nil
}

Check warning on line 52 in pkg/handlers/users/users_login.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/users/users_login.go#L50-L52

Added lines #L50 - L52 were not covered by tests

userService := h.userServiceFactory.New()
err = userService.UpdateByID(ctx, user.ID, map[string]any{
Expand Down
5 changes: 4 additions & 1 deletion pkg/handlers/webhooks/webhooks_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ import (
func (h *handler) PostWebhook(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

user, err := utils.GetUserFromCtx(c)
user, needRet, err := utils.GetUserFromCtx(c)

Check warning on line 54 in pkg/handlers/webhooks/webhooks_create.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/webhooks/webhooks_create.go#L54

Added line #L54 was not covered by tests
if err != nil {
return err
}
if needRet {
return nil
}

Check warning on line 60 in pkg/handlers/webhooks/webhooks_create.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/webhooks/webhooks_create.go#L58-L60

Added lines #L58 - L60 were not covered by tests

var req types.PostWebhookRequest
err = utils.BindValidate(c, &req)
Expand Down
23 changes: 19 additions & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,31 @@ func UnwrapJoinedErrors(err error) string {
}

// GetUserFromCtx ...
func GetUserFromCtx(c echo.Context) (*models.User, error) {
func GetUserFromCtx(c echo.Context) (*models.User, bool, error) {
iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return nil, xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
return nil, true, xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return nil, xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
return nil, true, xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
}
return user, nil
return user, false, nil
}

// GetUserFromCtxForDs ...
func GetUserFromCtxForDs(c echo.Context) (*models.User, bool, error) {
iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return nil, true, xerrors.NewDSError(c, xerrors.DSErrCodeUnauthorized)
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return nil, true, xerrors.NewDSError(c, xerrors.DSErrCodeUnauthorized)
}
return user, false, nil
}
Loading

0 comments on commit 9729129

Please sign in to comment.