summaryrefslogtreecommitdiffstats
path: root/test/test-confmgt_augeas.py
blob: 6fe8dba1738bcabc74e8c41ce55cc4919f99d0ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python


import sys
import func.overlord.client as fc
c = fc.Client("*")

print c.configmgt_augeas.getenv('AUGEAS_ROOT')

def chroottest():
    envdict={}
    print 'Get the env. variable AUGEAS_ROOT'
    for host,envlist in c.confmgt_augeas.getenv('AUGEAS_ROOT').iteritems():  
        print "Host: "+host
        augroot=envlist.get('AUGEAS_ROOT')
        print 'AUGEAS_ROOT: '+augroot
        envdict[host]=augroot
        if augroot == '/' or augroot == '(none)':
            print "The node "+host+" is not chrooted with AUGEAS_ROOT"
            print "You should consider setting this variable"
            print "before launching this test."
            sys.exit()
    print
    
    print 'Prepare the test environment'
    for host in envdict:
        augroot=envdict[host]
        client = fc.Client(host)
        print client.command.run('mkdir -p '+augroot+'/etc/ssh')
        print client.command.run('cp /etc/hosts '+augroot+'/etc')
        print client.command.run('cp -r /etc/ssh/* '+augroot+'/etc/ssh')
    print

chroottest()


