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

Close SSH connection of SFTP storage #1253

Merged
merged 1 commit into from
May 24, 2023

Conversation

TimoGuenther
Copy link
Contributor

Closes #1234 by making the SFTP storage close the underlying SSH connection after being used as a context manager.

This is done by adding Paramiko's convenient ClosingContextManager to the SFTP storage and implementing its hook for closing. I could have implemented the context manager manually, but Paramiko is required for SFTP, anyway.

In doing so, this bumps the minimum Paramiko version from 1.10.0 to 1.15.0, which is the version that introduced ClosingContextManager almost 9 years ago.

@jschneier jschneier merged commit a87dea6 into jschneier:master May 24, 2023
@jschneier
Copy link
Owner

Thanks looks great.

@TimoGuenther TimoGuenther deleted the close_sftp_connection branch May 25, 2023 06:00
@GT4C
Copy link

GT4C commented Jun 16, 2023

Hello, I'm trying to use this Feature in my project but i can´t implement the ClosingContextManager in my models.py
Can you give me an example of how to use it?

github-merge-queue bot referenced this pull request in mozilla/experimenter Sep 11, 2023
#9385)

Bumps [django-storages](https://github.com/jschneier/django-storages)
from 1.13.1 to 1.14.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/jschneier/django-storages/blob/master/CHANGELOG.rst">django-storages's
changelog</a>.</em></p>
<blockquote>
<p>1.14 (2023-09-04)</p>
<hr />
<h2>General</h2>
<ul>
<li><strong>Breaking</strong>: Drop support for Django 4.0
(<code>[#1235](https://github.com/jschneier/django-storages/issues/1235)</code>_)</li>
<li><strong>Breaking</strong>: The long deprecated &amp; removed (from
Django) <code>(modified|created|accessed)_time</code> methods have been
removed from the various storages, please replace with the
<code>get_(modified|created|accessed)_time</code> methods</li>
<li>Add support for saving <code>pathlib.PurePath</code> names
(<code>[#1278](https://github.com/jschneier/django-storages/issues/1278)</code>_)</li>
<li>Add support for Django 4.2
(<code>[#1236](https://github.com/jschneier/django-storages/issues/1236)</code>_)</li>
</ul>
<h2>Azure</h2>
<ul>
<li>Set <code>account_(name|key)</code> from
<code>connection_string</code> if not provided
(<code>[#1225](https://github.com/jschneier/django-storages/issues/1225)</code>_)</li>
</ul>
<h2>Dropbox</h2>
<ul>
<li><strong>Deprecated:</strong> The name
<code>DropboxStorage.location</code> has been deprecated, please rename
to <code>DropboxStorage.root_path</code>, a future version will
remove support for the old name.
(<code>[#1251](https://github.com/jschneier/django-storages/issues/1251)</code>_)</li>
<li>Storage and related names with a captialized B have been changed to
no longer have one e.g <code>DropboxStorage</code> has now replaced
<code>DropBoxStorage</code>. Aliases have been added so no change is
necessary at this time. A future version might deprecate the old names.
(<code>[#1250](https://github.com/jschneier/django-storages/issues/1250)</code>_)</li>
<li><code>DropboxStorage</code> now conforms to the
<code>BaseStorage</code> interface
(<code>[#1251](https://github.com/jschneier/django-storages/issues/1251)</code>_)</li>
<li>Fix name mangling when saving with certain complex root paths
(<code>[#1279](https://github.com/jschneier/django-storages/issues/1279)</code>_)</li>
</ul>
<h2>FTP</h2>
<ul>
<li>Use setting <code>BASE_URL</code> if it is defined
(<code>[#1238](https://github.com/jschneier/django-storages/issues/1238)</code>_)</li>
</ul>
<h2>Google Cloud</h2>
<ul>
<li><strong>Breaking</strong>: Support for the deprecated
<code>GS_CACHE_CONTROL</code> has been removed. Please set the
<code>cache_control</code> parameter of
<code>GS_OBJECT_PARAMETERS</code> instead.
(<code>[#1220](https://github.com/jschneier/django-storages/issues/1220)</code>_)</li>
</ul>
<h2>Libcloud</h2>
<ul>
<li>Reading a file that does not exist will now raise
<code>FileNotFoundError</code>
(<code>[#1191](https://github.com/jschneier/django-storages/issues/1191)</code>_)</li>
</ul>
<h2>SFTP</h2>
<ul>
<li>Add closing context manager for standalone usage to ensure
connections are cleaned up
(<code>[#1253](https://github.com/jschneier/django-storages/issues/1253)</code>_)</li>
</ul>
<h2>S3</h2>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/jschneier/django-storages/commit/d1cd2db41fd3bec01fe97d4b095159f974b91673"><code>d1cd2db</code></a>
Release version 1.14 (<a
href="https://redirect.github.com/jschneier/django-storages/issues/1291">#1291</a>)</li>
<li><a
href="https://github.com/jschneier/django-storages/commit/557226a9147545ea17a0f9edfbe5a974c09e1dde"><code>557226a</code></a>
ignore s3 namespace change in git blame (<a
href="https://redirect.github.com/jschneier/django-storages/issues/1290">#1290</a>)</li>
<li><a
href="https://github.com/jschneier/django-storages/commit/e1c3b38cc2e6c5bc8330db046386a0541dd1d39e"><code>e1c3b38</code></a>
[s3] change s3 backend namespace to s3 and add compat shim (<a
href="https://redirect.github.com/jschneier/django-storages/issues/1289">#1289</a>)</li>
<li><a
href="https://github.com/jschneier/django-storages/commit/c3a567d0aa13d248b9bef652cee8ff3d678f5522"><code>c3a567d</code></a>
point to docs for all configuration (<a
href="https://redirect.github.com/jschneier/django-storages/issues/1288">#1288</a>)</li>
<li><a
href="https://github.com/jschneier/django-storages/commit/abce92af42f8d66544146daf8d0a5124f5b576e5"><code>abce92a</code></a>
[general] remove dead Storage API methods (<a
href="https://redirect.github.com/jschneier/django-storages/issues/1287">#1287</a>)</li>
<li><a
href="https://github.com/jschneier/django-storages/commit/b02f7e26de45bdbd93f0ce6cd32beb64d4922a06"><code>b02f7e2</code></a>
[s3] fix retrieving SSE-C files (<a
href="https://redirect.github.com/jschneier/django-storages/issues/1286">#1286</a>)</li>
<li><a
href="https://github.com/jschneier/django-storages/commit/ad36a9ffd57b94afb051f7bb4715dc706af92b96"><code>ad36a9f</code></a>
[docs] document OPTIONS settings (<a
href="https://redirect.github.com/jschneier/django-storages/issues/1285">#1285</a>)</li>
<li><a
href="https://github.com/jschneier/django-storages/commit/19a15c2054ffc90994578fd2a99918ab81218864"><code>19a15c2</code></a>
[s3] fix saving files with string content (<a
href="https://redirect.github.com/jschneier/django-storages/issues/911">#911</a>)</li>
<li><a
href="https://github.com/jschneier/django-storages/commit/89ca5bc8e31095f4c9e8bdf851557e41c6364122"><code>89ca5bc</code></a>
[s3] add s3 extras_require (<a
href="https://redirect.github.com/jschneier/django-storages/issues/1284">#1284</a>)</li>
<li><a
href="https://github.com/jschneier/django-storages/commit/53d66750dd89e12afd826ca5483c936fe615cfd8"><code>53d6675</code></a>
[s3] S3Boto3StorageFile respects mode on readlines (<a
href="https://redirect.github.com/jschneier/django-storages/issues/1000">#1000</a>)</li>
<li>Additional commits viewable in <a
href="https://github.com/jschneier/django-storages/compare/1.13.1...1.14">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=django-storages&package-manager=pip&previous-version=1.13.1&new-version=1.14)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unclosed SFTP connection
3 participants