Skip to content

Commit b4df85f

Browse files
authored
Merge pull request #5 from klouddb/unused_hba_lines
added hba unused line handling in logparser
2 parents fab674b + 2496cee commit b4df85f

File tree

22 files changed

+1466
-26
lines changed

22 files changed

+1466
-26
lines changed

.github/workflows/integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ jobs:
7070
integrationtest setup --prefix "${{ matrix.log_prefix }}"
7171
sudo chmod -R 777 pglog/log0
7272
integrationtest test -p "${{ matrix.log_prefix }}" -f "pglog/log0/postgresql*.log"
73-
docker-compose down -v
73+
docker compose down -v

cmd/ciscollector/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/klouddb/klouddbshield/passwordmanager"
2323
"github.com/klouddb/klouddbshield/pkg/config"
2424
cons "github.com/klouddb/klouddbshield/pkg/const"
25+
"github.com/klouddb/klouddbshield/pkg/hbarules"
2526
"github.com/klouddb/klouddbshield/pkg/logger"
2627
"github.com/klouddb/klouddbshield/pkg/mysqldb"
2728
"github.com/klouddb/klouddbshield/pkg/parselog"
@@ -110,6 +111,8 @@ func main() {
110111
runInactiveUSersLogParser(ctx, cnf, store)
111112
// case cons.LogParserCMD_MismatchIPs:
112113
// runMismatchIPsLogParser(ctx, cnf)
114+
case cons.LogParserCMD_HBAUnusedLines:
115+
runHBAUnusedLinesLogParser(ctx, cnf, store)
113116
default:
114117
fmt.Println("Invalid command for log parser")
115118
os.Exit(1)
@@ -146,6 +149,62 @@ func updatePgSettings(ctx context.Context, store *sql.DB, pgSettings *model.PgSe
146149
pgSettings.LogConnections = ps.LogConnections
147150
}
148151

152+
func runHBAUnusedLinesLogParser(ctx context.Context, cnf *config.Config, store *sql.DB) {
153+
154+
// check if postgres setting contains required variable or connection logs
155+
if !strings.Contains(cnf.LogParser.PgSettings.LogLinePrefix, "%h") && !strings.Contains(cnf.LogParser.PgSettings.LogLinePrefix, "%r") {
156+
fmt.Println("Please set log_line_prefix to '%h' or '%r' or enable log_connections")
157+
return
158+
}
159+
160+
if !strings.Contains(cnf.LogParser.PgSettings.LogLinePrefix, "%u") || !strings.Contains(cnf.LogParser.PgSettings.LogLinePrefix, "%d") {
161+
fmt.Printf("In logline prefix, please set '%s' and '%s'\n", "%u", "%d") // using printf to avoid the warning for %d in println
162+
return
163+
}
164+
165+
baseParser := parselog.GetDynamicBaseParser(cnf.LogParser.PgSettings.LogLinePrefix)
166+
167+
var hbaRules []model.HBAFIleRules
168+
169+
// if user is passing hba conf file manually then he or she are expecting that file to be scanned
170+
if cnf.LogParser.HbaConfFile != "" {
171+
var err error
172+
hbaRules, err = hbarules.ScanHBAFile(ctx, store, cnf.LogParser.HbaConfFile)
173+
if err != nil {
174+
fmt.Println("Got error while scanning hba file:", err)
175+
return
176+
}
177+
} else if store != nil {
178+
var err error
179+
hbaRules, err = utils.GetDatabaseAndHostForUSerFromHbaFileRules(ctx, store)
180+
if err != nil {
181+
fmt.Println("Got error while getting hba rules:", err)
182+
return
183+
}
184+
} else {
185+
fmt.Println("Please provide hba file or database connection")
186+
return
187+
}
188+
189+
hbaValidator, err := hbarules.ParseHBAFileRules(hbaRules)
190+
if err != nil {
191+
fmt.Println("Got error while parsing hba rules:", err)
192+
return
193+
}
194+
195+
hbaUnusedLineParser := parselog.NewHbaUnusedLines(cnf, baseParser, hbaValidator)
196+
runner.RunFastParser(ctx, cnf, hbaUnusedLineParser.Feed, parselog.GetBaseParserValidator(baseParser))
197+
198+
if ctx.Err() != nil {
199+
fmt.Println("file parsing is taking longer then expected, please check the file or errors in" + logger.GetLogFileName())
200+
return
201+
}
202+
203+
fmt.Println("")
204+
fmt.Println("Unused lines found from given log file:", hbaValidator.GetUnusedLines())
205+
fmt.Println("")
206+
}
207+
149208
func runMismatchIPsLogParser(ctx context.Context, cnf *config.Config) {
150209

151210
// check if postgres setting contains required variable or connection logs

docker_testing/database

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
newdbs
2+
teestingdbs

docker_testing/integrationtest/setup.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func init() {
5353
}
5454

5555
func startPostgres() {
56-
cmd := exec.Command("docker-compose", "up", "--build", "-d", "postgres")
56+
cmd := exec.Command("docker", "compose", "up", "--build", "-d", "postgres")
5757
cmd.Stdout = os.Stdout
5858
cmd.Stderr = os.Stderr
5959
cmd.Stdin = os.Stdin
@@ -72,7 +72,7 @@ func startPostgres() {
7272
}
7373

7474
func createUSers() {
75-
cmd := exec.Command("docker-compose", "run", "--rm", "createuser")
75+
cmd := exec.Command("docker", "compose", "run", "--rm", "createuser")
7676
cmd.Stdout = os.Stdout
7777
cmd.Stderr = os.Stderr
7878
cmd.Stdin = os.Stdin
@@ -93,7 +93,7 @@ func createUSers() {
9393
func execPgbench(wg *sync.WaitGroup, pgUsers, ip string) {
9494
defer wg.Done()
9595
fmt.Println("executing pgbench command for users:", pgUsers, "and ip:", ip)
96-
cmd := exec.Command("docker-compose", "run", "--rm", "pgbench")
96+
cmd := exec.Command("docker", "compose", "run", "--rm", "pgbench")
9797
cmd.Env = append(os.Environ(),
9898
"PGUSERS="+pgUsers,
9999
"IP="+ip,

docker_testing/integrationtest/testcase.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"fmt"
56
"os"
67
"os/exec"
@@ -20,6 +21,7 @@ func init() {
2021
testInactiveUser(prefix, filename)
2122
// testMissingIPs(prefix, filename)
2223
testUniqueIPs(prefix, filename)
24+
testUnusedHbaLines(prefix, filename)
2325
},
2426
}
2527

@@ -152,3 +154,46 @@ func testMissingIPs(prefix, file string) {
152154

153155
fmt.Println("mismatch ip test is working fine for prefix:", prefix)
154156
}
157+
158+
func testUnusedHbaLines(prefix, file string) {
159+
cmd := exec.Command("ciscollector",
160+
"-logparser", cons.LogParserCMD_HBAUnusedLines,
161+
"-prefix", prefix,
162+
"-file-path", file,
163+
"-output-type", "json",
164+
"-hba-file", "./pg_hba.conf",
165+
)
166+
167+
// create io.Writer to store output and print it later
168+
var buf bytes.Buffer
169+
170+
cmd.Stdout = &buf
171+
cmd.Stderr = os.Stderr
172+
cmd.Stdin = os.Stdin
173+
174+
err := cmd.Run()
175+
if err != nil {
176+
fmt.Println("Got error while parsing file:", err)
177+
os.Exit(1)
178+
}
179+
180+
out := buf.String()
181+
if strings.Contains(out, "In logline prefix, please set '%u' and '%d'") || strings.Contains(out, "Please set log_line_prefix to '%h' or '%r' or enable log_connections") {
182+
fmt.Println("skipping test for unused files as required details are not available in prefix:", prefix)
183+
return
184+
}
185+
186+
if !strings.Contains(out, "Successfully parsed all files") {
187+
fmt.Println("Got error while parsing file:", out)
188+
// fail the command
189+
os.Exit(1)
190+
}
191+
192+
if strings.Contains(out, `Unused lines found from given log file: [11 23 28]`) {
193+
fmt.Println("unused lines test is working fine for prefix:", prefix)
194+
return
195+
}
196+
197+
fmt.Println("not getting valid unused lines:", out)
198+
os.Exit(1)
199+
}

docker_testing/pg_hba.conf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Allow any user on the local system to connect to any database with
2+
# any database user name using Unix-domain sockets (the default for local
3+
# connections).
4+
#
5+
# TYPE DATABASE USER ADDRESS METHOD
6+
local all all trust
7+
8+
# The same using local loopback TCP/IP connections.
9+
#
10+
# TYPE DATABASE USER ADDRESS METHOD
11+
host all all 192.168.1.1/24 trust
12+
13+
# The same as the previous line, but using a separate netmask column
14+
#
15+
# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
16+
host all user4 192.168.0.1 255.255.255.0 trust
17+
host all all 192.168.0.1 255.255.255.0 trust
18+
# host all all all trust
19+
20+
# The same over IPv6.
21+
#
22+
# TYPE DATABASE USER ADDRESS METHOD
23+
host all all ::1/128 trust
24+
25+
# The same using a host name (would typically cover both IPv4 and IPv6).
26+
#
27+
# TYPE DATABASE USER ADDRESS METHOD
28+
host all all localhost trust

docker_testing/runner.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
############################ widely used prefixes #################################
88
# PREFIXES=('%t %h %u %m ' '%m (%h:%u) ' '%m from %h by %u ' '%m in %d by %u@%h ' '%t %h %u [%p] ' '%m (%h:%u:%p) ' '%m from %h by %u pid=%p ' '%m in %d by %u@%h pid=%p ' '%t %h %u db=%d %m ' '%m in %d by %u@%h db=%d')
99
####################################################################################
10-
PREFIXES=('%t [%p]: ')
10+
PREFIXES=('%m in %d by %u@%h ')
1111

1212
FILE_SIZE=12MB # to modify file size
1313

@@ -42,4 +42,4 @@ done
4242

4343

4444
# Set permissions for pglog directory
45-
sudo chmod 777 pglog/*
45+
sudo chmod 777 pglog/*

docker_testing/users

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
user0
2+
user1
3+
user2
4+
user3
5+
user4

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/klouddb/klouddbshield
33
go 1.18
44

55
require (
6+
github.com/DATA-DOG/go-sqlmock v1.5.0
67
github.com/go-sql-driver/mysql v1.7.0
78
github.com/hashicorp/go-version v1.6.0
89
github.com/jackc/pgx/v4 v4.18.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
3838
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
3939
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4040
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
41+
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
42+
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
4143
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
4244
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg=
4345
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=

0 commit comments

Comments
 (0)