This commit is contained in:
1 parent
4cce5754d3
commit
0f777d3e68
8 files changed
+333
-213
No files matched your search
@@ -0,0 +1,47 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"scm/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CertKeyDownloadZip(w http.ResponseWriter, r *http.Request) {
|
||||||
|
query := r.URL.Query()
|
||||||
|
domain := query.Get("domain")
|
||||||
|
if domain == "" {
|
||||||
|
http.Error(w, "domain parameter is required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Пути к файлам сертификата и ключа
|
||||||
|
certPath := filepath.Join(os.Getenv("CERTS_PATH"), domain, domain+".crt")
|
||||||
|
keyPath := filepath.Join(os.Getenv("CERTS_PATH"), domain, domain+".key")
|
||||||
|
|
||||||
|
// Проверяем существование файлов
|
||||||
|
if _, err := os.Stat(certPath); os.IsNotExist(err) {
|
||||||
|
http.Error(w, "Certificate file not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
|
||||||
|
http.Error(w, "Key file not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Создаем ZIP архив
|
||||||
|
zipBuffer, err := utils.CreateDomainZip(certPath, keyPath, domain)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Error creating zip: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Устанавливаем заголовки для скачивания
|
||||||
|
w.Header().Set("Content-Type", "application/zip")
|
||||||
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s-cert-key.zip\"", domain))
|
||||||
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", zipBuffer.Len()))
|
||||||
|
|
||||||
|
// Отправляем ZIP архив
|
||||||
|
w.Write(zipBuffer.Bytes())
|
||||||
|
}
|
||||||
+193
-174
@@ -62,13 +62,15 @@
|
|||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<button
|
<a
|
||||||
type="button"
|
type="button"
|
||||||
class="btn btn-primary"
|
class="btn btn-primary"
|
||||||
id="downloadrootcert"
|
id="downloadrootcert"
|
||||||
|
href="/api/certs/download/?domain=root"
|
||||||
|
download="root.zip"
|
||||||
>
|
>
|
||||||
Download Root Certificate
|
Download Root Certificate
|
||||||
</button>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
@@ -91,71 +93,78 @@
|
|||||||
></button>
|
></button>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<div class="form-floating mb-3">
|
<form id="create_certificate_form">
|
||||||
<input
|
<div class="form-floating mb-3">
|
||||||
type="text"
|
<input
|
||||||
id="domain"
|
type="text"
|
||||||
class="form-control"
|
id="domain"
|
||||||
placeholder="example.domain"
|
class="form-control"
|
||||||
/>
|
placeholder="example.domain"
|
||||||
<label for="domain">Domain</label>
|
required
|
||||||
</div>
|
/>
|
||||||
<div class="form-floating mb-3">
|
<label for="domain">Domain</label>
|
||||||
<input
|
</div>
|
||||||
type="text"
|
<div class="form-floating mb-3">
|
||||||
id="commonname"
|
<input
|
||||||
class="form-control"
|
type="text"
|
||||||
placeholder="commonname"
|
id="commonname"
|
||||||
/>
|
class="form-control"
|
||||||
<label for="commonname">Common Name</label>
|
placeholder="commonname"
|
||||||
</div>
|
required
|
||||||
<div class="form-floating mb-3">
|
/>
|
||||||
<input
|
<label for="commonname">Common Name</label>
|
||||||
type="text"
|
</div>
|
||||||
id="organizationname"
|
<div class="form-floating mb-3">
|
||||||
class="form-control"
|
<input
|
||||||
placeholder="organizationname"
|
type="text"
|
||||||
/>
|
id="organizationname"
|
||||||
<label for="organizationname"
|
class="form-control"
|
||||||
>Organization Name</label
|
placeholder="organizationname"
|
||||||
>
|
required
|
||||||
</div>
|
/>
|
||||||
<div class="form-floating mb-3">
|
<label for="organizationname"
|
||||||
<input
|
>Organization Name</label
|
||||||
type="text"
|
>
|
||||||
id="organizationunit"
|
</div>
|
||||||
class="form-control"
|
<div class="form-floating mb-3">
|
||||||
placeholder="organizationunit"
|
<input
|
||||||
/>
|
type="text"
|
||||||
<label for="organizationunit"
|
id="organizationunit"
|
||||||
>Organization Unit</label
|
class="form-control"
|
||||||
>
|
placeholder="organizationunit"
|
||||||
</div>
|
required
|
||||||
<div class="form-floating mb-3">
|
/>
|
||||||
<input
|
<label for="organizationunit"
|
||||||
type="password"
|
>Organization Unit</label
|
||||||
id="password"
|
>
|
||||||
class="form-control"
|
</div>
|
||||||
placeholder="example.domain"
|
<div class="form-floating mb-3">
|
||||||
/>
|
<input
|
||||||
<label for="password">RootCA password</label>
|
type="password"
|
||||||
</div>
|
id="password"
|
||||||
</div>
|
class="form-control"
|
||||||
<div class="modal-footer">
|
placeholder="example.domain"
|
||||||
<button
|
required
|
||||||
type="button"
|
/>
|
||||||
data-bs-dismiss="modal"
|
<label for="password">RootCA password</label>
|
||||||
class="btn btn-secondary"
|
</div>
|
||||||
>
|
<div class="modal-footer">
|
||||||
Close
|
<button
|
||||||
</button>
|
type="button"
|
||||||
<button
|
data-bs-dismiss="modal"
|
||||||
id="createcert"
|
class="btn btn-secondary"
|
||||||
type="button"
|
>
|
||||||
class="btn btn-primary"
|
Close
|
||||||
>
|
</button>
|
||||||
Create
|
<button
|
||||||
</button>
|
id="createcert"
|
||||||
|
type="submit"
|
||||||
|
class="btn btn-primary"
|
||||||
|
>
|
||||||
|
Create
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -315,114 +324,124 @@
|
|||||||
</h5>
|
</h5>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<div class="form-floating mb-3">
|
<form id="createRootCertificateForm">
|
||||||
<input
|
<div class="form-floating mb-3">
|
||||||
type="text"
|
<input
|
||||||
id="rootcommonname"
|
type="text"
|
||||||
class="form-control"
|
id="rootcommonname"
|
||||||
placeholder="example.domain"
|
class="form-control"
|
||||||
/>
|
placeholder="example.domain"
|
||||||
<label for="rootcommonname">Common Name</label>
|
required
|
||||||
</div>
|
/>
|
||||||
<div class="form-floating mb-3">
|
<label for="rootcommonname">Common Name</label>
|
||||||
<input
|
</div>
|
||||||
type="text"
|
<div class="form-floating mb-3">
|
||||||
id="rootorganizationname"
|
<input
|
||||||
class="form-control"
|
type="text"
|
||||||
placeholder="organizationname"
|
id="rootorganizationname"
|
||||||
/>
|
class="form-control"
|
||||||
<label for="rootorganizationname"
|
placeholder="organizationname"
|
||||||
>Organization Name</label
|
required
|
||||||
|
/>
|
||||||
|
<label for="rootorganizationname"
|
||||||
|
>Organization Name</label
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="rootorganizationalunit"
|
||||||
|
class="form-control"
|
||||||
|
placeholder="organizationalunit"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<label for="rootorganizationalunit"
|
||||||
|
>Organizational Unit</label
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="rootCountry"
|
||||||
|
class="form-control"
|
||||||
|
placeholder="Country"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<label for="rootCountry">Country</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="rootLocality"
|
||||||
|
class="form-control"
|
||||||
|
placeholder="Asia/Novosibirsk"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<label for="rootLocality">Locality</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="rootProvince"
|
||||||
|
class="form-control"
|
||||||
|
placeholder="Tomsk Oblast"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<label for="rootProvince">Province</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="rootStreetAddress"
|
||||||
|
class="form-control"
|
||||||
|
placeholder="Tomsk Oblast, Russia"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<label for="rootStreetAddress"
|
||||||
|
>Street address</label
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="rootPostalCode"
|
||||||
|
class="form-control"
|
||||||
|
placeholder="646037"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<label for="rootPostalCode">Postal code</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
id="rootValidityYears"
|
||||||
|
class="form-control"
|
||||||
|
placeholder="10"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<label for="rootValidityYears"
|
||||||
|
>Validity years</label
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
id="rootkeypassword"
|
||||||
|
class="form-control"
|
||||||
|
placeholder="Password"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<label for="rootkeypassword">Password</label>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
id="createRootCrt"
|
||||||
|
type="submit"
|
||||||
|
class="btn btn-primary"
|
||||||
|
style="width: 100%"
|
||||||
>
|
>
|
||||||
</div>
|
Create
|
||||||
<div class="form-floating mb-3">
|
</button>
|
||||||
<input
|
</form>
|
||||||
type="text"
|
|
||||||
id="rootorganizationalunit"
|
|
||||||
class="form-control"
|
|
||||||
placeholder="organizationalunit"
|
|
||||||
/>
|
|
||||||
<label for="rootorganizationalunit"
|
|
||||||
>Organizational Unit</label
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
<div class="form-floating mb-3">
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
id="rootCountry"
|
|
||||||
class="form-control"
|
|
||||||
placeholder="Country"
|
|
||||||
/>
|
|
||||||
<label for="rootCountry">Country</label>
|
|
||||||
</div>
|
|
||||||
<div class="form-floating mb-3">
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
id="rootLocality"
|
|
||||||
class="form-control"
|
|
||||||
placeholder="Asia/Novosibirsk"
|
|
||||||
/>
|
|
||||||
<label for="rootLocality">Locality</label>
|
|
||||||
</div>
|
|
||||||
<div class="form-floating mb-3">
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
id="rootProvince"
|
|
||||||
class="form-control"
|
|
||||||
placeholder="Tomsk Oblast"
|
|
||||||
/>
|
|
||||||
<label for="rootProvince">Province</label>
|
|
||||||
</div>
|
|
||||||
<div class="form-floating mb-3">
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
id="rootStreetAddress"
|
|
||||||
class="form-control"
|
|
||||||
placeholder="Tomsk Oblast, Russia"
|
|
||||||
/>
|
|
||||||
<label for="rootStreetAddress"
|
|
||||||
>Street address</label
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
<div class="form-floating mb-3">
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
id="rootPostalCode"
|
|
||||||
class="form-control"
|
|
||||||
placeholder="646037"
|
|
||||||
/>
|
|
||||||
<label for="rootPostalCode">Postal code</label>
|
|
||||||
</div>
|
|
||||||
<div class="form-floating mb-3">
|
|
||||||
<input
|
|
||||||
type="number"
|
|
||||||
id="rootValidityYears"
|
|
||||||
class="form-control"
|
|
||||||
placeholder="10"
|
|
||||||
/>
|
|
||||||
<label for="rootValidityYears"
|
|
||||||
>Validity years</label
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
<div class="form-floating mb-3">
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
id="rootkeypassword"
|
|
||||||
class="form-control"
|
|
||||||
placeholder="Password"
|
|
||||||
/>
|
|
||||||
<label for="rootkeypassword">Password</label>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="modal-footer">
|
|
||||||
<button
|
|
||||||
id="createRootCrt"
|
|
||||||
type="button"
|
|
||||||
class="btn btn-primary"
|
|
||||||
style="width: 100%"
|
|
||||||
>
|
|
||||||
Create
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -34,9 +34,9 @@ const renderCards = () => {
|
|||||||
>
|
>
|
||||||
Cert
|
Cert
|
||||||
</button>
|
</button>
|
||||||
<button class="btn btn-primary" style="width: 100%">
|
<a class="btn btn-primary" style="width: 100%" href="${window.location.origin + "/api/certs/download/?domain=" + cert.certinfo.DNSNames[0]}" download="${cert.certinfo.DNSNames[0]}.zip">
|
||||||
Download
|
Download
|
||||||
</button>
|
</a>
|
||||||
<button class="btn btn-danger delcert" id="${cert.id}" style="width: 100%">
|
<button class="btn btn-danger delcert" id="${cert.id}" style="width: 100%">
|
||||||
Delete
|
Delete
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -3,42 +3,45 @@ const commonname = document.querySelector("#commonname");
|
|||||||
const organizationname = document.querySelector("#organizationname");
|
const organizationname = document.querySelector("#organizationname");
|
||||||
const password = document.querySelector("#password");
|
const password = document.querySelector("#password");
|
||||||
const organizationunit = document.querySelector("#organizationunit");
|
const organizationunit = document.querySelector("#organizationunit");
|
||||||
const createcert = document.querySelector("#createcert");
|
const create_certificate_form = document.querySelector(
|
||||||
|
"#create_certificate_form",
|
||||||
|
);
|
||||||
|
|
||||||
createcert.addEventListener("click", () => {
|
create_certificate_form.addEventListener("submit", (event) => {
|
||||||
const domainValue = domain.value;
|
event.preventDefault();
|
||||||
const commonnameValue = commonname.value;
|
const domainValue = domain.value;
|
||||||
const organizationnameValue = organizationname.value;
|
const commonnameValue = commonname.value;
|
||||||
const passwordValue = password.value;
|
const organizationnameValue = organizationname.value;
|
||||||
const organizationunitValue = organizationunit.value;
|
const passwordValue = password.value;
|
||||||
|
const organizationunitValue = organizationunit.value;
|
||||||
|
|
||||||
fetch(window.location.origin + "/api/certs/create", {
|
fetch(window.location.origin + "/api/certs/create", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
dns: [domainValue],
|
dns: [domainValue],
|
||||||
commonname: commonnameValue,
|
commonname: commonnameValue,
|
||||||
organizationname: [organizationnameValue],
|
organizationname: [organizationnameValue],
|
||||||
organizationunit: [organizationunitValue],
|
organizationunit: [organizationunitValue],
|
||||||
password: passwordValue,
|
password: passwordValue,
|
||||||
}),
|
}),
|
||||||
|
})
|
||||||
|
.then((data) => data.json())
|
||||||
|
.then((jsondata) => {
|
||||||
|
if (jsondata.status === "success") {
|
||||||
|
getCerts();
|
||||||
|
var createmodal = bootstrap.Modal.getOrCreateInstance(
|
||||||
|
document.getElementById("create_certificate"),
|
||||||
|
);
|
||||||
|
createmodal.hide();
|
||||||
|
alert("Certificate created successfully");
|
||||||
|
} else {
|
||||||
|
alert("Failed to create certificate:", jsondata.error);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.then((data) => data.json())
|
.catch((error) => {
|
||||||
.then((jsondata) => {
|
alert("An error occurred:", error);
|
||||||
if (jsondata.status === "success") {
|
});
|
||||||
getCerts();
|
|
||||||
var createmodal = bootstrap.Modal.getOrCreateInstance(
|
|
||||||
document.getElementById("create_certificate"),
|
|
||||||
);
|
|
||||||
createmodal.hide();
|
|
||||||
alert("Certificate created successfully");
|
|
||||||
} else {
|
|
||||||
alert("Failed to create certificate:", jsondata.error);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
alert("An error occurred:", error);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
const createRootCertificateModal = bootstrap.Modal.getOrCreateInstance(
|
const createRootCertificateModal = bootstrap.Modal.getOrCreateInstance(
|
||||||
document.querySelector("#createRootCertificate"),
|
document.querySelector("#createRootCertificate"),
|
||||||
);
|
);
|
||||||
const createRootCrtButton = document.querySelector("#createRootCrt");
|
const createRootCertificateForm = document.querySelector(
|
||||||
|
"#createRootCertificateForm",
|
||||||
|
);
|
||||||
|
|
||||||
const rootcommonnameinput = document.querySelector("#rootcommonname");
|
const rootcommonnameinput = document.querySelector("#rootcommonname");
|
||||||
const rootorganizationinput = document.querySelector("#rootorganizationname");
|
const rootorganizationinput = document.querySelector("#rootorganizationname");
|
||||||
@@ -29,7 +31,8 @@ const getRootCrt = () => {
|
|||||||
|
|
||||||
getRootCrt();
|
getRootCrt();
|
||||||
|
|
||||||
createRootCrtButton.addEventListener("click", () => {
|
createRootCertificateForm.addEventListener("submit", (event) => {
|
||||||
|
event.preventDefault();
|
||||||
const newRootCertificate = {
|
const newRootCertificate = {
|
||||||
CommonName: rootcommonnameinput.value,
|
CommonName: rootcommonnameinput.value,
|
||||||
Organization: [rootorganizationinput.value],
|
Organization: [rootorganizationinput.value],
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ func Configure() {
|
|||||||
http.Handle("/", api.SpaHandler(http.FS(api.Frontend)))
|
http.Handle("/", api.SpaHandler(http.FS(api.Frontend)))
|
||||||
http.HandleFunc("/api/certs", api.Certs)
|
http.HandleFunc("/api/certs", api.Certs)
|
||||||
http.HandleFunc("/api/certs/root", api.RootCert)
|
http.HandleFunc("/api/certs/root", api.RootCert)
|
||||||
|
http.HandleFunc("/api/certs/download/", api.CertKeyDownloadZip)
|
||||||
http.HandleFunc("/api/certs/create", api.CreateCert)
|
http.HandleFunc("/api/certs/create", api.CreateCert)
|
||||||
http.HandleFunc("/api/certs/delete", api.DeleteCert)
|
http.HandleFunc("/api/certs/delete", api.DeleteCert)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddFileToZipWithName(zipWriter *zip.Writer, filePath, fileName string) error {
|
||||||
|
data, err := os.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := zipWriter.Create(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = f.Write(data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateDomainZip(certPath, keyPath, domain string) (*bytes.Buffer, error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
zipWriter := zip.NewWriter(buf)
|
||||||
|
defer zipWriter.Close()
|
||||||
|
|
||||||
|
// Добавляем сертификат
|
||||||
|
err := AddFileToZipWithName(zipWriter, certPath, domain+".crt")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Добавляем ключ
|
||||||
|
err = AddFileToZipWithName(zipWriter, keyPath, domain+".key")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf, nil
|
||||||
|
}
|
||||||
Reference in new issue
Block a user