102 Commits

Author SHA1 Message Date
Tim Burke
ce9e56a6d1 lint: Consistently use assertIsInstance
This has been available since py32 and was backported to py27; there
is no point in us continuing to carry the old idiom forward.

Change-Id: I21f64b8b2970e2dd5f56836f7f513e7895a5dc88
2024-02-07 15:48:39 -08:00
Clay Gerrard
4a37a2976b slo: refactor GET/HEAD response handling
This patch reorganizes the SLO read response handling.  The main goal
was to push the response header replacement for both GET/HEAD SLO and
multipart-manifest=get paths all into a common return path.  A new
RespAttrs primitive is used to carry around some metadata details from
requests made in SLO.  The authors hope these changes make the code more
easily readable and easier to modify.

Drive-By: add new "friendly_close" function in common.utils so we can
drain empty/error responses more confidently (and use it in swob and
request_helpers).

Drive-By: the tests added in the Related-Change discovered a 500 on
If-[Un]Modified-Since conditional GET requests - it probably wasn't
important, but this refactor fixed it on accident as a side effect.

Closes-Bug: #2040178
Co-Authored-By: Alistair Coles <alistairncoles@gmail.com>
Co-Authored-By: Ashwin Nair <nairashwin952013@gmail.com>
Related-Change-Id: I54094f3d2098f56b755ec19cc9315d06a6ca8b15
Change-Id: Idc84e70539fc7480b6ecb86e2f0da904baf2c727
2023-11-10 15:26:28 -06:00
Clay Gerrard
9e065e2d23 fix swob HEAD handling for leak tracking
We added a "safety net" in swob that won't let HEAD responses return a
body.  It seems like we don't currently have many unittests that both
enforce leak tracking and ALSO make HEAD requests.  The way swob works
currently it's not possible for even a "well behaved client" to get a
clean assert with FakeSwift's leak tracking because the leak tracking
iterator isn't returned to the client to close and swob doesn't do
anything to close down and consume the HEAD response iterator it's
throwing away.

RelatedChange: I168e147aae7c1728e7e3fdabb7fba6f2d747d937
Change-Id: I079315ebeb772eaad1bf190fefd132b4087b4897
2023-08-16 23:49:35 +00:00
Tim Burke
2b5853f419 Quote paths before sending them to swob.Request.blank
Following the fix for https://bugs.python.org/issue43882, our py39 unit
tests started failing. This was because swob.Request.blank calls
stdlib's urlparse, which now strips out newlines. Since Request.blank
*also* always unquotes, just make sure we always quote the newlines we
want to use while testing.

Change-Id: Ia5857c70e51d8af3e42ecaced95525be578db127
2021-05-27 12:22:57 -07:00
Tim Burke
fa768b4342 Simplify wsgify()
Change-Id: Iec399aa8b58e72152a17265f2af1131f02667131
2020-05-27 03:19:13 +00:00
Tim Burke
1f7b97ec0f Add normalize_etag() helper function
... and drive-by a import rename

Co-Authored-By: Clay Gerrard <clay.gerrard@gmail.com>
Change-Id: I1eaf075ff9855cfa03e7991bdf33375b0e4397e6
2019-12-14 01:53:43 +00:00
Clay Gerrard
698717d886 Allow internal clients to use reserved namespace
Reserve the namespace starting with the NULL byte for internal
use-cases.  Backend services will allow path names to include the NULL
byte in urls and validate names in the reserved namespace.  Database
services will filter all names starting with the NULL byte from
responses unless the request includes the header:

    X-Backend-Allow-Reserved-Names: true

The proxy server will not allow path names to include the NULL byte in
urls unless a middlware has set the X-Backend-Allow-Reserved-Names
header.  Middlewares can use the reserved namespace to create objects
and containers that can not be directly manipulated by clients.  Any
objects and bytes created in the reserved namespace will be aggregated
to the user's account totals.

When deploying internal proxys developers and operators may configure
the gatekeeper middleware to translate the X-Allow-Reserved-Names header
to the Backend header so they can manipulate the reserved namespace
directly through the normal API.

UpgradeImpact: it's not safe to rollback from this change

