dodevops / terraform-provider-ldap

Terraform provider to manage an LDAP directory
https://registry.terraform.io/providers/dodevops/ldap/latest
MIT License
3 stars 4 forks source link

Feature request: Filters for `ldap_object` data source #6

Closed zanecodes closed 1 year ago

zanecodes commented 1 year ago

In addition to my use case in https://github.com/dodevops/terraform-provider-ldap/issues/5, I'd like to be able to search for a service account in Microsoft Active Directory by its servicePrincipalName attribute, since it must match the Vault server's hostname in order for Kerberos SPNEGO authentication to work correctly.

Several other LDAP providers support searching functionality, but have other downsides:

With that in mind, I would like to add the following optional attributes to the ldap_object data source:

This would be backwards compatible with the existing interface, as specifying only the dn attribute would behave the same as it does now, while specifying any of base_dn, scope, and filter would allow the user more flexibility.

I have a branch on my fork implementing this feature and would love to open a pull request and iterate on it.

dploeger commented 1 year ago

It's a great idea to have that. I'd suggest to implement this feature in a second DataSource (e.g. ldap_search) to separate getting one object instead of multiple objects.

zanecodes commented 1 year ago

That sounds like a good idea, I'll see what I can come up with! For my use case, I'm not concerned with searching for multiple objects, but it does seem like a valuable scenario to support.

I've half considered implementing the filter attribute as a Map of attribute/value pairs like trevex/ldap does, which would be transformed into an ANDed filter string, e.g.

ldap_object {
  base_dn = "dc=example,dc=com"
  scope = "wholeSubtree"
  filter = {
    uid = "example"
    memberOf = "cn=users,dc=example,dc=com"
  }
}

would produce the filter (&(uid=example)(memberOf=cn=users,dc=example,dc=com)).

However, this would substantially reduce the flexibility of the data source by limiting the filters that can be used, without (in my opinion) providing a significant benefit for users, since incorrectly constructed filters would produce an error during Terraform's plan phase because this is a data source.

dploeger commented 1 year ago

I'd say, keep it simple. Just use a string as a filter and you have the full flexibility.

gthieleb commented 1 year ago

I like the idea of having a search functionality instead of hardcoding the object that should be linked. In another provider I have seen that they make use of scope property to limit a nested search in the AD. Would you think to make this part of the search string?

dploeger commented 1 year ago

No, the scope should be a parameter as suggested. I just wouldn't implement structured fields that would build a search string.

dploeger commented 1 year ago

Oh. I just saw that I haven't reviewed the linked PR. Will do so tomorrow. Sorry for the wait.

dploeger commented 1 year ago

Released in 0.3.0