Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a "default" mount_point #674

Open
johnnybubonic opened this issue Mar 6, 2021 · 7 comments
Open

Set a "default" mount_point #674

johnnybubonic opened this issue Mar 6, 2021 · 7 comments
Assignees
Labels
enhancement a new feature or addition

Comments

@johnnybubonic
Copy link

When one is using an instance of hvac.Client primarily with multiple operations on a single/specific mountpoint, it can become verbose/tedious to specify this same mount_point multiple times. (Think multiple operations of listing and updating secrets across multiple paths within the same mount, for instance.)

Would it be a lot of work to have an attribute on the instance that specifies the default mount_point? The way I see this potentially being the most user-friendly and realistic is being able to instantiate an appropriate hvac.api.VaultApiBase subclass (such as hvac.api.secrets_engines.kv_v2.KvV2) with a __init__(self, mount_point = None). If mount_point is None, it'd use the DEFAULT_MOUNT_POINT value in each respective subclass (with 'mount_point' in each method still being able to override this and falling back to the class' attribute instead of the file-global DEFAULT_MOUNT_POINT).

Thoughts? Am I describing this clearly? I feel like I'm not, heh.

@johnnybubonic johnnybubonic changed the title ENHANCEMENT: Set a ""default" mount_point ENHANCEMENT: Set a "default" mount_point Mar 6, 2021
@jeffwecan
Copy link
Member

Yeah that makes plenty of sense and I imagine wouldn't be too terribly difficult to implement. 👍🏻

@jeffwecan jeffwecan added the enhancement a new feature or addition label Mar 8, 2021
@jeffwecan jeffwecan self-assigned this Mar 8, 2021
@iMisteg
Copy link

iMisteg commented May 25, 2021

There is no ready-made solution yet, can someone tell me how the functionality of setting the default value can be implemented on the current version of the code? I tried like this

from hvac.api.secrets_engines.kv_v2 import KvV2
KvV2.DEFAULT_MOUNT_POINT = 'kv'

But it doesn't work

@johnnybubonic
Copy link
Author

johnnybubonic commented May 26, 2021

@iMisteg This is why that won't work: https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

Namely, when the module (or submodule, in this case - the class, specifically) is imported, the method's definition is read in and held in memory. This includes whatever the value is of the method's parameters' defaults.

In other words, as soon as you import KvV2, it reads the method definitions and, in the process of parsing those definitions, it reads whatever DEFAULT_MOUNT_POINT is at that time. In KvV2's instance, this is secret. It does not matter if you change DEFAULT_MOUNT_POINT after importing, as it's already been resolved to a static value on import.

@jeffwecan There are two ways to fix this. Either you can make the default mount point an instance's attribute:

DEFAULT_MOUNT_POINT = 'secret'

# ...

class Foo(object):
    def __init__(self, mount_point = DEFAULT_MOUNT_POINT):
        self.mount_point = mount_point
# ...
    def bar(self):
        do_something_here(self.mount_point)

and Foo() would then need to be instantiated (baz = Foo(mount_point = 'something_else')).

OR

You can implement it on each method like so:

DEFAULT_MOUNT_POINT = 'secret'

# ...

class Foo(object):
    def __init__(self):
# ...
    def bar(self, mount_point = None):
        if not mount_point:
            mount_point = DEFAULT_MOUNT_POINT
        do_something_here(mount_point)

But it MUST be None for this to work (as I mentioned in my initial comment).

EDIT:

You can, of course, hybridize the two as well:

DEFAULT_MOUNT_POINT = 'secret'

# ...

class Foo(object):
    def __init__(self, mount_point = DEFAULT_MOUNT_POINT):
        self.mount_point = mount_point
# ...
    def bar(self, mount_point = None):
        if not mount_point:
            mount_point = self.mount_point
        do_something_here(mount_point)

This would provide the maximum flexibility, but is quite a bit extraneous.

@johnnybubonic
Copy link
Author

(The second (None default per-method) or third (hybrid) designs above would retain backwards compatibility.)

@eytanhanig
Copy link

Any progress on this? It's a real quality of life improvement.

@Stogas
Copy link

Stogas commented Feb 6, 2023

Agree, this is a very useful enhancement.

@mddataminr
Copy link

Paying customer here - This would be great to have!

@briantist briantist changed the title ENHANCEMENT: Set a "default" mount_point Set a "default" mount_point Jul 16, 2023
@briantist briantist self-assigned this Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement a new feature or addition
Projects
None yet
Development

No branches or pull requests

7 participants