Write your first Swift Proxy Middleware
In this article we will learn how to write simple swift proxy middleware
OpenStack Swift
OpenStack Swift, also known as OpenStack Object Storage, is open source software designed to manage the storage of large amounts of data cost-effectively on a long-term basis across clusters of standard server hardware.
basic swift architecture
How OpenStack Swift works
OpenStack Swift stores data as binary objects on the server operating system's underlying file system. Each object has associated metadata as part of the extended attributes of the file.
The OpenStack Swift architecture includes a proxy server and storage nodes. The proxy server implements the Swift REST-based application programming interface (API) to enable the transmission of read and write requests between clients and the storage servers via the HTTP protocol. Users employ commands such as PUT and GET to store and retrieve objects and their associated metadata from the Swift cluster, and the proxy server locates the objects by their hashtags and metadata. The proxy server also confirms the completion of writes to drives on the storage nodes.
Role of middleware
Typically middleware acts a bridge between user request/response and server (proxy/account /object/conatiner). The use of middleware is we can alter the request/response according to our needs. By default swift supports some of the middleware in addition to that you can extend by having your own middleware
we can see that between client and proxy server we have multiple middleware like catch errors, gatekeeper, temp url, static web etc similarly you can create your own middle ware and add to the pipeline as per your requirement.
Assuming you have python installed with open stack swift. I will be using linux based machine and file paths related to it. If you are using different machine file paths may differ but the implementation remains same
step 1: go to file /etc/swift/proxy-server.conf
This is is the configuration file which contains details about your middleware, you can see the pipeline with different middleware like healthcheck, cache, formpost, tempurl and so on
Now I added my_middleware here which I am going to define soon
step 2: Configure your middleware
You can do it in two ways
one way
Add the below two lines which are highlighted to /etc/swift/proxy-server.conf file
The use = egg:swift#my_middleware lines here are PasteDeploy entrypoints. Entrypoints are references to Python objects in packages that are named, require certain arguments, and expect a specific return value.
The entrypoint has to be specified in the /usr/lib/python2.7/site-packages/swift-2.7.2.dev33-py2.7.egg-info/entry_points.txt under the [paste.filter_factory] section
other way
Add the complete entry point entry to /etc/swift/proxy-server.conf file
step 3: create your new middleware file
Create a python file in the path we have specified in the configuration file /usr/lib/python2.7/site-packages/swift/commomn/middleware where all the midleware are defined.
I created my_middleware.py file in the specified path. Add the filter_factory function to your program
Now define the class SwiftSampleMiddleWare where actual changes reside
In this class your actual business logic reside. The import statements one is for accessing response and the other is WSGI(Web Server Gateway Interface). I am altering the response headers by adding an additional attribute to my actual response headers 'x-hello' which has value 'world'. similarly you can alter request/response according to your needs.
so finally your python file will be something like this
step 4: check the output
Now you have created your middleware file and configured in the swift pipeline so lets try to check whether our variable is getting added to the response headers
Restart the swift proxy server using command service openstack-swift-proxy restart
Next source /root/openrc then run swift list --debug
We can see that our attribute is added to the response headers
If you have any errors in between you can check in the /var/log/swift/proxy-server.log file for details
If you want more about middle implementation you can visit the official site. Thank You