Skip to content

Commit

Permalink
Change pfldapexplorer to take server from request instead of config
Browse files Browse the repository at this point in the history
Fix UT according to ldap back end changes
  • Loading branch information
VakarisZ committed Jun 23, 2023
1 parent 1f1af50 commit 60c05c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
41 changes: 11 additions & 30 deletions go/caddy/pfldapexplorer/pfldapexplorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"github.com/inverse-inc/packetfence/go/common/ldapSearchClient"
"github.com/inverse-inc/packetfence/go/connector"
"github.com/inverse-inc/packetfence/go/panichandler"
"github.com/inverse-inc/packetfence/go/pfconfigdriver"
"k8s.io/utils/strings/slices"
)

var ApiPrefix = "/api/v1"
Expand All @@ -35,6 +33,11 @@ type Handler struct {
connectors *connector.ConnectorsContainer
}

type SearchRequest struct {
Server ldapSearchClient.LdapServer `json:"server"`
SearchQuery ldapSearchClient.SearchQuery `json:"search_query"`
}

func init() {
caddy.RegisterPlugin("pfldapexplorer", caddy.Plugin{
ServerType: "http",
Expand Down Expand Up @@ -93,57 +96,35 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
return h.Next.ServeHTTP(w, r)
}

func getLdapServerFromConfig(ctx context.Context, serverId string) *ldapSearchClient.LdapServer {
var sections pfconfigdriver.PfconfigKeys
sections.PfconfigNS = "resource::authentication_sources_ldap"

pfconfigdriver.FetchDecodeSocket(ctx, &sections)
if slices.Contains(sections.Keys, serverId) {
var server ldapSearchClient.LdapServer
server.PfconfigNS = sections.PfconfigNS
server.PfconfigHashNS = serverId
pfconfigdriver.FetchDecodeSocket(ctx, &server)
return &server
} else {
return nil
}
}

func (h *Handler) HandleLDAPSearchRequest(res http.ResponseWriter, req *http.Request) {
var searchQuery = ldapSearchClient.SearchQuery{}
searchQuery.Context = connector.WithConnectorsContainer(req.Context(), h.connectors)
var searchRequest = SearchRequest{}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.LoggerWContext(*h.Ctx).Info(err.Error())
unifiedapierrors.Error(res, err.Error(), http.StatusBadRequest)
return
}

if err = json.Unmarshal(body, &searchQuery); err != nil {
if err = json.Unmarshal(body, &searchRequest); err != nil {
log.LoggerWContext(*h.Ctx).Info(err.Error())
unifiedapierrors.Error(res, err.Error(), http.StatusBadRequest)
return
}

ldapSearchServer := getLdapServerFromConfig(req.Context(), searchQuery.Server)
if ldapSearchServer == nil {
log.LoggerWContext(*h.Ctx).Info("Server " + searchQuery.Server + " not found")
unifiedapierrors.Error(res, "Server "+searchQuery.Server+" not found", http.StatusBadRequest)
return
}
searchRequest.SearchQuery.Context = connector.WithConnectorsContainer(req.Context(), h.connectors)

var factory ldapClient.ILdapClientFactory
if ldapSearchServer.UseConnector {
if searchRequest.Server.UseConnector {
factory = ldapClient.ProxyLdapClientFactory{}
} else {
factory = ldapClient.LdapClientFactory{}
}
ldapSearchClient := ldapSearchClient.LdapSearchClient{
LdapServer: ldapSearchServer,
LdapServer: &searchRequest.Server,
Timeout: serverConnectionTimeout,
LdapClientFactory: factory,
}
results, err := ldapSearchClient.SearchLdap(&searchQuery)
results, err := ldapSearchClient.SearchLdap(&searchRequest.SearchQuery)
if err != nil {
log.LoggerWContext(*h.Ctx).Info(err.Error())
unifiedapierrors.Error(res, err.Error(), http.StatusBadRequest)
Expand Down
4 changes: 4 additions & 0 deletions go/common/ldapSearchClient/LdapSearchClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type SearchQuery struct {
SizeLimit int `json:"size_limit"`
TimeLimit int `json:"time_limit"`
Attributes []string `json:"attributes,omitempty"`
// Server info

BindDN string `json:"bind_dn,omitempty"`
BindPassword string `json:"bind_password,omitempty"`
// TODO take a look at how this is used
Context context.Context `json:"context"`
}
Expand Down
2 changes: 1 addition & 1 deletion go/common/ldapSearchClient/ldapSearch_infra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *ConnectionSpy) Bind(username, password string) error {
return c.bindErr
}

func (c *ConnectionSpy) SearchWithPaging(searchRequest *ldap.SearchRequest, pagingSize uint32) (*ldap.SearchResult, error) {
func (c *ConnectionSpy) Search(searchRequest *ldap.SearchRequest) (*ldap.SearchResult, error) {
return &c.response, c.err
}

Expand Down

0 comments on commit 60c05c8

Please sign in to comment.