Skip to content

Commit

Permalink
feat: adds optional intermediate flag(s) and makes error/validation m…
Browse files Browse the repository at this point in the history
…ore consistent w/ tsa cert-utility.

Signed-off-by: ianhundere <[email protected]>
  • Loading branch information
ianhundere committed Nov 30, 2024
1 parent 3bc2887 commit e44ea4e
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 117 deletions.
48 changes: 27 additions & 21 deletions cmd/certificate_maker/certificate_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/sigstore/fulcio/pkg/certmaker"
"github.com/spf13/cobra"
Expand All @@ -36,8 +37,8 @@ var (

rootCmd = &cobra.Command{
Use: "fulcio-certificate-maker",
Short: "Create certificate chains for Fulcio CA",
Long: `A tool for creating root and leaf certificates for Fulcio CA with code signing capabilities`,
Short: "Create certificate chains for Fulcio",
Long: `A tool for creating root, intermediate, and leaf certificates for Fulcio with code signing capabilities`,
Version: version,
}

Expand All @@ -47,25 +48,28 @@ var (
RunE: runCreate,
}

kmsType string
kmsRegion string
kmsKeyID string
kmsVaultName string
kmsTenantID string
kmsCredsFile string
rootTemplatePath string
leafTemplatePath string
rootKeyID string
leafKeyID string
rootCertPath string
leafCertPath string
kmsType string
kmsRegion string
kmsKeyID string
kmsTenantID string
kmsCredsFile string
rootTemplatePath string
leafTemplatePath string
rootKeyID string
leafKeyID string
rootCertPath string
leafCertPath string
withIntermediate bool
intermediateKeyID string
intermediateTemplate string
intermediateCert string

rawJSON = []byte(`{
"level": "debug",
"encoding": "json",
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"initialFields": {"service": "sigstore-certificate-maker"},
"initialFields": {"service": "fulcio-certificate-maker"},
"encoderConfig": {
"messageKey": "message",
"levelKey": "level",
Expand All @@ -84,7 +88,6 @@ func init() {
createCmd.Flags().StringVar(&kmsType, "kms-type", "", "KMS provider type (awskms, cloudkms, azurekms)")
createCmd.Flags().StringVar(&kmsRegion, "kms-region", "", "KMS region")
createCmd.Flags().StringVar(&kmsKeyID, "kms-key-id", "", "KMS key identifier")
createCmd.Flags().StringVar(&kmsVaultName, "kms-vault-name", "", "Azure KMS vault name")
createCmd.Flags().StringVar(&kmsTenantID, "kms-tenant-id", "", "Azure KMS tenant ID")
createCmd.Flags().StringVar(&kmsCredsFile, "kms-credentials-file", "", "Path to credentials file (for Google Cloud KMS)")
createCmd.Flags().StringVar(&rootTemplatePath, "root-template", "pkg/certmaker/templates/root-template.json", "Path to root certificate template")
Expand All @@ -93,9 +96,16 @@ func init() {
createCmd.Flags().StringVar(&leafKeyID, "leaf-key-id", "", "KMS key identifier for leaf certificate")
createCmd.Flags().StringVar(&rootCertPath, "root-cert", "root.pem", "Output path for root certificate")
createCmd.Flags().StringVar(&leafCertPath, "leaf-cert", "leaf.pem", "Output path for leaf certificate")
createCmd.Flags().BoolVar(&withIntermediate, "with-intermediate", false, "Create certificate chain with intermediate CA")
createCmd.Flags().StringVar(&intermediateKeyID, "intermediate-key-id", "", "KMS key identifier for intermediate certificate")
createCmd.Flags().StringVar(&intermediateTemplate, "intermediate-template", "pkg/certmaker/templates/intermediate-template.json", "Path to intermediate certificate template")
createCmd.Flags().StringVar(&intermediateCert, "intermediate-cert", "intermediate.pem", "Output path for intermediate certificate")
}

func runCreate(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// Build KMS config from flags and environment
config := certmaker.KMSConfig{
Type: getConfigValue(kmsType, "KMS_TYPE"),
Expand All @@ -112,15 +122,11 @@ func runCreate(cmd *cobra.Command, args []string) error {
config.Options["credentials-file"] = credsFile
}
case "azurekms":
if vaultName := getConfigValue(kmsVaultName, "KMS_VAULT_NAME"); vaultName != "" {
config.Options["vault-name"] = vaultName
}
if tenantID := getConfigValue(kmsTenantID, "KMS_TENANT_ID"); tenantID != "" {
config.Options["tenant-id"] = tenantID
}
}

ctx := context.Background()
km, err := certmaker.InitKMS(ctx, config)
if err != nil {
return fmt.Errorf("failed to initialize KMS: %w", err)
Expand All @@ -134,7 +140,7 @@ func runCreate(cmd *cobra.Command, args []string) error {
return fmt.Errorf("leaf template error: %w", err)
}

return certmaker.CreateCertificates(km, config, rootTemplatePath, leafTemplatePath, rootCertPath, leafCertPath)
return certmaker.CreateCertificates(km, config, rootTemplatePath, leafTemplatePath, rootCertPath, leafCertPath, withIntermediate, intermediateKeyID, intermediateTemplate, intermediateCert)
}

func main() {
Expand Down
Loading

0 comments on commit e44ea4e

Please sign in to comment.