blob: 11dfcca76401420fceabc46df448710c64961c94 [file] [log] [blame]
Ankur53fdf562014-05-16 10:03:49 -07001// Package flag defines a method for parsing ACL flags and constructing
2// a security.Authorizer based on them.
3package flag
4
5import (
6 "bytes"
7 "errors"
8 "flag"
9
Tilak Sharma3ed30242014-08-11 11:45:55 -070010 vsecurity "veyron/security"
11
Ankur53fdf562014-05-16 10:03:49 -070012 "veyron2/security"
13)
14
15var (
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.)
24func 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 Sharma3ed30242014-08-11 11:45:55 -070032 return vsecurity.NewFileACLAuthorizer(*aclFile)
Ankur53fdf562014-05-16 10:03:49 -070033 }
34 a, err := security.LoadACL(bytes.NewBufferString(*acl))
35 if err != nil {
36 return nil
37 }
Tilak Sharma3ed30242014-08-11 11:45:55 -070038 return vsecurity.NewACLAuthorizer(a)
Ankur53fdf562014-05-16 10:03:49 -070039}