Change-Id: If912f71d8b0d03369680374e8233da85d8d38f85
2019-11-27 11:22:00 -06:00
Tim Burke
d270596b67 Consistently use io.BytesIO
Change-Id: Ic41b37ac75b5596a8307c4962be86f2a4b0d9731
2019-10-15 15:09:46 +02:00
Tim Burke
06e4533c2e py3: Fix header_to_environ_key
When doing things for the sake of WSGI, .upper(), .title() etc. should
only be used on bytes.

Change-Id: I130ba5014b7eff458d87ab29eb42fe45607c9a12
2019-06-10 13:47:33 -07:00
Zuul
310416abf3 Merge "py3: port s3api" 2019-05-04 22:49:38 +00:00
Gilles Biannic
a4cc353375 Make log format for requests configurable
Add the log_msg_template option in proxy-server.conf and log_format in
a/c/o-server.conf. It is a string parsable by Python's format()
function. Some fields containing user data might be anonymized by using
log_anonymization_method and log_anonymization_salt.

Change-Id: I29e30ef45fe3f8a026e7897127ffae08a6a80cd9
2019-05-02 17:43:25 -06:00
Tim Burke
3a9f3f8419 py3: port s3api
Drive-by: When passing a list or tuple to swob.Response as an app_iter,
check that it's full of byte strings.

Change-Id: Ifc35aacb2e45004f74c871f08ff3c52bc57c1463
2019-05-02 16:38:39 -07:00
Tim Burke
1e3f8a0e53 Address some review comments
Change-Id: Iacff8a7d7e37557b9a7694ac2df0cfc6b3492024
Related-Change: Ia5815602d05925c5de110accc4dfb1368203bd8d
2019-02-14 21:23:00 +00:00
Zuul
cb3692f8db Merge "swob.Match: remove quotes when checking __contains__" 2018-07-04 06:34:17 +00:00
Tim Burke
d03fc9bc54 swob: Stop auto-encoding unicode bodies
Instead, require that callers provide an encoding.

Related-Change: I31408f525ba9836f634a35581d4aee6fa2c9428f
Change-Id: I3e5ed9e4401eea76c375bb43ad4afc58b1d8006a
2018-06-28 09:58:44 -07:00
Tim Burke
158d4c28b2 swob.Match: remove quotes when checking __contains__
If we're going to drop the quotes during initialization, we should
probably drop them when making comparisons, too.

Change-Id: I90c6e8a7c6faf8fafb0cd64fabbc9629d6c4c48a
2018-06-27 16:52:38 -07:00
Tim Burke
efcf7e6a95 swob: Fix up some WSGI string business
Change-Id: Iee1bab5775b243c318aa22ee4a548d793e6684ca
2018-06-27 15:55:30 -07:00
Tim Burke
2b19c26498 py3: Port common/swob
Change-Id: I31408f525ba9836f634a35581d4aee6fa2c9428f
2018-06-20 16:52:02 -07:00
Tim Burke
77bfe7affa swob.Match: add __repr__
<swift.common.swob.Match object at 0x7fe589571d50> isn't quite as
obvious.

Change-Id: I7197e7732a4b56084b45ce6a113bc39ce8c7bd54
2018-04-10 14:21:51 -07:00
Tim Burke
baa4fa5d65 swob.Match: Optional whitespace is optional
https://tools.ietf.org/html/rfc7232#section-3 defines the form for
If-Match and If-None-Match as

    If-Match = "*" / 1#entity-tag
    If-None-Match = "*" / 1#entity-tag

https://tools.ietf.org/html/rfc7230#section-7 in turn defines the
1#<type> syntax as

    1#element => element *( OWS "," OWS element )

where OWS is *optional* whitespace. Our swob.Match object should respect
that optionality.

Change-Id: I6ee1c6674e0e9c156149319022fd289504bd3722
2018-03-26 21:58:20 +00:00
Tim Burke
c118059719 Respond 400 Bad Request when Accept headers fail to parse
Change-Id: I6eb4e4bca95e2ee4fecdb703394cb2419737922d
Closes-Bug: 1716509
2017-10-13 12:35:21 -07:00
Jenkins
21a0df4ae0 Merge "More assertion cleanup" 2017-09-13 06:44:02 +00:00
Tim Burke
f70feb39b9 swob: Raise RuntimeError instead of IndexError
... when the underlying application forgets to call start_response.

