2018-09-11 02:49:24 +00:00
|
|
|
// Copyright 2018 clair authors
|
2017-06-05 14:37:29 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-08-16 21:26:53 +00:00
|
|
|
package v3
|
2017-06-05 14:37:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2017-08-16 21:26:53 +00:00
|
|
|
pb "github.com/coreos/clair/api/v3/clairpb"
|
2017-06-05 14:37:29 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2018-09-11 02:49:24 +00:00
|
|
|
"github.com/coreos/clair/pkg/grpcutil"
|
2017-06-05 14:37:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
promResponseDurationMilliseconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
2017-08-16 21:26:53 +00:00
|
|
|
Name: "clair_v3_api_response_duration_milliseconds",
|
2017-07-12 21:04:05 +00:00
|
|
|
Help: "The duration of time it takes to receive and write a response to an V2 API request",
|
2017-06-05 14:37:29 +00:00
|
|
|
Buckets: prometheus.ExponentialBuckets(9.375, 2, 10),
|
|
|
|
}, []string{"route", "code"})
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(promResponseDurationMilliseconds)
|
|
|
|
}
|
|
|
|
|
2018-09-11 02:49:24 +00:00
|
|
|
func prometheusHandler(h http.Handler) http.Handler {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/", h)
|
|
|
|
mux.Handle("/metrics", prometheus.Handler())
|
|
|
|
return mux
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 19:09:24 +00:00
|
|
|
type httpStatusWriter struct {
|
2017-06-05 14:37:29 +00:00
|
|
|
http.ResponseWriter
|
|
|
|
|
|
|
|
StatusCode int
|
|
|
|
}
|
|
|
|
|
2018-08-30 19:09:24 +00:00
|
|
|
func (w *httpStatusWriter) WriteHeader(code int) {
|
2017-06-05 14:37:29 +00:00
|
|
|
w.StatusCode = code
|
|
|
|
w.ResponseWriter.WriteHeader(code)
|
|
|
|
}
|
|
|
|
|
2018-09-11 02:49:24 +00:00
|
|
|
func loggingHandler(h http.Handler) http.Handler {
|
2017-06-05 14:37:29 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
start := time.Now()
|
2018-08-30 19:09:24 +00:00
|
|
|
lrw := &httpStatusWriter{ResponseWriter: w, StatusCode: http.StatusOK}
|
2017-06-05 14:37:29 +00:00
|
|
|
|
2018-09-11 02:49:24 +00:00
|
|
|
h.ServeHTTP(lrw, r)
|
2017-06-05 14:37:29 +00:00
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
2017-07-12 21:04:05 +00:00
|
|
|
"remote addr": r.RemoteAddr,
|
|
|
|
"method": r.Method,
|
|
|
|
"request uri": r.RequestURI,
|
2018-09-11 02:49:24 +00:00
|
|
|
"status": strconv.Itoa(lrw.StatusCode),
|
2017-07-12 21:04:05 +00:00
|
|
|
"elapsed time (ms)": float64(time.Since(start).Nanoseconds()) * 1e-6,
|
2018-09-11 02:49:24 +00:00
|
|
|
}).Info("handled HTTP request")
|
2017-06-05 14:37:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-11 02:49:24 +00:00
|
|
|
// ListenAndServe serves the Clair v3 API over gRPC and the gRPC Gateway.
|
2018-12-29 13:39:16 +00:00
|
|
|
func ListenAndServe(addr, certFile, keyFile, caPath string, store database.Datastore) error {
|
2018-09-11 02:49:24 +00:00
|
|
|
srv := grpcutil.MuxedGRPCServer{
|
|
|
|
Addr: addr,
|
|
|
|
ServicesFunc: func(gsrv *grpc.Server) {
|
|
|
|
pb.RegisterAncestryServiceServer(gsrv, &AncestryServer{Store: store})
|
|
|
|
pb.RegisterNotificationServiceServer(gsrv, &NotificationServer{Store: store})
|
|
|
|
pb.RegisterStatusServiceServer(gsrv, &StatusServer{Store: store})
|
|
|
|
},
|
|
|
|
ServiceHandlerFuncs: []grpcutil.RegisterServiceHandlerFunc{
|
|
|
|
pb.RegisterAncestryServiceHandler,
|
|
|
|
pb.RegisterNotificationServiceHandler,
|
|
|
|
pb.RegisterStatusServiceHandler,
|
|
|
|
},
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 02:49:24 +00:00
|
|
|
middleware := func(h http.Handler) http.Handler {
|
|
|
|
return prometheusHandler(loggingHandler(h))
|
2018-08-30 19:09:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 02:49:24 +00:00
|
|
|
var err error
|
|
|
|
if caPath == "" {
|
|
|
|
err = srv.ListenAndServe(middleware)
|
2017-06-05 14:37:29 +00:00
|
|
|
} else {
|
2018-09-11 02:49:24 +00:00
|
|
|
err = srv.ListenAndServeTLS(certFile, keyFile, caPath, middleware)
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
2018-09-11 02:49:24 +00:00
|
|
|
return err
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|