/usr/share/pyshared/swiftsc/client.py is in python-swiftsc 0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | # -*- coding: utf-8 -*-
"""
Copyright (C) 2013 Kouhei Maeda <mkouhei@palmtb.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import requests
import os.path
import json
import sys
import _io
from swiftsc import utils
TIMEOUT = 5.000
def retrieve_token(auth_url, username, password,
tenant_name=None, verify=True):
"""
Arguments:
url : Swift API of authentication
https://<Host>/auth/<api_version>
ex. https://swift.example.org/auth/v1.0
using KeyStone as follows
https://<KeyStone>/<api_version>/tokens
ex. https://keystone.example.org/v2.0/tokens
username : Swift User name
password : Swift User password
tenant_name: tenant name of OpenStack
verify : True is check a host’s SSL certificate
"""
if tenant_name:
# using OpenStack KeyStone
payload = set_auth_info(username, password, tenant_name)
headers = {'Content-Type': 'application/json'}
r = requests.post(auth_url, headers=headers, data=json.dumps(payload),
timeout=TIMEOUT, verify=verify)
res_d = utils.return_json(r.json)
token = retrieve_token_keystone(res_d)
storage_url = retrieve_public_url_swift(res_d)
else:
# using tempauth of Swift
headers = {'X-Storage-User': username, 'X-Storage-Pass': password}
r = requests.get(auth_url, headers=headers,
timeout=TIMEOUT, verify=verify)
token = r.headers.get('X-Auth-Token')
storage_url = r.headers.get('X-Storage-Url')
return token, storage_url
def set_auth_info(username, password, tenant_name):
"""
Arguments:
username : keystone username
password : keystone password
tenant_name: keystone tenant name
"""
payload = {
"auth": {
"passwordCredentials": {
"username": username,
"password": password},
"tenantName": tenant_name}}
return payload
def retrieve_public_url_swift(r_json):
endpoints = [ep.get('endpoints')[0]
for ep in r_json.get('access').get('serviceCatalog')
if ep.get('name') == 'swift'][0]
return endpoints.get('publicURL')
def retrieve_token_keystone(r_json):
return r_json.get('access').get('token').get('id')
def list_containers(token, storage_url, verify=True):
"""
Arguments:
token : authentication token
storage_url: URL of swift storage
verify : True is check a host’s SSL certificate
"""
headers = {'X-Auth-Token': token}
payload = {'format': 'json'}
r = requests.get(storage_url, headers=headers,
params=payload, timeout=TIMEOUT, verify=verify)
# not use r.content that is data type is "str".
# You must encode to unicode and utf-8 by yourself
# if you use multibyte character.
r.encoding = 'utf-8'
return utils.return_json(r.json)
def create_container(token, storage_url, container_name, verify=True):
"""
Arguments:
token : authentication token
storage_url : URL of swift storage
container_name: container name
verify : True is check a host’s SSL certificate
Return: 201 (Created)
"""
headers = {'X-Auth-Token': token}
url = utils.generate_url([storage_url, container_name])
r = requests.put(url, headers=headers, timeout=TIMEOUT, verify=verify)
return r.status_code
def is_container(token, storage_url, container_name, verify=True):
"""
Arguments:
token : authentication token
storage_url : URL of swift storage
container_name: container name
verify : True is check a host’s SSL certificate
Return: boolean
"""
headers = {'X-Auth-Token': token}
url = utils.generate_url([storage_url, container_name])
r = requests.head(url, headers=headers, timeout=TIMEOUT, verify=verify)
return r.ok
def delete_container(token, storage_url, container_name, verify=True):
"""
Arguments:
token : authentication token
storage_url : URL of swift storage
container_name: container name
verify : True is check a host’s SSL certificate
Return: 204 (No Content)
"""
headers = {'X-Auth-Token': token}
url = utils.generate_url([storage_url, container_name])
r = requests.delete(url, headers=headers, timeout=TIMEOUT, verify=verify)
return r.status_code
def create_object(token, storage_url, container_name,
local_file, object_name=None, verify=True):
"""
Arguments:
token : authentication token
storage_url : URL of swift storage
container_name: container name
local_file : absolute path of upload file
or
file object (from stdin pipe)
object_name : object name (optional)
default is local_file basename or
verify : True is check a host’s SSL certificate
Return: 201 (Created)
"""
if ((sys.version_info < (3, 0) and
isinstance(local_file, file)) or
(sys.version_info > (3, 0) and
isinstance(local_file, _io.FileIO))):
# from stdin pipe
(mimetype,
content_length,
data) = utils.retrieve_info_from_buffer(local_file)
else:
with open(local_file, 'rb') as f:
data = f.read()
content_length = os.path.getsize(local_file)
mimetype = utils.check_mimetype(local_file)
if object_name is None:
object_name = os.path.basename(local_file)
# Failed to upload without "Content-Length" when uploading empty file
headers = {'X-Auth-Token': token,
'Content-Length': str(content_length),
'content-type': mimetype}
url = utils.generate_url([storage_url, container_name, object_name])
r = requests.put(url, headers=headers, data=data,
timeout=TIMEOUT, verify=verify)
return r.status_code
def list_objects(token, storage_url, container_name, verify=True):
"""
Arguments:
token : authentication token
storage_url : URL of swift storage
container_name: container name
verify : True is check a host’s SSL certificate
"""
headers = {'X-Auth-Token': token}
payload = {'format': 'json'}
url = utils.generate_url([storage_url, container_name]) + '/'
r = requests.get(url, headers=headers, params=payload,
timeout=TIMEOUT, verify=verify)
r.encoding = 'utf-8'
return utils.return_json(r.json)
def is_object(token, storage_url, container_name, object_name, verify=True):
"""
Arguments:
token : authentication token
storage_url : URL of swift storage
container_name: container name
object_name : object name
verify : True is check a host’s SSL certificate
Return: boolean
"""
headers = {'X-Auth-Token': token}
url = utils.generate_url([storage_url, container_name, object_name])
r = requests.head(url, headers=headers, timeout=TIMEOUT, verify=verify)
return r.ok
def retrieve_object(token, storage_url, container_name,
object_name, verify=True):
"""
Arguments:
token : authentication token
storage_url : URL of swift storage
container_name: container name
object_name : object name
verify : True is check a host’s SSL certificate
Return: object data
"""
headers = {'X-Auth-Token': token}
url = utils.generate_url([storage_url, container_name, object_name])
r = requests.get(url, headers=headers, timeout=TIMEOUT, verify=verify)
return r.ok, r.content
def copy_object(token, storage_url, container_name,
src_object_name, dest_object_name, verify=True):
"""
Arguments:
token : authentication token
storage_url : URL of swift storage
container_name : container name
src_object_name : object name of source
dest_object_name: object name of destination
verify : True is check a host’s SSL certificate
Return: 201 (Created)
"""
src_url = '/' + utils.generate_url([container_name, src_object_name])
headers = {'X-Auth-Token': token,
'Content-Length': "0",
'X-Copy-From': src_url}
dest_url = utils.generate_url([storage_url, container_name,
dest_object_name])
r = requests.put(dest_url, headers=headers, timeout=TIMEOUT, verify=verify)
return r.status_code
def delete_object(token, storage_url, container_name,
object_name, verify=True):
"""
Arguments:
token : authentication token
storage_url : URL of swift storage
container_name: container name
object_name : object name
verify : True is check a host’s SSL certificate
Return: 204 (No Content)
"""
headers = {'X-Auth-Token': token}
url = utils.generate_url([storage_url, container_name, object_name])
r = requests.delete(url, headers=headers, timeout=TIMEOUT, verify=verify)
return r.status_code
|