...

Source file src/github.com/sigstore/rekor/cmd/rekor-server/app/serve.go

Documentation: github.com/sigstore/rekor/cmd/rekor-server/app

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package app
    17  
    18  import (
    19  	"flag"
    20  	"net/http"
    21  	"time"
    22  
    23  	"cloud.google.com/go/profiler"
    24  	"github.com/go-chi/chi/middleware"
    25  	"github.com/go-openapi/loads"
    26  	"github.com/prometheus/client_golang/prometheus/promhttp"
    27  	"github.com/spf13/cobra"
    28  	"github.com/spf13/viper"
    29  	"sigs.k8s.io/release-utils/version"
    30  
    31  	"github.com/sigstore/rekor/pkg/api"
    32  	"github.com/sigstore/rekor/pkg/generated/restapi"
    33  	"github.com/sigstore/rekor/pkg/generated/restapi/operations"
    34  	"github.com/sigstore/rekor/pkg/log"
    35  	"github.com/sigstore/rekor/pkg/types/alpine"
    36  	alpine_v001 "github.com/sigstore/rekor/pkg/types/alpine/v0.0.1"
    37  	"github.com/sigstore/rekor/pkg/types/cose"
    38  	cose_v001 "github.com/sigstore/rekor/pkg/types/cose/v0.0.1"
    39  	"github.com/sigstore/rekor/pkg/types/dsse"
    40  	dsse_v001 "github.com/sigstore/rekor/pkg/types/dsse/v0.0.1"
    41  	hashedrekord "github.com/sigstore/rekor/pkg/types/hashedrekord"
    42  	hashedrekord_v001 "github.com/sigstore/rekor/pkg/types/hashedrekord/v0.0.1"
    43  	"github.com/sigstore/rekor/pkg/types/helm"
    44  	helm_v001 "github.com/sigstore/rekor/pkg/types/helm/v0.0.1"
    45  	"github.com/sigstore/rekor/pkg/types/intoto"
    46  	intoto_v001 "github.com/sigstore/rekor/pkg/types/intoto/v0.0.1"
    47  	intoto_v002 "github.com/sigstore/rekor/pkg/types/intoto/v0.0.2"
    48  	"github.com/sigstore/rekor/pkg/types/jar"
    49  	jar_v001 "github.com/sigstore/rekor/pkg/types/jar/v0.0.1"
    50  	"github.com/sigstore/rekor/pkg/types/rekord"
    51  	rekord_v001 "github.com/sigstore/rekor/pkg/types/rekord/v0.0.1"
    52  	"github.com/sigstore/rekor/pkg/types/rfc3161"
    53  	rfc3161_v001 "github.com/sigstore/rekor/pkg/types/rfc3161/v0.0.1"
    54  	"github.com/sigstore/rekor/pkg/types/rpm"
    55  	rpm_v001 "github.com/sigstore/rekor/pkg/types/rpm/v0.0.1"
    56  	"github.com/sigstore/rekor/pkg/types/tuf"
    57  	tuf_v001 "github.com/sigstore/rekor/pkg/types/tuf/v0.0.1"
    58  )
    59  
    60  // serveCmd represents the serve command
    61  var serveCmd = &cobra.Command{
    62  	Use:   "serve",
    63  	Short: "start http server with configured api",
    64  	Long:  `Starts a http server and serves the configured api`,
    65  	Run: func(_ *cobra.Command, _ []string) {
    66  		// Setup the logger to dev/prod
    67  		log.ConfigureLogger(viper.GetString("log_type"), viper.GetString("trace-string-prefix"))
    68  
    69  		// workaround for https://github.com/sigstore/rekor/issues/68
    70  		// from https://github.com/golang/glog/commit/fca8c8854093a154ff1eb580aae10276ad6b1b5f
    71  		_ = flag.CommandLine.Parse([]string{})
    72  
    73  		vi := version.GetVersionInfo()
    74  		viStr, err := vi.JSONString()
    75  		if err != nil {
    76  			viStr = vi.String()
    77  		}
    78  		log.Logger.Infof("starting rekor-server @ %v", viStr)
    79  
    80  		// overrides the correlation ID printed in logs, if config is set
    81  		middleware.RequestIDHeader = viper.GetString("http-request-id-header-name")
    82  
    83  		if viper.GetBool("gcp_cloud_profiling.enabled") {
    84  			cfg := profiler.Config{
    85  				Service:           viper.GetString("gcp_cloud_profiling.service"),
    86  				ServiceVersion:    viper.GetString("gcp_cloud_profiling.service_version"),
    87  				ProjectID:         viper.GetString("gcp_cloud_profiling.project_id"),
    88  				EnableOCTelemetry: viper.GetBool("gcp_cloud_profiling.enable_oc_telemetry"),
    89  			}
    90  
    91  			if err := profiler.Start(cfg); err != nil {
    92  				log.Logger.Warnf("Error configuring GCP Cloud Profiling: %v", err)
    93  			}
    94  		}
    95  
    96  		doc, _ := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
    97  		server := restapi.NewServer(operations.NewRekorServerAPI(doc))
    98  		defer func() {
    99  			if err := server.Shutdown(); err != nil {
   100  				log.Logger.Error(err)
   101  			}
   102  		}()
   103  		//TODO: make this a config option for server to load via viper field
   104  		//TODO: add command line option to print versions supported in binary
   105  
   106  		// these trigger loading of package and therefore init() methods to run
   107  		pluggableTypeMap := map[string][]string{
   108  			rekord.KIND:       {rekord_v001.APIVERSION},
   109  			rpm.KIND:          {rpm_v001.APIVERSION},
   110  			jar.KIND:          {jar_v001.APIVERSION},
   111  			intoto.KIND:       {intoto_v001.APIVERSION, intoto_v002.APIVERSION},
   112  			cose.KIND:         {cose_v001.APIVERSION},
   113  			rfc3161.KIND:      {rfc3161_v001.APIVERSION},
   114  			alpine.KIND:       {alpine_v001.APIVERSION},
   115  			helm.KIND:         {helm_v001.APIVERSION},
   116  			tuf.KIND:          {tuf_v001.APIVERSION},
   117  			hashedrekord.KIND: {hashedrekord_v001.APIVERSION},
   118  			dsse.KIND:         {dsse_v001.APIVERSION},
   119  		}
   120  		for k, v := range pluggableTypeMap {
   121  			log.Logger.Infof("Loading support for pluggable type '%v'", k)
   122  			log.Logger.Infof("Loading version '%v' for pluggable type '%v'", v, k)
   123  		}
   124  
   125  		server.Host = viper.GetString("rekor_server.address")
   126  		server.Port = int(viper.GetUint("port"))
   127  		server.EnabledListeners = []string{"http"}
   128  
   129  		treeID := viper.GetUint("trillian_log_server.tlog_id")
   130  
   131  		api.ConfigureAPI(treeID)
   132  		server.ConfigureAPI()
   133  
   134  		http.Handle("/metrics", promhttp.Handler())
   135  		go func() {
   136  			srv := &http.Server{
   137  				Addr:         ":2112",
   138  				ReadTimeout:  10 * time.Second,
   139  				WriteTimeout: 10 * time.Second,
   140  			}
   141  			_ = srv.ListenAndServe()
   142  		}()
   143  
   144  		if err := server.Serve(); err != nil {
   145  			log.Logger.Fatal(err)
   146  		}
   147  	},
   148  }
   149  
   150  func init() {
   151  	rootCmd.AddCommand(serveCmd)
   152  }
   153  

View as plain text