cloudflare/cfssl_trust

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
trust-store-2026.3.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

cli/import.go

122lines · modecode

1package cli
2
3import (
4 "crypto/x509"
5 "database/sql"
6 "fmt"
7 "io/ioutil"
8 "os"
9
10 "github.com/cloudflare/cfssl/helpers"
11 "github.com/cloudflare/cfssl_trust/model/certdb"
12 _ "github.com/mattn/go-sqlite3" // load sql driver
13 "github.com/spf13/cobra"
14 "github.com/spf13/viper"
15)
16
17var importCmd = &cobra.Command{
18 Use: "import",
19 Short: "Import certificates into the database.",
20 Long: "Import certificates into the database, marking them under a release as needed.",
21 Run: importer,
22}
23
24func init() {
25 rootCmd.AddCommand(importCmd)
26}
27
28func importCertificate(tx *sql.Tx, cert *x509.Certificate, rel *certdb.Release) error {
29 fmt.Printf("- importing serial %s SKI %x\n", cert.SerialNumber, cert.SubjectKeyId)
30 c := certdb.NewCertificate(cert)
31 _, err := certdb.Ensure(c, tx)
32 if err != nil {
33 return err
34 }
35
36 aia := certdb.NewAIA(c)
37 if aia != nil {
38 _, err = certdb.Ensure(aia, tx)
39 if err != nil {
40 return err
41 }
42 }
43
44 // The rest of the function deals with inserting the
45 // certificate into the relevant release table. The assumption
46 // here is that the release exists in the DB.
47 if rel == nil {
48 return nil
49 }
50
51 cr := certdb.NewCertificateRelease(c, rel)
52 _, err = certdb.Ensure(cr, tx)
53 return err
54
55}
56
57func importer(cmd *cobra.Command, args []string) {
58 dbPath := viper.GetString("database.path")
59 db, err := sql.Open("sqlite3", dbPath)
60 if err != nil {
61 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
62 os.Exit(1)
63 }
64
65 tx, err := db.Begin()
66 if err != nil {
67 if err != nil {
68 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
69 os.Exit(1)
70 }
71 }
72 defer func() {
73 if err == nil {
74 err = tx.Commit()
75 if err != nil {
76 fmt.Fprintf(os.Stderr, "[!] failed to commit transaction: %s\n", err)
77 os.Exit(1)
78 }
79 } else {
80 tx.Rollback()
81 }
82 }()
83
84 var rel *certdb.Release
85 if bundleRelease != "" {
86 rel, err = certdb.NewRelease(bundle, bundleRelease)
87 if err != nil {
88 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
89 os.Exit(1)
90 }
91
92 _, err = certdb.Ensure(rel, tx)
93 if err != nil {
94 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
95 os.Exit(1)
96 }
97 }
98
99 for _, path := range args {
100 fileContents, err := ioutil.ReadFile(path)
101 if err != nil {
102 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
103 os.Exit(1)
104 }
105
106 certs, err := helpers.ParseCertificatesPEM(fileContents)
107 if err != nil {
108 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
109 os.Exit(1)
110 }
111
112 for _, x509Cert := range certs {
113 err := importCertificate(tx, x509Cert, rel)
114 if err != nil {
115 fmt.Fprintf(os.Stderr, "[!] %s\n", err)
116 os.Exit(1)
117 }
118 }
119 }
120
121 db.Close()
122}
123