2016-01-27 19:07:58 +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 v1 implements the first version of the Clair API.
|
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2017-01-27 01:14:44 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2017-01-27 01:14:44 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-01-27 01:14:44 +00:00
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
)
|
2016-01-27 19:07:58 +00:00
|
|
|
|
2017-01-27 01:14:44 +00:00
|
|
|
var (
|
|
|
|
promResponseDurationMilliseconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
Name: "clair_api_response_duration_milliseconds",
|
|
|
|
Help: "The duration of time it takes to receieve and write a response to an API request",
|
|
|
|
Buckets: prometheus.ExponentialBuckets(9.375, 2, 10),
|
|
|
|
}, []string{"route", "code"})
|
2016-01-27 19:07:58 +00:00
|
|
|
)
|
|
|
|
|
2017-01-27 01:14:44 +00:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(promResponseDurationMilliseconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
type handler func(http.ResponseWriter, *http.Request, httprouter.Params, *context) (route string, status int)
|
|
|
|
|
|
|
|
func httpHandler(h handler, ctx *context) httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
start := time.Now()
|
|
|
|
route, status := h(w, r, p, ctx)
|
|
|
|
statusStr := strconv.Itoa(status)
|
|
|
|
if status == 0 {
|
|
|
|
statusStr = "???"
|
|
|
|
}
|
|
|
|
|
|
|
|
promResponseDurationMilliseconds.
|
|
|
|
WithLabelValues(route, statusStr).
|
|
|
|
Observe(float64(time.Since(start).Nanoseconds()) / float64(time.Millisecond))
|
|
|
|
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithFields(log.Fields{"remote addr": r.RemoteAddr, "method": r.Method, "request uri": r.RequestURI, "status": statusStr, "elapsed time": time.Since(start)}).Info("Handled HTTP request")
|
2017-01-27 01:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type context struct {
|
|
|
|
Store database.Datastore
|
|
|
|
PaginationKey string
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
// NewRouter creates an HTTP router for version 1 of the Clair API.
|
2017-01-27 01:14:44 +00:00
|
|
|
func NewRouter(store database.Datastore, paginationKey string) *httprouter.Router {
|
2016-01-27 19:07:58 +00:00
|
|
|
router := httprouter.New()
|
2017-01-27 01:14:44 +00:00
|
|
|
ctx := &context{store, paginationKey}
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
// Layers
|
2017-01-27 01:14:44 +00:00
|
|
|
router.POST("/layers", httpHandler(postLayer, ctx))
|
|
|
|
router.GET("/layers/:layerName", httpHandler(getLayer, ctx))
|
|
|
|
router.DELETE("/layers/:layerName", httpHandler(deleteLayer, ctx))
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
// Namespaces
|
2017-01-27 01:14:44 +00:00
|
|
|
router.GET("/namespaces", httpHandler(getNamespaces, ctx))
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
// Vulnerabilities
|
2017-01-27 01:14:44 +00:00
|
|
|
router.GET("/namespaces/:namespaceName/vulnerabilities", httpHandler(getVulnerabilities, ctx))
|
|
|
|
router.POST("/namespaces/:namespaceName/vulnerabilities", httpHandler(postVulnerability, ctx))
|
|
|
|
router.GET("/namespaces/:namespaceName/vulnerabilities/:vulnerabilityName", httpHandler(getVulnerability, ctx))
|
|
|
|
router.PUT("/namespaces/:namespaceName/vulnerabilities/:vulnerabilityName", httpHandler(putVulnerability, ctx))
|
|
|
|
router.DELETE("/namespaces/:namespaceName/vulnerabilities/:vulnerabilityName", httpHandler(deleteVulnerability, ctx))
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
// Fixes
|
2017-01-27 01:14:44 +00:00
|
|
|
router.GET("/namespaces/:namespaceName/vulnerabilities/:vulnerabilityName/fixes", httpHandler(getFixes, ctx))
|
|
|
|
router.PUT("/namespaces/:namespaceName/vulnerabilities/:vulnerabilityName/fixes/:fixName", httpHandler(putFix, ctx))
|
|
|
|
router.DELETE("/namespaces/:namespaceName/vulnerabilities/:vulnerabilityName/fixes/:fixName", httpHandler(deleteFix, ctx))
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
// Notifications
|
2017-01-27 01:14:44 +00:00
|
|
|
router.GET("/notifications/:notificationName", httpHandler(getNotification, ctx))
|
|
|
|
router.DELETE("/notifications/:notificationName", httpHandler(deleteNotification, ctx))
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
// Metrics
|
2017-01-27 01:14:44 +00:00
|
|
|
router.GET("/metrics", httpHandler(getMetrics, ctx))
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
return router
|
|
|
|
}
|