blob: b5832fd10f647ae4adfa733d4c8d113a95b4fddb [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#
2# Copyright 2008 Google Inc. All Rights Reserved.
3
4"""
5The acl module contains the objects and methods used to
6manage ACLs in Autotest.
7
8The valid actions are:
9add: adds acl(s), or users or hosts to an ACL
10remove: deletes acl(s), or users or hosts from an ACL
11list: lists acl(s)
12
13The common options are:
14--alist / -A: file containing a list of ACLs
15
16See topic_common.py for a High Level Design and Algorithm.
17
18"""
19
20import os, sys
21from autotest_lib.cli import topic_common, action_common
22
23
24class acl(topic_common.atest):
25 """ACL class
26 atest acl [create|delete|list|add|remove] <options>"""
27 usage_action = '[create|delete|list|add|remove]'
28 topic = 'acl_group'
29 msg_topic = 'ACL'
30 msg_items = '<acls>'
31
32 def __init__(self):
33 """Add to the parser the options common to all the ACL actions"""
34 super(acl, self).__init__()
35 self.parser.add_option('-A', '--alist',
36 help='File listing the ACLs',
37 type='string',
38 default=None,
39 metavar='ACL_FLIST')
40
41
42 def parse(self, flists=None, req_items='acls'):
43 """Consume the common acl options"""
44 if flists:
45 flists.append(('acls', 'alist', '', True))
46 else:
47 flists = [('acls', 'alist', '', True)]
48 return self.parse_with_flist(flists, req_items)
49
50
51 def get_items(self):
52 return self.acls
53
54
55class acl_help(acl):
56 """Just here to get the atest logic working.
57 Usage is set by its parent"""
58 pass
59
60
61class acl_list(action_common.atest_list, acl):
62 """atest acl list [--verbose]
63 [--user <users>|--mach <machine>|--alist <file>] [<acls>]"""
64 def __init__(self):
65 super(acl_list, self).__init__()
66
67 self.parser.add_option('-u', '--user',
68 help='List ACLs containing USER',
69 type='string',
70 metavar='USER')
71 self.parser.add_option('-m', '--machine',
72 help='List ACLs containing MACHINE',
73 type='string',
74 metavar='MACHINE')
75
76
77 def parse(self):
78 flists = [('users', '', 'user', False),
79 ('hosts', '', 'machine', False)]
80 (options, leftover) = super(acl_list, self).parse(flists,
81 req_items=None)
82
83 if ((self.users and (self.hosts or self.acls)) or
84 (self.hosts and self.acls)):
85 self.invalid_syntax('Only specify one of --user,'
86 '--machine or ACL')
87
88 if len(self.users) > 1:
89 self.invalid_syntax('Only specify one <user>')
90 if len(self.hosts) > 1:
91 self.invalid_syntax('Only specify one <machine>')
92
93 try:
94 self.users = self.users[0]
95 except IndexError:
96 pass
97
98 try:
99 self.hosts = self.hosts[0]
100 except IndexError:
101 pass
102 return (options, leftover)
103
104
105 def execute(self):
106 filters = {}
107 check_results = {}
108 if self.acls:
109 filters['name__in'] = self.acls
110 check_results['name__in'] = 'name'
111
112 if self.users:
113 filters['users__login'] = self.users
114 check_results['users__login'] = None
115
116 if self.hosts:
117 filters['hosts__hostname'] = self.hosts
118 check_results['hosts__hostname'] = None
119
120 return super(acl_list,
121 self).execute(op='get_acl_groups',
122 filters=filters,
123 check_results=check_results)
124
125
126 def output(self, results):
127 super(acl_list, self).output(results,
128 keys=['name', 'description'],
129 sublist_keys=['hosts', 'users'])
130
131
132class acl_create(action_common.atest_create, acl):
133 """atest acl create <acl> --desc <description>"""
134 def __init__(self):
135 super(acl_create, self).__init__()
136 self.parser.add_option('-d', '--desc',
137 help='Creates the ACL with the DESCRIPTION',
138 type='string')
139 self.parser.remove_option('--alist')
140
141
142 def parse(self):
143 (options, leftover) = super(acl_create, self).parse()
144
145 if not options.desc:
146 self.invalid_syntax('Must specify a description to create an ACL.')
147
148 self.data_item_key = 'name'
149 self.data['description'] = options.desc
150
151 if len(self.acls) > 1:
152 self.invalid_syntax('Can only create one ACL at a time')
153
154 return (options, leftover)
155
156
157class acl_delete(action_common.atest_delete, acl):
158 """atest acl delete [<acls> | --alist <file>"""
159 pass
160
161
162class acl_add_or_remove(acl):
163 def __init__(self):
164 super(acl_add_or_remove, self).__init__()
165 # Get the appropriate help for adding or removing.
166 words = self.usage_words
167 lower_words = tuple(word.lower() for word in words)
168
169 self.parser.add_option('-u', '--user',
170 help='%s USER(s) %s the ACL' % words,
171 type='string',
172 metavar='USER')
173 self.parser.add_option('-U', '--ulist',
174 help='File containing users to %s %s '
175 'the ACL' % lower_words,
176 type='string',
177 metavar='USER_FLIST')
178 self.parser.add_option('-m', '--machine',
179 help='%s MACHINE(s) %s the ACL' % words,
180 type='string',
181 metavar='MACHINE')
182 self.parser.add_option('-M', '--mlist',
183 help='File containing machines to %s %s '
184 'the ACL' % lower_words,
185 type='string',
186 metavar='MACHINE_FLIST')
187
188
189 def parse(self):
190 flists = [('users', 'ulist', 'user', False),
191 ('hosts', 'mlist', 'machine', False)]
192
193 (options, leftover) = super(acl_add_or_remove, self).parse(flists)
194
195 if (not getattr(self, 'users', None) and
196 not getattr(self, 'hosts', None)):
197 self.invalid_syntax('Specify at least one USER or MACHINE')
198
199 return (options, leftover)
200
201
202class acl_add(action_common.atest_add, acl_add_or_remove):
203 """atest acl add <acl> --user <user>|
204 --machine <machine>|--mlist <FILE>]"""
205 pass
206
207
208class acl_remove(action_common.atest_remove, acl_add_or_remove):
209 """atest acl remove [<acls> | --alist <file>
210 --user <user> | --machine <machine> | --mlist <FILE>]"""
211 pass