Skip to content

Commit 2dbe481

Browse files
Merge pull request #634 from peppelinux/xmlsec_tmpfiles
Delete temporary files by xmlsec
2 parents 8379d70 + cc45268 commit 2dbe481

File tree

7 files changed

+144
-144
lines changed

7 files changed

+144
-144
lines changed

docs/howto/config.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The basic structure of the configuration file is therefore like this::
3434
"key_file" : "my.key",
3535
"cert_file" : "ca.pem",
3636
"xmlsec_binary" : "/usr/local/bin/xmlsec1",
37+
"delete_tmpfiles": True,
3738
"metadata": {
3839
"local": ["edugain.xml"],
3940
},
@@ -318,6 +319,17 @@ Example::
318319

319320
"xmlsec_binary": "/usr/local/bin/xmlsec1",
320321

322+
delete_tmpfiles
323+
^^^^^^^^^^^^^^^
324+
325+
In many cases temporary files will have to be created during the
326+
encryption/decryption/signing/validation process.
327+
This option defines whether these temporary files will be automatically deleted when
328+
they are no longer needed. Setting this to False, will keep these files until they are
329+
manually deleted or automatically deleted by the OS (i.e Linux rules for /tmp).
330+
Absence of this option, defaults to True.
331+
332+
321333
valid_for
322334
^^^^^^^^^
323335

@@ -832,6 +844,7 @@ We start with a simple but fairly complete Service provider configuration::
832844
"key_file" : "./mykey.pem",
833845
"cert_file" : "./mycert.pem",
834846
"xmlsec_binary" : "/usr/local/bin/xmlsec1",
847+
"delete_tmpfiles": True,
835848
"attribute_map_dir": "./attributemaps",
836849
"metadata": {
837850
"local": ["idp.xml"]
@@ -880,6 +893,7 @@ A slightly more complex configuration::
880893
"key_file" : "./mykey.pem",
881894
"cert_file" : "./mycert.pem",
882895
"xmlsec_binary" : "/usr/local/bin/xmlsec1",
896+
"delete_tmpfiles": True,
883897
"metadata" : {
884898
"local": ["example.xml"],
885899
"remote": [{

src/saml2/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"allow_unknown_attributes",
7070
"crypto_backend",
7171
"id_attr_name",
72+
"delete_tmpfiles",
7273
]
7374

7475
SP_ARGS = [
@@ -243,6 +244,7 @@ def __init__(self, homedir="."):
243244
self.attribute = []
244245
self.attribute_profile = []
245246
self.requested_attribute_name_format = NAME_FORMAT_URI
247+
self.delete_tmpfiles = True
246248

247249
def setattr(self, context, attr, val):
248250
if context == "":
@@ -358,6 +360,12 @@ def load(self, cnf, metadata_construction=False):
358360
except TypeError: # Something that can't be a string
359361
setattr(self, arg, cnf[arg])
360362

363+
if not self.delete_tmpfiles:
364+
logger.warning(
365+
"delete_tmpfiles is set to False; "
366+
"temporary files will not be deleted."
367+
)
368+
361369
if "service" in cnf:
362370
for typ in ["aa", "idp", "sp", "pdp", "aq"]:
363371
try:

src/saml2/entity.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ def __init__(self, entity_type, config=None, config_file="",
144144
if _val.startswith("http"):
145145
r = requests.request("GET", _val)
146146
if r.status_code == 200:
147-
_, filename = make_temp(r.text, ".pem", False)
148-
setattr(self.config, item, filename)
147+
tmp = make_temp(r.text, ".pem", False, self.config.delete_tmpfiles)
148+
setattr(self.config, item, tmp.name)
149149
else:
150150
raise Exception(
151151
"Could not fetch certificate from %s" % _val)
@@ -560,8 +560,10 @@ def _encrypt_assertion(self, encrypt_cert, sp_entity_id, response,
560560
_cert = "%s%s" % (begin_cert, _cert)
561561
if end_cert not in _cert:
562562
_cert = "%s%s" % (_cert, end_cert)
563-
_, cert_file = make_temp(_cert.encode('ascii'), decode=False)
564-
response = self.sec.encrypt_assertion(response, cert_file,
563+
tmp = make_temp(_cert.encode('ascii'),
564+
decode=False,
565+
delete_tmpfiles=self.config.delete_tmpfiles)
566+
response = self.sec.encrypt_assertion(response, tmp.name,
565567
pre_encryption_part(),
566568
node_xpath=node_xpath)
567569
return response

0 commit comments

Comments
 (0)