[PATCH] samba-tool: Easily edit a users object in AD

Alexander Bokovoy ab at samba.org
Mon Jul 3 15:07:49 UTC 2017


On ma, 03 heinä 2017, Rowland Penny via samba-technical wrote:
> On Mon, 3 Jul 2017 14:57:06 +0100
> Rowland Penny via samba-technical <samba-technical at lists.samba.org>
> wrote:
> 
> > On Mon, 3 Jul 2017 16:50:28 +0300
> > Alexander Bokovoy <ab at samba.org> wrote:
> > 
> > > On ma, 03 heinä 2017, Rowland Penny via samba-technical wrote:
> > > > On Sat, 1 Jul 2017 18:08:10 +0100
> > > > Rowland Penny via samba-technical
> > > > <samba-technical at lists.samba.org> wrote:
> > > > 
> > > > > On Sat, 1 Jul 2017 13:27:07 +1200
> > > > > Douglas Bagnall <douglas.bagnall at catalyst.net.nz> wrote:
> > > > > 
> > > > > > On 01/07/17 00:43, Rowland Penny via samba-technical wrote:
> > > > > > > On Wed, 28 Jun 2017 12:27:53 +0300
> > > > > > > Alexander Bokovoy <ab at samba.org> wrote:
> > > > > > > 
> > > > > > >> On ke, 28 kesä 2017, Andrew Bartlett via samba-technical
> > > > > > >> wrote:
> > > > > > 
> > > > > > >>> You replace the EDITOR with a script that modifies the
> > > > > > >>> object's LDIF.
> > > > > > >> Yes, something like
> > > > > > >> EDITOR="sed -i -e 's/attribute: foo/attribute: bar/'"
> > > > > > >>
> > > > > > > 
> > > > > > > OK, this doesn't work, but if I create a dummy editor
> > > > > > > script, it does.
> > > > > > 
> > > > > > Yes, that isn't going to work because you are calling the
> > > > > > editor using a list, exactly like so
> > > > > > 
> > > > > >        call([editor, t_file.name])
> > > > > > 
> > > > > > which is interpreted as an exec-style argument list, meaning
> > > > > > it will look for an editor called "sed -i -e ...", not one
> > > > > > called "sed".  In this case it is probably mostly safe to
> > > > > > change it to
> > > > > > 
> > > > > >        call("%s %s" % (editor, t_file.name), shell=True)
> > > > > > 
> > > > > > which will use a shell to split the string.
> > > > > > 
> > > > > > > This is what I came up with:
> > > > > > > 
> > > > > > > #!/bin/sh
> > > > > > > user_ldif="$1"
> > > > > > > SED=$(which sed)
> > > > > > > $SED -i -e \'s/userAccountControl: 512/userAccountControl:
> > > > > > > 514/\' $user_ldif
> > > > > > > 
> > > > > > > If I create the above script as /tmp/editor.sh, make it
> > > > > > > executable and then pass it to 'samba-tool user USER' with
> > > > > > > '--editor=/tmp/editor.sh' it works.
> > > > > > > 
> > > > > > > Problem is, how do I either get the python test script to
> > > > > > > create the bash script and then use it, or how do I create
> > > > > > > the bash script somewhere in the source (if so where?) and
> > > > > > > then get the python test script to use it ?
> > > > > > 
> > > > > > Why not use tempfile.NamedTemporaryFile() again? and
> > > > > > subprocess.call()?
> > > > > > 
> > > > > > Douglas
> > > > > > 
> > > > > > > 
> > > > > > > Rowland
> > > > > > > 
> > > > > > 
> > > > > 
> > > > > I have tried what Douglas suggested, but I am getting nowhere, I
> > > > > do not want to alter the 'samba-tool user edit', I just want to
> > > > > get a python test script to run it and hopefully pass.
> > > > > 
> > > > > edit.py uses an editor and a human intervention to work, I can
> > > > > automate this with a simple bash script. What I cannot do is to
> > > > > get a python test script to fully automate this.
> > > > > 
> > > > > I think if I was to create the bash script in the source code it
> > > > > would work, but where would be the best place to put it ? and
> > > > > how would I obtain the path to it ?
> > > > > 
> > > > > Rowland
> > > > >   
> > > > > 
> > > > 
> > > > I couldn't get the python script to work, so I dumped it, in
> > > > favour of a bash script and this seems to work, but I don't fully
> > > > understand the output, here is a sample:
> > > > 
> > > > testsuite: samba.tests.samba_tool.edit(fl2008r2dc)
> > > > progress: push
> > > > time: 2017-07-03 13:27:14.000000Z
> > > > User 'sambatool1' created successfully
> > > > Modified User 'sambatool1' successfully
> > > > Deleted user sambatool1
> > > > teardown_env(fl2008r2dc)
> > > > samba: EOF on stdin - PID 2302 terminating
> > > > time: 2017-07-03 13:27:14.000000Z
> > > > progress: pop
> > > > command: /root/samba-master/python/samba/tests/samba_tool/edit.sh
> > > > $SERVER $USERNAME $PASSWORD 2>&1
> > > > | /root/samba-master/selftest/filter-subunit --fail-on-empty
> > > > --prefix="samba.tests.samba_tool.edit." --suffix="(fl2008r2dc)"
> > > > expanded
> > > > command: /root/samba-master/python/samba/tests/samba_tool/edit.sh
> > > > dc7 Administrator locDCpass7 2>&1
> > > > | /root/samba-master/selftest/filter-subunit --fail-on-empty
> > > > --prefix="samba.tests.samba_tool.edit." --suffix="(fl2008r2dc)"
> > > > testsuite-failure: samba.tests.samba_tool.edit(fl2008r2dc) [ Exit
> > > > code was 1
> > > > 
> > > > ]
> > > > 
> > > > samba child process 2302 exited with value 0
> > > > samba: EOF on stdin - PID 2234 terminating
> > > > SAMBA LOG of: DC7 pid 2302
> > > > samba: EOF on stdin - PID 2302 terminating
> > > > TOP 10 slowest tests
> > > > samba.tests.samba_tool.edit(fl2008r2dc) -> 0
> > > > 'testonly' finished successfully (29.037s)
> > > > 
> > > > The script creates the user, modifies the user and then deletes
> > > > the user, the script then exits with the exit code '0', but there
> > > > is this:
> > > > 
> > > > testsuite-failure: samba.tests.samba_tool.edit(fl2008r2dc) [
> > > > Exit code was 1
> > > > 
> > > > ]
> > > > 
> > > > Is this something to worry about ?
> > > > Where does the '1' exit code come from ?
> > > I think the script 'edit.sh' needs to return its output in a format
> > > expected by selftest/filter-subunit which is a normal subunit
> > > format.
> > > 
> > > source3/script/tests/test_preserve_case.sh is one of examples on how
> > > we do that in shell.
> > > 
> > 
> > Thanks, that helps a lot ;-)
> > 
> > Just need to rewrite my bash script using that as a template.
> > 
> > Rowland
> > 
> > 
> 
> OK, does this mean what I think it does:
> 
> testsuite: samba.tests.samba_tool.edit(fl2008r2dc)
> progress: push
> time: 2017-07-03 15:47:59.000000Z
> time: 2017-07-03 14:47:59.339430Z
> test: samba.tests.samba_tool.edit.Create_User(fl2008r2dc)
> time: 2017-07-03 14:47:59.619598Z
> successful: samba.tests.samba_tool.edit.Create_User(fl2008r2dc)
> time: 2017-07-03 14:47:59.629681Z
> test: samba.tests.samba_tool.edit.Edit_User(fl2008r2dc)
> time: 2017-07-03 14:47:59.817088Z
> successful: samba.tests.samba_tool.edit.Edit_User(fl2008r2dc)
> time: 2017-07-03 14:47:59.825323Z
> test: samba.tests.samba_tool.edit.Delete_User(fl2008r2dc)
> time: 2017-07-03 14:48:00.009445Z
> teardown_env(fl2008r2dc)
> samba: EOF on stdin - PID 4836 terminating
> successful: samba.tests.samba_tool.edit.Delete_User(fl2008r2dc)
> time: 2017-07-03 15:48:00.000000Z
> progress: pop
> command: /root/samba-master/python/samba/tests/samba_tool/edit.sh $SERVER $USERNAME $PASSWORD 2>&1  | /root/samba-master/selftest/filter-subunit --fail-on-empty --prefix="samba.tests.samba_tool.edit." --suffix="(fl2008r2dc)"
> expanded command: /root/samba-master/python/samba/tests/samba_tool/edit.sh dc7 Administrator locDCpass7 2>&1  | /root/samba-master/selftest/filter-subunit --fail-on-empty --prefix="samba.tests.samba_tool.edit." --suffix="(fl2008r2dc)"
> testsuite-success: samba.tests.samba_tool.edit(fl2008r2dc)
> 
> samba child process 4836 exited with value 0
> samba: EOF on stdin - PID 4767 terminating
> SAMBA LOG of: DC7 pid 4836
> samba: EOF on stdin - PID 4836 terminating
> TOP 10 slowest tests
> samba.tests.samba_tool.edit(fl2008r2dc) -> 1
> 'testonly' finished successfully (29.323s)
> 
> If it does, I will produce new patches ;-)
Looks like it works! Great!
-- 
/ Alexander Bokovoy



More information about the samba-technical mailing list