From b0fbcdeda51b01e325b1e51f79d4575d51f6b43a Mon Sep 17 00:00:00 2001 From: kgoins <14044530+kgoins@users.noreply.github.com> Date: Mon, 12 Apr 2021 18:55:26 -0400 Subject: [PATCH] added value_extractor --- go.mod | 3 ++- go.sum | 6 ++++-- pkg/value_extractor.go | 41 +++++++++++++++++++++++++++++++++++++ pkg/value_extractor_test.go | 17 +++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 pkg/value_extractor.go create mode 100644 pkg/value_extractor_test.go diff --git a/go.mod b/go.mod index 1184bc4..260a7dd 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/audibleblink/msldapuac v0.2.0 github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/icza/backscanner v0.0.0-20200205093934-2120fccb01f7 + github.com/kgoins/hashset v0.1.0 github.com/magiconair/properties v1.8.4 // indirect github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.3.3 // indirect @@ -17,7 +18,7 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.7.1 - github.com/stretchr/testify v1.6.1 + github.com/stretchr/testify v1.7.0 go.uber.org/zap v1.10.0 golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d // indirect gopkg.in/ini.v1 v1.61.0 // indirect diff --git a/go.sum b/go.sum index b589811..19618ee 100644 --- a/go.sum +++ b/go.sum @@ -114,6 +114,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kgoins/hashset v0.1.0 h1:fTE0PSDGSnpidJd7tT7tw/vdCCtOZ0OpTO6x53w2upg= +github.com/kgoins/hashset v0.1.0/go.mod h1:La39wQwoV2fuwcVhBbROEdyVY+/3lhlVlCb08jd+4Pc= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -206,8 +208,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= diff --git a/pkg/value_extractor.go b/pkg/value_extractor.go new file mode 100644 index 0000000..ade82f5 --- /dev/null +++ b/pkg/value_extractor.go @@ -0,0 +1,41 @@ +package ldsview + +import ( + hashset "github.com/kgoins/hashset/pkg" +) + +func GetValues(source *LdifParser, attrName string) ([]string, error) { + filterParts := []string{attrName} + filter := BuildAttributeFilter(filterParts) + source.SetAttributeFilter(filter) + + entities, done, cont := make(chan Entity), make(chan bool), make(chan bool) + + valSet := hashset.NewStrHashset() + go func(ents chan Entity, done chan bool, list *hashset.StrHashset) { + for entity := range ents { + if entity.IsEmpty() { + continue + } + + attr, found := entity.GetAttribute(attrName) + if !found { + continue + } + + valSet.Add(attr.Value.Values()...) + } + + done <- true //signals to BuildEntities we're done processing + cont <- true //signals to self we're done processing + + }(entities, done, &valSet) + + err := source.BuildEntities(entities, done) + if err != nil { + return nil, err + } + + <-cont // wait for dnList to be populated + return valSet.Values(), nil +} diff --git a/pkg/value_extractor_test.go b/pkg/value_extractor_test.go new file mode 100644 index 0000000..ba7e56e --- /dev/null +++ b/pkg/value_extractor_test.go @@ -0,0 +1,17 @@ +package ldsview + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValueExtractor_GetValues(t *testing.T) { + parser := NewLdifParser(TESTFILE) + + t.Run("parses the ldif objects correctly", func(t *testing.T) { + structure, err := GetValues(&parser, "cn") + assert.Nil(t, err) + assert.Greater(t, len(structure), 0) + }) +}