2015-04-08 22:43:59 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
"github.com/drone/drone/pkg/bus"
|
2015-05-17 18:42:56 +00:00
|
|
|
"github.com/drone/drone/pkg/queue"
|
2015-05-17 18:45:09 +00:00
|
|
|
"github.com/drone/drone/pkg/remote"
|
2015-05-17 21:25:04 +00:00
|
|
|
"github.com/drone/drone/pkg/runner"
|
2015-05-17 18:10:43 +00:00
|
|
|
"github.com/drone/drone/pkg/store"
|
2015-09-09 21:05:52 +00:00
|
|
|
"github.com/drone/drone/pkg/token"
|
2015-05-17 20:51:42 +00:00
|
|
|
common "github.com/drone/drone/pkg/types"
|
2015-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
2015-04-22 08:00:15 +00:00
|
|
|
func SetQueue(q queue.Queue) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
c.Set("queue", q)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToQueue(c *gin.Context) queue.Queue {
|
2015-05-17 02:46:12 +00:00
|
|
|
v, ok := c.Get("queue")
|
|
|
|
if !ok {
|
2015-04-22 08:00:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(queue.Queue)
|
|
|
|
}
|
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
func SetBus(r bus.Bus) gin.HandlerFunc {
|
2015-04-08 22:43:59 +00:00
|
|
|
return func(c *gin.Context) {
|
2015-05-17 18:10:43 +00:00
|
|
|
c.Set("bus", r)
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
func ToBus(c *gin.Context) bus.Bus {
|
|
|
|
v, ok := c.Get("bus")
|
2015-05-17 02:46:12 +00:00
|
|
|
if !ok {
|
2015-04-08 22:43:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-05-17 18:10:43 +00:00
|
|
|
return v.(bus.Bus)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ToRemote(c *gin.Context) remote.Remote {
|
2015-05-17 02:46:12 +00:00
|
|
|
v, ok := c.Get("remote")
|
|
|
|
if !ok {
|
2015-04-08 22:43:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(remote.Remote)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetRemote(r remote.Remote) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
c.Set("remote", r)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:56:06 +00:00
|
|
|
func ToRunner(c *gin.Context) runner.Runner {
|
2015-05-17 02:46:12 +00:00
|
|
|
v, ok := c.Get("runner")
|
|
|
|
if !ok {
|
2015-05-06 07:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(runner.Runner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetRunner(r runner.Runner) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
c.Set("runner", r)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
func ToPerm(c *gin.Context) *common.Perm {
|
2015-05-17 02:46:12 +00:00
|
|
|
v, ok := c.Get("perm")
|
|
|
|
if !ok {
|
2015-04-08 22:43:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(*common.Perm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToUser(c *gin.Context) *common.User {
|
2015-05-17 02:46:12 +00:00
|
|
|
v, ok := c.Get("user")
|
|
|
|
if !ok {
|
2015-04-08 22:43:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(*common.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToRepo(c *gin.Context) *common.Repo {
|
2015-05-17 02:46:12 +00:00
|
|
|
v, ok := c.Get("repo")
|
|
|
|
if !ok {
|
2015-04-08 22:43:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(*common.Repo)
|
|
|
|
}
|
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
func ToDatastore(c *gin.Context) store.Store {
|
|
|
|
return c.MustGet("datastore").(store.Store)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
func SetDatastore(ds store.Store) gin.HandlerFunc {
|
2015-04-08 22:43:59 +00:00
|
|
|
return func(c *gin.Context) {
|
|
|
|
c.Set("datastore", ds)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 21:05:52 +00:00
|
|
|
func SetUser() gin.HandlerFunc {
|
2015-04-08 22:43:59 +00:00
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
2015-09-09 21:05:52 +00:00
|
|
|
var store = ToDatastore(c)
|
|
|
|
var user *common.User
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-09 21:05:52 +00:00
|
|
|
_, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
|
|
|
|
var err error
|
|
|
|
user, err = store.UserLogin(t.Text)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return user.Hash, nil
|
|
|
|
})
|
2015-04-13 05:32:32 +00:00
|
|
|
|
2015-09-09 21:05:52 +00:00
|
|
|
if err == nil && user != nil && user.ID != 0 {
|
|
|
|
c.Set("user", user)
|
2015-04-13 05:32:32 +00:00
|
|
|
}
|
|
|
|
c.Next()
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetRepo() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
ds := ToDatastore(c)
|
|
|
|
u := ToUser(c)
|
|
|
|
owner := c.Params.ByName("owner")
|
|
|
|
name := c.Params.ByName("name")
|
2015-05-11 07:45:31 +00:00
|
|
|
r, err := ds.RepoName(owner, name)
|
2015-04-08 22:43:59 +00:00
|
|
|
switch {
|
|
|
|
case err != nil && u != nil:
|
2015-04-25 23:43:51 +00:00
|
|
|
c.Fail(404, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
case err != nil && u == nil:
|
2015-04-25 23:43:51 +00:00
|
|
|
c.Fail(401, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Set("repo", r)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetPerm() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
remote := ToRemote(c)
|
|
|
|
user := ToUser(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
perm := perms(remote, user, repo)
|
|
|
|
c.Set("perm", perm)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MustUser() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
u := ToUser(c)
|
|
|
|
if u == nil {
|
|
|
|
c.AbortWithStatus(401)
|
|
|
|
} else {
|
|
|
|
c.Set("user", u)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MustAdmin() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
u := ToUser(c)
|
|
|
|
if u == nil {
|
|
|
|
c.AbortWithStatus(401)
|
|
|
|
} else if !u.Admin {
|
|
|
|
c.AbortWithStatus(403)
|
|
|
|
} else {
|
|
|
|
c.Set("user", u)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckPull() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
u := ToUser(c)
|
|
|
|
m := ToPerm(c)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case u == nil && m == nil:
|
|
|
|
c.AbortWithStatus(401)
|
|
|
|
case u == nil && m.Pull == false:
|
|
|
|
c.AbortWithStatus(401)
|
|
|
|
case u != nil && m.Pull == false:
|
|
|
|
c.AbortWithStatus(404)
|
|
|
|
default:
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckPush() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
switch c.Request.Method {
|
|
|
|
case "GET", "OPTIONS":
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u := ToUser(c)
|
|
|
|
m := ToPerm(c)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case u == nil && m.Push == false:
|
|
|
|
c.AbortWithStatus(401)
|
|
|
|
case u != nil && m.Push == false:
|
|
|
|
c.AbortWithStatus(404)
|
|
|
|
default:
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetHeaders() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
|
|
|
c.Writer.Header().Add("Access-Control-Allow-Origin", "*")
|
|
|
|
c.Writer.Header().Add("X-Frame-Options", "DENY")
|
|
|
|
c.Writer.Header().Add("X-Content-Type-Options", "nosniff")
|
|
|
|
c.Writer.Header().Add("X-XSS-Protection", "1; mode=block")
|
|
|
|
c.Writer.Header().Add("Cache-Control", "no-cache")
|
|
|
|
c.Writer.Header().Add("Cache-Control", "no-store")
|
|
|
|
c.Writer.Header().Add("Cache-Control", "max-age=0")
|
|
|
|
c.Writer.Header().Add("Cache-Control", "must-revalidate")
|
|
|
|
c.Writer.Header().Add("Cache-Control", "value")
|
|
|
|
c.Writer.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
|
|
|
|
c.Writer.Header().Set("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
|
|
|
|
if c.Request.TLS != nil {
|
|
|
|
c.Writer.Header().Add("Strict-Transport-Security", "max-age=31536000")
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:21 +00:00
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization")
|
|
|
|
c.Writer.Header().Set("Allow", "HEAD,GET,POST,PUT,PATCH,DELETE,OPTIONS")
|
|
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
|
|
c.Writer.WriteHeader(200)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-08 22:43:59 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|