2 Permissions management on Arvados locator hashes.
4 The permissions structure for Arvados is as follows (from
5 https://arvados.org/issues/2328)
7 A Keep locator string has the following format:
9 [hash]+[size]+A[signature]@[timestamp]
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.:
15 acbd18db4cc2f85cedef654fccc4a4d8+3+A257f3f5f5f0a4e4626a18fc74bd42ec34dcb228a@7fffffff
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
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.
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.
48 // The PermissionSecret is the secret key used to generate SHA1
49 // digests for permission hints. apiserver and Keep must use the same
51 var PermissionSecret []byte
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)
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 == "" {
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())
79 "+A" + MakePermSignature(blob_hash, api_token, timestamp_hex) +
83 var signedLocatorRe = regexp.MustCompile(`^([[:xdigit:]]{32}).*\+A([[:xdigit:]]{40})@([[:xdigit:]]{8})`)
85 // VerifySignature returns true if the signature on the signed_locator
86 // can be verified using the given api_token.
87 func VerifySignature(signed_locator string, api_token string) bool {
88 matches := signedLocatorRe.FindStringSubmatch(signed_locator)
90 // Could not find a permission signature at all
93 blob_hash := matches[1]
96 if exp_time, err := ParseHexTimestamp(exp_hex); err != nil || exp_time.Before(time.Now()) {
97 // Signature is expired, or timestamp is unparseable
100 return sig_hex == MakePermSignature(blob_hash, api_token, exp_hex)
103 func ParseHexTimestamp(timestamp_hex string) (ts time.Time, err error) {
104 if ts_int, e := strconv.ParseInt(timestamp_hex, 16, 0); e == nil {
105 ts = time.Unix(ts_int, 0)