Skip to content

Commit

Permalink
* s3cmd: New command [sign]
Browse files Browse the repository at this point in the history
* S3/Utils.py: New function sign_string()
* S3/S3.py, S3/CloudFront.py: Use sign_string().


git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3cmd/trunk@378 830e0280-6d2a-0410-9c65-932aecc39d9d
  • Loading branch information
mludvig committed Feb 24, 2009
1 parent 9a3c1ab commit 0b8ea55
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,10 @@
2009-02-24 Michal Ludvig <michal@logix.cz>

* s3cmd: New command [sign]
* S3/Utils.py: New function sign_string()
* S3/S3.py, S3/CloudFront.py: Use sign_string().
* NEWS: Updated.

2009-02-17 Michal Ludvig <michal@logix.cz> 2009-02-17 Michal Ludvig <michal@logix.cz>


* Released version 0.9.9 * Released version 0.9.9
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,8 @@
s3cmd 1.0.0
===========
* New command 'sign' for signing for instance
the POST upload policies.

s3cmd 0.9.9 - 2009-02-17 s3cmd 0.9.9 - 2009-02-17
=========== ===========
New commands: New commands:
Expand Down
12 changes: 2 additions & 10 deletions S3/CloudFront.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,26 +4,18 @@
## License: GPL Version 2 ## License: GPL Version 2


import sys import sys
import base64
import time import time
import httplib import httplib
from logging import debug, info, warning, error from logging import debug, info, warning, error


try:
from hashlib import md5, sha1
except ImportError:
from md5 import md5
import sha as sha1
import hmac

try: try:
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
except ImportError: except ImportError:
import elementtree.ElementTree as ET import elementtree.ElementTree as ET


from Config import Config from Config import Config
from Exceptions import * from Exceptions import *
from Utils import getTreeFromXml, appendXmlTextNode, getDictFromTree, dateS3toPython from Utils import getTreeFromXml, appendXmlTextNode, getDictFromTree, dateS3toPython, sign_string
from S3Uri import S3Uri, S3UriS3 from S3Uri import S3Uri, S3UriS3


def output(message): def output(message):
Expand Down Expand Up @@ -349,7 +341,7 @@ def create_request(self, operation, dist_id = None, headers = None):


def sign_request(self, headers): def sign_request(self, headers):
string_to_sign = headers['x-amz-date'] string_to_sign = headers['x-amz-date']
signature = base64.encodestring(hmac.new(self.config.secret_key, string_to_sign, sha1).digest()).strip() signature = sign_string(string_to_sign)
debug(u"CloudFront.sign_request('%s') = %s" % (string_to_sign, signature)) debug(u"CloudFront.sign_request('%s') = %s" % (string_to_sign, signature))
return signature return signature


Expand Down
7 changes: 2 additions & 5 deletions S3/S3.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


import sys import sys
import os, os.path import os, os.path
import base64
import time import time
import httplib import httplib
import logging import logging
Expand All @@ -14,11 +13,9 @@
from stat import ST_SIZE from stat import ST_SIZE


try: try:
from hashlib import md5, sha1 from hashlib import md5
except ImportError: except ImportError:
from md5 import md5 from md5 import md5
import sha as sha1
import hmac


from Utils import * from Utils import *
from SortedDict import SortedDict from SortedDict import SortedDict
Expand Down Expand Up @@ -649,7 +646,7 @@ def sign_headers(self, method, resource, headers):
h += "/" + resource['bucket'] h += "/" + resource['bucket']
h += resource['uri'] h += resource['uri']
debug("SignHeaders: " + repr(h)) debug("SignHeaders: " + repr(h))
return base64.encodestring(hmac.new(self.config.secret_key, h, sha1).digest()).strip() return sign_string(h)


@staticmethod @staticmethod
def check_bucket_name(bucket, dns_strict = True): def check_bucket_name(bucket, dns_strict = True):
Expand Down
10 changes: 9 additions & 1 deletion S3/Utils.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import random import random
import rfc822 import rfc822
try: try:
from hashlib import md5 from hashlib import md5, sha1
except ImportError: except ImportError:
from md5 import md5 from md5 import md5
import sha as sha1
import hmac
import base64
import errno import errno


from logging import debug, info, warning, error from logging import debug, info, warning, error
Expand Down Expand Up @@ -253,3 +256,8 @@ def unicodise_safe(string, encoding = None):


return unicodise(deunicodise(string, encoding), encoding).replace(u'\ufffd', '?') return unicodise(deunicodise(string, encoding), encoding).replace(u'\ufffd', '?')


def sign_string(string_to_sign):
#debug("string_to_sign: %s" % string_to_sign)
signature = base64.encodestring(hmac.new(Config.Config().secret_key, string_to_sign, sha1).digest()).strip()
#debug("signature: %s" % signature)
return signature
8 changes: 8 additions & 0 deletions s3cmd
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1048,6 +1048,12 @@ def cmd_setacl(args):
if retsponse['status'] == 200: if retsponse['status'] == 200:
output(u"%s: ACL set to %s %s" % (uri, set_to_acl, seq_label)) output(u"%s: ACL set to %s %s" % (uri, set_to_acl, seq_label))


def cmd_sign(args):
string_to_sign = args.pop()
debug("string-to-sign: %r" % string_to_sign)
signature = Utils.sign_string(string_to_sign)
output("Signature: %s" % signature)

def resolve_list(lst, args): def resolve_list(lst, args):
retval = [] retval = []
for item in lst: for item in lst:
Expand Down Expand Up @@ -1281,6 +1287,8 @@ def get_commands_list():
{"cmd":"cp", "label":"Copy object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_cp, "argc":2}, {"cmd":"cp", "label":"Copy object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_cp, "argc":2},
{"cmd":"mv", "label":"Move object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_mv, "argc":2}, {"cmd":"mv", "label":"Move object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_mv, "argc":2},
{"cmd":"setacl", "label":"Modify Access control list for Bucket or Files", "param":"s3://BUCKET[/OBJECT]", "func":cmd_setacl, "argc":1}, {"cmd":"setacl", "label":"Modify Access control list for Bucket or Files", "param":"s3://BUCKET[/OBJECT]", "func":cmd_setacl, "argc":1},
{"cmd":"sign", "label":"Sign arbitrary string using the secret key", "param":"STRING-TO-SIGN", "func":cmd_sign, "argc":1},

## CloudFront commands ## CloudFront commands
{"cmd":"cflist", "label":"List CloudFront distribution points", "param":"", "func":CfCmd.info, "argc":0}, {"cmd":"cflist", "label":"List CloudFront distribution points", "param":"", "func":CfCmd.info, "argc":0},
{"cmd":"cfinfo", "label":"Display CloudFront distribution point parameters", "param":"[cf://DIST_ID]", "func":CfCmd.info, "argc":0}, {"cmd":"cfinfo", "label":"Display CloudFront distribution point parameters", "param":"[cf://DIST_ID]", "func":CfCmd.info, "argc":0},
Expand Down

0 comments on commit 0b8ea55

Please sign in to comment.