Change-Id: I092be4bfdf5b1d7819fc1540307eaa918970e5b6
Closes-Bug: 1462226
2017-09-11 15:21:22 +00:00
Viktor Varga
2cb74b1b84 Replaced assertTrue(False, msg) with fail(msg)
In some unit tests instead of self.fail(msg) statements
self.assertTrue(False, msg) were used, which might be ambiguous.

Using assertTrue(False, msg) gives the following message on fail:

    File "C:\Python361\lib\unittest\case.py", line 678, in assertTrue
      raise self.failureException(msg)
      AssertionError: False is not true : msg

'False is not true' message implies that unit test failed (as the
result is False while we asserted True).

Replaced with self.fail(msg) is less ambiguous and more readable.

    File "C:\Python361\lib\unittest\case.py", line 666, in fail
      raise self.failureException(msg)
      AssertionError: msg

TrivialFix

Change-Id: Ib56a0ed8549fd7af2724eb59222106888781e9c8
2017-06-21 12:16:33 +02:00
lingyongxu
ee9458a250 Using assertIsNone() instead of assertEqual(None)
Following OpenStack Style Guidelines:
[1] http://docs.openstack.org/developer/hacking/#unit-tests-and-assertraises
[H203] Unit test assertions tend to give better messages for more specific
assertions. As a result, assertIsNone(...) is preferred over
assertEqual(None, ...) and assertIs(..., None)

Change-Id: If4db8872c4f5705c1fff017c4891626e9ce4d1e4
2017-06-07 14:05:53 +08:00
Tim Burke
c6b9195db8 More assertion cleanup
Change-Id: Id88af19c5bfd0bcbbeabcf4eeb23beef4c50b1cb
Related-Change: I416831c8ad92f8445bc8d9560040a5ebf5c90702
2016-12-12 14:08:07 -08:00
Cao Xuan Hoang
3da144a3af Replace 'assertTrue(a not in b)' with 'assertNotIn(a, b)'
trivialfix

Change-Id: I416831c8ad92f8445bc8d9560040a5ebf5c90702
2016-12-12 16:23:09 +07:00
Tim Burke
e8a80e874a Let users know entity size in 416 responses
If a user sends a Range header with no satisfiable ranges, we send back
a 416 Requested Range Not Satisfiable response. Previously however,
there would be no indication of the size of the object they were
requesting, so they wouldn't know how to craft a satisfiable range. We
*do* send a Content-Length, but it is (correctly) the length of the
error message.

The RFC [1] has an answer for this:

>  A server generating a 416 (Range Not Satisfiable) response to a
>  byte-range request SHOULD send a Content-Range header field with an
>  unsatisfied-range value, as in the following example:
>
>    Content-Range: bytes */1234
>
>  The complete-length in a 416 response indicates the current length of
>  the selected representation.

Now, we'll send a Content-Range header for all 416 responses, including
those coming from the object server as well as those generated on a
proxy because of the Range mangling required to support EC policies.

[1] RFC 7233, section 4.2, although similar language was used in RFC
2616, sections 10.4.17 and 14.16

Change-Id: I80c7390fc6f84a10a212b0641bb07a64dfccbd45
2016-11-30 10:52:08 -08:00
Tim Burke
c91ca5335f Ignore Range values like "bytes=--0"
This is consistent with what we already do for other
semantically-invalid values like "bytes=--1". Previously, we would
return a 416 Requested Range Not Satisfiable response like we do for the
semantically-valid-but-not-really-meaningful "bytes=-0"

Change-Id: I932b42406c9a5ee7eaa6978a655e61022a957415
2016-10-29 18:33:48 +02:00
Jenkins
1e5c5c35bd Merge "Support multi-range GETs for static large objects." 2016-09-28 04:48:34 +00:00
Timur Alperovich
0900a2c244 Fix error handling in proxy/controllers/base.py.
There are a few issues going on in the
controllers/base.py:_get_response_parts_iter(). One is that the
"raise" statement that attempts to re-raise the GeneratorExit, may
re-raise ValueError if that was the last exception caught.

