From 5e6b38e0e701e25b3a7cfd5dade3df0e16c7781d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 26 Nov 2021 13:01:54 +0100 Subject: [PATCH] Server obtain remote from glob config not from context (#540) --- cmd/server/server.go | 6 +- cmd/server/setup.go | 4 +- server/api/build.go | 6 +- server/api/hook.go | 2 +- server/api/login.go | 9 ++- server/api/repo.go | 11 ++-- server/api/user.go | 6 +- server/config.go | 2 + server/remote/context.go | 37 ------------ server/remote/remote.go | 75 ------------------------ server/router/middleware/remote.go | 29 --------- server/router/middleware/session/repo.go | 7 +-- server/router/middleware/token/token.go | 3 +- 13 files changed, 29 insertions(+), 168 deletions(-) delete mode 100644 server/remote/context.go delete mode 100644 server/router/middleware/remote.go diff --git a/cmd/server/server.go b/cmd/server/server.go index e1efece7b..0449d2a7e 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -98,7 +98,7 @@ func run(c *cli.Context) error { ) } - remote_, err := SetupRemote(c) + remote_, err := setupRemote(c) if err != nil { log.Fatal().Err(err).Msg("") } @@ -142,7 +142,6 @@ func run(c *cli.Context) error { middleware.Version, middleware.Config(c), middleware.Store(c, store_), - middleware.Remote(remote_), ) var g errgroup.Group @@ -253,6 +252,9 @@ func setupEvilGlobals(c *cli.Context, v store.Store, r remote.Remote) { server.Config.Storage.Files = v server.Config.Storage.Config = v + // remote + server.Config.Services.Remote = r + // services server.Config.Services.Queue = setupQueue(c, v) server.Config.Services.Logs = logging.New() diff --git a/cmd/server/setup.go b/cmd/server/setup.go index 55e263845..bc03f9ab9 100644 --- a/cmd/server/setup.go +++ b/cmd/server/setup.go @@ -175,8 +175,8 @@ func setupEnvironService(c *cli.Context, s store.Store) model.EnvironService { return environments.Filesystem(c.StringSlice("environment")) } -// SetupRemote helper function to setup the remote from the CLI arguments. -func SetupRemote(c *cli.Context) (remote.Remote, error) { +// setupRemote helper function to setup the remote from the CLI arguments. +func setupRemote(c *cli.Context) (remote.Remote, error) { switch { case c.Bool("github"): return setupGithub(c) diff --git a/server/api/build.go b/server/api/build.go index 44f9f4ea9..e09488b34 100644 --- a/server/api/build.go +++ b/server/api/build.go @@ -261,7 +261,7 @@ func DeleteBuild(c *gin.Context) { func PostApproval(c *gin.Context) { var ( - remote_ = remote.FromContext(c) + remote_ = server.Config.Services.Remote store_ = store.FromContext(c) repo = session.Repo(c) user = session.User(c) @@ -372,7 +372,7 @@ func PostApproval(c *gin.Context) { func PostDecline(c *gin.Context) { var ( - remote_ = remote.FromContext(c) + remote_ = server.Config.Services.Remote store_ = store.FromContext(c) repo = session.Repo(c) @@ -415,7 +415,7 @@ func GetBuildQueue(c *gin.Context) { // PostBuild restarts a build func PostBuild(c *gin.Context) { - remote_ := remote.FromContext(c) + remote_ := server.Config.Services.Remote store_ := store.FromContext(c) repo := session.Repo(c) diff --git a/server/api/hook.go b/server/api/hook.go index 22b715779..cc2b75af3 100644 --- a/server/api/hook.go +++ b/server/api/hook.go @@ -76,7 +76,7 @@ func BlockTilQueueHasRunningItem(c *gin.Context) { } func PostHook(c *gin.Context) { - remote_ := remote.FromContext(c) + remote_ := server.Config.Services.Remote store_ := store.FromContext(c) tmpRepo, build, err := remote_.Hook(c.Request) diff --git a/server/api/login.go b/server/api/login.go index 8794076ff..2ad679d1a 100644 --- a/server/api/login.go +++ b/server/api/login.go @@ -25,7 +25,6 @@ import ( "github.com/woodpecker-ci/woodpecker/server" "github.com/woodpecker-ci/woodpecker/server/model" - "github.com/woodpecker-ci/woodpecker/server/remote" "github.com/woodpecker-ci/woodpecker/server/store" "github.com/woodpecker-ci/woodpecker/shared/httputil" "github.com/woodpecker-ci/woodpecker/shared/token" @@ -55,7 +54,7 @@ func HandleAuth(c *gin.Context) { // cannot, however, remember why, so need to revisit this line. c.Writer.Header().Del("Content-Type") - tmpuser, err := remote.Login(c, c.Writer, c.Request) + tmpuser, err := server.Config.Services.Remote.Login(c, c.Writer, c.Request) if err != nil { log.Error().Msgf("cannot authenticate user. %s", err) c.Redirect(303, "/login?error=oauth_error") @@ -81,7 +80,7 @@ func HandleAuth(c *gin.Context) { // if self-registration is enabled for whitelisted organizations we need to // check the user's organization membership. if len(config.Orgs) != 0 { - teams, terr := remote.Teams(c, tmpuser) + teams, terr := server.Config.Services.Remote.Teams(c, tmpuser) if terr != nil || !config.IsMember(teams) { log.Error().Msgf("cannot verify team membership for %s.", u.Login) c.Redirect(303, "/login?error=access_denied") @@ -118,7 +117,7 @@ func HandleAuth(c *gin.Context) { // if self-registration is enabled for whitelisted organizations we need to // check the user's organization membership. if len(config.Orgs) != 0 { - teams, terr := remote.Teams(c, u) + teams, terr := server.Config.Services.Remote.Teams(c, u) if terr != nil || !config.IsMember(teams) { log.Error().Msgf("cannot verify team membership for %s.", u.Login) c.Redirect(303, "/login?error=access_denied") @@ -166,7 +165,7 @@ func GetLoginToken(c *gin.Context) { return } - login, err := remote.Auth(c, in.Access, in.Refresh) + login, err := server.Config.Services.Remote.Auth(c, in.Access, in.Refresh) if err != nil { _ = c.AbortWithError(http.StatusUnauthorized, err) return diff --git a/server/api/repo.go b/server/api/repo.go index e5009fbd0..e29aa3b37 100644 --- a/server/api/repo.go +++ b/server/api/repo.go @@ -26,14 +26,13 @@ import ( "github.com/woodpecker-ci/woodpecker/server" "github.com/woodpecker-ci/woodpecker/server/model" - "github.com/woodpecker-ci/woodpecker/server/remote" "github.com/woodpecker-ci/woodpecker/server/router/middleware/session" "github.com/woodpecker-ci/woodpecker/server/store" "github.com/woodpecker-ci/woodpecker/shared/token" ) func PostRepo(c *gin.Context) { - remote_ := remote.FromContext(c) + remote_ := server.Config.Services.Remote store_ := store.FromContext(c) user := session.User(c) repo := session.Repo(c) @@ -177,7 +176,7 @@ func GetRepoPermissions(c *gin.Context) { func GetRepoBranches(c *gin.Context) { repo := session.Repo(c) user := session.User(c) - r := remote.FromContext(c) + r := server.Config.Services.Remote branches, err := r.Branches(c, user, repo) if err != nil { @@ -190,7 +189,7 @@ func GetRepoBranches(c *gin.Context) { func DeleteRepo(c *gin.Context) { remove, _ := strconv.ParseBool(c.Query("remove")) - remote_ := remote.FromContext(c) + remote_ := server.Config.Services.Remote store_ := store.FromContext(c) repo := session.Repo(c) @@ -221,7 +220,7 @@ func DeleteRepo(c *gin.Context) { } func RepairRepo(c *gin.Context) { - remote_ := remote.FromContext(c) + remote_ := server.Config.Services.Remote store_ := store.FromContext(c) repo := session.Repo(c) user := session.User(c) @@ -275,7 +274,7 @@ func RepairRepo(c *gin.Context) { } func MoveRepo(c *gin.Context) { - remote_ := remote.FromContext(c) + remote_ := server.Config.Services.Remote store_ := store.FromContext(c) repo := session.Repo(c) user := session.User(c) diff --git a/server/api/user.go b/server/api/user.go index 0b178864a..226a4bb0a 100644 --- a/server/api/user.go +++ b/server/api/user.go @@ -24,8 +24,8 @@ import ( "github.com/gorilla/securecookie" "github.com/rs/zerolog/log" + "github.com/woodpecker-ci/woodpecker/server" "github.com/woodpecker-ci/woodpecker/server/model" - "github.com/woodpecker-ci/woodpecker/server/remote" "github.com/woodpecker-ci/woodpecker/server/router/middleware/session" "github.com/woodpecker-ci/woodpecker/server/shared" "github.com/woodpecker-ci/woodpecker/server/store" @@ -38,7 +38,7 @@ func GetSelf(c *gin.Context) { func GetFeed(c *gin.Context) { store_ := store.FromContext(c) - remote_ := remote.FromContext(c) + remote_ := server.Config.Services.Remote user := session.User(c) latest, _ := strconv.ParseBool(c.Query("latest")) @@ -87,7 +87,7 @@ func GetFeed(c *gin.Context) { func GetRepos(c *gin.Context) { store_ := store.FromContext(c) - remote_ := remote.FromContext(c) + remote_ := server.Config.Services.Remote user := session.User(c) all, _ := strconv.ParseBool(c.Query("all")) diff --git a/server/config.go b/server/config.go index e0cc5f63e..ec88e4d46 100644 --- a/server/config.go +++ b/server/config.go @@ -24,6 +24,7 @@ import ( "github.com/woodpecker-ci/woodpecker/server/model" "github.com/woodpecker-ci/woodpecker/server/pubsub" "github.com/woodpecker-ci/woodpecker/server/queue" + "github.com/woodpecker-ci/woodpecker/server/remote" ) var Config = struct { @@ -35,6 +36,7 @@ var Config = struct { Secrets model.SecretService Registries model.RegistryService Environ model.EnvironService + Remote remote.Remote } Storage struct { // Users model.UserStore diff --git a/server/remote/context.go b/server/remote/context.go deleted file mode 100644 index e076c508a..000000000 --- a/server/remote/context.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2018 Drone.IO Inc. -// -// 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 remote - -import ( - "context" -) - -const key = "remote" - -// Setter defines a context that enables setting values. -type Setter interface { - Set(string, interface{}) -} - -// FromContext returns the Remote associated with this context. -func FromContext(c context.Context) Remote { - return c.Value(key).(Remote) -} - -// ToContext adds the Remote to this context if it supports -// the Setter interface. -func ToContext(c Setter, r Remote) { - c.Set(key, r) -} diff --git a/server/remote/remote.go b/server/remote/remote.go index 020290d30..eda3f1ff8 100644 --- a/server/remote/remote.go +++ b/server/remote/remote.go @@ -96,78 +96,3 @@ func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } type Refresher interface { Refresh(context.Context, *model.User) (bool, error) } - -// Login authenticates the session and returns the -// remote user details. -func Login(c context.Context, w http.ResponseWriter, r *http.Request) (*model.User, error) { - return FromContext(c).Login(c, w, r) -} - -// Auth authenticates the session and returns the remote user -// login for the given token and secret -func Auth(c context.Context, token, secret string) (string, error) { - return FromContext(c).Auth(c, token, secret) -} - -// Teams fetches a list of team memberships from the remote system. -func Teams(c context.Context, u *model.User) ([]*model.Team, error) { - return FromContext(c).Teams(c, u) -} - -// Repo fetches the named repository from the remote system. -func Repo(c context.Context, u *model.User, owner, repo string) (*model.Repo, error) { - return FromContext(c).Repo(c, u, owner, repo) -} - -// Repos fetches a list of repos from the remote system. -func Repos(c context.Context, u *model.User) ([]*model.Repo, error) { - return FromContext(c).Repos(c, u) -} - -// Perm fetches the named repository permissions from -// the remote system for the specified user. -func Perm(c context.Context, u *model.User, owner, repo string) (*model.Perm, error) { - return FromContext(c).Perm(c, u, owner, repo) -} - -// Status sends the commit status to the remote system. -// An example would be the GitHub pull request status. -func Status(c context.Context, u *model.User, r *model.Repo, b *model.Build, link string, proc *model.Proc) error { - return FromContext(c).Status(c, u, r, b, link, proc) -} - -// Netrc returns a .netrc file that can be used to clone -// private repositories from a remote system. -func Netrc(c context.Context, u *model.User, r *model.Repo) (*model.Netrc, error) { - return FromContext(c).Netrc(u, r) -} - -// Activate activates a repository by creating the post-commit hook and -// adding the SSH deploy key, if applicable. -func Activate(c context.Context, u *model.User, r *model.Repo, link string) error { - return FromContext(c).Activate(c, u, r, link) -} - -// Deactivate removes a repository by removing all the post-commit hooks -// which are equal to link and removing the SSH deploy key. -func Deactivate(c context.Context, u *model.User, r *model.Repo, link string) error { - return FromContext(c).Deactivate(c, u, r, link) -} - -// Hook parses the post-commit hook from the Request body -// and returns the required data in a standard format. -func Hook(c context.Context, r *http.Request) (*model.Repo, *model.Build, error) { - return FromContext(c).Hook(r) -} - -// Refresh refreshes an oauth token and expiration for the given -// user. It returns true if the token was refreshed, false if the -// token was not refreshed, and error if it failed to refersh. -func Refresh(c context.Context, u *model.User) (bool, error) { - remote := FromContext(c) - refresher, ok := remote.(Refresher) - if !ok { - return false, nil - } - return refresher.Refresh(c, u) -} diff --git a/server/router/middleware/remote.go b/server/router/middleware/remote.go deleted file mode 100644 index c04ca6cef..000000000 --- a/server/router/middleware/remote.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2018 Drone.IO Inc. -// -// 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 middleware - -import ( - "github.com/gin-gonic/gin" - - "github.com/woodpecker-ci/woodpecker/server/remote" -) - -// Remote is a middleware function that initializes the Remote and attaches to -// the context of every http.Request. -func Remote(v remote.Remote) gin.HandlerFunc { - return func(c *gin.Context) { - remote.ToContext(c, v) - } -} diff --git a/server/router/middleware/session/repo.go b/server/router/middleware/session/repo.go index 5230e16cd..5c3bf959d 100644 --- a/server/router/middleware/session/repo.go +++ b/server/router/middleware/session/repo.go @@ -18,12 +18,11 @@ import ( "net/http" "time" + "github.com/gin-gonic/gin" "github.com/rs/zerolog/log" - "github.com/gin-gonic/gin" - + "github.com/woodpecker-ci/woodpecker/server" "github.com/woodpecker-ci/woodpecker/server/model" - "github.com/woodpecker-ci/woodpecker/server/remote" "github.com/woodpecker-ci/woodpecker/server/store" ) @@ -98,7 +97,7 @@ func SetPerm() gin.HandlerFunc { user.Login, repo.FullName, err) } if time.Unix(perm.Synced, 0).Add(time.Hour).Before(time.Now()) { - perm, err = remote.FromContext(c).Perm(c, user, repo.Owner, repo.Name) + perm, err = server.Config.Services.Remote.Perm(c, user, repo.Owner, repo.Name) if err == nil { log.Debug().Msgf("Synced user permission for %s %s", user.Login, repo.FullName) perm.Repo = repo.FullName diff --git a/server/router/middleware/token/token.go b/server/router/middleware/token/token.go index b4d05d46d..baefdd5b7 100644 --- a/server/router/middleware/token/token.go +++ b/server/router/middleware/token/token.go @@ -21,6 +21,7 @@ import ( "github.com/gin-gonic/gin" + "github.com/woodpecker-ci/woodpecker/server" "github.com/woodpecker-ci/woodpecker/server/remote" "github.com/woodpecker-ci/woodpecker/server/router/middleware/session" "github.com/woodpecker-ci/woodpecker/server/store" @@ -35,7 +36,7 @@ func Refresh(c *gin.Context) { // check if the remote includes the ability to // refresh the user token. - remote_ := remote.FromContext(c) + remote_ := server.Config.Services.Remote refresher, ok := remote_.(remote.Refresher) if !ok { c.Next()