2024-02-27 09:56:15 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/auth"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"gitlab.com/wpetit/goweb/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Mount struct {
|
|
|
|
agentRepo datastore.AgentRepository
|
2024-02-27 14:14:30 +01:00
|
|
|
tenantRepo datastore.TenantRepository
|
2024-03-12 16:22:35 +01:00
|
|
|
specDefRepo datastore.SpecDefinitionRepository
|
2024-02-27 09:56:15 +01:00
|
|
|
authenticators []auth.Authenticator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mount) Mount(r chi.Router) {
|
|
|
|
r.NotFound(m.notFound)
|
|
|
|
|
|
|
|
r.Post("/register", m.registerAgent)
|
|
|
|
|
|
|
|
r.Group(func(r chi.Router) {
|
|
|
|
r.Use(auth.Middleware(m.authenticators...))
|
|
|
|
|
2024-03-03 18:40:56 +01:00
|
|
|
r.Get("/session", m.getSession)
|
|
|
|
|
2024-02-27 09:56:15 +01:00
|
|
|
r.Route("/agents", func(r chi.Router) {
|
2024-02-27 14:14:30 +01:00
|
|
|
r.With(assertUserWithWriteAccess).Post("/claim", m.claimAgent)
|
2024-02-27 09:56:15 +01:00
|
|
|
|
2024-02-27 14:14:30 +01:00
|
|
|
r.With(assertQueryAccess).Get("/", m.queryAgents)
|
2024-02-27 09:56:15 +01:00
|
|
|
|
2024-02-27 14:14:30 +01:00
|
|
|
r.With(assertAgentOrUserWithReadAccess).Get("/{agentID}", m.getAgent)
|
|
|
|
r.With(assertAgentOrUserWithWriteAccess).Put("/{agentID}", m.updateAgent)
|
|
|
|
r.With(assertUserWithWriteAccess).Delete("/{agentID}", m.deleteAgent)
|
2024-02-27 09:56:15 +01:00
|
|
|
|
2024-02-27 14:14:30 +01:00
|
|
|
r.With(assertAgentOrUserWithReadAccess).Get("/{agentID}/specs", m.getAgentSpecs)
|
2024-03-12 16:22:35 +01:00
|
|
|
r.With(assertUserWithWriteAccess).Post("/{agentID}/specs", m.updateAgentSpec)
|
|
|
|
r.With(assertUserWithWriteAccess).Delete("/{agentID}/specs", m.deleteAgentSpec)
|
2024-02-27 14:14:30 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
r.Route("/tenants", func(r chi.Router) {
|
2024-02-27 17:01:24 +01:00
|
|
|
r.With(assertQueryAccess).Get("/", m.queryTenants)
|
2024-02-27 14:14:30 +01:00
|
|
|
r.With(assertAdminAccess).Post("/", m.createTenant)
|
|
|
|
r.With(assertAdminOrTenantReadAccess).Get("/{tenantID}", m.getTenant)
|
|
|
|
r.With(assertAdminOrTenantWriteAccess).Put("/{tenantID}", m.updateTenant)
|
|
|
|
r.With(assertAdminAccess).Delete("/{tenantID}", m.deleteTenant)
|
2024-02-27 09:56:15 +01:00
|
|
|
})
|
2024-03-12 16:22:35 +01:00
|
|
|
|
|
|
|
r.Route("/specs", func(r chi.Router) {
|
|
|
|
r.With(assertQueryAccess).Get("/", m.querySpecDefinitions)
|
|
|
|
r.With(assertQueryAccess).Get("/{specDefName}/{specDefVersion}", m.getSpecDefinition)
|
|
|
|
})
|
2024-02-27 09:56:15 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mount) notFound(w http.ResponseWriter, r *http.Request) {
|
|
|
|
api.ErrorResponse(w, http.StatusNotFound, ErrCodeNotFound, nil)
|
|
|
|
}
|
|
|
|
|
2024-03-12 16:22:35 +01:00
|
|
|
func NewMount(agentRepo datastore.AgentRepository, tenantRepo datastore.TenantRepository, specDefRepo datastore.SpecDefinitionRepository, authenticators ...auth.Authenticator) *Mount {
|
|
|
|
return &Mount{agentRepo, tenantRepo, specDefRepo, authenticators}
|
2024-02-27 09:56:15 +01:00
|
|
|
}
|