Merge branch 'master' into 7167-keep-rsync-test-setup
[arvados.git] / services / keepstore / 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         "git.curoverse.com/arvados.git/sdk/go/keepclient"
40         "time"
41 )
42
43 // The PermissionSecret is the secret key used to generate SHA1
44 // digests for permission hints. apiserver and Keep must use the same
45 // key.
46 var PermissionSecret []byte
47
48 // SignLocator takes a blobLocator, an apiToken and an expiry time, and
49 // returns a signed locator string.
50 func SignLocator(blobLocator string, apiToken string, expiry time.Time) string {
51         return keepclient.SignLocator(blobLocator, apiToken, expiry, PermissionSecret)
52 }
53
54 // VerifySignature returns nil if the signature on the signedLocator
55 // can be verified using the given apiToken. Otherwise it returns
56 // either ExpiredError (if the timestamp has expired, which is
57 // something the client could have figured out independently) or
58 // PermissionError.
59 func VerifySignature(signedLocator string, apiToken string) error {
60         err := keepclient.VerifySignature(signedLocator, apiToken, PermissionSecret)
61         if err != nil {
62                 if err == keepclient.PermissionError {
63                         return PermissionError
64                 } else if err == keepclient.ExpiredError {
65                         return ExpiredError
66                 }
67         }
68         return err
69 }