bump to 0.5 in master

This commit is contained in:
Brad Rydzewski
2016-05-02 12:21:25 -07:00
parent 53eac09f34
commit 0fb4aeda3f
43 changed files with 802 additions and 1347 deletions

View File

@@ -1,45 +1,33 @@
package middleware
import (
"github.com/codegangsta/cli"
"github.com/drone/drone/shared/token"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/ianschenck/envflag"
)
var (
secret = envflag.String("DRONE_AGENT_SECRET", "", "")
noauth = envflag.Bool("AGENT_NO_AUTH", false, "")
)
const agentKey = "agent"
// Agent is a middleware function that initializes the authorization middleware
// Agents is a middleware function that initializes the authorization middleware
// for agents to connect to the queue.
func AgentMust() gin.HandlerFunc {
if *secret == "" {
logrus.Fatalf("please provide the agent secret to authenticate agent requests")
func Agents(cli *cli.Context) gin.HandlerFunc {
secret := cli.String("agent-secret")
if secret == "" {
logrus.Fatalf("failed to generate token from DRONE_AGENT_SECRET")
}
t := token.New(token.AgentToken, "")
s, err := t.Sign(*secret)
t := token.New(secret, "")
s, err := t.Sign(secret)
if err != nil {
logrus.Fatalf("invalid agent secret. %s", err)
logrus.Fatalf("failed to generate token from DRONE_AGENT_SECRET. %s", err)
}
logrus.Infof("using agent secret %s", *secret)
logrus.Infof("using agent secret %s", secret)
logrus.Warnf("agents can connect with token %s", s)
return func(c *gin.Context) {
parsed, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
return *secret, nil
})
if err != nil {
c.AbortWithError(403, err)
} else if parsed.Kind != token.AgentToken {
c.AbortWithStatus(403)
} else {
c.Next()
}
c.Set(agentKey, secret)
}
}

17
router/middleware/bus.go Normal file
View File

@@ -0,0 +1,17 @@
package middleware
import (
"github.com/drone/drone/bus"
"github.com/codegangsta/cli"
"github.com/gin-gonic/gin"
)
// Bus is a middleware function that initializes the Event Bus and attaches to
// the context of every http.Request.
func Bus(cli *cli.Context) gin.HandlerFunc {
v := bus.New()
return func(c *gin.Context) {
bus.ToContext(c, v)
}
}

View File

@@ -0,0 +1,24 @@
package middleware
import (
"github.com/drone/drone/cache"
"github.com/codegangsta/cli"
"github.com/gin-gonic/gin"
)
// Cache is a middleware function that initializes the Cache and attaches to
// the context of every http.Request.
func Cache(cli *cli.Context) gin.HandlerFunc {
v := setupCache(cli)
return func(c *gin.Context) {
cache.ToContext(c, v)
}
}
// helper function to create the cache from the CLI context.
func setupCache(c *cli.Context) cache.Cache {
return cache.NewTTL(
c.Duration("cache-ttl"),
)
}

View File

@@ -0,0 +1,40 @@
package middleware
import (
"github.com/drone/drone/model"
"github.com/codegangsta/cli"
"github.com/gin-gonic/gin"
)
const configKey = "config"
// Config is a middleware function that initializes the Configuration and
// attaches to the context of every http.Request.
func Config(cli *cli.Context) gin.HandlerFunc {
v := setupConfig(cli)
return func(c *gin.Context) {
c.Set(configKey, v)
}
}
// helper function to create the configuration from the CLI context.
func setupConfig(c *cli.Context) *model.Config {
return &model.Config{
Open: c.Bool("open"),
Yaml: c.String("yaml"),
Shasum: c.String("yaml") + ".sig",
Secret: c.String("agent-secret"),
Admins: sliceToMap(c.StringSlice("admin")),
Orgs: sliceToMap(c.StringSlice("orgs")),
}
}
// helper function to convert a string slice to a map.
func sliceToMap(s []string) map[string]bool {
v := map[string]bool{}
for _, ss := range s {
v[ss] = true
}
return v
}

View File

@@ -0,0 +1,17 @@
package middleware
import (
"github.com/drone/drone/queue"
"github.com/codegangsta/cli"
"github.com/gin-gonic/gin"
)
// Queue is a middleware function that initializes the Queue and attaches to
// the context of every http.Request.
func Queue(cli *cli.Context) gin.HandlerFunc {
v := queue.New()
return func(c *gin.Context) {
queue.ToContext(c, v)
}
}

