1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-24 10:07:21 +02:00
woodpecker/router/middleware/agent.go

34 lines
812 B
Go
Raw Normal View History

2016-04-22 02:10:19 +02:00
package middleware
import (
2016-05-02 21:21:25 +02:00
"github.com/codegangsta/cli"
2016-04-22 02:10:19 +02:00
"github.com/drone/drone/shared/token"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
)
2016-05-02 21:21:25 +02:00
const agentKey = "agent"
2016-04-22 02:10:19 +02:00
2016-05-02 21:21:25 +02:00
// Agents is a middleware function that initializes the authorization middleware
2016-04-22 02:10:19 +02:00
// for agents to connect to the queue.
2016-05-02 21:21:25 +02:00
func Agents(cli *cli.Context) gin.HandlerFunc {
secret := cli.String("agent-secret")
if secret == "" {
logrus.Fatalf("failed to generate token from DRONE_AGENT_SECRET")
2016-04-22 02:10:19 +02:00
}
2016-05-04 08:36:23 +02:00
t := token.New(token.AgentToken, secret)
2016-05-02 21:21:25 +02:00
s, err := t.Sign(secret)
2016-04-22 02:10:19 +02:00
if err != nil {
2016-05-02 21:21:25 +02:00
logrus.Fatalf("failed to generate token from DRONE_AGENT_SECRET. %s", err)
2016-04-22 02:10:19 +02:00
}
2016-05-02 21:21:25 +02:00
logrus.Infof("using agent secret %s", secret)
2016-04-22 02:10:19 +02:00
logrus.Warnf("agents can connect with token %s", s)
return func(c *gin.Context) {
2016-05-02 21:21:25 +02:00
c.Set(agentKey, secret)
2016-04-22 02:10:19 +02:00
}
}