Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - ndejong

#1
After taking a look at what is required to register a global callback I don't believe it is possible to do from the context of a Plugin - which logically makes good sense.

That said, my own experience is with other frameworks and I'm not expert with Phalcon so there may be an approach buried away that is not clear, perhaps as a Dependency Injection but even then it would not seem possible without adding code to the core.

If anyone knows of an approach to attach a callback to the core from a plugin please do post herewith - while it does not seem like it should be possible it would be good to know if it is.
#2
Resolution on this has been that @AdSchellevis added the UUID to items via a `@uuid` parameter, thus enabling the ability to write as such:

{{item['@uuid']}}


Reference to the discussion on Github:-
https://github.com/opnsense/core/issues/2664#issuecomment-415675706

Thanks @AdSchellevis !!
#3
there is a return uuids statement missing at the end of my cut-n-paste - chat on the PR about it
#4
Pull request here:-
https://github.com/opnsense/core/pull/2665

In other news I need help de-tangling my commits, this PR includes previous commits - gack - will figure it out tomorrow I guess
#5
Ahh, thanks for helping me see though this - I missed where the that `server` variable was coming from - putting a PR together now for this then:-


    def getUUIDs(self, tag):
        """ retrieve a list of uuids from the item list returned by tag
        :param tag: tag in dot notation (section.item)
        :return: []
        """
        uuids = []
        for item in self.toList(tag):
            for uuid in self._template_in_data['__uuid__']:
                if self._template_in_data['__uuid__'][uuid] == item:
                    uuids.append(uuid)
        return uuids

#6
... because that means changing core code.

I'm not interested in calls made to `save()` by my plugin, I'm interested in creating an event callback to my plugin for all events (including legacy code) that happen to make a `save()`
#7
If I understand things correctly, the Nginx workaround approach looks dangerous for the same reason as I describe in the Issue, that is, you can very easily have a name collision among plugins.

As in the Nginx example, it would be pretty easy for another plugin to use a tag-name "server"
#8
.... actually, there is a subtle but important difference, that is being able to access the UUID - the `toList()` approach does not allow you to access the item UUID
#9
ahh - I just took this up as an issue in between this being posted - you guys are awesome.

https://github.com/opnsense/core/issues/2664

N
#10
I'm having a challenge in figuring out out to obtain the UUID of an item when rendering templates - by way of example I have this configuration structure:-

<VerbNetworks>
  <autossh version="0.1.0">
    <tunnels>
      <tunnel uuid="490829b9-f1b0-42ad-8dad-182de0c06afb">
        <user>myuser</name>
        <host>panic.verbnetworks.com</type>
      </tunnel>
    </tunnels>
  </autossh>
</VerbNetworks>


I note the template helper functions `getUUID()` and `getUUIDtag()` in `template_helpers.py` that take a UUID as an input parameter and return data associated with that UUID - this is not what I want despite the `getUUID()` function having a correct sounding name.

What (I think) I want to write is something like the following:-

{% for tunnel_uuid, tunnel_item in 'VerbNetworks.autossh.tunnels.tunnel'.iteritems() %}
[autossh_{{ tunnel_uuid }}]
{% endfor %}

It is not clear to me how to write or express the tag-name that gets passed to `.iteritems()` and thus the above code fails.

In a maybe-perfect world it might be nice if you could obtain a list of UUIDs for a tag-name using a helper function that I imagine might be called "getUUIDs()"

{% for tunnel_uuid in helpers.getUUIDs('VerbNetworks.autossh.tunnels.tunnel') %}
{% set tunnel = helpers.getUUID(tunnel_uuid) %}
Host {{ tunnel_uuid }}
  User {{ tunnel.user }}
  HostName {{ tunnel.host }}
{% endfor %}


Before I set about trying to write and introduce such a new helper function is there another way of achieving what I'm setting out to do here?
#11
Perhaps the question to my last post should be:-

Does anyone know if all functionality that writes to the config.xml file, especially legacy code functionality, all pass through the same save() function that is capable of attaching a Phalcon style callback to it?
#12
Hi Fabian

That makes sense

What I am suggesting would require new functionality at the core which in turn would create a major new dependency (aka Redis) on the OPNsense system - all of which sounds very resource heavy and is way beyond my pay-grade :)

What did not occur to me earlier is that Phalcon actually has a callback mechanism that allows you to register for callbacks on events:-
- https://docs.phalconphp.com/en/3.3/api/Phalcon_Events_Manager

With this, it seems like it might be possible to achieve what I was originally thinking of here without the heavyweight Redis approach.

A quick grep of both the opnsense-core and opnsense-plugins repos shows only one instance of the Phalcon `attach()` function being used `opnsense/mvc/app/config/services_api.php` so perhaps use of callbacks in OPNsense is not (yet) a common thing??

#13
Recently I found myself writing a whole monitoring daemon [#1] to watch for system changes which then called some function - in the back of my mind what I really wanted was some way to register myself for a callback action when other system actions occurred.

I also noticed recently that a plugin and package for Redis exists which might open up a reasonable mechanism to implement a system wide callback mechanism using Redis pubsub [#2]

My high level estimate is for something to be added to `ControllerBase.php` that extends the Phalcon `afterDispatch()` or `afterExecuteRoute()` functions that publish details of controller actions to Redis - this then would allow other components to subscribe to events published and then cause actions etc.

That said, I'm not sure this would entirely address my own use case because the event I'm looking for is anything that causes the configuration to change which can happen via legacy code that does not come down the MVC route - maybe there is another place to catch all `save()` calls somewhere which writes appropriate data to the Redis pubsub.

Also, it is not clear to me how much pressure Redis might put on the limited resources of lower end hardware and if this then has flow on effects on other functionality.

Links:-
[#1] - https://github.com/verbnetworks/opnsense-plugins/blob/master/net-mgmt/configsync/src/opnsense/scripts/VerbNetworks/ConfigSync/MonitorDaemon.py
[#2] - https://redis.io/topics/pubsub

#14
Urgh - this is embarrassing :(

Franco, thank you for taking the time in responding both here and the ticket, it is entirely my fault.

To help others hopefully not make the same mistake:-

- packages on the OPNsense host listed under System > Firmware > Packages are those packages that are currently installed, not those that "can be" installed as per the Plugins.  It's silly that I made this mistake, it seems so obvious but this is indeed what led me astray to begin with.

- packages listed under https://github.com/opnsense/tools/blob/master/config/[release]/ports.conf are not in alphabetical order, they are prefixed by their package-subpath.  I know looked here myself before placing the ticket but neglected to do a simple string search to find autossh further down the file.

Good news, there is nothing required to be done  :/

Thanks again Franco - will make up for it by making sure the promised Autossh plugin gets completed and submitted for review.

N
#15
Hi,

We are in the process of writing another OPNsense plugin that requires an additional standard FreeBSD distribution package, namely autossh.

Have done dirty hacks in the past by manually pulling in packages from the FreeBSD repo and installing manually when those packages are not available via the opnsense repo but this is surely not the right thing to be doing.

What is the correct way (if any) to add package dependencies to a plugin?  If anyone could point to other plugin examples that handle any part this that would be great.

Thanks in advance.

N