Marcus Janietz
Marcus Janietz

Reputation: 25

Parse hash for value from a table

I am writing a AWS-Federation proxy in Rails. This means I grab for some groups using net-ldap on our local ActiveDirectory and want to compare those to a list and look for matches. My NetLDAP-searchresult is this hash:

[#<Net::LDAP::Entry:0x000000048cfdd0 @myhash={:dn=>["CN=Username,OU=Support,OU=mycompany ,OU=Organisation,DC=mycompany,DC=com"], :memberof=>["CN=My AWS Groupname,CN=Receiver,CN=Users,DC=mycompany,DC=com"]}>] 

Now I want to parse this hash and look for matches in a local "groups" table. It looks like that:

Name               AWS-Role
My AWS-Groupname   Some Group
AWS-Othergroup     Some Other-Group

I have a group-model. What is a best practices approach? I've never done something like this before. Would I use a Regex here? Do I loop the groups through all tables? What's the rails way to do this?

edited for more information

Upvotes: 0

Views: 193

Answers (1)

Noel Frostpaw
Noel Frostpaw

Reputation: 3999

I'm going to assume a few things here, since I don't know where you get the LDAP search results from, but assuming your hash looks like this:

EDIT: based on the additional information:

// Example
require 'net-ldap'

entry = Net::LDAP::Entry.new
entry.dn = ["CN=Username,OU=Support,OU=mycompany ,OU=Organisation,DC=mycompany,DC=com"]
entry[:memberof] =["CN=My AWS Groupname,CN=Receiver,CN=Users,DC=mycompany,DC=com"]
name = entry.memberof.first.split(',').first.gsub('CN=', '')

And assuming you have a model called Group that is mapped to this "groups" table, you can do something like this:

Group.where(name: name).any?

If you find any results, it means you have a match in the table. But this completely depends on the table structure and hash. To properly answer your question, I'd need to see what Objects you have in Rails, and what the structure of your Hash looks like.

EDIT: Updated my answer based on the received feedback. Use code at own risk.

Upvotes: 1

Related Questions