CloudSlang / cs-content

Ready-Made CloudSlang Flows and Operations
http://www.cloudslang.io/
73 stars 180 forks source link

LDAP (search, add, update, some sample flows) #361

Open meirwah opened 9 years ago

sylvainblot commented 8 years ago

Hi, I was looking for a simple way to retrieve a DN attribute, I end up with that:

namespace: foobar.tools.ldap

operation:
  name: ldap_query

  inputs:
    - url
    - search_query
    - propertyName
    - username
    - password
  action:
    python_script: |
        from javax.naming import Context
        from javax.naming.directory import InitialDirContext

        from java.util import Hashtable

        settings = Hashtable()
        settings.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory")
        settings.put(Context.PROVIDER_URL, url)
        settings.put(Context.SECURITY_PRINCIPAL, username)
        settings.put(Context.SECURITY_CREDENTIALS, password)
        ctx = InitialDirContext(settings)

        attrs = ctx.getAttributes(search_query)
        ldap_result = str(attrs.get(propertyName).get())

        del settings
        del ctx
        del attrs

  outputs:
    - ldap_result: ldap_result
  results:
    - SUCCESS

For your information, I had to use javax classes because there is no working ldap python modules under Jython. Ldap3 has a method that is too long, and python-ldap uses C bindings.

Also I had to "del settings", It could be great to only extract variable from the python scope that are defined as output.

Regards

levice14 commented 8 years ago

hi @sylvainblot -- I agree that using del var seems strange at first, but keeping the user aware of what happens is a must

as you suggested, the solution could be to only publish certain variables, but cannot do that based on operation outputs since they can have any expression, not only - var: ${ var } (and the action should not depend on operation) -- if you have other suggestions, please let us know (you can also open an issue to discuss the problem)

sylvainblot commented 8 years ago

Hi @Bonczidai That make sense. At least It could be great to properly warn user about unserializable variables and document it.

cannatag commented 8 years ago

Hi, I'm the author of the ldap3 package. I don't use Jython, but ldap3 is pure Python code compatible tested again Python from version 2.6 up to version 3.5. What do you mean with "Ldap3 has a method that is too long"? There is such a limitation on method size in Jython? Which is the method involved? I would be happy to help, if I could.

Bye, Giovanni

sylvainblot commented 8 years ago

Hi @cannatag

The first error I'm facing is:

SEVERE: Command failed java.lang.RuntimeException: Slang Error : Error executing python script: Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/data/sylvain/csa_yaml/cslang/python-lib/ldap3/__init__.py", line 402, in <module>
    from .core.server import Server
  File "/data/sylvain/csa_yaml/cslang/python-lib/ldap3/core/server.py", line 35, in <module>
    from ..protocol.rfc4512 import SchemaInfo, DsaInfo
  File "/data/sylvain/csa_yaml/cslang/python-lib/ldap3/protocol/rfc4512.py", line 30, in <module>
    from .oid import CLASS_ABSTRACT, CLASS_STRUCTURAL, CLASS_AUXILIARY, ATTRIBUTE_USER_APPLICATION, \
