2015-11-13 19:11:28 +00:00
|
|
|
// Copyright 2015 clair authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VersionRouter is an HTTP router that forwards requests to the appropriate
|
|
|
|
// router depending on the API version specified in the requested URI.
|
|
|
|
type VersionRouter map[string]*httprouter.Router
|
|
|
|
|
|
|
|
// NewVersionRouter instantiates a VersionRouter and every sub-routers that are
|
|
|
|
// necessary to handle supported API versions.
|
2015-12-28 20:03:29 +00:00
|
|
|
func NewVersionRouter(to time.Duration, env *Env) *VersionRouter {
|
2015-11-13 19:11:28 +00:00
|
|
|
return &VersionRouter{
|
2015-12-28 20:03:29 +00:00
|
|
|
"/v1": NewRouterV1(to, env),
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP forwards requests to the appropriate router depending on the API
|
|
|
|
// version specified in the requested URI and remove the version information
|
|
|
|
// from the request URL.Path, without modifying the request uRequestURI.
|
|
|
|
func (vs VersionRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
urlStr := r.URL.String()
|
|
|
|
var version string
|
|
|
|
if len(urlStr) >= 3 {
|
|
|
|
version = urlStr[:3]
|
|
|
|
}
|
|
|
|
if router, _ := vs[version]; router != nil {
|
|
|
|
// Remove the version number from the request path to let the router do its
|
|
|
|
// job but do not update the RequestURI
|
|
|
|
r.URL.Path = strings.Replace(r.URL.Path, version, "", 1)
|
|
|
|
router.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRouterV1 creates a new router for the API (Version 1)
|
2015-12-28 20:03:29 +00:00
|
|
|
func NewRouterV1(to time.Duration, env *Env) *httprouter.Router {
|
2015-11-13 19:11:28 +00:00
|
|
|
router := httprouter.New()
|
2015-12-28 20:03:29 +00:00
|
|
|
|
|
|
|
// Create a wrapper that will wrap a Handle into a httprouter.Handle and that adds
|
|
|
|
// logging and time-out capabilities.
|
|
|
|
wrap := func(fn Handle, e *Env) httprouter.Handle {
|
|
|
|
return Logger(TimeOut(to, WrapHandle(fn, e)))
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// General
|
2015-12-28 20:03:29 +00:00
|
|
|
router.GET("/versions", wrap(GETVersions, env))
|
|
|
|
router.GET("/health", wrap(GETHealth, env))
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
// Layers
|
2015-12-28 20:03:29 +00:00
|
|
|
router.POST("/layers", wrap(POSTLayers, env))
|
2016-01-20 18:57:51 +00:00
|
|
|
router.DELETE("/layers/:name", wrap(DELETELayers, env))
|
|
|
|
router.GET("/layers/:name", wrap(GETLayers, env))
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
// Vulnerabilities
|
2015-12-28 20:03:29 +00:00
|
|
|
// router.POST("/vulnerabilities", wrap(logic.POSTVulnerabilities))
|
|
|
|
// router.PUT("/vulnerabilities/:id", wrap(logic.PUTVulnerabilities))
|
|
|
|
// router.GET("/vulnerabilities/:id", wrap(logic.GETVulnerabilities))
|
|
|
|
// router.DELETE("/vulnerabilities/:id", wrap(logic.DELVulnerabilities))
|
|
|
|
// router.GET("/vulnerabilities/:id/introducing-layers", wrap(logic.GETVulnerabilitiesIntroducingLayers))
|
|
|
|
// router.POST("/vulnerabilities/:id/affected-layers", wrap(logic.POSTVulnerabilitiesAffectedLayers))
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHealthRouter creates a new router that only serve the Health function on /
|
2015-12-28 20:03:29 +00:00
|
|
|
func NewHealthRouter(env *Env) *httprouter.Router {
|
2015-11-13 19:11:28 +00:00
|
|
|
router := httprouter.New()
|
2015-12-28 20:03:29 +00:00
|
|
|
router.GET("/", WrapHandle(GETHealth, env))
|
2015-11-13 19:11:28 +00:00
|
|
|
return router
|
|
|
|
}
|