Skip to content

Commit

Permalink
add rotation to frontend table
Browse files Browse the repository at this point in the history
  • Loading branch information
havron committed Jun 16, 2021
1 parent 722179f commit 3390c89
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
1 change: 1 addition & 0 deletions lemur/certificates/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ class CertificateShortOutputSchema(LemurOutputSchema):
name = fields.String()
owner = fields.Email()
notify = fields.Boolean()
rotation = fields.Boolean()
authority = fields.Nested(AuthorityNestedOutputSchema)
issuer = fields.String()
cn = fields.String()
Expand Down
13 changes: 13 additions & 0 deletions lemur/certificates/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ def update_notify(cert, notify_flag):
return database.update(cert)


def update_rotation(cert, rotation_flag):
"""
Toggle rotation value which is a boolean
:param rotation_flag: new rotation value
:param cert: Certificate object to be updated
:return:
"""
cert.rotation = rotation_flag
return database.update(cert)


def create_certificate_roles(**kwargs):
# create a role for the owner and assign it
owner_role = role_service.get_or_create(
Expand Down Expand Up @@ -534,6 +545,8 @@ def render(args):
)
elif "notify" in filt:
query = query.filter(Certificate.notify == truthiness(terms[1]))
elif "rotation" in filt:
query = query.filter(Certificate.rotation == truthiness(terms[1]))
elif "active" in filt:
query = query.filter(Certificate.active == truthiness(terms[1]))
elif "cn" in terms:
Expand Down
23 changes: 17 additions & 6 deletions lemur/certificates/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def get(self, certificate_name):
"id": 2
},
"active": true,
"rotation": true,
"domains": [{
"sensitive": false,
"id": 1090,
Expand Down Expand Up @@ -242,6 +243,7 @@ def get(self, certificate_name):
parser.add_argument("owner", type=inputs.boolean, location="args")
parser.add_argument("id", type=str, location="args")
parser.add_argument("active", type=inputs.boolean, location="args")
parser.add_argument("rotation", type=inputs.boolean, location="args")
parser.add_argument(
"destinationId", type=int, dest="destination_id", location="args"
)
Expand Down Expand Up @@ -931,21 +933,22 @@ def put(self, certificate_id, data=None):
@validate_schema(certificate_edit_input_schema, certificate_output_schema)
def post(self, certificate_id, data=None):
"""
.. http:post:: /certificates/1/update/notify
.. http:post:: /certificates/1/update/switches
Update certificate notification
Update certificate switches: notification or rotation.
**Example request**:
.. sourcecode:: http
POST /certificates/1/update/notify HTTP/1.1
POST /certificates/1/update/switches HTTP/1.1
Host: example.com
Accept: application/json, text/javascript
Content-Type: application/json;charset=UTF-8
{
"notify": false
"notify": false, # optional
"rotation": false # optional
}
**Example response**:
Expand Down Expand Up @@ -980,6 +983,7 @@ def post(self, certificate_id, data=None):
"description": null,
"deleted": null,
"notify": false,
"rotation": false,
"notifications": [{
"id": 1
}]
Expand Down Expand Up @@ -1029,7 +1033,14 @@ def post(self, certificate_id, data=None):
403,
)

cert = service.update_notify(cert, data.get("notify"))
notify = data.get("notify")
if notify:
cert = service.update_notify(cert, notify)

rotate = data.get("rotation")
if rotate:
cert = service.update_rotation(cert, rotate)

log_service.create(g.current_user, "update_cert", certificate=cert)
return cert

Expand Down Expand Up @@ -1516,7 +1527,7 @@ def put(self, certificate_id, data=None):
Certificates, "/certificates/<int:certificate_id>", endpoint="certificate"
)
api.add_resource(
Certificates, "/certificates/<int:certificate_id>/update/notify", endpoint="certificateUpdateNotify"
Certificates, "/certificates/<int:certificate_id>/update/switches", endpoint="certificateUpdateSwitches"
)
api.add_resource(CertificatesStats, "/certificates/stats", endpoint="certificateStats")
api.add_resource(
Expand Down
2 changes: 1 addition & 1 deletion lemur/static/app/angular/certificates/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ angular.module('lemur')
return certificate.customGET('key');
};

CertificateService.updateNotify = function (certificate) {
CertificateService.updateSwitches = function (certificate) {
return certificate.post();
};

Expand Down
21 changes: 20 additions & 1 deletion lemur/static/app/angular/certificates/view/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ angular.module('lemur')
};

$scope.updateNotify = function (certificate) {
CertificateService.updateNotify(certificate).then(
CertificateService.updateSwitches(certificate).then(
function () {
toaster.pop({
type: 'success',
Expand All @@ -133,6 +133,25 @@ angular.module('lemur')
certificate.notify = false;
});
};
$scope.updateRotation = function (certificate) {
CertificateService.updateSwitches(certificate).then(
function () {
toaster.pop({
type: 'success',
title: certificate.name,
body: 'Updated!'
});
},
function (response) {
toaster.pop({
type: 'error',
title: certificate.name,
body: 'Unable to update! ' + response.data.message,
timeout: 100000
});
certificate.rotation = false;
});
};
$scope.getCertificateStatus = function () {
var def = $q.defer();
def.resolve([{'title': 'True', 'id': true}, {'title': 'False', 'id': false}]);
Expand Down
6 changes: 6 additions & 0 deletions lemur/static/app/angular/certificates/view/view.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ <h2 class="featurette-heading">Certificates
<li><span class="text-muted">{{ certificate.owner }}</span></li>
</ul>
</td>
<td data-title="'Autorotate'" filter="{ 'rotation': 'select' }" filter-data="getCertificateStatus()">
<form>
<switch ng-change="updateRotation(certificate)" id="rotation" name="rotation"
ng-model="certificate.rotation" class="green small"></switch>
</form>
</td>
<td data-title="'Notify'" filter="{ 'notify': 'select' }" filter-data="getCertificateStatus()">
<form>
<switch ng-change="updateNotify(certificate)" id="status" name="status"
Expand Down
24 changes: 24 additions & 0 deletions lemur/tests/test_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,30 @@ def test_certificate_post_update_notify(client, certificate, token, status):
assert response.json.get("notify") == toggled_notify


@pytest.mark.parametrize(
"token,status",
[
(VALID_USER_HEADER_TOKEN, 403),
(VALID_ADMIN_HEADER_TOKEN, 200),
(VALID_ADMIN_API_TOKEN, 200),
("", 401),
],
)
def test_certificate_post_update_notify(client, certificate, token, status):
# negate the current rotation flag and pass it to update POST call to flip the rotation
toggled_rotation = not certificate.rotation

response = client.post(
api.url_for(Certificates, certificate_id=certificate.id),
data=json.dumps({"rotation": toggled_rotation}),
headers=token
)

assert response.status_code == status
if status == 200:
assert response.json.get("rotation") == toggled_rotation


@pytest.mark.parametrize(
"token,status",
[
Expand Down

0 comments on commit 3390c89

Please sign in to comment.