java.lang.RuntimeException: Method code too large!
        at org.python.objectweb.asm.MethodWriter.a(Unknown Source)
        at org.python.objectweb.asm.ClassWriter.toByteArray(Unknown Source)
        at org.python.compiler.ClassFile.write(ClassFile.java:215)
        at org.python.compiler.Module.write(Module.java:620)
        at org.python.compiler.Module.compile(Module.java:676)
        at org.python.core.imp.compileSource(imp.java:370)
        at org.python.core.imp.createFromSource(imp.java:388)
        at org.python.core.imp.loadFromSource(imp.java:661)
        at org.python.core.imp.find_module(imp.java:543)
        at org.python.core.PyModule.impAttr(PyModule.java:106)
        at org.python.core.imp.import_next(imp.java:842)
        at org.python.core.imp.import_module_level(imp.java:959)
        at org.python.core.imp.importName(imp.java:1062)
        at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
        at org.python.core.PyObject.__call__(PyObject.java:431)
        at org.python.core.__builtin__.__import__(__builtin__.java:1232)
        at org.python.core.imp.importFromAs(imp.java:1156)
        at org.python.core.imp.importFrom(imp.java:1132)
        at ldap3.protocol.rfc4512$py.f$0(/data/sylvain/csa_yaml/cslang/python-lib/ldap3/protocol/rfc4512.py:778)
        at ldap3.protocol.rfc4512$py.call_function(/data/sylvain/csa_yaml/cslang/python-lib/ldap3/protocol/rfc4512.py)
        at org.python.core.PyTableCode.call(PyTableCode.java:167)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.imp.createFromCode(imp.java:436)
        at org.python.core.imp.createFromSource(imp.java:396)
        at org.python.core.imp.loadFromSource(imp.java:661)
        at org.python.core.imp.find_module(imp.java:543)
        at org.python.core.PyModule.impAttr(PyModule.java:106)
        at org.python.core.imp.import_next(imp.java:842)
        at org.python.core.imp.import_logic(imp.java:904)
        at org.python.core.imp.import_module_level(imp.java:978)
        at org.python.core.imp.importName(imp.java:1062)
        at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
        at org.python.core.PyObject.__call__(PyObject.java:431)
        at org.python.core.__builtin__.__import__(__builtin__.java:1232)
        at org.python.core.imp.importFromAs(imp.java:1156)
        at org.python.core.imp.importFrom(imp.java:1132)
        at ldap3.core.server$py.f$0(/data/sylvain/csa_yaml/cslang/python-lib/ldap3/core/server.py:51)
        at ldap3.core.server$py.call_function(/data/sylvain/csa_yaml/cslang/python-lib/ldap3/core/server.py)
        at org.python.core.PyTableCode.call(PyTableCode.java:167)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.imp.createFromCode(imp.java:436)
        at org.python.core.imp.createFromSource(imp.java:396)
        at org.python.core.imp.loadFromSource(imp.java:661)
        at org.python.core.imp.find_module(imp.java:543)
        at org.python.core.PyModule.impAttr(PyModule.java:106)
        at org.python.core.imp.import_next(imp.java:842)
        at org.python.core.imp.import_logic(imp.java:904)
        at org.python.core.imp.import_module_level(imp.java:978)
        at org.python.core.imp.importName(imp.java:1062)
        at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
        at org.python.core.PyObject.__call__(PyObject.java:431)
        at org.python.core.__builtin__.__import__(__builtin__.java:1232)
        at org.python.core.imp.importFromAs(imp.java:1156)
        at org.python.core.imp.importFrom(imp.java:1132)
        at ldap3$py.f$0(/data/sylvain/csa_yaml/cslang/python-lib/ldap3/__init__.py:424)
        at ldap3$py.call_function(/data/sylvain/csa_yaml/cslang/python-lib/ldap3/__init__.py)
        at org.python.core.PyTableCode.call(PyTableCode.java:167)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.imp.createFromCode(imp.java:436)
        at org.python.core.imp.createFromSource(imp.java:396)
        at org.python.core.imp.loadFromSource(imp.java:661)
        at org.python.core.imp.find_module(imp.java:543)
        at org.python.core.imp.import_next(imp.java:840)
        at org.python.core.imp.import_module_level(imp.java:959)
        at org.python.core.imp.importName(imp.java:1062)
        at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
        at org.python.core.PyObject.__call__(PyObject.java:431)
        at org.python.core.__builtin__.__import__(__builtin__.java:1232)
        at org.python.core.imp.importOne(imp.java:1081)
        at org.python.pycode._pyx114.f$0(<string>:2)
        at org.python.pycode._pyx114.call_function(<string>)
        at org.python.core.PyTableCode.call(PyTableCode.java:167)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.Py.runCode(Py.java:1386)
        at org.python.core.Py.exec(Py.java:1430)
        at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:267)
        at io.cloudslang.lang.runtime.steps.ActionSteps.executePythonScript(ActionSteps.java:301)
        at io.cloudslang.lang.runtime.steps.ActionSteps.runPythonAction(ActionSteps.java:257)
        at io.cloudslang.lang.runtime.steps.ActionSteps.prepareAndRunPythonAction(ActionSteps.java:246)
        at io.cloudslang.lang.runtime.steps.ActionSteps.doAction(ActionSteps.java:85)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56)
        at java.lang.reflect.Method.invoke(Method.java:620)
        at io.cloudslang.worker.execution.reflection.ReflectionAdapterImpl.executeControlAction(ReflectionAdapterImpl.java:62)
        at io.cloudslang.worker.execution.services.ExecutionServiceImpl.executeStep(ExecutionServiceImpl.java:326)
        at io.cloudslang.worker.execution.services.ExecutionServiceImpl.execute(ExecutionServiceImpl.java:80)
        at io.cloudslang.worker.management.services.SimpleExecutionRunnable.executeRegularStep(SimpleExecutionRunnable.java:159)
        at io.cloudslang.worker.management.services.SimpleExecutionRunnable.run(SimpleExecutionRunnable.java:119)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:483)
        at java.util.concurrent.FutureTask.run(FutureTask.java:274)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1157)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:627)
        at io.cloudslang.worker.management.services.WorkerThreadFactory$1.run(WorkerThreadFactory.java:33)
        at java.lang.Thread.run(Thread.java:809)

java.lang.RuntimeException: java.lang.RuntimeException: Method code too large!

and then if I remove all lines related to microsoft and novell I get:

SEVERE: Command failed java.lang.RuntimeException: Slang Error : Error executing python script: Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/data/sylvain/csa_yaml/cslang/python-lib/ldap3/__init__.py", line 403, in <module>
    from .core.connection import Connection
  File "/data/sylvain/csa_yaml/cslang/python-lib/ldap3/core/connection.py", line 37, in <module>
    from ..extend import ExtendedOperationsRoot
  File "/data/sylvain/csa_yaml/cslang/python-lib/ldap3/extend/__init__.py", line 38, in <module>
    from .standard.modifyPassword import ModifyPassword
  File "/data/sylvain/csa_yaml/cslang/python-lib/ldap3/extend/standard/modifyPassword.py", line 30, in <module>
    from ...protocol.sasl.sasl import validate_simple_password
  File "/data/sylvain/csa_yaml/cslang/python-lib/ldap3/protocol/sasl/sasl.py", line 26, in <module>
    import stringprep
ImportError: No module named stringprep

Seems like the issue is not going to be solved by the jython team: http://bugs.jython.org/issue1758320

King regards, Sylvain

pe-pan commented 7 years ago

Hi @sylvainblot, I'm trying the very same and I got to the very same issues (both you are describing). Apart from the "java code being called from python action" you showed above, is there any other workaround (how to execute LDAP operations from CS flow)? Thanks, PP

sylvainblot commented 7 years ago

Hi @pe-pan I ended up doing it in java instead of python_script, I can't provide you more informations, I'm not working on that topic anymore. Good luck!