jysk-oss / netbox-securecrt-inventory

NetBox SecureCRT Inventory sync
MIT License
5 stars 0 forks source link

Feature Request - Allow Greater Session Customization Based on Object Attributes #5

Open adparis99 opened 1 week ago

adparis99 commented 1 week ago

Hello there!

The idea of this feature request is somewhat two-fold:

Some sample use cases:

I imagine the end-user implementation in the config file would look something like this:

netbox_url: https://nb-prod.domain.com
netbox_token: 12345678
root_path: NetBox
session_options:
    connection_protocol: {{ 'RDP' if obj.type is virtual_machine else obj.cf.my_custom_field }} # <<< example logic
    credential: {{ }} # <<< more logic here
...

Let me know what you think.

patrickfnielsen commented 1 week ago

I like the idea, and actually have a use case in our team for connection_protocol changes as well. I'll have a look at how to tackle this in a fleksible way - your suggestion is definitely not bad!

patrickfnielsen commented 6 days ago

I have played around with a few things, and found the following to be pretty flexible (but breaking change in the config):

netbox_url: <url>e
netbox_token: <token>
root_path: NetBox
periodic_sync_enable: true
periodic_sync_interval: 120

session:
  path: "{tenant_name}/{region_name}/{site_name}/{device_role}"
  device_name: "{device_name}"
  session_options:
    connection_protocol: "{{ findTag(device.Tags, 'connection_protocol') ?? 'SSH' }}"
    credential: t0_panie

  overrides:
    - target: path
      condition: "{{ site_group == 'test' }}"
      value: _Test/{region_name}/{site_name}

    - target: path
      condition: "{{ type == 'virtual_machine' }}"
      value: _Servers/{region_name}

    - target: device_name
      condition: "{{ device_name endsWith '.1' }}"
      value: "{{ replace(device_name, '.1', '') }}"

Overrides have the following values: target: The target attribute to override, can be one of: path, device_name, description, credentials, connection_protocol condition: Any expression that returns true or false value: The value to apply, can also be an expression

Expressions use https://expr-lang.org/ - and they have access to the device, and site (and a number of predefined options that should be documented).

Do you think the above would fit the use-case you had in mind? If you look at the expression in connection_protocol you can see how I have used a tag to be able to override it, and default to SSH.

stranden commented 5 days ago

Hi Patrick,

Would it be possible to implement support for proxy/jumphost connections with this approach?

session:
  path: "{tenant_name}/{region_name}/{site_name}/{device_role}"
  device_name: "{device_name}"
  session_options:
    connection_protocol: "{{ findTag(device.Tags, 'connection_protocol') ?? 'SSH' }}"
    proxy: "{{ findTag(device.Tags, 'proxy') ?? 'None' }}"

I can create a separate issue for it if you think that is needed.

patrickfnielsen commented 5 days ago

@stranden that should be easy enough to add - though currently it would have to be the absolute path, so like NetBox/_Servers/Denmark/jumphost01.test.com or jumphost01.test.com if it is defined at the root outside of the netbox sync.

stranden commented 5 days ago

Sounds good @patrickfnielsen!

I think it would be possible to override the path for the jumphost, and then have the tag proxy: NetBox/jumphost01.test.com (which must match your override?)

netbox_url: <url>e
netbox_token: <token>
root_path: NetBox
periodic_sync_enable: true
periodic_sync_interval: 120

session:
  path: "{tenant_name}/{region_name}/{site_name}/{device_role}"
  device_name: "{device_name}"
  session_options:
    connection_protocol: "{{ findTag(device.Tags, 'connection_protocol') ?? 'SSH' }}"
    proxy: "{{ findTag(device.Tags, 'proxy') ?? 'None' }}"
    credential: t0_panie

  overrides:
    - target: path
      condition: "{{ device_name == 'jumphost01.test.com' }}"
      value: {device_name}

I think this is an awesome project! 👍

patrickfnielsen commented 3 days ago

My work in progress branch is up here: https://github.com/jysk-oss/netbox-securecrt-inventory/tree/session-customization - I hope to be able to cut a release sometime next week.