Repository navigation
Expand file tree
/
Copy pathcsr.go
More file actions
161 lines (136 loc) Β· 4.46 KB
/
Copy pathcsr.go
File metadata and controls
161 lines (136 loc) Β· 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"certwiz/pkg/cert"
"certwiz/pkg/ui"
"github.com/spf13/cobra"
)
var (
csrCN string
csrOrg string
csrOrgUnit string
csrCountry string
csrState string
csrLocality string
csrEmail string
csrSANs []string
csrKeySize int
csrOutput string
)
var csrCmd = &cobra.Command{
Use: "csr",
Short: "Generate a Certificate Signing Request (CSR)",
Long: `Generate a Certificate Signing Request (CSR) and private key.
A CSR is used to request a certificate from a Certificate Authority (CA).
It contains your public key and identity information.
Examples:
# Basic CSR generation
cert csr --cn example.com
# CSR with organization details
cert csr --cn example.com --org "Example Inc" --country US --state CA
# CSR with Subject Alternative Names
cert csr --cn example.com --san example.com --san www.example.com --san api.example.com
# CSR with custom output directory and key size
cert csr --cn secure.example.com --key-size 4096 --output /etc/ssl/`,
RunE: func(cmd *cobra.Command, args []string) error {
if csrCN == "" {
err := fmt.Errorf("common name (--cn) is required")
reportError(cmd, err)
return err
}
// Prepare options
options := cert.CSROptions{
CommonName: csrCN,
Organization: csrOrg,
OrganizationalUnit: csrOrgUnit,
Country: csrCountry,
Province: csrState,
Locality: csrLocality,
EmailAddress: csrEmail,
SANs: csrSANs,
KeySize: csrKeySize,
}
// Generate CSR
if !jsonOutput {
fmt.Printf("%s Generating Certificate Signing Request...\n", ui.Emoji("π", "[CSR]"))
}
csrPath := filepath.Join(csrOutput, sanitizeFilename(csrCN)+".csr")
keyPath := filepath.Join(csrOutput, sanitizeFilename(csrCN)+".key")
err := cert.GenerateCSR(options, csrPath, keyPath)
if err != nil {
err = fmt.Errorf("failed to generate CSR: %w", err)
reportError(cmd, err)
return err
}
if jsonOutput {
printJSON(cert.JSONOperationResult{
Success: true,
Message: "Certificate Signing Request generated successfully",
Files: []string{csrPath, keyPath},
})
return nil
}
// Display success message
ui.ShowSuccess("Certificate Signing Request generated successfully!")
fmt.Println()
fmt.Printf("%s Files created:\n", ui.Emoji("π", "[FILES]"))
fmt.Printf(" %s CSR: %s\n", ui.Emoji("π", "[CSR]"), csrPath)
fmt.Printf(" %s Private Key: %s\n", ui.Emoji("π", "[KEY]"), keyPath)
fmt.Println()
fmt.Printf("%s Next steps:\n", ui.Emoji("π", "[NEXT]"))
fmt.Println(" 1. Submit the CSR to your Certificate Authority")
fmt.Println(" 2. Keep the private key secure - you'll need it with the signed certificate")
fmt.Println(" 3. Once you receive the signed certificate, install it with the private key")
// Optionally display the CSR details
fmt.Println()
fmt.Printf("%s CSR Details:\n", ui.Emoji("π", "[INFO]"))
if err := displayCSRInfo(csrPath); err != nil {
ui.ShowInfo(fmt.Sprintf("Could not display CSR details: %v", err))
}
return nil
},
}
func init() {
csrCmd.Flags().StringVar(&csrCN, "cn", "", "Common Name (required)")
csrCmd.Flags().StringVar(&csrOrg, "org", "", "Organization")
csrCmd.Flags().StringVar(&csrOrgUnit, "org-unit", "", "Organizational Unit")
csrCmd.Flags().StringVar(&csrCountry, "country", "", "Country (2-letter code)")
csrCmd.Flags().StringVar(&csrState, "state", "", "State or Province")
csrCmd.Flags().StringVar(&csrLocality, "locality", "", "Locality or City")
csrCmd.Flags().StringVar(&csrEmail, "email", "", "Email Address")
csrCmd.Flags().StringSliceVar(&csrSANs, "san", []string{}, "Subject Alternative Name: DNS name, IP:<address>, email:<address>, or uri:<uri> (repeatable)")
csrCmd.Flags().IntVarP(&csrKeySize, "key-size", "k", 2048, "RSA key size in bits")
csrCmd.Flags().StringVarP(&csrOutput, "output", "o", ".", "Output directory for CSR and key files")
rootCmd.AddCommand(csrCmd)
}
func displayCSRInfo(csrPath string) error {
data, err := os.ReadFile(csrPath)
if err != nil {
return err
}
// Parse and display CSR info
info, err := cert.ParseCSR(data)
if err != nil {
return err
}
ui.DisplayCSRInfo(info)
return nil
}
var filenameSanitizer = strings.NewReplacer(
"/", "_",
"\\", "_",
":", "_",
"*", "_",
"?", "_",
"\"", "_",
"<", "_",
">", "_",
"|", "_",
" ", "_",
)
func sanitizeFilename(name string) string {
return filenameSanitizer.Replace(name)
}