scorecard/cmd/serve.go

125 lines
3.2 KiB
Go
Raw Normal View History

// 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"
"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
"github.com/google/go-github/v32/github"
2021-05-24 06:51:52 +03:00
"github.com/shurcooL/githubv4"
"github.com/spf13/cobra"
"go.uber.org/zap"
2020-10-27 22:23:48 +03:00
"github.com/ossf/scorecard/checks"
"github.com/ossf/scorecard/clients/githubrepo"
2020-10-27 22:23:48 +03:00
"github.com/ossf/scorecard/pkg"
"github.com/ossf/scorecard/repos"
"github.com/ossf/scorecard/roundtripper"
2020-10-20 03:26:48 +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) {
cfg := zap.NewProductionConfig()
cfg.Level.SetLevel(*logLevel)
logger, err := cfg.Build()
if err != nil {
log.Fatalf("unable to construct logger: %v", err)
}
//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")
const length = 3
s := strings.SplitN(repoParam, "/", length)
if len(s) != length {
2020-10-27 22:23:48 +03:00
rw.WriteHeader(http.StatusBadRequest)
}
repo := repos.RepoURL{}
if err := repo.Set(repoParam); err != nil {
rw.WriteHeader(http.StatusBadRequest)
2020-10-20 16:52:57 +03:00
}
sugar.Info(repoParam)
2020-10-20 03:26:48 +03:00
ctx := r.Context()
rt := roundtripper.NewTransport(ctx, sugar)
httpClient := &http.Client{
Transport: rt,
}
githubClient := github.NewClient(httpClient)
graphClient := githubv4.NewClient(httpClient)
repoClient := githubrepo.CreateGithubRepoClient(ctx, githubClient)
repoResult, err := pkg.RunScorecards(ctx, repo, checks.AllChecks, repoClient, httpClient, githubClient, graphClient)
if err != nil {
sugar.Error(err)
rw.WriteHeader(http.StatusInternalServerError)
}
if r.Header.Get("Content-Type") == "application/json" {
if err := repoResult.AsJSON(showDetails, rw); err != nil {
sugar.Error(err)
rw.WriteHeader(http.StatusInternalServerError)
}
return
}
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)
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">
<title>Scorecard Results for: {{.Repo}}</title>
2020-10-20 16:52:57 +03:00
</head>
<body>
{{range .Checks}}
2020-10-20 16:52:57 +03:00
<div>
<p>{{ .Name }}: {{ .Pass }}</p>
2020-10-20 16:52:57 +03:00
</div>
{{end}}
</body>
</html>`