Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features #151

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions server/core/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package core
import (
"context"
stderrors "errors"
"fmt"
"net/http"
"sort"

Expand Down Expand Up @@ -103,13 +102,13 @@ type BlockedAccounts struct {
Error *string `json:"error"`
}

// FilterCmd defines a struct for command filters with optional fields for Account and IP Address.
// FilterCmd defines a struct for command filters with optional fields for Accounts and IP Address.
type FilterCmd struct {
// Account represents the user account identifier used for filtering actions related to blocking.
Account string `json:"account,omitempty"`
// Accounts represents an optional filter criterion for user accounts in the FilterCmd struct.
Accounts []string `json:"accounts,omitempty"`

// IPAddress represents an optional filter criterion for IP addresses in the FilterCmd struct.
IPAddress string `json:"ip_address,omitempty"`
IPAddress []string `json:"ip_addresses,omitempty"`
}

// generic handles the generic authentication logic based on the selected service type.
Expand Down Expand Up @@ -223,7 +222,7 @@ func listBlockedIPAddresses(ctx context.Context, filterCmd *FilterCmd, guid stri

key := config.LoadableConfig.Server.Redis.Prefix + global.RedisBruteForceHashKey

resultList, err := rediscli.ReadHandle.HGetAll(ctx, key).Result()
ipAddresses, err := rediscli.ReadHandle.HGetAll(ctx, key).Result()
if err != nil {
if !stderrors.Is(err, redis.Nil) {
level.Error(log.Logger).Log(global.LogKeyGUID, guid, global.LogKeyError, err)
Expand All @@ -238,22 +237,27 @@ func listBlockedIPAddresses(ctx context.Context, filterCmd *FilterCmd, guid stri
stats.RedisReadCounter.Inc()
}
} else {
if filterCmd != nil && filterCmd.IPAddress != "" {
filteredIP := make(map[string]string)
if filterCmd != nil {
filteredIPs := make(map[string]string)

for network, bucket := range resultList {
if util.IsInNetwork([]string{network}, guid, filterCmd.IPAddress) {
filteredIP[network] = bucket

break
}
if len(filterCmd.IPAddress) == 0 {
ipAddresses = make(map[string]string)
}

resultList = filteredIP
for _, filterIPWanted := range filterCmd.IPAddress {
for network, bucket := range ipAddresses {
if util.IsInNetwork([]string{network}, guid, filterIPWanted) {
filteredIPs[network] = bucket

break
}
}

ipAddresses = filteredIPs
}
}

blockedIPAddresses.IPAddresses = resultList
blockedIPAddresses.IPAddresses = ipAddresses
blockedIPAddresses.Error = nil
}

Expand Down Expand Up @@ -282,25 +286,27 @@ func listBlockedAccounts(ctx context.Context, filterCmd *FilterCmd, guid string)

return blockedAccounts, err
} else {
if filterCmd != nil && filterCmd.Account != "" {
var account string
if filterCmd != nil {
var (
account string
filteredAccounts []string
)

for _, accountWanted := range filterCmd.Accounts {
for _, account = range accounts {
if account == accountWanted {
break
} else {
account = ""
}
}

for _, account = range accounts {
if account == filterCmd.Account {
break
} else {
account = ""
if account != "" {
filteredAccounts = append(filteredAccounts, account)
}
}

if account != "" {
accounts = []string{account}
} else {
errMsg := fmt.Sprintf("Account %s not found", filterCmd.Account)
blockedAccounts.Error = &errMsg

return blockedAccounts, nil
}
accounts = filteredAccounts
}

for _, account := range accounts {
Expand Down
Loading