Ankur | 53fdf56 | 2014-05-16 10:03:49 -0700 | [diff] [blame] | 1 | // Package flag defines a method for parsing ACL flags and constructing |
| 2 | // a security.Authorizer based on them. |
| 3 | package flag |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "errors" |
| 8 | "flag" |
| 9 | |
Tilak Sharma | 3ed3024 | 2014-08-11 11:45:55 -0700 | [diff] [blame^] | 10 | vsecurity "veyron/security" |
| 11 | |
Ankur | 53fdf56 | 2014-05-16 10:03:49 -0700 | [diff] [blame] | 12 | "veyron2/security" |
| 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | acl = flag.String("acl", "", "acl is an optional JSON-encoded security.ACL that is used to construct a security.Authorizer. Example: \"{\"veyron/alice\":\"RW\"}\" is a JSON-encoded ACL that allows all principals matching \"veyron/alice\" to access all methods with ReadLabel or WriteLabel. If this flag is provided then the \"--acl_file\" must be absent.") |
| 17 | aclFile = flag.String("acl_file", "", "acl_file is an optional path to a file containing a JSON-encoded security.ACL that is used to construct a security.Authorizer. If this flag is provided then the \"--acl_file\" flag must be absent.") |
| 18 | ) |
| 19 | |
| 20 | // NewAuthorizerOrDie constructs an Authorizer based on the provided "--acl" or |
| 21 | // "--acl_file" flags. If both flags are provided the function panics, and if |
| 22 | // neither flag is provided a nil Authorizer is returned (Note that services with |
| 23 | // nil Authorizers are provided with default authorization by the framework.) |
| 24 | func NewAuthorizerOrDie() security.Authorizer { |
| 25 | if len(*acl) == 0 && len(*aclFile) == 0 { |
| 26 | return nil |
| 27 | } |
| 28 | if len(*acl) != 0 && len(*aclFile) != 0 { |
| 29 | panic(errors.New("only one of the flags \"--acl\" or \"--acl_file\" must be provided")) |
| 30 | } |
| 31 | if len(*aclFile) != 0 { |
Tilak Sharma | 3ed3024 | 2014-08-11 11:45:55 -0700 | [diff] [blame^] | 32 | return vsecurity.NewFileACLAuthorizer(*aclFile) |
Ankur | 53fdf56 | 2014-05-16 10:03:49 -0700 | [diff] [blame] | 33 | } |
| 34 | a, err := security.LoadACL(bytes.NewBufferString(*acl)) |
| 35 | if err != nil { |
| 36 | return nil |
| 37 | } |
Tilak Sharma | 3ed3024 | 2014-08-11 11:45:55 -0700 | [diff] [blame^] | 38 | return vsecurity.NewACLAuthorizer(a) |
Ankur | 53fdf56 | 2014-05-16 10:03:49 -0700 | [diff] [blame] | 39 | } |