cloudflare/cfssl_trust

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
72e5ca0d26bbcab6b255f5495e1c2e255f0300a5

Branches

Tags

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

Clone

HTTPS

Download ZIP

cli/root.go

116lines · modecode

1package cli
2
3import (
4 "database/sql"
5 "fmt"
6 "os"
7 "path/filepath"
8
9 "github.com/cloudflare/cfssl/log"
10 "github.com/cloudflare/cfssl_trust/config"
11 "github.com/cloudflare/cfssl_trust/release"
12 "github.com/spf13/cobra"
13 "github.com/spf13/viper"
14)
15
16var (
17 cfgFile string
18 dbFile string
19 bundle string
20 bundleRelease string
21)
22
23func root(cmd *cobra.Command, args []string) {
24}
25
26var configLocations = []string{
27 "/etc/cfssl",
28 "/usr/local/cfssl",
29 filepath.Join(config.GoPath(), "src", "github.com", "cloudflare", "cfssl_trust"),
30}
31
32var RootCmd = &cobra.Command{
33 Use: "cfssl-trust",
34 Short: "Manage a trust database for root and intermediate bundles.",
35 Long: ``,
36 Run: root,
37}
38
39func Execute() {
40 if err := RootCmd.Execute(); err != nil {
41 fmt.Println(err)
42 os.Exit(-1)
43 }
44}
45
46// If err isn't nil, this should rollback the transaction. If err is
47// nil, it should commit the transaction. Finally, it should close the
48// database.
49func cleanup(tx *sql.Tx, db *sql.DB, err error) {
50 if tx != nil {
51 if err != nil {
52 err = tx.Rollback()
53 if err != nil {
54 fmt.Fprintf(os.Stderr, "[!] error while rolling back transaction: %s\n", err)
55 os.Exit(1)
56 }
57 } else {
58 err = tx.Commit()
59 if err != nil {
60 fmt.Fprintf(os.Stderr, "[!] error while committing transaction: %s\n", err)
61 os.Exit(1)
62 }
63 }
64 }
65
66 if db != nil {
67 err = db.Close()
68 if err != nil {
69 fmt.Fprintf(os.Stderr, "[!] error while closing database: %s\n", err)
70 os.Exit(1)
71 }
72 }
73}
74
75func init() {
76 cobra.OnInitialize(initConfig)
77
78 RootCmd.PersistentFlags().StringVarP(&bundle, "bundle", "b", "int", "select a bundle (ca or int)")
79 RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "f", "", "config file (default is /etc/cfssl/cfssl-trust.yaml)")
80 RootCmd.PersistentFlags().StringVarP(&dbFile, "db", "d", "", "path to trust database")
81 RootCmd.PersistentFlags().StringVarP(&bundleRelease, "release", "r", "", "select a release")
82
83 viper.BindPFlag("database.path", RootCmd.PersistentFlags().Lookup("db"))
84}
85
86// initConfig reads in config file and ENV variables if set.
87func initConfig() {
88 if cfgFile != "" { // enable ability to specify config file via flag
89 viper.SetConfigFile(cfgFile)
90 } else {
91 viper.SetConfigName("cfssl-trust") // name of config file (without extension)
92 for _, dir := range configLocations {
93 viper.AddConfigPath(dir)
94 }
95 viper.AddConfigPath(".")
96 }
97
98 viper.SetEnvPrefix("CFSSL_TRUST")
99 viper.AutomaticEnv() // read in environment variables that match
100
101 // If a config file is found, read it in.
102 err := viper.ReadInConfig()
103 if err == nil {
104 log.Info("cfssl-trust: loading from config file ", viper.ConfigFileUsed())
105 }
106
107 if bundleRelease != "" {
108 rel, err := release.Parse(bundleRelease)
109 if err != nil {
110 fmt.Fprintf(os.Stderr, "[!] Invalid release '%s'.\n", bundleRelease)
111 fmt.Fprintf(os.Stderr, "\tReason: %s\n", err)
112 os.Exit(1)
113 }
114 fmt.Println("selected release", rel)
115 }
116}
117