-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.fish
102 lines (90 loc) · 3.46 KB
/
gen.fish
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
begin
function is_valid_integer -a value
return (string match -qr '^\d+$' -- "$value"; and test "$value" -gt 0)
end
function ssh_key -a filename comment
if test -z "$filename"
set filename 'key'
end
if test -z "$comment"
set comment (whoami)@(hostname)
end
ssh-keygen -o -a 100 -t ed25519 -C "$comment" -f "$filename"
end
function x509_cert -a cname days
if test -z "$cname"
set cname 'localhost'
end
if test -z "$days"
set days 365
else if not is_valid_integer "$bits"
echo -s \
(set_color $fish_color_error) \
"error: days value must be a positive integer and greater than zero" \
(set_color normal)
return 1
end
openssl req \
-x509 \
-newkey rsa:4096 \
-sha256 \
-nodes \
-days $days \
-subj "/CN=$cname" \
-keyout key.pem \
-out cert.pem
end
function rand_base64 -a bits
if test -z "$bits"
set bits 32
else if not is_valid_integer "$bits"
echo -s \
(set_color $fish_color_error) \
"error: bit-length must be a positive integer and greater than zero" \
(set_color normal)
return 1
end
openssl rand -base64 $bits
end
function gen -d 'Generates either an X.509 cert, SSH key, or random base64 string'
set -l options
set options $options (fish_opt -s B -l bits --optional-val)
set options $options (fish_opt -s N -l cn --optional-val)
set options $options (fish_opt -s D -l days --optional-val)
set options $options (fish_opt -s f -l filename --optional-val)
set options $options (fish_opt -s C -l comment --optional-val)
argparse --ignore-unknown $options -- $argv
set params (string match --invert -- '-*' $argv)
switch "$params[1]"
case 'x509' 'cert'
x509_cert $_flag_cn $_flag_days
case 'ssh' 'key'
ssh_key $_flag_filename $_flag_comment
case 'base64' 'b64'
rand_base64 $_flag_bits
case '*'
echo "Generate either an X.509 cert, SSH key, or random base64 string."
echo ''
echo "Usage: $_ ENTITY [OPTS...]"
echo ''
echo 'Entities:'
echo ' x509/cert Generate an X.509 certificate'
echo ' ssh/key Generate an SSH key'
echo ' base64/b64 Generate a random base64 string'
echo ''
echo 'X.509 Options:'
echo ' --cn=<NAME> Certificate common name [default: "localhost"]'
echo ' --days=<NUM> Certificate validity period [default: 365]'
echo ''
echo 'SSH Options:'
echo ' --filename=<NAME> SSH key filename [default: "key"]'
echo " --comment=<TEXT> SSH key comment [default: \"$(whoami)@$(hostname)\"]"
echo ''
echo 'Base64 Options:'
echo ' --bits=<NUM> Base64 binary bit-length [default: 32]'
echo ''
echo 'Parameters:'
echo ' ENTITY An entity name [required]'
end
end
end