feat: Command that generates a private key with well known params.
This commit is contained in:
1 parent
56fc799cf6
commit
6eb97bb780
7 files changed
+143
No files matched your search
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "git.handmadecities.com/HMC/shhh/internal/clis/hmpki"
|
||||
|
||||
func main() {
|
||||
hmpki.Execute()
|
||||
}
|
||||
@@ -1,3 +1,10 @@
|
||||
module git.handmadecities.com/HMC/shhh
|
||||
|
||||
go 1.27.2
|
||||
|
||||
require github.com/spf13/cobra v1.10.2
|
||||
|
||||
require (
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
||||
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
|
||||
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
@@ -0,0 +1,28 @@
|
||||
package hmpki
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Output string
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(generateCmd)
|
||||
|
||||
// All "generate" commands output an artifact to a file,
|
||||
// be it a key or a cert, etc.
|
||||
generateCmd.PersistentFlags().StringVarP(
|
||||
&Output,
|
||||
"output",
|
||||
"o",
|
||||
"",
|
||||
"output filename",
|
||||
)
|
||||
generateCmd.MarkPersistentFlagRequired("output")
|
||||
generateCmd.MarkPersistentFlagFilename("output")
|
||||
}
|
||||
|
||||
var generateCmd = &cobra.Command{
|
||||
Use: "generate",
|
||||
Short: "Generate pki resources (keys/certs)",
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package hmpki
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"os"
|
||||
|
||||
"git.handmadecities.com/HMC/shhh/pkg/pki"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
generateCmd.AddCommand(generateKeyCmd)
|
||||
}
|
||||
|
||||
var generateKeyCmd = &cobra.Command{
|
||||
Use: "key",
|
||||
Short: "Generate a private key and save it to a file",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
der, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pemBytes := pem.EncodeToMemory(&pem.Block{
|
||||
Type: "PRIVATE KEY",
|
||||
Bytes: der,
|
||||
})
|
||||
|
||||
kf, err := os.OpenFile(
|
||||
Output,
|
||||
// Write only, and fail if it alreay exists.
|
||||
os.O_WRONLY|os.O_CREATE|os.O_EXCL,
|
||||
pki.PrivateKeyFilePerms,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = kf.Write(pemBytes); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = kf.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package hmpki
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Get a root cobra command going, then code for generating:
|
||||
// - Generate CA key
|
||||
// - Generate CA cert
|
||||
// - Generate server cert/key
|
||||
// - Generate client cert/key
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "hmpki",
|
||||
Short: "Generate keys/certs with set roles for mTLS auth between services",
|
||||
// Run: func(cmd *cobra.Command, args []string) {
|
||||
// fmt.Println("I'm a cobra command!")
|
||||
// },
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package pki
|
||||
|
||||
import "os"
|
||||
|
||||
const PrivateKeyFilePerms os.FileMode = 0600
|
||||
Reference in new issue
Block a user