Added name~ regex match to infoblox with --infoblox-name-regex argument

This commit is contained in:
Craig Post 2023-02-10 13:29:53 -07:00
parent a0d5932d6b
commit fabc588750
No known key found for this signature in database
GPG Key ID: 5F3F118C67D4D278
4 changed files with 50 additions and 11 deletions

View File

@ -260,7 +260,8 @@ func main() {
View: cfg.InfobloxView, View: cfg.InfobloxView,
MaxResults: cfg.InfobloxMaxResults, MaxResults: cfg.InfobloxMaxResults,
DryRun: cfg.DryRun, DryRun: cfg.DryRun,
FQDNRexEx: cfg.InfobloxFQDNRegEx, FQDNRegEx: cfg.InfobloxFQDNRegEx,
NameRegEx: cfg.InfobloxNameRegEx,
CreatePTR: cfg.InfobloxCreatePTR, CreatePTR: cfg.InfobloxCreatePTR,
CacheDuration: cfg.InfobloxCacheDuration, CacheDuration: cfg.InfobloxCacheDuration,
}, },

View File

@ -123,6 +123,7 @@ type Config struct {
InfobloxView string InfobloxView string
InfobloxMaxResults int InfobloxMaxResults int
InfobloxFQDNRegEx string InfobloxFQDNRegEx string
InfobloxNameRegEx string
InfobloxCreatePTR bool InfobloxCreatePTR bool
InfobloxCacheDuration int InfobloxCacheDuration int
DynCustomerName string DynCustomerName string
@ -489,6 +490,7 @@ func (cfg *Config) ParseFlags(args []string) error {
app.Flag("infoblox-view", "DNS view (default: \"\")").Default(defaultConfig.InfobloxView).StringVar(&cfg.InfobloxView) app.Flag("infoblox-view", "DNS view (default: \"\")").Default(defaultConfig.InfobloxView).StringVar(&cfg.InfobloxView)
app.Flag("infoblox-max-results", "Add _max_results as query parameter to the URL on all API requests. The default is 0 which means _max_results is not set and the default of the server is used.").Default(strconv.Itoa(defaultConfig.InfobloxMaxResults)).IntVar(&cfg.InfobloxMaxResults) app.Flag("infoblox-max-results", "Add _max_results as query parameter to the URL on all API requests. The default is 0 which means _max_results is not set and the default of the server is used.").Default(strconv.Itoa(defaultConfig.InfobloxMaxResults)).IntVar(&cfg.InfobloxMaxResults)
app.Flag("infoblox-fqdn-regex", "Apply this regular expression as a filter for obtaining zone_auth objects. This is disabled by default.").Default(defaultConfig.InfobloxFQDNRegEx).StringVar(&cfg.InfobloxFQDNRegEx) app.Flag("infoblox-fqdn-regex", "Apply this regular expression as a filter for obtaining zone_auth objects. This is disabled by default.").Default(defaultConfig.InfobloxFQDNRegEx).StringVar(&cfg.InfobloxFQDNRegEx)
app.Flag("infoblox-name-regex", "Apply this regular expression as a filter on the name field for obtaining infoblox records. This is disabled by default.").Default(defaultConfig.InfobloxNameRegEx).StringVar(&cfg.InfobloxNameRegEx)
app.Flag("infoblox-create-ptr", "When using the Infoblox provider, create a ptr entry in addition to an entry").Default(strconv.FormatBool(defaultConfig.InfobloxCreatePTR)).BoolVar(&cfg.InfobloxCreatePTR) app.Flag("infoblox-create-ptr", "When using the Infoblox provider, create a ptr entry in addition to an entry").Default(strconv.FormatBool(defaultConfig.InfobloxCreatePTR)).BoolVar(&cfg.InfobloxCreatePTR)
app.Flag("infoblox-cache-duration", "When using the Infoblox provider, set the record TTL (0s to disable).").Default(strconv.Itoa(defaultConfig.InfobloxCacheDuration)).IntVar(&cfg.InfobloxCacheDuration) app.Flag("infoblox-cache-duration", "When using the Infoblox provider, set the record TTL (0s to disable).").Default(strconv.Itoa(defaultConfig.InfobloxCacheDuration)).IntVar(&cfg.InfobloxCacheDuration)
app.Flag("dyn-customer-name", "When using the Dyn provider, specify the Customer Name").Default("").StringVar(&cfg.DynCustomerName) app.Flag("dyn-customer-name", "When using the Dyn provider, specify the Customer Name").Default("").StringVar(&cfg.DynCustomerName)

View File

@ -58,7 +58,8 @@ type StartupConfig struct {
DryRun bool DryRun bool
View string View string
MaxResults int MaxResults int
FQDNRexEx string FQDNRegEx string
NameRegEx string
CreatePTR bool CreatePTR bool
CacheDuration int CacheDuration int
} }
@ -85,15 +86,17 @@ type infobloxRecordSet struct {
// additional query parameter on all get requests // additional query parameter on all get requests
type ExtendedRequestBuilder struct { type ExtendedRequestBuilder struct {
fqdnRegEx string fqdnRegEx string
nameRegEx string
maxResults int maxResults int
ibclient.WapiRequestBuilder ibclient.WapiRequestBuilder
} }
// NewExtendedRequestBuilder returns a ExtendedRequestBuilder which adds // NewExtendedRequestBuilder returns a ExtendedRequestBuilder which adds
// _max_results query parameter to all GET requests // _max_results query parameter to all GET requests
func NewExtendedRequestBuilder(maxResults int, fqdnRegEx string) *ExtendedRequestBuilder { func NewExtendedRequestBuilder(maxResults int, fqdnRegEx string, nameRegEx string) *ExtendedRequestBuilder {
return &ExtendedRequestBuilder{ return &ExtendedRequestBuilder{
fqdnRegEx: fqdnRegEx, fqdnRegEx: fqdnRegEx,
nameRegEx: nameRegEx,
maxResults: maxResults, maxResults: maxResults,
} }
} }
@ -107,10 +110,16 @@ func (mrb *ExtendedRequestBuilder) BuildRequest(t ibclient.RequestType, obj ibcl
if mrb.maxResults > 0 { if mrb.maxResults > 0 {
query.Set("_max_results", strconv.Itoa(mrb.maxResults)) query.Set("_max_results", strconv.Itoa(mrb.maxResults))
} }
_, ok := obj.(*ibclient.ZoneAuth) _, zoneAuthQuery := obj.(*ibclient.ZoneAuth)
if ok && t == ibclient.GET && mrb.fqdnRegEx != "" { if zoneAuthQuery && t == ibclient.GET && mrb.fqdnRegEx != "" {
query.Set("fqdn~", mrb.fqdnRegEx) query.Set("fqdn~", mrb.fqdnRegEx)
} }
// if we are not doing a ZoneAuth query, support the name filter
if !zoneAuthQuery && mrb.nameRegEx != "" {
query.Set("name~", mrb.nameRegEx)
}
req.URL.RawQuery = query.Encode() req.URL.RawQuery = query.Encode()
} }
return return
@ -142,9 +151,9 @@ func NewInfobloxProvider(ibStartupCfg StartupConfig) (*ProviderConfig, error) {
requestBuilder ibclient.HttpRequestBuilder requestBuilder ibclient.HttpRequestBuilder
err error err error
) )
if ibStartupCfg.MaxResults != 0 || ibStartupCfg.FQDNRexEx != "" { if ibStartupCfg.MaxResults != 0 || ibStartupCfg.FQDNRegEx != "" || ibStartupCfg.NameRegEx != "" {
// use our own HttpRequestBuilder which sets _max_results parameter on GET requests // use our own HttpRequestBuilder which sets _max_results parameter on GET requests
requestBuilder = NewExtendedRequestBuilder(ibStartupCfg.MaxResults, ibStartupCfg.FQDNRexEx) requestBuilder = NewExtendedRequestBuilder(ibStartupCfg.MaxResults, ibStartupCfg.FQDNRegEx, ibStartupCfg.NameRegEx)
} else { } else {
// use the default HttpRequestBuilder of the infoblox client // use the default HttpRequestBuilder of the infoblox client
requestBuilder, err = ibclient.NewWapiRequestBuilder(hostCfg, authCfg) requestBuilder, err = ibclient.NewWapiRequestBuilder(hostCfg, authCfg)
@ -166,7 +175,7 @@ func NewInfobloxProvider(ibStartupCfg StartupConfig) (*ProviderConfig, error) {
zoneIDFilter: ibStartupCfg.ZoneIDFilter, zoneIDFilter: ibStartupCfg.ZoneIDFilter,
dryRun: ibStartupCfg.DryRun, dryRun: ibStartupCfg.DryRun,
view: ibStartupCfg.View, view: ibStartupCfg.View,
fqdnRegEx: ibStartupCfg.FQDNRexEx, fqdnRegEx: ibStartupCfg.FQDNRegEx,
createPTR: ibStartupCfg.CreatePTR, createPTR: ibStartupCfg.CreatePTR,
cacheDuration: ibStartupCfg.CacheDuration, cacheDuration: ibStartupCfg.CacheDuration,
} }

View File

@ -698,7 +698,7 @@ func TestExtendedRequestFDQDRegExBuilder(t *testing.T) {
Password: "abcd", Password: "abcd",
} }
requestBuilder := NewExtendedRequestBuilder(0, "^staging.*test.com$") requestBuilder := NewExtendedRequestBuilder(0, "^staging.*test.com$", "")
requestBuilder.Init(hostCfg, authCfg) requestBuilder.Init(hostCfg, authCfg)
obj := ibclient.NewZoneAuth(ibclient.ZoneAuth{}) obj := ibclient.NewZoneAuth(ibclient.ZoneAuth{})
@ -712,6 +712,33 @@ func TestExtendedRequestFDQDRegExBuilder(t *testing.T) {
assert.True(t, req.URL.Query().Get("fqdn~") == "") assert.True(t, req.URL.Query().Get("fqdn~") == "")
} }
func TestExtendedRequestNameRegExBuilder(t *testing.T) {
hostCfg := ibclient.HostConfig{
Host: "localhost",
Port: "8080",
Version: "2.3.1",
}
authCfg := ibclient.AuthConfig{
Username: "user",
Password: "abcd",
}
requestBuilder := NewExtendedRequestBuilder(0, "", "^staging.*test.com$")
requestBuilder.Init(hostCfg, authCfg)
obj := ibclient.NewEmptyRecordCNAME()
req, _ := requestBuilder.BuildRequest(ibclient.GET, obj, "", &ibclient.QueryParams{})
assert.True(t, req.URL.Query().Get("name~") == "^staging.*test.com$")
req, _ = requestBuilder.BuildRequest(ibclient.CREATE, obj, "", &ibclient.QueryParams{})
assert.True(t, req.URL.Query().Get("name~") == "")
}
func TestExtendedRequestMaxResultsBuilder(t *testing.T) { func TestExtendedRequestMaxResultsBuilder(t *testing.T) {
hostCfg := ibclient.HostConfig{ hostCfg := ibclient.HostConfig{
Host: "localhost", Host: "localhost",
@ -724,7 +751,7 @@ func TestExtendedRequestMaxResultsBuilder(t *testing.T) {
Password: "abcd", Password: "abcd",
} }
requestBuilder := NewExtendedRequestBuilder(54321, "") requestBuilder := NewExtendedRequestBuilder(54321, "", "")
requestBuilder.Init(hostCfg, authCfg) requestBuilder.Init(hostCfg, authCfg)
obj := ibclient.NewEmptyRecordCNAME() obj := ibclient.NewEmptyRecordCNAME()
@ -743,7 +770,7 @@ func TestGetObject(t *testing.T) {
hostCfg := ibclient.HostConfig{} hostCfg := ibclient.HostConfig{}
authCfg := ibclient.AuthConfig{} authCfg := ibclient.AuthConfig{}
transportConfig := ibclient.TransportConfig{} transportConfig := ibclient.TransportConfig{}
requestBuilder := NewExtendedRequestBuilder(1000, "mysite.com") requestBuilder := NewExtendedRequestBuilder(1000, "mysite.com", "")
requestor := mockRequestor{} requestor := mockRequestor{}
client, _ := ibclient.NewConnector(hostCfg, authCfg, transportConfig, requestBuilder, &requestor) client, _ := ibclient.NewConnector(hostCfg, authCfg, transportConfig, requestBuilder, &requestor)