From a7f16094395301affa05963d7b46d0af1f3535b1 Mon Sep 17 00:00:00 2001 From: Carlos Cardenas Date: Thu, 11 Jul 2013 15:47:43 -0700 Subject: [PATCH] added expires to use ISO 8601 DateTime format --- bin/msign | 34 ++++++++++++++++++++++++++++++---- docs/man/msign.md | 14 +++++++++----- man/man1/msign.1 | 18 +++++++++++++----- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/bin/msign b/bin/msign index 4cdc395..529a4f9 100755 --- a/bin/msign +++ b/bin/msign @@ -29,9 +29,10 @@ var OPTIONS_PARSER = dashdash.createParser({ options: manta.DEFAULT_CLI_OPTIONS.concat([ { names: ['expires', 'e'], - type: 'positiveInteger', - help: 'expiration time (epoch). Default is 1hr from now.', - 'default': Math.floor((new Date().getTime() / 1000) + 3600), + type: 'string', + help: 'expiration time (ISO 8601 DateTimeString or time since' + + ' the epoch in seconds). Default is 1hr from now.', + 'default': null, helpArg: 'EXPIRATION' }, { @@ -103,6 +104,31 @@ function parseOptions() { return (opts); } +function validateDate(dateString) { + var num_reg =/^\d+$/; + var iso_reg =/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/; + var rtn; + + if (dateString === null) { + return Math.floor((Date.now() / 1000) + 3600); + } + + if (num_reg.test(dateString)) { + rtn = parseInt(dateString); + } + else if (iso_reg.test(dateString)) { + date = new Date(dateString); + if (isNaN(date.valueOf())) { + ifError(date); + } + rtn = Math.floor(date.getTime() / 1000); + } + else { + ifError('Inproper Date format'); + } + + return (rtn); +} ///--- Mainline @@ -122,7 +148,7 @@ function parseOptions() { var _opts = { algorithm: opts.algorithm, - expires: opts.expires, + expires: validateDate(opts.expires), host: url.parse(opts.url).host, keyId: opts.keyId, log: opts.log, diff --git a/docs/man/msign.md b/docs/man/msign.md index e0e0767..0b53e69 100644 --- a/docs/man/msign.md +++ b/docs/man/msign.md @@ -1,4 +1,4 @@ -msign 1 "May 2013" Manta "Manta Commands" +msign 1 "July 2013" Manta "Manta Commands" ======================================= NAME @@ -19,11 +19,11 @@ the environment (whether environment variables or command line switches) creates time-expiring URLs that can be shared with others. This is useful to generate HTML links, for example. -The default expiration for URLs is 5 minutes from `now`, but this can be changed +The default expiration for URLs is 1 hour from `now`, but this can be changed with the `expires` option. The expires option is designed to be used in conjunction with the UNIX date command. In general, you should use the date command with a modifier (the syntax is different between BSD and GNU forms), and -format the output to epoch time. +format the output to epoch time or use an ISO 8601 format. EXAMPLES -------- @@ -32,6 +32,10 @@ Assuming the GNU date command, generate a signed URL that expires in one month: $ msign -e $(date -d "1 month" "+%s") /$MANTA_USER/stor/tmp +or use an ISO 8601 format: + + $ msign -e $(date +%Y-%m-%dT%H:%M:%S.000Z) /$MANTA_USER/stor/tmp + OPTIONS ------- @@ -39,8 +43,8 @@ OPTIONS Authenticate as account (login name). `-e, --expires expiration` - Signed URL should last until EXPIRATION (seconds since epoch). Default is 300 - seconds from `now`. + Signed URL should last until EXPIRATION (ISO 8601 DateTime string or + seconds since epoch). Default is 1 hour from `now`. `-h, --help` Print a help message and exit. diff --git a/man/man1/msign.1 b/man/man1/msign.1 index d2721d2..8f5865d 100644 --- a/man/man1/msign.1 +++ b/man/man1/msign.1 @@ -1,4 +1,4 @@ -.TH msign 1 "May 2013" Manta "Manta Commands" +.TH msign 1 "July 2013" Manta "Manta Commands" .SH NAME .PP msign \- create a signed URL to a Manta object @@ -12,11 +12,11 @@ the environment (whether environment variables or command line switches) creates time\-expiring URLs that can be shared with others. This is useful to generate HTML links, for example. .PP -The default expiration for URLs is 5 minutes from \fB\fCnow\fR, but this can be changed +The default expiration for URLs is 1 hour from \fB\fCnow\fR, but this can be changed with the \fB\fCexpires\fR option. The expires option is designed to be used in conjunction with the UNIX date command. In general, you should use the date command with a modifier (the syntax is different between BSD and GNU forms), and -format the output to epoch time. +format the output to epoch time or use an ISO 8601 format. .SH EXAMPLES .PP Assuming the GNU date command, generate a signed URL that expires in one month: @@ -26,14 +26,22 @@ Assuming the GNU date command, generate a signed URL that expires in one month: $ msign -e $(date -d "1 month" "+%s") /$MANTA_USER/stor/tmp .fi .RE +.PP +or use an ISO 8601 format: +.PP +.RS +.nf +$ msign -e $(date +%Y-%m-%dT%H:%M:%S.000Z) /$MANTA_USER/stor/tmp +.fi +.RE .SH OPTIONS .TP \fB\fC-a, --account login\fR Authenticate as account (login name). .TP \fB\fC-e, --expires expiration\fR -Signed URL should last until EXPIRATION (seconds since epoch). Default is 300 -seconds from \fB\fCnow\fR. +Signed URL should last until EXPIRATION (ISO 8601 DateTime string or +seconds since epoch). Default is 1 hour from \fB\fCnow\fR. .TP \fB\fC-h, --help\fR Print a help message and exit.