-
Notifications
You must be signed in to change notification settings - Fork 226
/
root.go
153 lines (136 loc) · 4.59 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2022 Google LLC. All Rights Reserved.
//
// 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 cmd implements subcommands of ctclient, the command-line utility for
// interacting with CT logs.
package cmd
import (
"context"
"crypto/tls"
"flag"
"fmt"
"net/http"
"os"
"strings"
"time"
ct "github.com/google/certificate-transparency-go"
"github.com/google/certificate-transparency-go/client"
"github.com/google/certificate-transparency-go/jsonclient"
"github.com/google/certificate-transparency-go/loglist3"
"github.com/google/certificate-transparency-go/x509util"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog/v2"
)
const connectionFlags = "{--log_uri uri | --log_name name [--log_list {file|uri}]} [--pub_key file]"
var (
skipHTTPSVerify bool
logName string
logList string
logURI string
pubKey string
)
func init() {
// Add flags added with "flag" package, including klog, to Cobra flag set.
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
flags := rootCmd.PersistentFlags()
flags.BoolVar(&skipHTTPSVerify, "skip_https_verify", false, "Skip verification of HTTPS transport connection")
flags.StringVar(&logName, "log_name", "", "Name of log to retrieve information from --log_list for")
flags.StringVar(&logList, "log_list", loglist3.AllLogListURL, "Location of master log list (URL or filename)")
flags.StringVar(&logURI, "log_uri", "https://ct.googleapis.com/rocketeer", "CT log base URI")
flags.StringVar(&pubKey, "pub_key", "", "Name of file containing log's public key")
}
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "ctclient",
Short: "A command line client for Certificate Transparency logs",
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
flag.Parse()
},
}
// Execute adds all child commands to the root command and sets flags
// appropriately. It needs to be called exactly once by main().
func Execute() {
if err := rootCmd.Execute(); err != nil {
klog.Fatal(err)
}
}
func signatureToString(signed *ct.DigitallySigned) string {
return fmt.Sprintf("Signature: Hash=%v Sign=%v Value=%x", signed.Algorithm.Hash, signed.Algorithm.Signature, signed.Signature)
}
func exitWithDetails(err error) {
if err, ok := err.(client.RspError); ok {
klog.Infof("HTTP details: status=%d, body:\n%s", err.StatusCode, err.Body)
}
klog.Exit(err.Error())
}
func connect(ctx context.Context) *client.LogClient {
var tlsCfg *tls.Config
if skipHTTPSVerify {
klog.Warning("Skipping HTTPS connection verification")
tlsCfg = &tls.Config{InsecureSkipVerify: skipHTTPSVerify}
}
httpClient := &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
TLSHandshakeTimeout: 30 * time.Second,
ResponseHeaderTimeout: 30 * time.Second,
MaxIdleConnsPerHost: 10,
DisableKeepAlives: false,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsCfg,
},
}
opts := jsonclient.Options{UserAgent: "ct-go-ctclient/1.0"}
if pubKey != "" {
pubkey, err := os.ReadFile(pubKey)
if err != nil {
klog.Exit(err)
}
opts.PublicKey = string(pubkey)
}
uri := logURI
if logName != "" {
llData, err := x509util.ReadFileOrURL(logList, httpClient)
if err != nil {
klog.Exitf("Failed to read log list: %v", err)
}
ll, err := loglist3.NewFromJSON(llData)
if err != nil {
klog.Exitf("Failed to build log list: %v", err)
}
logs := ll.FindLogByName(logName)
if len(logs) == 0 {
klog.Exitf("No log with name like %q found in loglist %q", logName, logList)
}
if len(logs) > 1 {
logNames := make([]string, len(logs))
for i, log := range logs {
logNames[i] = fmt.Sprintf("%q", log.Description)
}
klog.Exitf("Multiple logs with name like %q found in loglist: %s", logName, strings.Join(logNames, ","))
}
uri = logs[0].URL
if opts.PublicKey == "" {
opts.PublicKeyDER = logs[0].Key
}
}
klog.V(1).Infof("Use CT log at %s", uri)
logClient, err := client.New(uri, httpClient, opts)
if err != nil {
klog.Exit(err)
}
return logClient
}