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

gateway: move all the authn/authz logic from api to action #350

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
gateway: move all the authn/authz logic from api to action
  • Loading branch information
alessandro-sorint committed Dec 15, 2022
commit 881081d6ec98ab08902aa89738ff523ddca75d2d
12 changes: 4 additions & 8 deletions internal/services/gateway/action/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectReq
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("project %q already exists", projectPath))
}

gitSource, rs, la, err := h.GetUserGitSource(ctx, req.RemoteSourceName, curUserID)
gitSource, rs, la, err := h.GetUserGitSource(ctx, req.RemoteSourceName)
if err != nil {
return nil, errors.Wrapf(err, "failed to create gitsource client")
}
Expand Down Expand Up @@ -225,8 +225,6 @@ func (h *ActionHandler) UpdateProject(ctx context.Context, projectRef string, re
}

func (h *ActionHandler) ProjectUpdateRepoLinkedAccount(ctx context.Context, projectRef string) (*csapitypes.Project, error) {
curUserID := common.CurrentUserID(ctx)

p, _, err := h.configstoreClient.GetProject(ctx, projectRef)
if err != nil {
return nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to get project %q", projectRef))
Expand All @@ -240,7 +238,7 @@ func (h *ActionHandler) ProjectUpdateRepoLinkedAccount(ctx context.Context, proj
return nil, util.NewAPIError(util.ErrForbidden, errors.Errorf("user not authorized"))
}

gitsource, _, la, err := h.GetUserGitSource(ctx, p.RemoteSourceID, curUserID)
gitsource, _, la, err := h.GetUserGitSource(ctx, p.RemoteSourceID)
if err != nil {
return nil, errors.Wrapf(err, "failed to create gitsource client")
}
Expand Down Expand Up @@ -420,8 +418,6 @@ func (h *ActionHandler) DeleteProject(ctx context.Context, projectRef string) er
}

func (h *ActionHandler) ProjectCreateRun(ctx context.Context, projectRef, branch, tag, refName, commitSHA string) error {
curUserID := common.CurrentUserID(ctx)

p, _, err := h.configstoreClient.GetProject(ctx, projectRef)
if err != nil {
return util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to get project %q", projectRef))
Expand All @@ -435,7 +431,7 @@ func (h *ActionHandler) ProjectCreateRun(ctx context.Context, projectRef, branch
return util.NewAPIError(util.ErrForbidden, errors.Errorf("user not authorized"))
}

gitSource, rs, _, err := h.GetUserGitSource(ctx, p.RemoteSourceID, curUserID)
gitSource, rs, _, err := h.GetUserGitSource(ctx, p.RemoteSourceID)
if err != nil {
return errors.Wrapf(err, "failed to create gitsource client")
}
Expand Down Expand Up @@ -602,7 +598,7 @@ func (h *ActionHandler) RefreshRemoteRepositoryInfo(ctx context.Context, project
return nil, util.NewAPIError(util.ErrForbidden, errors.Errorf("user not authorized"))
}

gitSource, _, _, err := h.GetUserGitSource(ctx, p.RemoteSourceID, common.CurrentUserID(ctx))
gitSource, _, _, err := h.GetUserGitSource(ctx, p.RemoteSourceID)
if err != nil {
return nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to get remote source %q", p.RemoteSourceID))
}
Expand Down
17 changes: 11 additions & 6 deletions internal/services/gateway/action/projectgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"path"

"agola.io/agola/internal/errors"
"agola.io/agola/internal/services/gateway/common"
"agola.io/agola/internal/util"
csapitypes "agola.io/agola/services/configstore/api/types"
cstypes "agola.io/agola/services/configstore/types"
Expand Down Expand Up @@ -49,13 +50,17 @@ func (h *ActionHandler) GetProjectGroupProjects(ctx context.Context, projectGrou
}

type CreateProjectGroupRequest struct {
CurrentUserID string
Name string
ParentRef string
Visibility cstypes.Visibility
Name string
ParentRef string
Visibility cstypes.Visibility
}

func (h *ActionHandler) CreateProjectGroup(ctx context.Context, req *CreateProjectGroupRequest) (*csapitypes.ProjectGroup, error) {
userID := common.CurrentUserID(ctx)
if userID == "" {
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("user not authenticated"))
}

if !util.ValidateName(req.Name) {
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("invalid projectGroup name %q", req.Name))
}
Expand All @@ -73,9 +78,9 @@ func (h *ActionHandler) CreateProjectGroup(ctx context.Context, req *CreateProje
return nil, util.NewAPIError(util.ErrForbidden, errors.Errorf("user not authorized"))
}

user, _, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID)
user, _, err := h.configstoreClient.GetUser(ctx, userID)
if err != nil {
return nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to get user %q", req.CurrentUserID))
return nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to get user %q", userID))
}

parentRef := req.ParentRef
Expand Down
22 changes: 14 additions & 8 deletions internal/services/gateway/action/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *ActionHandler) GetCurrentUser(ctx context.Context, userRef string) (*Pr
return nil, errors.Errorf("user not logged in")
}

