Hi :wave: How do I import a local module in a Lua ...
# help
t
Hi đź‘‹ How do I import a local module in a Lua script used for a LakeFS hook? So I declared a lakefs hook as follows:
Copy code
name: test_action
...
hooks:
 - id: test_hook
   type: lua
   properties:
     script_path: _scripts/test_script.lua
and I placed my Lua script under the
_script/
prefix in my repo, and also added another module
mymodule.lua
under that same prefix:
Copy code
_scripts/
  test_script.lua
  mymodule.lua
and I want to import and use
mymodule
in
test_script.lua
and I tried a few things but I keep getting “module not found” errors. I’m not very familiar with Lua so I’m not sure if this is a Lua programming thing or if it’s related to where the scripts are stored and organized in Lakefs server. Any help with this is greatly appreciated
i
Hi Taha, that’s a great question. Not sure if I’ve seen an example of Lua module sharing in lakeFS. Let me find out and update this thread.
🙏 1
o
Good question indeed! Unfortunately, this is currently not supported. We intentionally disable any external IO to be made by the executing script. The reasoning is security - this increases the surface area of what a Lua script could do. In the case of module imports, it means validating the entire dependency graph for read permissions: The executing hook would need to be able to read the loaded module, and any module loaded by that loaded module.. while possible, it adds complexity and requires through testing so wasn't prioritized until now. Could you elaborate a bit about the use case?
t
I see! the use case is that I’m implementing multiple Lakefs Actions and Hooks that share some of the same functionality. So packaging that common functionality in a separate shared module for code-reuse will improve maintainability compared to copy pasting the same logic in multiple places.
What if we require the users to explicitly define the dependencies when declaring the Action. Something like:
Copy code
name: test_action
...
hooks:
 - id: test_hook
   type: lua
   properties:
     script_path: _scripts/test_script.lua
     script_dependencies:
       - _script/mymodule.lua
and then only allow importing from those declared dependencies. I’m wondering if that would reduce the security risk and simplify managing permissions?
o
Possibly! definitely worth looking into such an approach. perhaps something similar to python's
sys.path
could be adapted to this.
An approach that I could suggest for now, (which is not the most elegant) is a big "mono-file" including all the hooks. You can then pass a
properties.args.run
flag that accepts a name of a function to run. This has the benefit of only having to maintain one copy of shared code, but is probably not as easy to maintain as proper modules. Here's an example of what that might look like:
Copy code
name: Execute Things
description: do things
on:
  post-commit:
hooks:
  - id: do_stuff
    type: lua
    properties:
      script_path: scripts/run_stuff.lua
      args:
        run: this_function
With the script simply executing the passed function:
Copy code
function this_function()
    print("doing this!!")
  end
  
  function that_function()
    print("doing that!!")
  end
  
  
  actions = {
    ["this_function"] = this_function,
    ["that_function"] = that_function,
  }
  
  actions[args.run]()
Would that be helpful as an interim solution?
t
that’s an interesting idea! I worry that we’ll be mixing too many concerns in the same big file. But I think it’s definitely worth considering, especially if the different hooks share a significant amount of logic.