vitess.io/vitess@v0.16.2/go/cmd/vttlstest/vttlstest.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreedto in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"github.com/spf13/cobra"
    21  
    22  	"vitess.io/vitess/go/exit"
    23  	"vitess.io/vitess/go/vt/logutil"
    24  	"vitess.io/vitess/go/vt/tlstest"
    25  )
    26  
    27  var (
    28  	root       = "."
    29  	parent     = "ca"
    30  	serial     = "01"
    31  	commonName string
    32  
    33  	rootCmd = &cobra.Command{
    34  		Use:   "vttlstest",
    35  		Short: "vttlstest is a tool for generating test certificates, keys, and related artifacts for TLS tests.",
    36  		Long:  "vttlstest is a tool for generating test certificates, keys, and related artifacts for TLS tests.",
    37  	}
    38  
    39  	createCACmd = &cobra.Command{
    40  		Use:                   "CreateCA [--root <dir>]",
    41  		DisableFlagsInUseLine: true,
    42  		Example:               "CreateCA --root /tmp",
    43  		Short:                 "Create certificate authority",
    44  		Long:                  "Create certificate authority",
    45  		Args:                  cobra.NoArgs,
    46  		Run:                   runCreateCA,
    47  	}
    48  
    49  	createIntermediateCACmd = &cobra.Command{
    50  		Use:                   "CreateIntermediateCA [--root <dir>] [--parent <name>] [--serial <serial>] [--common-name <CN>] <CA name>",
    51  		DisableFlagsInUseLine: true,
    52  		Example:               "CreateIntermediateCA --root /tmp --parent ca mail.mycoolsite.com",
    53  		Short:                 "Create intermediate certificate authority",
    54  		Long:                  "Create intermediate certificate authority",
    55  		Args:                  cobra.ExactArgs(1),
    56  		Run:                   runCreateIntermediateCA,
    57  	}
    58  
    59  	createCRLCmd = &cobra.Command{
    60  		Use:                   "CreateCRL [--root <dir>] <server>",
    61  		DisableFlagsInUseLine: true,
    62  		Example:               "CreateCRL --root /tmp mail.mycoolsite.com",
    63  		Short:                 "Create certificate revocation list",
    64  		Long:                  "Create certificate revocation list",
    65  		Args:                  cobra.ExactArgs(1),
    66  		Run:                   runCreateCRL,
    67  	}
    68  
    69  	createSignedCertCmd = &cobra.Command{
    70  		Use:                   "CreateSignedCert [--root <dir>] [--parent <name>] [--serial <serial>] [--common-name <CN>] <cert name>",
    71  		DisableFlagsInUseLine: true,
    72  		Example:               "CreateSignedCert --root /tmp --common-name mail.mysite.com --parent mail.mycoolsite.com postman1",
    73  		Short:                 "Create signed certificate",
    74  		Long:                  "Create signed certificate",
    75  		Args:                  cobra.ExactArgs(1),
    76  		Run:                   runCreateSignedCert,
    77  	}
    78  
    79  	revokeCertCmd = &cobra.Command{
    80  		Use:                   "RevokeCert [--root <dir>] [--parent <name>] <cert name>",
    81  		DisableFlagsInUseLine: true,
    82  		Example:               "RevokeCert --root /tmp --parent mail.mycoolsite.com postman1",
    83  		Short:                 "Revoke a certificate",
    84  		Long:                  "Revoke a certificate",
    85  		Args:                  cobra.ExactArgs(1),
    86  		Run:                   runRevokeCert,
    87  	}
    88  )
    89  
    90  func init() {
    91  	rootCmd.PersistentFlags().StringVar(&root, "root", root, "root directory for all artifacts")
    92  
    93  	rootCmd.AddCommand(createCACmd)
    94  	rootCmd.AddCommand(createIntermediateCACmd)
    95  	rootCmd.AddCommand(createCRLCmd)
    96  	rootCmd.AddCommand(createSignedCertCmd)
    97  	rootCmd.AddCommand(revokeCertCmd)
    98  
    99  	for _, cmd := range []*cobra.Command{createIntermediateCACmd, createSignedCertCmd} {
   100  		cmd.Flags().StringVar(&parent, "parent", parent, "Parent cert name to use. Use 'ca' for the toplevel CA.")
   101  		cmd.Flags().StringVar(&serial, "serial", serial, "Serial number for the certificate to create. Should be different for two certificates with the same parent.")
   102  		cmd.Flags().StringVar(&commonName, "common-name", commonName, "Common name for the certificate. If empty, uses the name.")
   103  	}
   104  	revokeCertCmd.Flags().StringVar(&parent, "parent", parent, "Parent cert name to use. Use 'ca' for the toplevel CA.")
   105  }
   106  
   107  func runCreateCA(cmd *cobra.Command, args []string) {
   108  	tlstest.CreateCA(root)
   109  }
   110  
   111  func runCreateIntermediateCA(cmd *cobra.Command, args []string) {
   112  	name := args[0]
   113  	if commonName == "" {
   114  		commonName = name
   115  	}
   116  
   117  	tlstest.CreateIntermediateCA(root, parent, serial, name, commonName)
   118  }
   119  
   120  func runCreateCRL(cmd *cobra.Command, args []string) {
   121  	ca := args[0]
   122  	tlstest.CreateCRL(root, ca)
   123  }
   124  
   125  func runCreateSignedCert(cmd *cobra.Command, args []string) {
   126  	name := args[0]
   127  	if commonName == "" {
   128  		commonName = name
   129  	}
   130  
   131  	tlstest.CreateSignedCert(root, parent, serial, name, commonName)
   132  }
   133  
   134  func runRevokeCert(cmd *cobra.Command, args []string) {
   135  	name := args[0]
   136  	tlstest.RevokeCertAndRegenerateCRL(root, parent, name)
   137  }
   138  
   139  func main() {
   140  	defer exit.Recover()
   141  	defer logutil.Flush()
   142  
   143  	cobra.CheckErr(rootCmd.Execute())
   144  }