cloudflare/cfssl_trust

Public

mirrored from https://github.com/cloudflare/cfssl_trustAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
trust-store-2022.7.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

cli/search.go

70lines · modecode

1package cli
2
3import (
4 "database/sql"
5 "fmt"
6 "os"
7
8 "github.com/cloudflare/cfssl_trust/info"
9 "github.com/spf13/cobra"
10 "github.com/spf13/viper"
11)
12
13var searchCmd = &cobra.Command{
14 Use: "search",
15 Short: "Search for certificates.",
16 Long: `
17Search for certificates that match a set of search terms. Search terms
18have the form type:regexp, e.g. ski:01234567. The supported types are:
19
20 - ski
21 - aki
22 - subject
23 - issuer
24 - release
25 - bundle
26
27Multiple search terms are supported; for example "ski:1234567 issuer:Example".
28
29The subject and example use the string form of the issuer as produced
30by the info command; for example, a certificate with 'Example' in the
31subject's organisation field can be search for using
32
33 subject:O=Example
34
35The support regular expression syntax is the RE2 syntax used by the Go
36programming language described at https://golang.org/s/re2syntax.
37`,
38 Run: search,
39}
40
41func init() {
42 rootCmd.AddCommand(searchCmd)
43}
44
45func search(cmd *cobra.Command, args []string) {
46 if len(args) == 0 {
47 os.Exit(0)
48 }
49
50 dbPath := viper.GetString("database.path")
51 db, err := sql.Open("sqlite3", dbPath)
52 if err != nil {
53 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
54 os.Exit(1)
55 }
56
57 results, err := info.Query(db, args)
58 if err != nil {
59 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
60 os.Exit(1)
61 }
62
63 for _, cert := range results {
64 err = info.WriteCertificateMetadata(os.Stdout, cert)
65 if err != nil {
66 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
67 os.Exit(1)
68 }
69 }
70}
71