New route to expose PGP Keys

This commit is contained in:
nemunaire 2022-09-10 12:14:11 +02:00
parent f184c7d8de
commit 376a059541
3 changed files with 35 additions and 16 deletions

1
api.go
View File

@ -14,6 +14,7 @@ func declareAPIRoutes(router *gin.Engine) {
declareAPIAuthRoutes(apiRoutes) declareAPIAuthRoutes(apiRoutes)
declareAPISurveysRoutes(apiRoutes) declareAPISurveysRoutes(apiRoutes)
declareAPIWorksRoutes(apiRoutes) declareAPIWorksRoutes(apiRoutes)
declareAPIKeysRoutes(apiRoutes)
declareCallbacksRoutes(apiRoutes) declareCallbacksRoutes(apiRoutes)
authRoutes := router.Group("") authRoutes := router.Group("")

30
keys.go
View File

@ -13,6 +13,36 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func declareAPIKeysRoutes(router *gin.RouterGroup) {
usersRoutes := router.Group("/users/:uid")
usersRoutes.Use(userHandler)
usersRoutes.GET("/pgp_keys", func(c *gin.Context) {
var u *User
if user, ok := c.Get("user"); ok {
u = user.(*User)
} else {
u = c.MustGet("LoggedUser").(*User)
}
keys, err := u.GetKeys()
if err != nil {
log.Println("Unable to GetKeys:", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieve your keys. Please try again in a few moment."})
return
}
var ret []byte
for _, key := range keys {
if key.Type == "pgp" {
ret = append(ret, []byte(key.Content)...)
}
}
c.Data(http.StatusOK, "application/pgp-keys", ret)
})
}
func declareAPIAuthKeysRoutes(router *gin.RouterGroup) { func declareAPIAuthKeysRoutes(router *gin.RouterGroup) {
router.GET("/keys", func(c *gin.Context) { router.GET("/keys", func(c *gin.Context) {
var u *User var u *User

View File

@ -349,24 +349,12 @@ func TriggerTagUpdate(c *gin.Context, work *Work, repo *Repository, u *User, tag
repo_tag = *tag repo_tag = *tag
} }
var pgp_keys string
keys, err := u.GetKeys()
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"errmsg": "Something goes really wrong with your PGP keys: unable to retrieve them."})
return
}
for _, k := range keys {
pgp_keys += k.Content + "\n"
}
client := drone.NewClient(droneEndpoint, droneConfig) client := drone.NewClient(droneEndpoint, droneConfig)
result, err := client.BuildCreate("srs", "atsebay.t-worker", "", "master", map[string]string{ result, err := client.BuildCreate("srs", "atsebay.t-worker", "", "master", map[string]string{
"REPO_URL": repo.URI, "REPO_URL": repo.URI,
"REPO_TAG": repo_tag, "REPO_TAG": repo_tag,
"USER_PGP_PUBKEY": pgp_keys, "LOGIN": u.Login,
"LOGIN": u.Login, "DEST": fmt.Sprintf("%d", work.Id),
"DEST": fmt.Sprintf("%d", work.Id),
}) })
if err != nil { if err != nil {
log.Println("Unable to communicate with Drone:", err.Error()) log.Println("Unable to communicate with Drone:", err.Error())