Skip to content
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

fix: always close file descriptors after parsing LDIF files #159

Merged
merged 1 commit into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
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
20 changes: 6 additions & 14 deletions docker-jans-persistence-loader/scripts/couchbase_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,16 @@ def import_ldif(self, bucket_mappings):

for _, mapping in bucket_mappings.items():
for file_ in mapping["files"]:
logger.info(f"Importing {file_} file")
src = f"/app/templates/{file_}"
dst = f"/app/tmp/{file_}"
os.makedirs(os.path.dirname(dst), exist_ok=True)

render_ldif(src, dst, ctx)
parser = LDIFParser(open(dst, "rb"))

query_file = f"/app/tmp/{file_}.n1ql"
with open(dst, "rb") as fd:
parser = LDIFParser(fd)

with open(query_file, "a+") as f:
for dn, entry in parser.parse():
if len(entry) <= 2:
continue
Expand All @@ -359,19 +359,11 @@ def import_ldif(self, bucket_mappings):
entry["dn"] = [dn]
entry = transform_entry(entry, attr_processor)
data = json.dumps(entry)
# using INSERT will cause duplication error, but the data is left intact
query = 'INSERT INTO `%s` (KEY, VALUE) VALUES ("%s", %s);\n' % (mapping["bucket"], key, data)
f.write(query)

# exec query
logger.info("Importing {} file into {} bucket (if needed)".format(file_, mapping["bucket"]))
with open(query_file) as f:
for line in f:
query = line.strip()
if not query:
continue

# using INSERT will cause duplication error, but the data is left intact
query = 'INSERT INTO `%s` (KEY, VALUE) VALUES ("%s", %s)' % (mapping["bucket"], key, data)
req = self.client.exec_query(query)

if not req.ok:
logger.warning("Failed to execute query, reason={}".format(req.json()))

Expand Down
7 changes: 4 additions & 3 deletions docker-jans-persistence-loader/scripts/ldap_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ def import_ldif(self):

render_ldif(src, dst, ctx)

parser = LDIFParser(open(dst, "rb"))
for dn, entry in parser.parse():
self.add_entry(dn, entry)
with open(dst, "rb") as fd:
parser = LDIFParser(fd)
for dn, entry in parser.parse():
self.add_entry(dn, entry)

def add_entry(self, dn, attrs):
max_wait_time = 300
Expand Down