Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ca generation command #24

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions cmd/certs/generateca.go
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)
}