Skip to content

Commit

Permalink
lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
morde08 committed Oct 18, 2024
1 parent 3b51a0f commit ccd05c1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 62 deletions.
64 changes: 32 additions & 32 deletions cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,38 +289,38 @@ func fetchZones() []cloudflare.Zone {
return z
}

func fetchFirewallRules(zoneID string) map[string]string {
ctx := context.Background()
listOfRules, _, err := cloudflareAPI.FirewallRules(ctx,
cloudflare.ZoneIdentifier(zoneID),
cloudflare.FirewallRuleListParams{})
if err != nil {
log.Fatalf("Error fetching firewall rules: %s", err)
}
firewallRulesMap := make(map[string]string)

for _, rule := range listOfRules {
firewallRulesMap[rule.ID] = rule.Description
}

listOfRulesets, err := cloudflareAPI.ListRulesets(ctx, cloudflare.ZoneIdentifier(zoneID), cloudflare.ListRulesetsParams{})
if err != nil {
log.Fatalf("Error listing rulesets: %s", err)
}
for _, rulesetDesc := range listOfRulesets {
if rulesetDesc.Phase == "http_request_firewall_managed" {
ruleset, err := cloudflareAPI.GetRuleset(ctx, cloudflare.ZoneIdentifier(zoneID), rulesetDesc.ID)
if err != nil {
log.Fatalf("Error fetching ruleset: %s", err)
}
for _, rule := range ruleset.Rules {
firewallRulesMap[rule.ID] = rule.Description
}
}
}

return firewallRulesMap
}
//func fetchFirewallRules(zoneID string) map[string]string {
// ctx := context.Background()
// listOfRules, _, err := cloudflareAPI.FirewallRules(ctx,
// cloudflare.ZoneIdentifier(zoneID),
// cloudflare.FirewallRuleListParams{})
// if err != nil {
// log.Fatalf("Error fetching firewall rules: %s", err)
// }
// firewallRulesMap := make(map[string]string)
//
// for _, rule := range listOfRules {
// firewallRulesMap[rule.ID] = rule.Description
// }
//
// listOfRulesets, err := cloudflareAPI.ListRulesets(ctx, cloudflare.ZoneIdentifier(zoneID), cloudflare.ListRulesetsParams{})
// if err != nil {
// log.Fatalf("Error listing rulesets: %s", err)
// }
// for _, rulesetDesc := range listOfRulesets {
// if rulesetDesc.Phase == "http_request_firewall_managed" {
// ruleset, err := cloudflareAPI.GetRuleset(ctx, cloudflare.ZoneIdentifier(zoneID), rulesetDesc.ID)
// if err != nil {
// log.Fatalf("Error fetching ruleset: %s", err)
// }
// for _, rule := range ruleset.Rules {
// firewallRulesMap[rule.ID] = rule.Description
// }
// }
// }
//
// return firewallRulesMap
//}

func fetchAccounts() []cloudflare.Account {
ctx := context.Background()
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func runExporter() {

if len(viper.GetString("cf_api_token")) > 0 {
cloudflareAPI, err = cloudflare.NewWithAPIToken(viper.GetString("cf_api_token"))

} else {
cloudflareAPI, err = cloudflare.New(viper.GetString("cf_api_key"), viper.GetString("cf_api_email"))
}
Expand Down
57 changes: 28 additions & 29 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ func mustRegisterMetrics(deniedMetrics MetricsSet) {
if !deniedMetrics.Has(r2ObjectCountMetricName) {
prometheus.MustRegister(r2ObjectCount)
}

}

func fetchWorkerAnalytics(account cloudflare.Account, wg *sync.WaitGroup) {
Expand Down Expand Up @@ -664,34 +663,34 @@ func addHTTPGroups(z *zoneResp, name string, account string) {
zoneUniquesTotal.With(prometheus.Labels{"zone": name, "account": account}).Add(float64(zt.Unique.Uniques))
}

func addFirewallGroups(z *zoneResp, name string, account string) {
// Nothing to do.
if len(z.FirewallEventsAdaptiveGroups) == 0 {
return
}
rulesMap := fetchFirewallRules(z.ZoneTag)
for _, g := range z.FirewallEventsAdaptiveGroups {
zoneFirewallEventsCount.With(
prometheus.Labels{
"zone": name,
"account": account,
"action": g.Dimensions.Action,
"source": g.Dimensions.Source,
"rule": normalizeRuleName(rulesMap[g.Dimensions.RuleID]),
"host": g.Dimensions.ClientRequestHTTPHost,
"country": g.Dimensions.ClientCountryName,
}).Add(float64(g.Count))
}
}

func normalizeRuleName(initialText string) string {
maxLength := 200
nonSpaceName := strings.ReplaceAll(strings.ToLower(initialText), " ", "_")
if len(nonSpaceName) > maxLength {
return nonSpaceName[:maxLength]
}
return nonSpaceName
}
//func addFirewallGroups(z *zoneResp, name string, account string) {
// // Nothing to do.
// if len(z.FirewallEventsAdaptiveGroups) == 0 {
// return
// }
// rulesMap := fetchFirewallRules(z.ZoneTag)
// for _, g := range z.FirewallEventsAdaptiveGroups {
// zoneFirewallEventsCount.With(
// prometheus.Labels{
// "zone": name,
// "account": account,
// "action": g.Dimensions.Action,
// "source": g.Dimensions.Source,
// "rule": normalizeRuleName(rulesMap[g.Dimensions.RuleID]),
// "host": g.Dimensions.ClientRequestHTTPHost,
// "country": g.Dimensions.ClientCountryName,
// }).Add(float64(g.Count))
// }
//}

//func normalizeRuleName(initialText string) string {
// maxLength := 200
// nonSpaceName := strings.ReplaceAll(strings.ToLower(initialText), " ", "_")
// if len(nonSpaceName) > maxLength {
// return nonSpaceName[:maxLength]
// }
// return nonSpaceName
//}

func addHealthCheckGroups(z *zoneResp, name string, account string) {
if len(z.HealthCheckEventsAdaptiveGroups) == 0 {
Expand Down

0 comments on commit ccd05c1

Please sign in to comment.