cloudflare/cfssl_trust
Publicmirrored fromhttps://github.com/cloudflare/cfssl_trustAvailable
cli/dump.go
63lines · modecode
| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | |
| 8 | "github.com/cloudflare/cfssl_trust/dump" |
| 9 | "github.com/spf13/cobra" |
| 10 | "github.com/spf13/viper" |
| 11 | ) |
| 12 | |
| 13 | var dumpCmd = &cobra.Command{ |
| 14 | Use: "dump", |
| 15 | Short: "Dump a certificate to standard output or file.", |
| 16 | Long: "Dump a certificate to standard output or file given its SKI.", |
| 17 | Run: dumper, |
| 18 | } |
| 19 | |
| 20 | func init() { |
| 21 | rootCmd.AddCommand(dumpCmd) |
| 22 | } |
| 23 | |
| 24 | func dumper(cmd *cobra.Command, args []string) { |
| 25 | if len(args) == 0 { |
| 26 | os.Exit(0) |
| 27 | } |
| 28 | |
| 29 | dbPath := viper.GetString("database.path") |
| 30 | db, err := sql.Open("sqlite3", dbPath) |
| 31 | if err != nil { |
| 32 | fmt.Fprintf(os.Stderr, "[!] %s\n", err) |
| 33 | os.Exit(1) |
| 34 | } |
| 35 | |
| 36 | tx, err := db.Begin() |
| 37 | if err != nil { |
| 38 | fmt.Fprintf(os.Stderr, "[!] %s\n", err) |
| 39 | os.Exit(1) |
| 40 | } |
| 41 | |
| 42 | defer func() { |
| 43 | if err == nil { |
| 44 | err = tx.Commit() |
| 45 | if err != nil { |
| 46 | fmt.Fprintf(os.Stderr, "[!] failed to commit transaction: %s\n", err) |
| 47 | os.Exit(1) |
| 48 | } |
| 49 | } else { |
| 50 | tx.Rollback() |
| 51 | } |
| 52 | }() |
| 53 | |
| 54 | for _, ski := range args { |
| 55 | cert, err := dump.CertPEM(tx, ski) |
| 56 | if err != nil { |
| 57 | fmt.Fprintf(os.Stderr, "[!] %s\n", err) |
| 58 | os.Exit(1) |
| 59 | } |
| 60 | |
| 61 | fmt.Println(string(cert)) |
| 62 | } |
| 63 | } |
| 64 | |