Skip to content

Commit 12a1c5d

Browse files
authored
Merge pull request #4322 from jmcrawford45/jaredcrawford-PS-4420
Implement clean for S3DestinationPlugin, add confirm on dest remove
2 parents 0c402b7 + 5e2f4c4 commit 12a1c5d

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
Unreleased
5+
~~~~~~~~~~~~~~~~~~~~
6+
This release adds an implementation of `S3DestinationPlugin.clean`. This means that when S3 destinations are removed via
7+
the UI, Lemur will now delete the associated AWS resource(s).
48

59
1.2.0 - `2022-01-31`
610
~~~~~~~~~~~~~~~~~~~~

lemur/plugins/lemur_aws/plugin.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
.. moduleauthor:: Mikhail Khodorovskiy <mikhail.khodorovskiy@jivesoftware.com>
3333
.. moduleauthor:: Harm Weites <harm@weites.com>
3434
"""
35-
35+
from os.path import join
3636
import sys
3737
from acme.errors import ClientError
3838
from flask import current_app
@@ -591,9 +591,7 @@ def upload(self, name, body, private_key, chain, options, **kwargs):
591591
s3.put(
592592
self.get_option("bucket", options),
593593
self.get_option("region", options),
594-
"{prefix}/{name}.{extension}".format(
595-
prefix=self.get_option("prefix", options), name=name, extension=ext
596-
),
594+
join(self.get_option("prefix", options), f"{name}.{ext}"),
597595
data,
598596
self.get_option("encrypt", options),
599597
account_number=self.get_option("accountNumber", options),
@@ -669,6 +667,12 @@ def delete_acme_token(self, token_path, options, **kwargs):
669667
"filename": filename})
670668
return response
671669

670+
def clean(self, certificate, options, **kwargs):
671+
prefix = self.get_option("prefix", options)
672+
s3.delete(bucket_name=self.get_option("bucket", options),
673+
prefixed_object_name=join(prefix, f"{certificate.name}.pem"),
674+
account_number=self.get_option("accountNumber", options))
675+
672676

673677
class SNSNotificationPlugin(ExpirationNotificationPlugin):
674678
title = "AWS SNS"

lemur/plugins/lemur_aws/tests/test_plugin.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections import namedtuple
2+
from os.path import join
13
import boto3
24
from moto import mock_sts, mock_s3, mock_ec2, mock_elb, mock_elbv2, mock_acm
35

@@ -9,6 +11,58 @@ def test_get_certificates(app):
911
assert p
1012

1113

14+
@mock_sts()
15+
@mock_s3()
16+
def test_clean(app):
17+
from lemur.common.utils import check_validation
18+
from lemur.plugins.base import plugins
19+
20+
bucket = "public-bucket"
21+
account = "123456789012"
22+
prefix = "some-path/more-path/"
23+
24+
additional_options = [
25+
{
26+
"name": "bucket",
27+
"value": bucket,
28+
"type": "str",
29+
"required": True,
30+
"validation": check_validation(r"[0-9a-z.-]{3,63}"),
31+
"helpMessage": "Must be a valid S3 bucket name!",
32+
},
33+
{
34+
"name": "accountNumber",
35+
"type": "str",
36+
"value": account,
37+
"required": True,
38+
"validation": check_validation(r"[0-9]{12}"),
39+
"helpMessage": "A valid AWS account number with permission to access S3",
40+
},
41+
{
42+
"name": "prefix",
43+
"type": "str",
44+
"value": prefix,
45+
"required": False,
46+
"helpMessage": "Must be a valid S3 object prefix!",
47+
},
48+
]
49+
50+
s3_client = boto3.client('s3')
51+
s3_client.create_bucket(Bucket=bucket)
52+
53+
p = plugins.get("aws-s3")
54+
Certificate = namedtuple("Certificate", ["name"])
55+
certificate = Certificate(name="certificate")
56+
s3_client.put_object(
57+
Bucket=bucket,
58+
Body="PEM_DATA",
59+
Key=join(prefix, f"{certificate.name}.pem"),
60+
)
61+
assert s3_client.list_objects(Bucket=bucket)["Contents"]
62+
p.clean(certificate, additional_options)
63+
assert "Contents" not in s3_client.list_objects(Bucket=bucket)
64+
65+
1266
@mock_sts()
1367
@mock_s3()
1468
def test_upload_acme_token(app):

lemur/static/app/angular/certificates/certificate/destinations.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<td><a class="btn btn-sm btn-info" href="#/destinations/{{ destination.id }}/certificates">{{ destination.label }}</a></td>
2222
<td><span class="text-muted">{{ destination.description }}</span></td>
2323
<td>
24-
<button type="button" ng-click="certificate.removeDestination($index)" class="btn btn-danger btn-sm pull-right">Remove</button>
24+
<button type="button" ng-click="certificate.removeDestination($index)" confirm-click="Proceed to delete certificate resources in {{ destination.label }}?" class="btn btn-danger btn-sm pull-right">Remove</button>
2525
</td>
2626
</tr>
2727
</table>

0 commit comments

Comments
 (0)