def basictest():
    #print 'Backup sshd_config'
    #print c.confmgt_augeas.backup('/etc/ssh/sshd_config')
    #print

    print 'Delete the Parameter PermitRootLogin in sshd_config'
    print c.confmgt_augeas.rm('/etc/ssh/sshd_config','PermitRootLogin')
    print
    
    print 'Delete the Parameter Port in sshd_config with an Augeas-style path'
    print c.confmgt_augeas.rm('/etc/ssh/sshd_config/Port')
    print
    
    print 'Get sshd_config Port value.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config','Port')
    print
    
    print 'Set Port to 22 in sshd_config'
    print c.confmgt_augeas.set('/etc/ssh/sshd_config','Port','22')
    print
    
    print 'Get sshd_config Port value.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config','Port')
    print
    
    print 'Try to delete a non existant parameter in sshd_config'
    print c.confmgt_augeas.rm('/etc/ssh/sshd_config','Nawak')
    print
    
    print 'Try to delete a parameter in a non existant file.'
    print c.confmgt_augeas.rm('/etc/ssh/nimp','Nawak')
    print
    
    print 'Get sshd_config PermitRootLogin value.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config','PermitRootLogin')
    print
    
    print 'Set PermitRootLogin to yes in sshd_config'
    print c.confmgt_augeas.set('/etc/ssh/sshd_config','PermitRootLogin','yes')
    print
    
    print 'Set PermitRootLogin to no in sshd_config with an Augeas-style path.'
    print c.confmgt_augeas.set('/etc/ssh/sshd_config/PermitRootLogin','','no')
    print
    
    print 'Set PermitRootLogin to yes in sshd_config with an Augeas-style path.'
    print c.confmgt_augeas.set('/etc/ssh/sshd_config/PermitRootLogin','','yes')
    print
    
    print 'Get sshd_config PermitRootLogin value.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config','PermitRootLogin')
    print
    
    print 'Get sshd_config PermitRootLogin value with an Augeas-style path.'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config/PermitRootLogin')
    print
    
    print 'Attempt to get a value for a non existant param in sshd_config'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config','Nawak')
    print
    
    print 'Attempt to get a value for an empty param in sshd_config'
    print c.confmgt_augeas.get('/etc/ssh/sshd_config','Subsystem')
    print
    
    print 'Search for conf. entry in hosts file with canonical hostname = localhost' 
    print c.confmgt_augeas.match('/etc/hosts','canonical','localhost')
    print
    
    #print 'List all direct children of hosts (not very useful)'
    #print c.confmgt_augeas.ls('/etc/hosts/*')
    #print
    
    print 'List all direct children parameters of 1st hosts entry.'
    for host,paramlist in c.confmgt_augeas.ls('/etc/hosts/1').iteritems():
        print "Host: "+host
        if type(paramlist) == type({}):
            for node in paramlist['nodes']:
                print node[0]+" = "+node[1]
        else:
            print paramlist
    print
    
    print 'List all children nodes of 1st hosts entry.' 
    for host,paramlist in c.confmgt_augeas.printconf('/etc/hosts/1').iteritems():
        print "Host: "+host
        if type(paramlist) == type({}):
            for node in paramlist['nodes']:
                print node[0]+" = "+node[1]
        else:
            print paramlist
    print
    
    print 'Get values of 1st host entry.'
    print c.confmgt_augeas.get('/etc/hosts/','1')
    print
    
    print 'List all values for parameter of 1st fstab entry.' 
    minionDict=c.confmgt_augeas.ls('/etc/fstab/1')
    for host,entry in minionDict.iteritems():
        print "Host: "+host
        if type(entry) == type({}):
            print "Entry path: "+entry['path']
            for node in entry['nodes']:
                print node[0]+" = "+node[1]
        else:
            print entry
    print
    
    print 'Get ipaddr of /etc/hosts 1st entry.'
    print c.confmgt_augeas.get('/etc/hosts/1','ipaddr')
    print
    #
    #print 'List all direct children parameters of sshd_config' 
    #for host,paramlist in c.confmgt_augeas.ls('/etc/ssh/sshd_config').iteritems():
    #   print "Host: "+host
    #   for node in paramlist['nodes']:
    #       print node[0]+" = "+node[1]
    #print
    #
    print 'List all children nodes of sshd_config'
    for host,paramlist in c.confmgt_augeas.printconf('/etc/ssh/sshd_config').iteritems():
       print "Host: "+host
       for node in paramlist['nodes']:
           print node[0]+" = "+node[1]
    print
    
    print 'List all direct children of AcceptEnv entries in sshd_config'
    for host,paramlist in c.confmgt_augeas.ls('/etc/ssh/sshd_config/AcceptEnv').iteritems():
        print "Host: "+host
        if type(paramlist)==type({}):
            for node in paramlist['nodes']:
                print node[0]+" = "+node[1]
        else:
            print paramlist
    print
    
    print 'See all AcceptEnv entries in sshd_config'
    for host,paramlist in c.confmgt_augeas.printconf('/etc/ssh/sshd_config/AcceptEnv').iteritems():
        print "Host: "+host
        if type(paramlist)==type({}):
            for node in paramlist['nodes']:
                print node[0]+" = "+node[1]
        else:
            print paramlist
    print
    
    print 'Try to match PermitRootLogin yes in sshd_config'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config','PermitRootLogin','yes')
    print
    
    print 'Try to match PermitRootLogin yes in sshd_config with an Augeas-style path'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config/PermitRootLogin','','yes')
    print
    
    print 'Try to match PermitRootLogin yes in some config. files.'
    print c.confmgt_augeas.match('/etc/*/*','PermitRootLogin','yes')
    print
    
    print 'Try to match AcceptEnv in sshd_config'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config','AcceptEnv')
    print
    
    print 'Try to match PermitRootLogin in sshd_config'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config','PermitRootLogin')
    print
    
    print 'Try to match PermitRootLogin in sshd_config with an Augeas-style path.'
    print c.confmgt_augeas.match('/etc/ssh/sshd_config/PermitRootLogin')
    print
    
    print 'Try to match canonical entries in hosts file.'
    print c.confmgt_augeas.match('/etc/hosts','canonical')
    print
    
    print 'Try to match canonical entries in hosts file with an Augeas-style path.'
    print c.confmgt_augeas.match('/etc/hosts/*/canonical')
    print
    
    print 'Augeas metainformation.'
    print c.confmgt_augeas.ls('/','/augeas')
    print c.confmgt_augeas.get('/','save','/augeas')
    
    #Not supposed to work:
    print c.confmgt_augeas.set('/','save','backup','/augeas')
    print c.confmgt_augeas.set('/save','','backup','/augeas')
    
    print c.confmgt_augeas.get('/save','','/augeas')
    print c.confmgt_augeas.get('/files/etc/hosts/lens','info','/augeas')
    
    
    
    print 'Add a new variable FOO at the end of the last AcceptEnv line of sshd_config'
    print "And we don't want to do this twice."
    foomatch=c.confmgt_augeas.match('/etc/ssh/sshd_config','AcceptEnv/*','FOO')
    for host,matchlist in foomatch.iteritems():
        if not matchlist:
            client = fc.Client(host)
            print client.confmgt_augeas.set('/etc/ssh/sshd_config/AcceptEnv[last()]','10000','FOO')
    print
    
    
    print 'Change the (canonical) hostname associated to a specific IP in hosts file.'
    hostfile='/etc/hosts'
    ip='1.2.3.4'
    newCanonical='fozzie'
    #newCanonical='piggy'
    # We search which entry in /etc/hosts refers to the IP
    ipmatch = c.confmgt_augeas.match(hostfile,'ipaddr',ip)
    # for each minion concerned 
    for host,entry in ipmatch.iteritems():
        # The first and unique entry in the list, entry[0], is what we searched for 
        # We check that the target canonical hostname is not already set
        if (type(entry) == type([]) and entry):
            oldCanonical=c.confmgt_augeas.get(entry[0],'canonical')[host]['value']
            if oldCanonical != newCanonical:
                print c.confmgt_augeas.set(entry[0],'canonical',newCanonical)
            else:
                print 'Nothing to do'
        else:
            print repr(entry)+' - no match'
    print

basictest()

# Extended path syntax
def extendedtest():
    print 'Tests on extended paths'
    # not working:
    print c.confmgt_augeas.match('//error/descendant-or-self::*','/augeas')
    print 
    
    print c.confmgt_augeas.get('/etc/hosts/*[ipaddr = "127.0.0.1"]/canonical')
    print c.confmgt_augeas.get('/etc/hosts/*[ipaddr = "127.0.0.1"]/','canonical')
    print

    print c.confmgt_augeas.get("/etc//ipaddr[. = '127.0.0.1']")
    print

    print c.confmgt_augeas.match('/etc/hosts/*/ipaddr')
    print c.confmgt_augeas.match('/etc/hosts/*/','ipaddr')

    # not working:
    #print c.confmgt_augeas.printconf('/etc/hosts/*/','ipaddr')
    #print

    print c.confmgt_augeas.match('/etc/pam.d/*[.//module="pam_limits.so"]')
    print 

    # not working (wrong):
    print c.confmgt_augeas.match('/etc//1')

    # not working (wrong):
    print c.confmgt_augeas.match('/descendant-or-self::4')

extendedtest()