Fix repositories.get_all_permissions, add tests. closes #3546
[arvados.git] / services / keep / src / keep / perms.go
1 /*
2 Permissions management on Arvados locator hashes.
3
4 The permissions structure for Arvados is as follows (from
5 https://arvados.org/issues/2328)
6
7 A Keep locator string has the following format:
8
9     [hash]+[size]+A[signature]@[timestamp]
10
11 The "signature" string here is a cryptographic hash, expressed as a
12 string of hexadecimal digits, and timestamp is a 32-bit Unix timestamp
13 expressed as a hexadecimal number.  e.g.:
14
15     acbd18db4cc2f85cedef654fccc4a4d8+3+A257f3f5f5f0a4e4626a18fc74bd42ec34dcb228a@7fffffff
16
17 The signature represents a guarantee that this locator was generated
18 by either Keep or the API server for use with the supplied API token.
19 If a request to Keep includes a locator with a valid signature and is
20 accompanied by the proper API token, the user has permission to GET
21 that object.
22
23 The signature may be generated either by Keep (after the user writes a
24 block) or by the API server (if the user has can_read permission on
25 the specified object). Keep and API server share a secret that is used
26 to generate signatures.
27
28 To verify a permission hint, Keep generates a new hint for the
29 requested object (using the locator string, the timestamp, the
30 permission secret and the user's API token, which must appear in the
31 request headers) and compares it against the hint included in the
32 request. If the permissions do not match, or if the API token is not
33 present, Keep returns a 401 error.
34 */
35
36 package main
37
38 import (
39         "crypto/hmac"
40         "crypto/sha1"
41         "fmt"
42         "regexp"
43         "strconv"
44         "strings"
45         "time"
46 )
47
48 // The PermissionSecret is the secret key used to generate SHA1
49 // digests for permission hints. apiserver and Keep must use the same
50 // key.
51 var PermissionSecret []byte
52
53 // MakePermSignature returns a string representing the signed permission
54 // hint for the blob identified by blob_hash, api_token and expiration timestamp.
55 func MakePermSignature(blob_hash string, api_token string, expiry string) string {
56         hmac := hmac.New(sha1.New, PermissionSecret)
57         hmac.Write([]byte(blob_hash))
58         hmac.Write([]byte("@"))
59         hmac.Write([]byte(api_token))
60         hmac.Write([]byte("@"))
61         hmac.Write([]byte(expiry))
62         digest := hmac.Sum(nil)
63         return fmt.Sprintf("%x", digest)
64 }
65
66 // SignLocator takes a blob_locator, an api_token and an expiry time, and
67 // returns a signed locator string.
68 func SignLocator(blob_locator string, api_token string, expiry time.Time) string {
69         // If no permission secret or API token is available,
70         // return an unsigned locator.
71         if PermissionSecret == nil || api_token == "" {
72                 return blob_locator
73         }
74         // Extract the hash from the blob locator, omitting any size hint that may be present.
75         blob_hash := strings.Split(blob_locator, "+")[0]
76         // Return the signed locator string.
77         timestamp_hex := fmt.Sprintf("%08x", expiry.Unix())
78         return blob_locator +
79                 "+A" + MakePermSignature(blob_hash, api_token, timestamp_hex) +
80                 "@" + timestamp_hex
81 }
82
83 // VerifySignature returns true if the signature on the signed_locator
84 // can be verified using the given api_token.
85 func VerifySignature(signed_locator string, api_token string) bool {
86         if re, err := regexp.Compile(`^([a-f0-9]{32}(\+[0-9]+)?).*\+A[[:xdigit:]]+@([[:xdigit:]]{8})`); err == nil {
87                 if matches := re.FindStringSubmatch(signed_locator); matches != nil {
88                         blob_locator := matches[1]
89                         timestamp_hex := matches[3]
90                         if expire_ts, err := ParseHexTimestamp(timestamp_hex); err == nil {
91                                 // Fail signatures with expired timestamps.
92                                 if expire_ts.Before(time.Now()) {
93                                         return false
94                                 }
95                                 return signed_locator == SignLocator(blob_locator, api_token, expire_ts)
96                         }
97                 }
98         }
99         return false
100 }
101
102 func ParseHexTimestamp(timestamp_hex string) (ts time.Time, err error) {
103         if ts_int, e := strconv.ParseInt(timestamp_hex, 16, 0); e == nil {
104                 ts = time.Unix(ts_int, 0)
105         } else {
106                 err = e
107         }
108         return ts, err
109 }