Secondly, the range may not actually be set in the backend_headers
(need to investigate further, as that could actually be faulty tests,
since learn_size_from_content_range should always set it). The patch
changes the Range construction to throw a ValueError if None is passed
in that case.

Lastly, the range may be only half-defined, e.g. bytes=0-. In that
case, the check of how many bytes are expected vs how many bytes have
been sent does not make sense.

Change-Id: Ida5adf3d33c736240b2c4bae5510b5289f03dee2
2016-09-07 14:00:57 -07:00
Samuel Merritt
4bcd3d7f6d Support multi-range GETs for static large objects.
Bonus consistency: 416 responses now always have a body. Before, if
you had "swob.HTTPRequestedRangeNotSatisfiable()", you'd get a body,
but if you had "swob.Response(..., conditional_response=True)", then
you'd get a length-0 response body. Now you always get a response
body. It's just the default <html><h1>..., but at it's always there.

Bonus efficiency: do a little caching of sub-SLO manifests to avoid
needless re-fetches. This kicks in when there are multiple references
to the same sub-SLO in a given manifest. The caching only holds 20
sub-SLOs so that a malicious user can't build a giant SLO tree and use
it to run the proxy out of memory (we're already holding up to 10
manifests in memory at a time since a SLO can include another SLO to a
depth of 10; this doesn't make the situation too much worse).

Change-Id: I24716e3271cf3370642e3755447e717fd7d9957c
2016-08-18 15:56:06 -07:00
Prashanth Pai
46d61a4dcd Refactor server side copy as middleware
Rewrite server side copy and 'object post as copy' feature as middleware to
simplify the PUT method in the object controller code. COPY is no longer
a verb implemented as public method in Proxy application.

The server side copy middleware is inserted to the left of dlo, slo and
versioned_writes middlewares in the proxy server pipeline. As a result,
dlo and slo copy_hooks are no longer required. SLO manifests are now
validated when copied so when copying a manifest to another account the
referenced segments must be readable in that account for the manifest
copy to succeed (previously this validation was not made, meaning the
manifest was copied but could be unusable if the segments were not
readable).

With this change, there should be no change in functionality or existing
behavior. This is asserted with (almost) no changes required to existing
functional tests.

Some notes (for operators):
* Middleware required to be auto-inserted before slo and dlo and
  versioned_writes
* Turning off server side copy is not configurable.
* object_post_as_copy is no longer a configurable option of proxy server
  but of this middleware. However, for smooth upgrade, config option set
  in proxy server app is also read.

DocImpact: Introducing server side copy as middleware

Co-Authored-By: Alistair Coles <alistair.coles@hpe.com>
Co-Authored-By: Thiago da Silva <thiago@redhat.com>

Change-Id: Ic96a92e938589a2f6add35a40741fd062f1c29eb
Signed-off-by: Prashanth Pai <ppai@redhat.com>
Signed-off-by: Thiago da Silva <thiago@redhat.com>
2016-05-11 14:55:51 -04:00
Samuel Merritt
ca304cd08e Ignore negative suffix-byte-range requests.
If the client asked for "Range: bytes=--123", Swift would respond with
a 206 and a Content-Length of -123. Now that Range header is ignored
just like all kinds of other invalid Range headers.

Change-Id: I30d4522d223076ce342d20c52f57ff0eb2aea1f4
Closes-Bug: 1571106
2016-04-15 17:22:44 -07:00
Samuel Merritt
9430f4c9f5 Move HeaderKeyDict to avoid an inline import
There was a function in swift.common.utils that was importing
swob.HeaderKeyDict at call time. It couldn't import it at compilation
time since utils can't import from swob or else it blows up with a
circular import error.

This commit just moves HeaderKeyDict into swift.common.header_key_dict
so that we can remove the inline import.

Change-Id: I656fde8cc2e125327c26c589cf1045cb81ffc7e5
2016-03-07 12:26:48 -08:00
Takashi Kajinami
8e4347afd5 Fix proxy-server's support for chunked transferring in GET object
Proxy-server now requires Content-Length in the response header
when getting object and does not support chunked transferring with
"Transfer-Encoding: chunked"

This doesn't matter in normal swift, but prohibits us from putting
any middelwares to execute something like streaming processing of
objects, which can't calculate the length of their response body
before they start to send their response.

Change-Id: I60fc6c86338d734e39b7e5f1e48a2647995045ef
2016-03-02 22:56:13 +09:00
Tim Burke
e13a03c379 Stop overriding builtin range
Change-Id: I315f8b554bb9e96659b455f4158f074961bd6498
2016-01-20 16:06:26 -08:00
Alistair Coles
ad722b3ed3 Include params in multipart message part content-type
The content-type header inserted into a multipart message part
is missing any params such as charset because its value is being
fetched via the swob.Response content_type property, which conforms
to webob spec and strips off all params.

This was noticed in work on feature/crypto branch because the crypto
meta param was being stripped off content-type in multipart messages,
preventing the content-type being decrypted. But in general there is
no suggestion in the multipart message spec [1] that params should
not be included. In fact examples in [1] show the charset param
included in the content-type value.

To ensure that the multipart message part content-type includes the
original content-type params, fetch it directly from the response
headers.

[1] http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

Change-Id: Iff7274aa631a92cd7332212ed8b4378c27da4a1f
2015-11-25 18:57:48 +00:00
janonymous
f5f9d791b0 pep8 fix: assertEquals -> assertEqual
assertEquals is deprecated in py3, replacing it.

Change-Id: Ida206abbb13c320095bb9e3b25a2b66cc31bfba8
Co-Authored-By: Ondřej Nový <ondrej.novy@firma.seznam.cz>
2015-10-11 12:57:25 +02:00
Victor Stinner
c0af385173 py3: Replace urllib imports with six.moves.urllib
The urllib, urllib2 and urlparse modules of Python 2 were reorganized
into a new urllib namespace on Python 3. Replace urllib, urllib2 and
urlparse imports with six.moves.urllib to make the modified code
compatible with Python 2 and Python 3.

The initial patch was generated by the urllib operation of the sixer
tool on: bin/* swift/ test/.

Change-Id: I61a8c7fb7972eabc7da8dad3b3d34bceee5c5d93
2015-10-08 15:24:13 +02:00
Victor Stinner
5fbfbba787 Port swob to Python 3
* HeaderEnvironProxy: replace UserDict.DictMixin with
  collections.MutableMapping, add __iter__ and __len__ methods, and
  add more unit tests
* Replace url* imports with six.moves.urllib

Change-Id: I9ed22d0dd52ee7ac8fa16571f82c45975cfdffff
2015-08-27 01:00:26 +02:00
Jenkins
187cea5445 Merge "Replace StringIO with BytesIO for WSGI input" 2015-07-24 06:52:40 +00:00
Jenkins
260e976e50 Merge "Get StringIO and cStringIO from six.moves" 2015-07-24 06:52:36 +00:00
janonymous
cd7b2db550 unit tests: Replace "self.assert_" by "self.assertTrue"
The assert_() method is deprecated and can be safely replaced by assertTrue().
This patch makes sure that running the tests does not create undesired
warnings.

Change-Id: I0602ba39ef93263386644ee68088d5f65fcb4a71
2015-07-21 19:23:00 +05:30
Victor Stinner
8753e452b0 Replace StringIO with BytesIO for WSGI input
wsgi.input is a binary stream (bytes), not a text stream (unicode).

* Replace StringIO with BytesIO for WSGI input
* Replace StringIO('') with StringIO() and replace WsgiStringIO('') with
  WsgiStringIO(): an empty string is already the default value

Change-Id: I09c9527be2265a6847189aeeb74a17261ddc781a
2015-07-15 16:56:33 +02:00
Victor Stinner
6e70f3fa32 Get StringIO and cStringIO from six.moves
* replace "from cStringIO import StringIO"
  with "from six.moves import cStringIO as StringIO"
* replace "from StringIO import StringIO"
  with "from six import StringIO"
* replace "import cStringIO" and "cStringIO.StringIO()"
  with "from six import moves" and "moves.cStringIO()"
* replace "import StringIO" and "StringIO.StringIO()"
  with "import six" and "six.StringIO()"

This patch was generated by the stringio operation of the sixer tool:
https://pypi.python.org/pypi/sixer

Change-Id: Iacba77fec3045f96773d1090c0bd48613729a561
2015-07-15 16:56:33 +02:00
Victor Stinner
e5c962a28c Replace xrange() with six.moves.range()
Patch generated by the xrange operation of the sixer tool:
https://pypi.python.org/pypi/sixer

Manual changes:

* Fix indentation for pep8 checks
* Fix TestGreenthreadSafeIterator.test_access_is_serialized of
  test.unit.common.test_utils:
  replace range(1, 11) with list(range(1, 11))
* Fix UnsafeXrange docstring, revert change

Change-Id: Icb7e26135c5e57b5302b8bfe066b33cafe69fe4d
2015-06-23 07:29:15 +00:00
janonymous
09e7477a39 Replace it.next() with next(it) for py3 compat
The Python 2 next() method of iterators was renamed to __next__() on
Python 3. Use the builtin next() function instead which works on Python
2 and Python 3.

Change-Id: Ic948bc574b58f1d28c5c58e3985906dee17fa51d
2015-06-15 22:10:45 +05:30
Samuel Merritt
4f2ed8bcd0 EC: support multiple ranges for GET requests
This commit lets clients receive multipart/byteranges responses (see
RFC 7233, Appendix A) for erasure-coded objects. Clients can already
do this for replicated objects, so this brings EC closer to feature
parity (ha!).

GetOrHeadHandler got a base class extracted from it that treats an
HTTP response as a sequence of byte-range responses. This way, it can
continue to yield whole fragments, not just N-byte pieces of the raw
HTTP response, since an N-byte piece of a multipart/byteranges
response is pretty much useless.

There are a couple of bonus fixes in here, too. For starters, download
resuming now works on multipart/byteranges responses. Before, it only
worked on 200 responses or 206 responses for a single byte
range. Also, BufferedHTTPResponse grew a readline() method.

Also, the MIME response for replicated objects got tightened up a
little. Before, it had some leading and trailing CRLFs which, while
allowed by RFC 7233, provide no benefit. Now, both replicated and EC
multipart/byteranges avoid extraneous bytes. This let me re-use the
Content-Length calculation in swob instead of having to either hack
around it or add extraneous whitespace to match.

Change-Id: I16fc65e0ec4e356706d327bdb02a3741e36330a0
2015-06-03 11:42:00 -07:00
Samuel Merritt
b1eda4aef8 Allow sending object metadata after data
This lets the proxy server send object metadata to the object server
after the object data. This is necessary for EC, as it allows us to
compute the etag of the object in the proxy server and still store it
with the object.

The wire format is a multipart MIME document. For sanity during a
rolling upgrade, the multipart MIME document is only sent to the
object server if it indicates, via 100 Continue header, that it knows
how to consume it.

Example 1 (new proxy, new obj server):

   proxy: PUT /p/a/c/o
          X-Backend-Obj-Metadata-Footer: yes

     obj: 100 Continue
        X-Obj-Metadata-Footer: yes

   proxy: --MIMEmimeMIMEmime...

Example2: (new proxy, old obj server)

   proxy: PUT /p/a/c/o
          X-Backend-Obj-Metadata-Footer: yes

     obj: 100 Continue

   proxy: <obj body>

Co-Authored-By: Alistair Coles <alistair.coles@hp.com>
Co-Authored-By: Thiago da Silva <thiago@redhat.com>
Co-Authored-By: John Dickinson <me@not.mn>
Co-Authored-By: Clay Gerrard <clay.gerrard@gmail.com>
Co-Authored-By: Tushar Gohad <tushar.gohad@intel.com>
Co-Authored-By: Paul Luse <paul.e.luse@intel.com>
Co-Authored-By: Christian Schwede <christian.schwede@enovance.com>
Co-Authored-By: Yuan Zhou <yuan.zhou@intel.com>
Change-Id: Id38f7e93e3473f19ff88123ae0501000ed9b2e89
2015-04-14 00:52:17 -07:00