Skip to content

Adds feature to Edit Custom certificates. #4425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion backend/internal/certificate.js
Original file line number Diff line number Diff line change
@@ -272,7 +272,9 @@ const internalCertificate = {
.then(utils.omitRow(omissions()))
.then((saved_row) => {
saved_row.meta = internalCertificate.cleanMeta(saved_row.meta);
data.meta = internalCertificate.cleanMeta(data.meta);
if (data.meta) {
data.meta = internalCertificate.cleanMeta(data.meta);
}

// Add row.nice_name for custom certs
if (saved_row.provider === 'other') {
18 changes: 18 additions & 0 deletions backend/routes/nginx/certificates.js
Original file line number Diff line number Diff line change
@@ -147,6 +147,24 @@ router
.catch(next);
})

/**
* PUT /api/nginx/certificates/123
*
* Updates a specific certificate
*/
.put((req, res, next) => {
const data = { id: req.params.certificate_id, ...req.body };
apiValidator(schema.getValidationSchema('/nginx/certificates/{certID}', 'put'), data)
.then((data) => {
return internalCertificate.update(res.locals.access, data);
})
.then((result) => {
res.status(201)
.send(result);
})
.catch(next);
})

/**
* DELETE /api/nginx/certificates/123
*
77 changes: 77 additions & 0 deletions backend/schema/paths/nginx/certificates/certID/put.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"operationId": "updateCertificate",
"summary": "Update a Certificate",
"tags": ["Certificates"],
"security": [
{
"BearerAuth": ["certificates"]
}
],
"parameters": [
{
"in": "path",
"name": "certID",
"schema": {
"type": "integer",
"minimum": 1
},
"required": true,
"example": 1
}
],
"requestBody": {
"description": "Certificate Payload",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"additionalProperties": false,
"required": ["provider"],
"properties": {
"id": {
"$ref": "../../../../common.json#/properties/id"
},
"provider": {
"$ref": "../../../../components/certificate-object.json#/properties/provider"
},
"nice_name": {
"$ref": "../../../../components/certificate-object.json#/properties/nice_name"
}
}
}
}
}
},
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"examples": {
"default": {
"value": {
"id": 4,
"created_on": "2024-10-09T05:31:58.000Z",
"modified_on": "2024-10-09T05:32:11.000Z",
"owner_user_id": 1,
"provider": "letsencrypt",
"nice_name": "test.example.com",
"domain_names": ["test.example.com"],
"expires_on": "2025-01-07T04:34:18.000Z",
"meta": {
"letsencrypt_email": "jc@jc21.com",
"letsencrypt_agree": true,
"dns_challenge": false
}
}
}
},
"schema": {
"$ref": "../../../../components/certificate-object.json"
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions backend/schema/swagger.json
Original file line number Diff line number Diff line change
@@ -70,6 +70,9 @@
"get": {
"$ref": "./paths/nginx/certificates/certID/get.json"
},
"put": {
"$ref": "./paths/nginx/certificates/certID/put.json"
},
"delete": {
"$ref": "./paths/nginx/certificates/certID/delete.json"
}
18 changes: 13 additions & 5 deletions frontend/js/app/nginx/certificates/form.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><%- i18n('certificates', 'form-title', {provider: provider}) %></h5>
<h5 class="modal-title"><%- i18n('certificates', 'form-title' , {provider: provider, id: id}) %></h5>
<button type="button" class="close cancel non-loader-content" aria-label="Close" data-dismiss="modal">&nbsp;</button>
</div>
<div class="modal-body">
@@ -148,18 +148,26 @@
</div>
<div class="col-sm-12 col-md-12 other-ssl">
<div class="form-group">
<div class="form-label"><%- i18n('certificates', 'other-certificate-key') %><span class="form-required">*</span></div>
<div class="form-label"><%- i18n('certificates', 'other-certificate-key') %>
<% if (isNew()) { %>
<span class="form-required">*</span>
<% } %>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" name="meta[other_certificate_key]" id="other_certificate_key" required>
<input type="file" class="custom-file-input" name="meta[other_certificate_key]" id="other_certificate_key" <%- isNew() ? 'required' : '' %>>
<label id="other_certificate_key_label" class="custom-file-label"><%- i18n('str', 'choose-file') %></label>
</div>
</div>
</div>
<div class="col-sm-12 col-md-12 other-ssl">
<div class="form-group">
<div class="form-label"><%- i18n('certificates', 'other-certificate') %><span class="form-required">*</span></div>
<div class="form-label"><%- i18n('certificates', 'other-certificate') %>
<% if (isNew()) { %>
<span class="form-required">*</span>
<% } %>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" name="meta[other_certificate]" id="other_certificate">
<input type="file" class="custom-file-input" name="meta[other_certificate]" id="other_certificate" <%- isNew() ? 'required' : '' %>>
<label id="other_certificate_label" class="custom-file-label"><%- i18n('str', 'choose-file') %></label>
</div>
</div>
35 changes: 25 additions & 10 deletions frontend/js/app/nginx/certificates/form.js
Original file line number Diff line number Diff line change
@@ -90,6 +90,7 @@ module.exports = Mn.View.extend({
}

let data = this.ui.form.serializeJSON();
data.id = this.model.get('id');
data.provider = this.model.get('provider');
let ssl_files = [];

@@ -125,30 +126,38 @@ module.exports = Mn.View.extend({
if (typeof data.domain_names === 'string' && data.domain_names) {
data.domain_names = data.domain_names.split(',');
}
} else if (data.provider === 'other' && !this.model.hasSslFiles()) {
} else if (data.provider === 'other') {
const isNew = data.id == null;
// check files are attached
if (!this.ui.other_certificate[0].files.length || !this.ui.other_certificate[0].files[0].size) {
// Check Certificate
const hasCertificateFile = this.ui.other_certificate[0].files.length && this.ui.other_certificate[0].files[0].size
if (isNew && !hasCertificateFile) {
alert('Certificate file is not attached');
return;
} else {
}
if (hasCertificateFile) {
if (this.ui.other_certificate[0].files[0].size > this.max_file_size) {
alert('Certificate file is too large (> 100kb)');
return;
}
ssl_files.push({name: 'certificate', file: this.ui.other_certificate[0].files[0]});
ssl_files.push({ name: 'certificate', file: this.ui.other_certificate[0].files[0] });
}

if (!this.ui.other_certificate_key[0].files.length || !this.ui.other_certificate_key[0].files[0].size) {
// Check Certificate Key
const hasCertificateKeyFile = this.ui.other_certificate_key[0].files.length && this.ui.other_certificate_key[0].files[0].size
if (isNew && !hasCertificateKeyFile) {
alert('Certificate key file is not attached');
return;
} else {
}
if (hasCertificateKeyFile) {
if (this.ui.other_certificate_key[0].files[0].size > this.max_file_size) {
alert('Certificate key file is too large (> 100kb)');
return;
}
ssl_files.push({name: 'certificate_key', file: this.ui.other_certificate_key[0].files[0]});
ssl_files.push({ name: 'certificate_key', file: this.ui.other_certificate_key[0].files[0] });
}

// Check Intermediate Certificate
if (this.ui.other_intermediate_certificate[0].files.length && this.ui.other_intermediate_certificate[0].files[0].size) {
if (this.ui.other_intermediate_certificate[0].files[0].size > this.max_file_size) {
alert('Intermediate Certificate file is too large (> 100kb)');
@@ -170,20 +179,23 @@ module.exports = Mn.View.extend({
}

new Promise(resolve => {
if (data.provider === 'other') {
if (data.provider === 'other' && ssl_files.length) {
resolve(App.Api.Nginx.Certificates.validate(form_data));
} else {
resolve();
}
})
.then(() => {
return App.Api.Nginx.Certificates.create(data);
return data.id == null
? App.Api.Nginx.Certificates.create(data)
: App.Api.Nginx.Certificates.update(data);
})
.then(result => {
this.model.set(result);

// Now upload the certs if we need to
if (data.provider === 'other') {
const hasCertificateFiles = form_data.has('certificate') || form_data.has('certificate_key') || form_data.has('intermediate_certificate');
if (data.provider === 'other' && hasCertificateFiles) {
return App.Api.Nginx.Certificates.upload(this.model.get('id'), form_data)
.then(result => {
this.model.set('meta', _.assign({}, this.model.get('meta'), result));
@@ -240,6 +252,9 @@ module.exports = Mn.View.extend({
this.getUI(ui).text(e.target.files[0].name)
},
templateContext: {
isNew: function () {
return this.id == null;
},
getLetsencryptEmail: function () {
return typeof this.meta.letsencrypt_email !== 'undefined' ? this.meta.letsencrypt_email : App.Cache.User.get('email');
},
4 changes: 4 additions & 0 deletions frontend/js/app/nginx/certificates/list/item.ejs
Original file line number Diff line number Diff line change
@@ -53,6 +53,10 @@
<a href="#" class="test dropdown-item"><i class="dropdown-icon fe fe-globe"></i> <%- i18n('certificates', 'test-reachability') %></a>
<% } %>
<div class="dropdown-divider"></div>
<% } else { %>
<a href="#" class="replace dropdown-item"><i class="dropdown-icon fe fe-edit"></i>
<%- i18n('certificates', 'edit') %></a>
<div class="dropdown-divider"></div>
<% } %>
<a href="#" class="delete dropdown-item"><i class="dropdown-icon fe fe-trash-2"></i> <%- i18n('str', 'delete') %></a>
<% if (active_domain_names().length > 0) { %>
6 changes: 6 additions & 0 deletions frontend/js/app/nginx/certificates/list/item.js
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ module.exports = Mn.View.extend({
ui: {
host_link: '.host-link',
renew: 'a.renew',
replace: 'a.replace',
delete: 'a.delete',
download: 'a.download',
test: 'a.test'
@@ -22,6 +23,11 @@ module.exports = Mn.View.extend({
App.Controller.showNginxCertificateRenew(this.model);
},

'click @ui.replace': function (e) {
e.preventDefault();
App.Controller.showNginxCertificateForm(this.model);
},

'click @ui.delete': function (e) {
e.preventDefault();
App.Controller.showNginxCertificateDeleteConfirm(this.model);
3 changes: 2 additions & 1 deletion frontend/js/i18n/messages.json
Original file line number Diff line number Diff line change
@@ -187,7 +187,7 @@
"title": "SSL Certificates",
"empty": "There are no SSL Certificates",
"add": "Add SSL Certificate",
"form-title": "Add {provider, select, letsencrypt{Let's Encrypt} other{Custom}} Certificate",
"form-title": "{id, select, undefined{Add} other{Edit}} {provider, select, letsencrypt{Let's Encrypt} other{Custom}} Certificate",
"delete": "Delete SSL Certificate",
"delete-confirm": "Are you sure you want to delete this SSL Certificate? Any hosts using it will need to be updated later.",
"help-title": "SSL Certificates",
@@ -196,6 +196,7 @@
"other-certificate-key": "Certificate Key",
"other-intermediate-certificate": "Intermediate Certificate",
"force-renew": "Renew Now",
"edit": "Edit",
"test-reachability": "Test Server Reachability",
"reachability-title": "Test Server Reachability",
"reachability-info": "Test whether the domains are reachable from the public internet using Site24x7. This is not necessary when using the DNS Challenge.",
7 changes: 7 additions & 0 deletions frontend/js/models/certificate.js
Original file line number Diff line number Diff line change
@@ -21,6 +21,13 @@ const model = Backbone.Model.extend({
};
},

/**
* @returns {Boolean}
*/
isNew: function () {
return this.get('id') == null;
},

/**
* @returns {Boolean}
*/