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 (
|
2015-12-15 17:03:42 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"io/ioutil"
|
2015-11-13 19:11:28 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tylerb/graceful"
|
2015-11-24 05:18:11 +00:00
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
"github.com/coreos/clair/api/context"
|
2015-12-07 21:38:50 +00:00
|
|
|
"github.com/coreos/clair/config"
|
2015-11-24 05:18:11 +00:00
|
|
|
"github.com/coreos/clair/utils"
|
2016-01-28 20:13:11 +00:00
|
|
|
"github.com/coreos/pkg/capnslog"
|
2015-11-13 19:11:28 +00:00
|
|
|
)
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
const timeoutResponse = `{"Error":{"Message":"Clair failed to respond within the configured timeout window.","Type":"Timeout"}}`
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2016-01-28 20:13:11 +00:00
|
|
|
var log = capnslog.NewPackageLogger("github.com/coreos/clair", "api")
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
func Run(config *config.APIConfig, ctx *context.RouteContext, st *utils.Stopper) {
|
2015-12-07 21:38:50 +00:00
|
|
|
defer st.End()
|
|
|
|
|
|
|
|
// Do not run the API service if there is no config.
|
|
|
|
if config == nil {
|
|
|
|
log.Infof("main API service is disabled.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infof("starting main API on port %d.", config.Port)
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2015-12-15 17:03:42 +00:00
|
|
|
tlsConfig, err := tlsClientConfig(config.CAFile)
|
2015-11-24 05:18:11 +00:00
|
|
|
if err != nil {
|
2015-12-16 17:01:51 +00:00
|
|
|
log.Fatalf("could not initialize client cert authentication: %s\n", err)
|
2015-11-24 05:18:11 +00:00
|
|
|
}
|
|
|
|
if tlsConfig != nil {
|
2015-12-16 17:01:51 +00:00
|
|
|
log.Info("main API configured with client certificate authentication")
|
2015-11-24 05:18:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
srv := &graceful.Server{
|
|
|
|
Timeout: 0, // Already handled by our TimeOut middleware
|
|
|
|
NoSignalHandling: true, // We want to use our own Stopper
|
|
|
|
Server: &http.Server{
|
2015-12-07 21:38:50 +00:00
|
|
|
Addr: ":" + strconv.Itoa(config.Port),
|
2015-11-24 05:18:11 +00:00
|
|
|
TLSConfig: tlsConfig,
|
2016-01-27 19:07:58 +00:00
|
|
|
Handler: http.TimeoutHandler(newAPIHandler(ctx), config.Timeout, timeoutResponse),
|
2015-11-13 19:11:28 +00:00
|
|
|
},
|
|
|
|
}
|
2016-01-27 19:07:58 +00:00
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
listenAndServeWithStopper(srv, st, config.CertFile, config.KeyFile)
|
2016-01-27 19:07:58 +00:00
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
log.Info("main API stopped")
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
func RunHealth(config *config.APIConfig, ctx *context.RouteContext, st *utils.Stopper) {
|
2016-02-01 20:42:41 +00:00
|
|
|
defer st.End()
|
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
// Do not run the API service if there is no config.
|
|
|
|
if config == nil {
|
|
|
|
log.Infof("health API service is disabled.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infof("starting health API on port %d.", config.HealthPort)
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
srv := &graceful.Server{
|
|
|
|
Timeout: 10 * time.Second, // Interrupt health checks when stopping
|
|
|
|
NoSignalHandling: true, // We want to use our own Stopper
|
|
|
|
Server: &http.Server{
|
2015-12-07 21:38:50 +00:00
|
|
|
Addr: ":" + strconv.Itoa(config.HealthPort),
|
2016-01-27 19:07:58 +00:00
|
|
|
Handler: http.TimeoutHandler(newHealthHandler(ctx), config.Timeout, timeoutResponse),
|
2015-11-13 19:11:28 +00:00
|
|
|
},
|
|
|
|
}
|
2016-01-27 19:07:58 +00:00
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
listenAndServeWithStopper(srv, st, "", "")
|
2016-01-27 19:07:58 +00:00
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
log.Info("health API stopped")
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// listenAndServeWithStopper wraps graceful.Server's
|
|
|
|
// ListenAndServe/ListenAndServeTLS and adds the ability to interrupt them with
|
|
|
|
// the provided utils.Stopper
|
|
|
|
func listenAndServeWithStopper(srv *graceful.Server, st *utils.Stopper, certFile, keyFile string) {
|
|
|
|
go func() {
|
|
|
|
<-st.Chan()
|
|
|
|
srv.Stop(0)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if certFile != "" && keyFile != "" {
|
|
|
|
log.Info("API: TLS Enabled")
|
|
|
|
err = srv.ListenAndServeTLS(certFile, keyFile)
|
|
|
|
} else {
|
|
|
|
err = srv.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
2016-02-01 23:51:13 +00:00
|
|
|
if err != nil {
|
|
|
|
if opErr, ok := err.(*net.OpError); !ok || (ok && opErr.Op != "accept") {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 17:03:42 +00:00
|
|
|
|
|
|
|
// tlsClientConfig initializes a *tls.Config using the given CA. The resulting
|
|
|
|
// *tls.Config is meant to be used to configure an HTTP server to do client
|
|
|
|
// certificate authentication.
|
|
|
|
//
|
|
|
|
// If no CA is given, a nil *tls.Config is returned; no client certificate will
|
2015-12-16 17:01:51 +00:00
|
|
|
// be required and verified. In other words, authentication will be disabled.
|
2015-12-15 17:03:42 +00:00
|
|
|
func tlsClientConfig(caPath string) (*tls.Config, error) {
|
|
|
|
if caPath == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
caCert, err := ioutil.ReadFile(caPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ClientCAs: caCertPool,
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsConfig, nil
|
|
|
|
}
|