-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04761d7
commit c2392c4
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package certs | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/chia-network/go-chia-libs/pkg/tls" | ||
"github.com/chia-network/go-modules/pkg/slogs" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// generateCACmd represents the generate CA command | ||
var generateCACmd = &cobra.Command{ | ||
Use: "generate-ca", | ||
Short: "Generates a new random CA", | ||
Example: "chia-tools certs generate-ca", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// Get the public CA cert and key byte slices | ||
publicCACrtBytes, publicCAKeyBytes := tls.GetChiaCACertAndKey() | ||
|
||
// Generate a private CA cert and key | ||
privateCACrt, privateCAKey, err := tls.GenerateNewCA() | ||
if err != nil { | ||
slogs.Logr.Fatal("encountered error generating new private CA cert and key", "error", err) | ||
} | ||
|
||
// Encode the private CA cert and key to PEM byte slices | ||
privateCACrtBytes, privateCAKeyBytes, err := tls.EncodeCertAndKeyToPEM(privateCACrt, privateCAKey) | ||
if err != nil { | ||
slogs.Logr.Fatal("encountered error encoding private CA cert and key to PEM", "error", err) | ||
} | ||
|
||
toMarshal := map[string]string{ | ||
"chia_ca.crt": string(publicCACrtBytes), | ||
"chia_ca.key": string(publicCAKeyBytes), | ||
"private_ca.crt": string(privateCACrtBytes), | ||
"private_ca.key": string(privateCAKeyBytes), | ||
} | ||
|
||
var marshalled []byte | ||
if viper.GetBool("ca-gen-as-json") { | ||
marshalled, err = json.Marshal(toMarshal) | ||
} else { | ||
marshalled, err = yaml.Marshal(toMarshal) | ||
} | ||
|
||
if err != nil { | ||
slogs.Logr.Fatal("error marshalling", "error", err) | ||
} | ||
fmt.Print(string(marshalled)) | ||
}, | ||
} | ||
|
||
func init() { | ||
generateCACmd.PersistentFlags().Bool("as-json", false, "Output as JSON blob instead of yaml") | ||
cobra.CheckErr(viper.BindPFlag("ca-gen-as-json", generateCACmd.PersistentFlags().Lookup("as-json"))) | ||
|
||
certsCmd.AddCommand(generateCACmd) | ||
} |