Browse Source

create script 01.08.25:19.10

root 1 year ago
parent
commit
812a23e03c
2 changed files with 125 additions and 2 deletions
  1. 2 2
      README.md
  2. 123 0
      src/creatcert.sh

+ 2 - 2
README.md

@@ -1,3 +1,3 @@
1
-# creatcert
2
-
1
+* shell:bash<br>
2
+--
3 3
 Create manual cert SSL for web or Porxmox node PVE

+ 123 - 0
src/creatcert.sh

@@ -0,0 +1,123 @@
1
+#!/usr/bin/env bash
2
+# script create ssl certs
3
+#
4
+
5
+##!! manual values
6
+# Set the TLD domain we want to use
7
+BASE_DOMAIN="mydomain.ru"
8
+
9
+# Days for the cert to live
10
+DAYS=3600
11
+
12
+# A blank passphrase
13
+PASSPHRASE=""
14
+
15
+# Generated configuration file
16
+CONFIG_FILE="config.txt"
17
+
18
+Country="RU";
19
+City="MyCity";
20
+Fname="myfirm";
21
+Demail="admin@mydomain.ru"
22
+
23
+## script values
24
+version="0.0.5";
25
+sname="creatcert";
26
+
27
+# - options
28
+cmd=$1;
29
+opt=$2;
30
+
31
+# script path
32
+path_script=$( cd -- $( dirname -- "${BASH_SOURCE[0]}" ) &> /dev/null && pwd );
33
+
34
+# SSL path
35
+path_ssl="/etc/ssl";
36
+
37
+function createSSL() {
38
+
39
+cat > $CONFIG_FILE <<-EOF
40
+[req]
41
+default_bits = 2048
42
+prompt = no
43
+default_md = sha256
44
+x509_extensions = v3_req
45
+distinguished_name = dn
46
+
47
+[dn]
48
+C = CA
49
+ST = $Country
50
+L = $City
51
+O = $Fname
52
+OU = $Fname
53
+emailAddress = $Demail
54
+CN = $BASE_DOMAIN
55
+
56
+[v3_req]
57
+subjectAltName = @alt_names
58
+
59
+[alt_names]
60
+DNS.1 = *.$BASE_DOMAIN
61
+DNS.2 = $BASE_DOMAIN
62
+EOF
63
+
64
+# The file name can be anything
65
+FILE_NAME="$BASE_DOMAIN"
66
+
67
+# Remove previous keys
68
+echo "Removing existing certs like $FILE_NAME.*"
69
+find -maxdepth 1 -type f -name $FILE_NAME.crt -exec rm -rv "{}" + >/dev/null
70
+find -maxdepth 1 -type f -name $FILE_NAME.key -exec rm -rv "{}" + >/dev/null
71
+find -maxdepth 1 -type f -name $FILE_NAME.info -exec rm -rv "{}" + >/dev/null
72
+
73
+echo "Generating certs for $BASE_DOMAIN"
74
+
75
+# Generate our Private Key, CSR and Certificate
76
+# Use SHA-2 as SHA-1 is unsupported from Jan 1, 2017
77
+
78
+openssl req -new -x509 -newkey rsa:2048 -sha256 -nodes -keyout "$FILE_NAME.key" -days $DAYS -out "$FILE_NAME.crt" -passin pass:$PASSPHRASE -config "$CONFIG_FILE"
79
+
80
+# OPTIONAL - write an info to see the details of the generated crt
81
+openssl x509 -noout -fingerprint -text < "$FILE_NAME.crt" > "$FILE_NAME.info"
82
+
83
+# Protect the key
84
+chmod 400 "$FILE_NAME.key"
85
+
86
+## Registred from ssl
87
+if [ -d $path_ssl ]; then
88
+  cat $FILE_NAME.key > $path_ssl/private/privkey_$FILE_NAME.pem;
89
+  cat $FILE_NAME.crt > $path_ssl/private/fullchain_$FILE_NAME.pem;
90
+  cat $FILE_NAME.crt > $path_ssl/certs/$FILE_NAME.pem;
91
+  cat $FILE_NAME.key >> $path_ssl/certs/$FILE_NAME.pem;
92
+  cd $path_ssl/certs;
93
+  chmod 600 $FILE_NAME.pem
94
+  ln -sf $FILE_NAME.pem `openssl x509 -noout -hash < $FILE_NAME.pem`.0
95
+  cd $path_ssl
96
+fi
97
+if [ "$opt" == "pve" ];then
98
+  pve_name="$HOSTNAME"
99
+  if [ -d /etc/pve/nodes/$pve_name ];then
100
+    cat $path_script/$FILE_NAME.crt >/etc/pve/nodes/$pve_name/pve_ssl.pem
101
+    cat $path_script/$FILE_NAME.key >/etc/pve/nodes/$pve_name/pve_ssl.key
102
+    sudo pveproxy restart
103
+  fi
104
+fi
105
+}
106
+
107
+case "$cmd" in
108
+  ## create cert
109
+  "--create" | "--create" )
110
+  createSSL;
111
+  ;;
112
+
113
+  ## start defaults
114
+  * )
115
+    echo "script - $sname"
116
+    echo "version- $version"
117
+    echo "start create ssl certs: creatcert --create"
118
+    echo "start create ssl cert for poroxmox pve node: --create pve"
119
+    echo " Vanted: write from script manual values for domain!"
120
+    ;;
121
+  esac
122
+
123
+exit