cloudflare/cfssl_trust
Publicmirrored from https://github.com/cloudflare/cfssl_trustAvailable
cli/search.go
70lines · modecode
| 1 | package cli |
| 2 | |
| 3 | import ( |
| 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 | |
| 13 | var searchCmd = &cobra.Command{ |
| 14 | Use: "search", |
| 15 | Short: "Search for certificates.", |
| 16 | Long: ` |
| 17 | Search for certificates that match a set of search terms. Search terms |
| 18 | have 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 | |
| 27 | Multiple search terms are supported; for example "ski:1234567 issuer:Example". |
| 28 | |
| 29 | The subject and example use the string form of the issuer as produced |
| 30 | by the info command; for example, a certificate with 'Example' in the |
| 31 | subject's organisation field can be search for using |
| 32 | |
| 33 | subject:O=Example |
| 34 | |
| 35 | The support regular expression syntax is the RE2 syntax used by the Go |
| 36 | programming language described at https://golang.org/s/re2syntax. |
| 37 | `, |
| 38 | Run: search, |
| 39 | } |
| 40 | |
| 41 | func init() { |
| 42 | rootCmd.AddCommand(searchCmd) |
| 43 | } |
| 44 | |
| 45 | func 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 | |