102
router/middleware/remote.go Normal file
View File

@@ -0,0 +1,102 @@
package middleware
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/drone/drone/remote"
"github.com/drone/drone/remote/bitbucket"
"github.com/drone/drone/remote/bitbucketserver"
"github.com/drone/drone/remote/github"
"github.com/drone/drone/remote/gitlab"
"github.com/drone/drone/remote/gogs"
"github.com/gin-gonic/gin"
)
// Remote is a middleware function that initializes the Remote and attaches to
// the context of every http.Request.
func Remote(c *cli.Context) gin.HandlerFunc {
v, err := setupRemote(c)
if err != nil {
logrus.Fatalln(err)
}
return func(c *gin.Context) {
remote.ToContext(c, v)
}
}
// 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)
case c.Bool("gitlab"):
return setupGitlab(c)
case c.Bool("bitbucket"):
return setupBitbucket(c)
case c.Bool("stash"):
return setupStash(c)
case c.Bool("gogs"):
return setupGogs(c)
default:
return nil, fmt.Errorf("version control system not configured")
}
}
// helper function to setup the Bitbucket remote from the CLI arguments.
func setupBitbucket(c *cli.Context) (remote.Remote, error) {
return bitbucket.New(
c.String("bitbucket-client"),
c.String("bitbucket-server"),
), nil
}
// helper function to setup the Gogs remote from the CLI arguments.
func setupGogs(c *cli.Context) (remote.Remote, error) {
return gogs.New(gogs.Opts{
URL: c.String("gogs-server"),
Username: c.String("gogs-git-username"),
Password: c.String("gogs-git-password"),
PrivateMode: c.Bool("gogs-private-mode"),
SkipVerify: c.Bool("gogs-skip-verify"),
})
}
// helper function to setup the Stash remote from the CLI arguments.
func setupStash(c *cli.Context) (remote.Remote, error) {
return bitbucketserver.New(bitbucketserver.Opts{
URL: c.String("stash-server"),
Username: c.String("stash-git-username"),
Password: c.String("stash-git-password"),
ConsumerKey: c.String("stash-consumer-key"),
ConsumerRSA: c.String("stash-consumer-rsa"),
SkipVerify: c.Bool("stash-skip-verify"),
})
}
// helper function to setup the Gitlab remote from the CLI arguments.
func setupGitlab(c *cli.Context) (remote.Remote, error) {
return gitlab.New(gitlab.Opts{
URL: c.String("gitlab-server"),
Client: c.String("gitlab-client"),
Secret: c.String("gitlab-sercret"),
Username: c.String("gitlab-git-username"),
Password: c.String("gitlab-git-password"),
PrivateMode: c.Bool("gitlab-private-mode"),
SkipVerify: c.Bool("gitlab-skip-verify"),
})
}
// helper function to setup the GitHub remote from the CLI arguments.
func setupGithub(c *cli.Context) (remote.Remote, error) {
return github.New(
c.String("github-server"),
c.String("github-client"),
c.String("github-sercret"),
c.StringSlice("github-scope"),
c.Bool("github-private-mode"),
c.Bool("github-skip-verify"),
c.BoolT("github-merge-ref"),
)
}

View File

@@ -0,0 +1,22 @@
package session
import (
"github.com/drone/drone/shared/token"
"github.com/gin-gonic/gin"
)
// AuthorizeAgent authorizes requsts from build agents to access the queue.
func AuthorizeAgent(c *gin.Context) {
secret := c.MustGet("agent").(string)
parsed, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
return secret, nil
})
if err != nil {
c.AbortWithError(403, err)
} else if parsed.Kind != token.AgentToken {
c.AbortWithStatus(403)
} else {
c.Next()
}
}

View File

