blob: d6f5baaa351625a57ef988f8a2b03ab1e594a7d3 [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
mbligh9deeefa2009-05-01 23:11:08 +000041 self.topic_parse_info = topic_common.item_parse_info(
42 attribute_name='acls',
43 filename_option='alist',
44 use_leftover=True)
mblighbe630eb2008-08-01 16:41:48 +000045
46
47 def get_items(self):
48 return self.acls
49
50
51class acl_help(acl):
52 """Just here to get the atest logic working.
53 Usage is set by its parent"""
54 pass
55
56
57class acl_list(action_common.atest_list, acl):
58 """atest acl list [--verbose]
59 [--user <users>|--mach <machine>|--alist <file>] [<acls>]"""
60 def __init__(self):
61 super(acl_list, self).__init__()
62
63 self.parser.add_option('-u', '--user',
64 help='List ACLs containing USER',
65 type='string',
66 metavar='USER')
67 self.parser.add_option('-m', '--machine',
68 help='List ACLs containing MACHINE',
69 type='string',
70 metavar='MACHINE')
71
72
73 def parse(self):
mbligh9deeefa2009-05-01 23:11:08 +000074 user_info = topic_common.item_parse_info(attribute_name='users',
75 inline_option='user')
76 host_info = topic_common.item_parse_info(attribute_name='hosts',
77 inline_option='machine')
78
79 (options, leftover) = super(acl_list, self).parse([user_info,
80 host_info])
mblighbe630eb2008-08-01 16:41:48 +000081
82 if ((self.users and (self.hosts or self.acls)) or
83 (self.hosts and self.acls)):
84 self.invalid_syntax('Only specify one of --user,'
85 '--machine or ACL')
86
87 if len(self.users) > 1:
88 self.invalid_syntax('Only specify one <user>')
89 if len(self.hosts) > 1:
90 self.invalid_syntax('Only specify one <machine>')
91
92 try:
93 self.users = self.users[0]
94 except IndexError:
95 pass
96
97 try:
98 self.hosts = self.hosts[0]
99 except IndexError:
100 pass
101 return (options, leftover)
102
103
104 def execute(self):
105 filters = {}
106 check_results = {}
107 if self.acls:
108 filters['name__in'] = self.acls
109 check_results['name__in'] = 'name'
110
111 if self.users:
112 filters['users__login'] = self.users
113 check_results['users__login'] = None
114
115 if self.hosts:
116 filters['hosts__hostname'] = self.hosts
117 check_results['hosts__hostname'] = None
118
119 return super(acl_list,
120 self).execute(op='get_acl_groups',
121 filters=filters,
122 check_results=check_results)
123
124
125 def output(self, results):
mbligh838c7472009-05-13 20:56:50 +0000126 # If an ACL was specified, always print its details
127 if self.acls or self.verbose:
128 sublist_keys=('hosts', 'users')
129 else:
130 sublist_keys=()
131
mblighbe630eb2008-08-01 16:41:48 +0000132 super(acl_list, self).output(results,
mbligh838c7472009-05-13 20:56:50 +0000133 keys=('name', 'description'),
134 sublist_keys=sublist_keys)
mblighbe630eb2008-08-01 16:41:48 +0000135
136
137class acl_create(action_common.atest_create, acl):
138 """atest acl create <acl> --desc <description>"""
139 def __init__(self):
140 super(acl_create, self).__init__()
141 self.parser.add_option('-d', '--desc',
142 help='Creates the ACL with the DESCRIPTION',
143 type='string')
144 self.parser.remove_option('--alist')
145
146
147 def parse(self):
mbligh9deeefa2009-05-01 23:11:08 +0000148 (options, leftover) = super(acl_create, self).parse(req_items='acls')
mblighbe630eb2008-08-01 16:41:48 +0000149
150 if not options.desc:
151 self.invalid_syntax('Must specify a description to create an ACL.')
152
153 self.data_item_key = 'name'
154 self.data['description'] = options.desc
155
156 if len(self.acls) > 1:
157 self.invalid_syntax('Can only create one ACL at a time')
158
159 return (options, leftover)
160
161
162class acl_delete(action_common.atest_delete, acl):
163 """atest acl delete [<acls> | --alist <file>"""
164 pass
165
166
167class acl_add_or_remove(acl):
168 def __init__(self):
169 super(acl_add_or_remove, self).__init__()
170 # Get the appropriate help for adding or removing.
171 words = self.usage_words
172 lower_words = tuple(word.lower() for word in words)
173
174 self.parser.add_option('-u', '--user',
175 help='%s USER(s) %s the ACL' % words,
176 type='string',
177 metavar='USER')
178 self.parser.add_option('-U', '--ulist',
179 help='File containing users to %s %s '
180 'the ACL' % lower_words,
181 type='string',
182 metavar='USER_FLIST')
183 self.parser.add_option('-m', '--machine',
184 help='%s MACHINE(s) %s the ACL' % words,
185 type='string',
186 metavar='MACHINE')
187 self.parser.add_option('-M', '--mlist',
188 help='File containing machines to %s %s '
189 'the ACL' % lower_words,
190 type='string',
191 metavar='MACHINE_FLIST')
192
193
194 def parse(self):
mbligh9deeefa2009-05-01 23:11:08 +0000195 user_info = topic_common.item_parse_info(attribute_name='users',
196 inline_option='user',
197 filename_option='ulist')
198 host_info = topic_common.item_parse_info(attribute_name='hosts',
199 inline_option='machine',
200 filename_option='mlist')
201 (options, leftover) = super(acl_add_or_remove,
202 self).parse([user_info, host_info],
203 req_items='acls')
mblighbe630eb2008-08-01 16:41:48 +0000204
205 if (not getattr(self, 'users', None) and
206 not getattr(self, 'hosts', None)):
207 self.invalid_syntax('Specify at least one USER or MACHINE')
208
209 return (options, leftover)
210
211
212class acl_add(action_common.atest_add, acl_add_or_remove):
213 """atest acl add <acl> --user <user>|
214 --machine <machine>|--mlist <FILE>]"""
215 pass
216
217
218class acl_remove(action_common.atest_remove, acl_add_or_remove):
219 """atest acl remove [<acls> | --alist <file>
220 --user <user> | --machine <machine> | --mlist <FILE>]"""
221 pass