harness-drone/router/middleware/agent.go

34 lines
812 B
Go
Raw Normal View History

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