| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package cipd | |
| 6 | |
| 7 import ( | |
| 8 "net/http" | |
| 9 "time" | |
| 10 | |
| 11 "infra/libs/logging" | |
| 12 ) | |
| 13 | |
| 14 // PackageACLChangeAction defines a flavor of PackageACLChange. | |
| 15 type PackageACLChangeAction string | |
| 16 | |
| 17 const ( | |
| 18 // GrantRole is used in PackageACLChange to request a role to be granted
. | |
| 19 GrantRole PackageACLChangeAction = "GRANT" | |
| 20 // RevokeRole is used in PackageACLChange to request a role to be revoke
d. | |
| 21 RevokeRole PackageACLChangeAction = "REVOKE" | |
| 22 ) | |
| 23 | |
| 24 // PackageACL is per package path per role access control list that is a part of | |
| 25 // larger overall ACL: ACL for package "a/b/c" is a union of PackageACLs for "a" | |
| 26 // "a/b" and "a/b/c". | |
| 27 type PackageACL struct { | |
| 28 // PackagePath is a package subpath this ACL is defined for. | |
| 29 PackagePath string | |
| 30 // Role is a role that listed users have, e.g. 'READER', 'WRITER', ... | |
| 31 Role string | |
| 32 // Principals list users and groups granted the role. | |
| 33 Principals []string | |
| 34 // ModifiedBy specifies who modified the list the last time. | |
| 35 ModifiedBy string | |
| 36 // ModifiedTs is a timestamp when the list was modified the last time. | |
| 37 ModifiedTs time.Time | |
| 38 } | |
| 39 | |
| 40 // ACLOptions contains parameters shared by FetchACL and ModifyACL functions. | |
| 41 type ACLOptions struct { | |
| 42 // ServiceURL is root URL of the backend service, or "" to use default s
ervice. | |
| 43 ServiceURL string | |
| 44 // Client is http.Client to use to make requests, default is http.Defaul
tClient. | |
| 45 Client *http.Client | |
| 46 // Log is a logger to use for logs, default is logging.DefaultLogger. | |
| 47 Log logging.Logger | |
| 48 // PackagePath is a package subpath to fetch or modify ACLs for. | |
| 49 PackagePath string | |
| 50 } | |
| 51 | |
| 52 // FetchACLOptions contains parameters for FetchACL function. | |
| 53 type FetchACLOptions struct { | |
| 54 ACLOptions | |
| 55 } | |
| 56 | |
| 57 // FetchACL returns a list of PackageACL objects (parent paths first) that | |
| 58 // together define access control list for given package subpath. | |
| 59 func FetchACL(options FetchACLOptions) ([]PackageACL, error) { | |
| 60 // Fill in default options. | |
| 61 if options.ServiceURL == "" { | |
| 62 options.ServiceURL = DefaultServiceURL() | |
| 63 } | |
| 64 if options.Client == nil { | |
| 65 options.Client = http.DefaultClient | |
| 66 } | |
| 67 if options.Log == nil { | |
| 68 options.Log = logging.DefaultLogger | |
| 69 } | |
| 70 remote := newRemoteService(options.Client, options.ServiceURL, options.L
og) | |
| 71 return remote.fetchACL(options.PackagePath) | |
| 72 } | |
| 73 | |
| 74 // PackageACLChange is a mutation to some package ACL. | |
| 75 type PackageACLChange struct { | |
| 76 // Action defines what action to perform: GrantRole or RevokeRole. | |
| 77 Action PackageACLChangeAction | |
| 78 // Role to grant or revoke to a user or group. | |
| 79 Role string | |
| 80 // Principal is a user or a group to grant or revoke a role for. | |
| 81 Principal string | |
| 82 } | |
| 83 | |
| 84 // ModifyACLOptions contains parameters for ModifyACL function. | |
| 85 type ModifyACLOptions struct { | |
| 86 ACLOptions | |
| 87 | |
| 88 // Changes defines changes to apply. | |
| 89 Changes []PackageACLChange | |
| 90 } | |
| 91 | |
| 92 // ModifyACL applies a set of PackageACLChanges to a package path. | |
| 93 func ModifyACL(options ModifyACLOptions) error { | |
| 94 // Fill in default options. | |
| 95 if options.ServiceURL == "" { | |
| 96 options.ServiceURL = DefaultServiceURL() | |
| 97 } | |
| 98 if options.Client == nil { | |
| 99 options.Client = http.DefaultClient | |
| 100 } | |
| 101 if options.Log == nil { | |
| 102 options.Log = logging.DefaultLogger | |
| 103 } | |
| 104 remote := newRemoteService(options.Client, options.ServiceURL, options.L
og) | |
| 105 return remote.modifyACL(options.PackagePath, options.Changes) | |
| 106 } | |
| OLD | NEW |