@@ -44,6 +44,10 @@ func SetUser() gin.HandlerFunc {
return user.Hash, err
})
if err == nil {
confv := c.MustGet("config")
if conf, ok := confv.(*model.Config); ok {
user.Admin = conf.IsAdmin(user)
}
c.Set("user", user)
// if this is a session token (ie not the API token)

View File

@@ -0,0 +1,27 @@
package middleware
import (
"github.com/codegangsta/cli"
"github.com/drone/drone/store"
"github.com/drone/drone/store/datastore"
"github.com/gin-gonic/gin"
)
// Store is a middleware function that initializes the Datastore and attaches to
// the context of every http.Request.
func Store(cli *cli.Context) gin.HandlerFunc {
v := setupStore(cli)
return func(c *gin.Context) {
store.ToContext(c, v)
c.Next()
}
}
// helper function to create the datastore from the CLI context.
func setupStore(c *cli.Context) store.Store {
return datastore.New(
c.String("driver"),
c.String("datasource"),
)
}

View File

@@ -0,0 +1,17 @@
package middleware
import (
"github.com/drone/drone/stream"
"github.com/codegangsta/cli"
"github.com/gin-gonic/gin"
)
// Stream is a middleware function that initializes the Stream and attaches to
// the context of every http.Request.
func Stream(cli *cli.Context) gin.HandlerFunc {
v := stream.New()
return func(c *gin.Context) {
stream.ToContext(c, v)
}
}

View File

@@ -0,0 +1,12 @@
package middleware
import (
"github.com/drone/drone/version"
"github.com/gin-gonic/gin"
)
// Version is a middleware function that appends the Drone version information
// to the HTTP response. This is intended for debugging and troubleshooting.
func Version(c *gin.Context) {
c.Header("X-DRONE-VERSION", version.Version)
}

View File

@@ -1,201 +1,199 @@
package router
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/drone/drone/router/middleware/header"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/router/middleware/token"
"github.com/drone/drone/server"
"github.com/drone/drone/static"
"github.com/drone/drone/template"
)
func Load(middleware ...gin.HandlerFunc) http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.SetHTMLTemplate(template.Load())
e.StaticFS("/static", static.FileSystem())
e.Use(header.NoCache)
e.Use(header.Options)
e.Use(header.Secure)
e.Use(middleware...)
e.Use(session.SetUser())
e.Use(token.Refresh)
e.GET("/", server.ShowIndex)
e.GET("/repos", server.ShowAllRepos)
e.GET("/login", server.ShowLogin)
e.GET("/login/form", server.ShowLoginForm)
e.GET("/logout", server.GetLogout)
// TODO below will Go away with React UI
settings := e.Group("/settings")
{
settings.Use(session.MustUser())
settings.GET("/profile", server.ShowUser)
}
repo := e.Group("/repos/:owner/:name")
{
repo.Use(session.SetRepo())
repo.Use(session.SetPerm())
repo.Use(session.MustPull)
repo.GET("", server.ShowRepo)
repo.GET("/builds/:number", server.ShowBuild)
repo.GET("/builds/:number/:job", server.ShowBuild)
repo_settings := repo.Group("/settings")
{
repo_settings.GET("", session.MustPush, server.ShowRepoConf)
repo_settings.GET("/encrypt", session.MustPush, server.ShowRepoEncrypt)
repo_settings.GET("/badges", server.ShowRepoBadges)
}
}
// TODO above will Go away with React UI
user := e.Group("/api/user")
{
user.Use(session.MustUser())
user.GET("", server.GetSelf)
user.GET("/feed", server.GetFeed)
user.GET("/repos", server.GetRepos)
user.GET("/repos/remote", server.GetRemoteRepos)
user.POST("/token", server.PostToken)
user.DELETE("/token", server.DeleteToken)
}
users := e.Group("/api/users")
{
users.Use(session.MustAdmin())
users.GET("", server.GetUsers)
users.POST("", server.PostUser)
users.GET("/:login", server.GetUser)
users.PATCH("/:login", server.PatchUser)
users.DELETE("/:login", server.DeleteUser)
}
repos := e.Group("/api/repos/:owner/:name")
{
repos.POST("", server.PostRepo)
repo := repos.Group("")
{
repo.Use(session.SetRepo())
repo.Use(session.SetPerm())
repo.Use(session.MustPull)
repo.GET("", server.GetRepo)
repo.GET("/builds", server.GetBuilds)
repo.GET("/builds/:number", server.GetBuild)
repo.GET("/logs/:number/:job", server.GetBuildLogs)
repo.POST("/sign", session.MustPush, server.Sign)
repo.POST("/secrets", session.MustPush, server.PostSecret)
repo.DELETE("/secrets/:secret", session.MustPush, server.DeleteSecret)
// requires push permissions
repo.PATCH("", session.MustPush, server.PatchRepo)
repo.DELETE("", session.MustPush, server.DeleteRepo)
repo.POST("/builds/:number", session.MustPush, server.PostBuild)
repo.DELETE("/builds/:number/:job", session.MustPush, server.DeleteBuild)
}
}
badges := e.Group("/api/badges/:owner/:name")
{
badges.GET("/status.svg", server.GetBadge)
badges.GET("/cc.xml", server.GetCC)
}
e.POST("/hook", server.PostHook)
e.POST("/api/hook", server.PostHook)
stream := e.Group("/api/stream")
{
stream.Use(session.SetRepo())
stream.Use(session.SetPerm())
stream.Use(session.MustPull)
stream.GET("/:owner/:name", server.GetRepoEvents)
stream.GET("/:owner/:name/:build/:number", server.GetStream)
}
auth := e.Group("/authorize")
{
auth.GET("", server.GetLogin)
auth.POST("", server.GetLogin)
auth.POST("/token", server.GetLoginToken)
}
queue := e.Group("/api/queue")
{
queue.Use(session.AuthorizeAgent)
queue.POST("/pull", server.Pull)
queue.POST("/pull/:os/:arch", server.Pull)
queue.POST("/wait/:id", server.Wait)
queue.POST("/stream/:id", server.Stream)
queue.POST("/status/:id", server.Update)
}
// DELETE THESE
// gitlab := e.Group("/gitlab/:owner/:name")
// {
// gitlab.Use(session.SetRepo())
// gitlab.GET("/commits/:sha", GetCommit)
// gitlab.GET("/pulls/:number", GetPullRequest)
//
// redirects := gitlab.Group("/redirect")
// {
// redirects.GET("/commits/:sha", RedirectSha)
// redirects.GET("/pulls/:number", RedirectPullRequest)
// }
// }
// bots := e.Group("/bots")
// {
// bots.Use(session.MustUser())
// bots.POST("/slack", Slack)
// bots.POST("/slack/:command", Slack)
// }
return normalize(e)
}
// THIS HACK JOB IS GOING AWAY SOON.
//
// import (
// "net/http"
// "strings"
//
// "github.com/gin-gonic/gin"
//
// "github.com/drone/drone/api"
// "github.com/drone/drone/router/middleware"
// "github.com/drone/drone/router/middleware/header"
// "github.com/drone/drone/router/middleware/session"
// "github.com/drone/drone/router/middleware/token"
// "github.com/drone/drone/static"
// "github.com/drone/drone/template"
// "github.com/drone/drone/web"
// )
//
// func Load(middlewares ...gin.HandlerFunc) http.Handler {
// e := gin.New()
// e.Use(gin.Recovery())
//
// e.SetHTMLTemplate(template.Load())
// e.StaticFS("/static", static.FileSystem())
//
// e.Use(header.NoCache)
// e.Use(header.Options)
// e.Use(header.Secure)
// e.Use(middlewares...)
// e.Use(session.SetUser())
// e.Use(token.Refresh)
//
// e.GET("/", web.ShowIndex)
// e.GET("/repos", web.ShowAllRepos)
// e.GET("/login", web.ShowLogin)
// e.GET("/login/form", web.ShowLoginForm)
// e.GET("/logout", web.GetLogout)
//
// settings := e.Group("/settings")
// {
// settings.Use(session.MustUser())
// settings.GET("/profile", web.ShowUser)
// }
// repo := e.Group("/repos/:owner/:name")
// {
// repo.Use(session.SetRepo())
// repo.Use(session.SetPerm())
// repo.Use(session.MustPull)
//
// repo.GET("", web.ShowRepo)
// repo.GET("/builds/:number", web.ShowBuild)
// repo.GET("/builds/:number/:job", web.ShowBuild)
//
// repo_settings := repo.Group("/settings")
// {
// repo_settings.GET("", session.MustPush, web.ShowRepoConf)
// repo_settings.GET("/encrypt", session.MustPush, web.ShowRepoEncrypt)
// repo_settings.GET("/badges", web.ShowRepoBadges)
// }
// }
//
// user := e.Group("/api/user")
// {
// user.Use(session.MustUser())
// user.GET("", api.GetSelf)
// user.GET("/feed", api.GetFeed)
// user.GET("/repos", api.GetRepos)
// user.GET("/repos/remote", api.GetRemoteRepos)
// user.POST("/token", api.PostToken)
// user.DELETE("/token", api.DeleteToken)
// }
//
// users := e.Group("/api/users")
// {
// users.Use(session.MustAdmin())
// users.GET("", api.GetUsers)
// users.POST("", api.PostUser)
// users.GET("/:login", api.GetUser)
// users.PATCH("/:login", api.PatchUser)
// users.DELETE("/:login", api.DeleteUser)
// }
//
// repos := e.Group("/api/repos/:owner/:name")
// {
// repos.POST("", api.PostRepo)
//
// repo := repos.Group("")
// {
// repo.Use(session.SetRepo())
// repo.Use(session.SetPerm())
// repo.Use(session.MustPull)
//
// repo.GET("", api.GetRepo)
// repo.GET("/key", api.GetRepoKey)
// repo.POST("/key", api.PostRepoKey)
// repo.GET("/builds", api.GetBuilds)
// repo.GET("/builds/:number", api.GetBuild)
// repo.GET("/logs/:number/:job", api.GetBuildLogs)
// repo.POST("/sign", session.MustPush, api.Sign)
//
// repo.POST("/secrets", session.MustPush, api.PostSecret)
// repo.DELETE("/secrets/:secret", session.MustPush, api.DeleteSecret)
//
// // requires authenticated user
// repo.POST("/encrypt", session.MustUser(), api.PostSecure)
//
// // requires push permissions
// repo.PATCH("", session.MustPush, api.PatchRepo)
// repo.DELETE("", session.MustPush, api.DeleteRepo)
//
// repo.POST("/builds/:number", session.MustPush, api.PostBuild)
// repo.DELETE("/builds/:number/:job", session.MustPush, api.DeleteBuild)
// }
// }
//
// badges := e.Group("/api/badges/:owner/:name")
// {
// badges.GET("/status.svg", web.GetBadge)
// badges.GET("/cc.xml", web.GetCC)
// }
//
// e.POST("/hook", web.PostHook)
// e.POST("/api/hook", web.PostHook)
//
// stream := e.Group("/api/stream")
// {
// stream.Use(session.SetRepo())
// stream.Use(session.SetPerm())
// stream.Use(session.MustPull)
//
// stream.GET("/:owner/:name", web.GetRepoEvents)
// stream.GET("/:owner/:name/:build/:number", web.GetStream)
// }
//
// bots := e.Group("/bots")
// {
// bots.Use(session.MustUser())
// bots.POST("/slack", web.Slack)
// bots.POST("/slack/:command", web.Slack)
// }
//
// auth := e.Group("/authorize")
// {
// auth.GET("", web.GetLogin)
// auth.POST("", web.GetLogin)
// auth.POST("/token", web.GetLoginToken)
// }
//
// queue := e.Group("/api/queue")
// {
// queue.Use(middleware.AgentMust())
// queue.POST("/pull", api.Pull)
// queue.POST("/pull/:os/:arch", api.Pull)
// queue.POST("/wait/:id", api.Wait)
// queue.POST("/stream/:id", api.Stream)
// queue.POST("/status/:id", api.Update)
// }
//
// gitlab := e.Group("/gitlab/:owner/:name")
// {
// gitlab.Use(session.SetRepo())
// gitlab.GET("/commits/:sha", web.GetCommit)
// gitlab.GET("/pulls/:number", web.GetPullRequest)
//
// redirects := gitlab.Group("/redirect")
// {
// redirects.GET("/commits/:sha", web.RedirectSha)
// redirects.GET("/pulls/:number", web.RedirectPullRequest)
// }
// }
//
// return normalize(e)
// }
//
// // normalize is a helper function to work around the following
// // issue with gin. https://github.com/gin-gonic/gin/issues/388
// func normalize(h http.Handler) http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//
// parts := strings.Split(r.URL.Path, "/")[1:]
// switch parts[0] {
// case "settings", "bots", "repos", "api", "login", "logout", "", "authorize", "hook", "static", "gitlab":
// // no-op
// default:
//
// if len(parts) > 2 && parts[2] != "settings" {
// parts = append(parts[:2], append([]string{"builds"}, parts[2:]...)...)
// }
//
// // prefix the URL with /repo so that it
// // can be effectively routed.
// parts = append([]string{"", "repos"}, parts...)
//
// // reconstruct the path
// r.URL.Path = strings.Join(parts, "/")
// }
//
// h.ServeHTTP(w, r)
// })
// }
// normalize is a helper function to work around the following
// issue with gin. https://github.com/gin-gonic/gin/issues/388
func normalize(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")[1:]
switch parts[0] {
case "settings", "bots", "repos", "api", "login", "logout", "", "authorize", "hook", "static", "gitlab":
// no-op
default:
if len(parts) > 2 && parts[2] != "settings" {
parts = append(parts[:2], append([]string{"builds"}, parts[2:]...)...)
}
// prefix the URL with /repo so that it
// can be effectively routed.
parts = append([]string{"", "repos"}, parts...)
// reconstruct the path
r.URL.Path = strings.Join(parts, "/")
}
h.ServeHTTP(w, r)
})
}