-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
67 lines (57 loc) · 1.86 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package client
import (
"context"
"crypto/tls"
"fmt"
"github.com/cloudquery/plugin-sdk/plugins/source"
"github.com/cloudquery/plugin-sdk/schema"
"github.com/cloudquery/plugin-sdk/specs"
"github.com/dihedron/cq-plugin-utils/format"
"github.com/go-ldap/ldap/v3"
"github.com/rs/zerolog"
)
type Client struct {
Logger zerolog.Logger
Specs *Spec
Client *ldap.Conn
}
func (c *Client) ID() string {
return "github.com/dihedron/cq-source-ldap"
}
func New(ctx context.Context, logger zerolog.Logger, s specs.Source, opts source.Options) (schema.ClientMeta, error) {
var pluginSpec Spec
if err := s.UnmarshalSpec(&pluginSpec); err != nil {
return nil, fmt.Errorf("failed to unmarshal plugin spec: %w", err)
}
logger.Debug().Str("spec", format.ToJSON(pluginSpec)).Msg("plugin spec")
logger.Debug().Str("endpoint", pluginSpec.Endpoint).Msg("connecting to LDAP server...")
var dialOpts []ldap.DialOpt
if pluginSpec.SkipTLS {
dialOpts = []ldap.DialOpt{
ldap.DialWithTLSConfig(
&tls.Config{
InsecureSkipVerify: true,
},
),
}
}
client, err := ldap.DialURL(pluginSpec.Endpoint, dialOpts...)
if err != nil {
logger.Error().Err(err).Str("endpoint", pluginSpec.Endpoint).Msg("error connecting to LDAP server")
return nil, err
}
logger.Info().Str("endpoint", pluginSpec.Endpoint).Msg("connected to LDAP server")
logger.Debug().Str("username", pluginSpec.Username).Msg("binding to LDAP server...")
err = client.Bind(pluginSpec.Username, pluginSpec.Password)
if err != nil {
logger.Error().Err(err).Str("endpoint", pluginSpec.Endpoint).Str("username", pluginSpec.Username).Msg("error binding to LDAP server")
defer client.Close()
return nil, err
}
logger.Info().Str("endpoint", pluginSpec.Endpoint).Str("username", pluginSpec.Username).Msg("bound to LDAP server")
return &Client{
Logger: logger,
Specs: &pluginSpec,
Client: client,
}, nil
}