blob: 0b20acf8b3b315df44984030b62c86429bb99e0a [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
Dale Curtis8adf7892011-09-08 16:13:36 -070082 users = getattr(self, 'users')
83 hosts = getattr(self, 'hosts')
84 acls = getattr(self, 'acls')
85 if ((users and (hosts or acls)) or
86 (hosts and acls)):
mblighbe630eb2008-08-01 16:41:48 +000087 self.invalid_syntax('Only specify one of --user,'
88 '--machine or ACL')
89
Dale Curtis8adf7892011-09-08 16:13:36 -070090 if len(users) > 1:
mblighbe630eb2008-08-01 16:41:48 +000091 self.invalid_syntax('Only specify one <user>')
Dale Curtis8adf7892011-09-08 16:13:36 -070092 if len(hosts) > 1:
mblighbe630eb2008-08-01 16:41:48 +000093 self.invalid_syntax('Only specify one <machine>')
94
95 try:
Dale Curtis8adf7892011-09-08 16:13:36 -070096 self.users = users[0]
mblighbe630eb2008-08-01 16:41:48 +000097 except IndexError:
98 pass
99
100 try:
Dale Curtis8adf7892011-09-08 16:13:36 -0700101 self.hosts = hosts[0]
mblighbe630eb2008-08-01 16:41:48 +0000102 except IndexError:
103 pass
104 return (options, leftover)
105
106
107 def execute(self):
108 filters = {}
109 check_results = {}
110 if self.acls:
111 filters['name__in'] = self.acls
112 check_results['name__in'] = 'name'
113
114 if self.users:
115 filters['users__login'] = self.users
116 check_results['users__login'] = None
117
118 if self.hosts:
119 filters['hosts__hostname'] = self.hosts
120 check_results['hosts__hostname'] = None
121
122 return super(acl_list,
123 self).execute(op='get_acl_groups',
124 filters=filters,
125 check_results=check_results)
126
127
128 def output(self, results):
mbligh838c7472009-05-13 20:56:50 +0000129 # If an ACL was specified, always print its details
130 if self.acls or self.verbose:
131 sublist_keys=('hosts', 'users')
132 else:
133 sublist_keys=()
134
mblighbe630eb2008-08-01 16:41:48 +0000135 super(acl_list, self).output(results,
mbligh838c7472009-05-13 20:56:50 +0000136 keys=('name', 'description'),
137 sublist_keys=sublist_keys)
mblighbe630eb2008-08-01 16:41:48 +0000138
139
140class acl_create(action_common.atest_create, acl):
141 """atest acl create <acl> --desc <description>"""
142 def __init__(self):
143 super(acl_create, self).__init__()
144 self.parser.add_option('-d', '--desc',
145 help='Creates the ACL with the DESCRIPTION',
146 type='string')
147 self.parser.remove_option('--alist')
148
149
150 def parse(self):
mbligh9deeefa2009-05-01 23:11:08 +0000151 (options, leftover) = super(acl_create, self).parse(req_items='acls')
mblighbe630eb2008-08-01 16:41:48 +0000152
153 if not options.desc:
154 self.invalid_syntax('Must specify a description to create an ACL.')
155
156 self.data_item_key = 'name'
157 self.data['description'] = options.desc
158
159 if len(self.acls) > 1:
160 self.invalid_syntax('Can only create one ACL at a time')
161
162 return (options, leftover)
163
164
165class acl_delete(action_common.atest_delete, acl):
166 """atest acl delete [<acls> | --alist <file>"""
167 pass
168
169
170class acl_add_or_remove(acl):
171 def __init__(self):
172 super(acl_add_or_remove, self).__init__()
173 # Get the appropriate help for adding or removing.
174 words = self.usage_words
175 lower_words = tuple(word.lower() for word in words)
176
177 self.parser.add_option('-u', '--user',
178 help='%s USER(s) %s the ACL' % words,
179 type='string',
180 metavar='USER')
181 self.parser.add_option('-U', '--ulist',
182 help='File containing users to %s %s '
183 'the ACL' % lower_words,
184 type='string',
185 metavar='USER_FLIST')
186 self.parser.add_option('-m', '--machine',
187 help='%s MACHINE(s) %s the ACL' % words,
188 type='string',
189 metavar='MACHINE')
190 self.parser.add_option('-M', '--mlist',
191 help='File containing machines to %s %s '
192 'the ACL' % lower_words,
193 type='string',
194 metavar='MACHINE_FLIST')
195
196
197 def parse(self):
mbligh9deeefa2009-05-01 23:11:08 +0000198 user_info = topic_common.item_parse_info(attribute_name='users',
199 inline_option='user',
200 filename_option='ulist')
201 host_info = topic_common.item_parse_info(attribute_name='hosts',
202 inline_option='machine',
203 filename_option='mlist')
204 (options, leftover) = super(acl_add_or_remove,
205 self).parse([user_info, host_info],
206 req_items='acls')
mblighbe630eb2008-08-01 16:41:48 +0000207
208 if (not getattr(self, 'users', None) and
209 not getattr(self, 'hosts', None)):
210 self.invalid_syntax('Specify at least one USER or MACHINE')
211
212 return (options, leftover)
213
214
215class acl_add(action_common.atest_add, acl_add_or_remove):
216 """atest acl add <acl> --user <user>|
217 --machine <machine>|--mlist <FILE>]"""
218 pass
219
220
221class acl_remove(action_common.atest_remove, acl_add_or_remove):
222 """atest acl remove [<acls> | --alist <file>
223 --user <user> | --machine <machine> | --mlist <FILE>]"""
224 pass