2020-10-26 23:22:13 +03:00
|
|
|
// Copyright 2020 Security Scorecard 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.
|
|
|
|
|
2020-10-20 03:26:48 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-20 16:52:57 +03:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2020-12-23 00:20:10 +03:00
|
|
|
"log"
|
2020-10-20 03:26:48 +03:00
|
|
|
"net/http"
|
2020-10-27 22:23:48 +03:00
|
|
|
"os"
|
2020-10-20 16:52:57 +03:00
|
|
|
"strings"
|
2020-10-20 03:26:48 +03:00
|
|
|
|
2021-05-24 06:51:52 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-10-08 02:16:01 +03:00
|
|
|
"github.com/ossf/scorecard/v3/checks"
|
2021-11-15 05:46:41 +03:00
|
|
|
"github.com/ossf/scorecard/v3/clients"
|
2021-10-08 02:16:01 +03:00
|
|
|
"github.com/ossf/scorecard/v3/clients/githubrepo"
|
|
|
|
"github.com/ossf/scorecard/v3/pkg"
|
2020-10-20 03:26:48 +03:00
|
|
|
)
|
|
|
|
|
2021-05-22 20:19:52 +03:00
|
|
|
//nolint:gochecknoinits
|
2020-10-20 03:26:48 +03:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(serveCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Short: "Serve the scorecard program over http",
|
|
|
|
Long: ``,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-09-02 05:32:26 +03:00
|
|
|
logger, err := githubrepo.NewLogger(*logLevel)
|
2021-04-19 22:18:34 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to construct logger: %v", err)
|
|
|
|
}
|
2020-12-23 00:20:10 +03:00
|
|
|
//nolint
|
2020-10-20 03:26:48 +03:00
|
|
|
defer logger.Sync() // flushes buffer, if any
|
|
|
|
sugar := logger.Sugar()
|
2020-10-20 16:52:57 +03:00
|
|
|
t, err := template.New("webpage").Parse(tpl)
|
|
|
|
if err != nil {
|
|
|
|
sugar.Panic(err)
|
|
|
|
}
|
2020-10-20 03:26:48 +03:00
|
|
|
|
|
|
|
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
|
2020-10-20 16:52:57 +03:00
|
|
|
repoParam := r.URL.Query().Get("repo")
|
2021-02-17 04:55:36 +03:00
|
|
|
const length = 3
|
|
|
|
s := strings.SplitN(repoParam, "/", length)
|
|
|
|
if len(s) != length {
|
2020-10-27 22:23:48 +03:00
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
2021-10-29 19:07:46 +03:00
|
|
|
repo, err := githubrepo.MakeGithubRepo(repoParam)
|
|
|
|
if err != nil {
|
2021-02-15 05:03:33 +03:00
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
2020-10-20 16:52:57 +03:00
|
|
|
}
|
2020-10-20 03:26:48 +03:00
|
|
|
ctx := r.Context()
|
2021-09-02 05:32:26 +03:00
|
|
|
repoClient := githubrepo.CreateGithubRepoClient(ctx, logger)
|
2021-11-17 06:04:37 +03:00
|
|
|
ossFuzzRepoClient, err := githubrepo.CreateOssFuzzRepoClient(ctx, logger)
|
|
|
|
if err != nil {
|
|
|
|
sugar.Error(err)
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
defer ossFuzzRepoClient.Close()
|
2021-11-15 05:46:41 +03:00
|
|
|
ciiClient := clients.DefaultCIIBestPracticesClient()
|
2021-11-17 06:04:37 +03:00
|
|
|
repoResult, err := pkg.RunScorecards(ctx, repo, checks.AllChecks, repoClient, ossFuzzRepoClient, ciiClient)
|
2021-06-21 09:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
sugar.Error(err)
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2021-02-15 05:03:33 +03:00
|
|
|
|
|
|
|
if r.Header.Get("Content-Type") == "application/json" {
|
2021-07-20 21:36:35 +03:00
|
|
|
if err := repoResult.AsJSON(showDetails, *logLevel, rw); err != nil {
|
2021-02-15 05:03:33 +03:00
|
|
|
sugar.Error(err)
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-04-19 22:49:51 +03:00
|
|
|
if err := t.Execute(rw, repoResult); err != nil {
|
2020-10-20 16:52:57 +03:00
|
|
|
sugar.Warn(err)
|
|
|
|
}
|
2020-10-20 03:26:48 +03:00
|
|
|
})
|
2020-10-27 22:23:48 +03:00
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "8080"
|
|
|
|
}
|
|
|
|
fmt.Printf("Listening on localhost:%s\n", port)
|
2020-12-23 00:20:10 +03:00
|
|
|
err = http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", port), nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("ListenAndServe ", err)
|
|
|
|
}
|
2020-10-20 03:26:48 +03:00
|
|
|
},
|
|
|
|
}
|
2020-10-20 16:52:57 +03:00
|
|
|
|
|
|
|
const tpl = `
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
2021-04-19 22:49:51 +03:00
|
|
|
<title>Scorecard Results for: {{.Repo}}</title>
|
2020-10-20 16:52:57 +03:00
|
|
|
</head>
|
|
|
|
<body>
|
2021-04-27 00:57:00 +03:00
|
|
|
{{range .Checks}}
|
2020-10-20 16:52:57 +03:00
|
|
|
<div>
|
2021-04-10 15:26:56 +03:00
|
|
|
<p>{{ .Name }}: {{ .Pass }}</p>
|
2020-10-20 16:52:57 +03:00
|
|
|
</div>
|
|
|
|
{{end}}
|
|
|
|
</body>
|
|
|
|
</html>`
|