user, _, err := h.configstoreClient.GetUser(ctx, userRef)
user, _, err := h.configstoreClient.GetUser(ctx, common.CurrentUserID(ctx))
if err != nil {
return nil, util.NewAPIError(util.KindFromRemoteError(err), err)
}
Expand Down Expand Up @@ -87,12 +87,13 @@ func (h *ActionHandler) GetUser(ctx context.Context, userRef string) (*cstypes.U
return user, nil
}

func (h *ActionHandler) GetUserOrgs(ctx context.Context, userRef string) ([]*csapitypes.UserOrgsResponse, error) {
if !common.IsUserLogged(ctx) {
return nil, errors.Errorf("user not logged in")
func (h *ActionHandler) GetUserOrgs(ctx context.Context) ([]*csapitypes.UserOrgsResponse, error) {
userID := common.CurrentUserID(ctx)
if userID == "" {
return nil, errors.Errorf("user not authenticated")
}

orgs, _, err := h.configstoreClient.GetUserOrgs(ctx, userRef)
orgs, _, err := h.configstoreClient.GetUserOrgs(ctx, userID)
if err != nil {
return nil, util.NewAPIError(util.KindFromRemoteError(err), err)
}
Expand Down Expand Up @@ -1020,15 +1021,20 @@ func (h *ActionHandler) UserCreateRun(ctx context.Context, req *UserCreateRunReq
return h.CreateRuns(ctx, creq)
}

func (h *ActionHandler) GetUserGitSource(ctx context.Context, remoteSourceRef, userRef string) (gitsource.GitSource, *cstypes.RemoteSource, *cstypes.LinkedAccount, error) {
func (h *ActionHandler) GetUserGitSource(ctx context.Context, remoteSourceRef string) (gitsource.GitSource, *cstypes.RemoteSource, *cstypes.LinkedAccount, error) {
userID := common.CurrentUserID(ctx)
if userID == "" {
return nil, nil, nil, errors.Errorf("user not authenticated")
}

rs, _, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceRef)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "failed to get remote source %q", remoteSourceRef)
}

linkedAccounts, _, err := h.configstoreClient.GetUserLinkedAccounts(ctx, userRef)
linkedAccounts, _, err := h.configstoreClient.GetUserLinkedAccounts(ctx, userID)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "failed to get user %q linked accounts", userRef)
return nil, nil, nil, errors.Wrapf(err, "failed to get user %q linked accounts", userID)
}

var la *cstypes.LinkedAccount
Expand Down
15 changes: 3 additions & 12 deletions internal/services/gateway/api/projectgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import (
"net/http"
"net/url"

"agola.io/agola/internal/errors"
"agola.io/agola/internal/services/gateway/action"
"agola.io/agola/internal/services/gateway/common"
"agola.io/agola/internal/util"
csapitypes "agola.io/agola/services/configstore/api/types"
cstypes "agola.io/agola/services/configstore/types"
Expand Down Expand Up @@ -50,17 +48,10 @@ func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
return
}

userID := common.CurrentUserID(ctx)
if userID == "" {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("user not authenticated")))
return
}

creq := &action.CreateProjectGroupRequest{
Name: req.Name,
ParentRef: req.ParentRef,
Visibility: cstypes.Visibility(req.Visibility),
CurrentUserID: userID,
Name: req.Name,
ParentRef: req.ParentRef,
Visibility: cstypes.Visibility(req.Visibility),
}

projectGroup, err := h.ah.CreateProjectGroup(ctx, creq)
Expand Down
9 changes: 1 addition & 8 deletions internal/services/gateway/api/remoterepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"agola.io/agola/internal/errors"
gitsource "agola.io/agola/internal/gitsources"
"agola.io/agola/internal/services/gateway/action"
"agola.io/agola/internal/services/gateway/common"
"agola.io/agola/internal/util"
csclient "agola.io/agola/services/configstore/client"
gwapitypes "agola.io/agola/services/gateway/api/types"
Expand Down Expand Up @@ -53,13 +52,7 @@ func (h *UserRemoteReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
vars := mux.Vars(r)
remoteSourceRef := vars["remotesourceref"]

userID := common.CurrentUserID(ctx)
if userID == "" {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("user not authenticated")))
return
}

gitsource, _, _, err := h.ah.GetUserGitSource(ctx, remoteSourceRef, userID)
gitsource, _, _, err := h.ah.GetUserGitSource(ctx, remoteSourceRef)
if err != nil {
util.HTTPError(w, err)
h.log.Err(err).Send()
Expand Down
8 changes: 1 addition & 7 deletions internal/services/gateway/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,13 +632,7 @@ func NewUserOrgsHandler(log zerolog.Logger, ah *action.ActionHandler) *UserOrgsH
func (h *UserOrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

userID := common.CurrentUserID(ctx)
if userID == "" {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("user not authenticated")))
return
}

userOrgs, err := h.ah.GetUserOrgs(ctx, userID)
userOrgs, err := h.ah.GetUserOrgs(ctx)
if util.HTTPError(w, err) {
h.log.Err(err).Send()
return
Expand Down