Skip to content

Commit 4664bc1

Browse files
chore: randomize alb content (#71)
* randomize alb content Signed-off-by: Kavindu Dodanduwa <[email protected]> * fix test Signed-off-by: Kavindu Dodanduwa <[email protected]> --------- Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent dc2d6f7 commit 4664bc1

File tree

3 files changed

+128
-76
lines changed

3 files changed

+128
-76
lines changed

generators/elb.go

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package generators
22

33
import (
44
"fmt"
5+
"math/rand"
56
)
67

78
type ALBGen struct {
@@ -15,19 +16,31 @@ func NewALBGen() *ALBGen {
1516
}
1617

1718
func (a *ALBGen) Generate() (int64, error) {
19+
accountID := randomSampleAccountID()
20+
1821
customizer := albCustomizer{
19-
logType: randomSchema(),
20-
timestamp: iso8601Now(),
21-
creationTime: iso8601Now(),
22-
clientIPPort: fmt.Sprintf("%s:%d", randomIP(), randomPort()),
23-
targetIPPort: fmt.Sprintf("%s:%d", randomIP(), randomPort()),
24-
targetPortList: fmt.Sprintf("%s:%d", randomIP(), randomPort()),
25-
request: `"GET http://www.example.com:80/ HTTP/1.1"`,
26-
elbID: "appA/loadbalancer/123456789",
27-
targetARN: "arn:aws:elasticloadbalancing:us-east-1:123456789:targetID",
28-
traceID: `"trace=123456789"`,
29-
userAgent: `"curl/7.46.0"`,
30-
requestID: "TID_123456789",
22+
cipher: randomSSLCipher(),
23+
clientIPPort: fmt.Sprintf("%s:%d", randomIP(), randomPort()),
24+
creationTime: iso8601Now(),
25+
domain: randomDomain(),
26+
elbID: fmt.Sprintf("appA/loadbalancer/%s", accountID),
27+
elbStatus: randomStatus(),
28+
logType: randomSchema(),
29+
receivedBytes: randomBytesSize(),
30+
request: fmt.Sprintf("\"GET %s://%s:80/ HTTP/1.1\"", randomSchema(), randomDomain()),
31+
requestID: randomAZ09String(5),
32+
requestProcTime: randomProcessingTime(),
33+
responseProcTime: randomProcessingTime(),
34+
sentBytes: randomBytesSize(),
35+
sslProtocol: sslProtocol(),
36+
targetARN: fmt.Sprintf("arn:aws:elasticloadbalancing:%s:%s:targetID", randomRegion(), accountID),
37+
targetIPPort: fmt.Sprintf("%s:%d", randomIP(), randomPort()),
38+
targetPortList: fmt.Sprintf("%s:%d", randomIP(), randomPort()),
39+
targetProcTime: randomProcessingTime(),
40+
targetStatus: randomStatus(),
41+
timestamp: iso8601Now(),
42+
traceID: fmt.Sprintf("\"trace=%d\"", rand.Intn(1000)),
43+
userAgent: fmt.Sprintf("\"%s\"", userAgents[rand.Intn(len(userAgents))]),
3144
}
3245

3346
err := a.buf.write([]byte(buildALBLogLine(customizer)))
@@ -45,53 +58,53 @@ func (a *ALBGen) GetAndReset() []byte {
4558
// helpers
4659

4760
const (
48-
requestProcessingTimeMs = "0.000"
49-
targetProcessingTimeMs = "0.001"
50-
responseProcessingTimeMs = "0.000"
51-
elbStatusCode = "200"
52-
targetStatusCode = "200"
53-
receivedBytes = "34"
54-
sentBytes = "366"
55-
matchedRulePriority = "0"
56-
actionsExecuted = `"forward"`
57-
targetStatusList = `"200"`
58-
sslCipher = "-"
59-
sslProtocol = "-"
60-
domainName = `"-"`
61-
chosenCertArn = `"-"`
62-
redirectURL = `"-"`
63-
errorReason = `"-"`
64-
targetHealthReason = `"-"`
65-
targetHealthDescription = `"-"`
61+
matchedRulePriority = "0"
62+
actionsExecuted = `"forward"`
63+
targetStatusList = `"200"`
64+
chosenCertArn = `"-"`
65+
redirectURL = `"-"`
66+
errorReason = `"-"`
67+
targetHealthReason = `"-"`
68+
targetHealthDescription = `"-"`
6669
)
6770

6871
type albCustomizer struct {
69-
logType string
70-
timestamp string
71-
creationTime string
72-
request string
73-
elbID string
74-
targetARN string
75-
traceID string
76-
userAgent string
77-
clientIPPort string
78-
targetIPPort string
79-
targetPortList string
80-
requestID string
72+
cipher string
73+
clientIPPort string
74+
creationTime string
75+
domain string
76+
elbID string
77+
elbStatus string
78+
logType string
79+
receivedBytes int
80+
request string
81+
requestID string
82+
requestProcTime float32
83+
responseProcTime float32
84+
sentBytes int
85+
sslProtocol string
86+
targetARN string
87+
targetIPPort string
88+
targetPortList string
89+
targetProcTime float32
90+
targetStatus string
91+
timestamp string
92+
traceID string
93+
userAgent string
8194
}
8295

8396
func buildALBLogLine(input albCustomizer) string {
8497
logLine := fmt.Sprintf(
85-
"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n",
98+
"%s %s %s %s %s %0.3f %0.3f %0.3f %s %s %d %d %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n",
8699
input.logType, input.timestamp, input.elbID,
87100
input.clientIPPort, input.targetIPPort,
88-
requestProcessingTimeMs, targetProcessingTimeMs, responseProcessingTimeMs,
89-
elbStatusCode, targetStatusCode,
90-
receivedBytes, sentBytes,
101+
input.requestProcTime, input.targetProcTime, input.responseProcTime,
102+
input.elbStatus, input.targetStatus,
103+
input.receivedBytes, input.sentBytes,
91104
input.request, input.userAgent,
92-
sslCipher, sslProtocol,
105+
input.cipher, input.sslProtocol,
93106
input.targetARN, input.traceID,
94-
domainName, chosenCertArn,
107+
input.domain, chosenCertArn,
95108
matchedRulePriority, input.creationTime,
96109
actionsExecuted, redirectURL, errorReason,
97110
input.targetPortList, targetStatusList,

generators/elb_test.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,28 @@ const upstreamALBHTTP = `http 2018-07-02T22:23:00.186641Z app/my-loadbalancer/50
1313
func Test_buildALB(t *testing.T) {
1414
t.Run("Validate AWS documented HTTP ALB line", func(t *testing.T) {
1515
line := buildALBLogLine(albCustomizer{
16-
logType: "http",
17-
timestamp: "2018-07-02T22:23:00.186641Z",
18-
creationTime: "2018-07-02T22:22:48.364000Z",
19-
request: `"GET http://www.example.com:80/ HTTP/1.1"`,
20-
elbID: "app/my-loadbalancer/50dc6c495c0c9188",
21-
targetARN: "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067",
22-
traceID: `"Root=1-58337262-36d228ad5d99923122bbe354"`,
23-
userAgent: `"curl/7.46.0"`,
24-
clientIPPort: "192.168.131.39:2817",
25-
targetIPPort: "10.0.0.1:80",
26-
targetPortList: `"10.0.0.1:80"`,
27-
requestID: "TID_1234abcd5678ef90",
16+
logType: "http",
17+
timestamp: "2018-07-02T22:23:00.186641Z",
18+
creationTime: "2018-07-02T22:22:48.364000Z",
19+
request: `"GET http://www.example.com:80/ HTTP/1.1"`,
20+
elbID: "app/my-loadbalancer/50dc6c495c0c9188",
21+
targetARN: "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067",
22+
traceID: `"Root=1-58337262-36d228ad5d99923122bbe354"`,
23+
userAgent: `"curl/7.46.0"`,
24+
clientIPPort: "192.168.131.39:2817",
25+
targetIPPort: "10.0.0.1:80",
26+
targetPortList: `"10.0.0.1:80"`,
27+
requestID: "TID_1234abcd5678ef90",
28+
requestProcTime: 0.000,
29+
targetProcTime: 0.001,
30+
responseProcTime: 0.000,
31+
elbStatus: "200",
32+
targetStatus: "200",
33+
cipher: "-",
34+
sslProtocol: "-",
35+
domain: `"-"`,
36+
receivedBytes: 34,
37+
sentBytes: 366,
2838
})
2939

3040
require.Equal(t, upstreamALBHTTP, strings.TrimSpace(line))

generators/utils.go

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,39 @@ const charsCapital = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
1313

1414
var ff = false
1515
var tt = true
16-
var schema = []string{"http", "https"}
17-
var vpcActions = []string{"ACCEPT", "REJECT"}
16+
17+
var contentTypes = []string{"text/html", "application/json", "text/plain", "application/xml"}
18+
var countryCodes = []string{"US", "GB", "DE", "FR", "IN", "CN", "JP", "AU", "CA", "BR"}
19+
var sampleDomains = []string{"example.com", "test.com", "sample.org", "demo.net"}
1820
var httpMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}
21+
var httpSchema = []string{"http", "https"}
1922
var httpSourceIDs = []string{"E2A1BCD34FGH56", "E3B2CDE45GHI67", "E4C3DEF56HIJ78", "E5D4EFG67IJK89"}
2023
var queryStrings = []string{"", "a=1&b=2", "user=abc", "id=12345", "page=1", "search=term"}
21-
var uriPaths = []string{"/", "/home", "/api/resource", "/login"}
22-
var contentTypes = []string{"text/html", "application/json", "text/plain", "application/xml"}
23-
var userAgents = []string{
24-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
25-
var countryCodes = []string{"US", "GB", "DE", "FR", "IN", "CN", "JP", "AU", "CA", "BR"}
26-
var sampleRuleIDs = []string{"rule-1", "rule-2", "rule-3", "rule-4", "rule-5"}
27-
var sampleAccountIDs = []string{"123456789012", "987654321098", "111122223333", "444455556666", "777788889999"}
28-
var samplePrincipalIDs = []string{"AID1234567890", "AID0987654321", "AID1111222233", "AID7777888899"}
24+
var randomFragments = []string{"", "#browser", "#app"}
25+
var randomPhrases = []string{"some random phrase", "another random phrase", "yet another random phrase", "log on something"}
2926
var regions = []string{"us-east-1", "us-west-1", "us-west-2", "eu-west-1", "eu-central-1"}
3027
var s3EventNames = []string{"PutObject", "GetObject", "DeleteObject", "ListObjects"}
31-
var randomPhrases = []string{"some random phrase", "another random phrase", "yet another random phrase", "log on something"}
32-
var randomFragments = []string{"", "#browser", "#app"}
28+
var sampleAccountIDs = []string{"123456789012", "987654321098", "111122223333", "444455556666", "777788889999"}
29+
var samplePrincipalIDs = []string{"AID1234567890", "AID0987654321", "AID1111222233", "AID7777888899"}
30+
var sampleRuleIDs = []string{"rule-1", "rule-2", "rule-3", "rule-4", "rule-5"}
31+
var sslCiphers = []string{"ECDHE-RSA-AES128-GCM-SHA256", "ECDHE-RSA-AES256-GCM-SHA384", "AES128-GCM-SHA256"}
32+
var sslProtocols = []string{"TLSv1.2", "TLSv1.3"}
33+
var statuses = []string{"200", "400", "500"}
34+
var uriPaths = []string{"/", "/home", "/api/resource", "/login"}
35+
var userAgents = []string{"Mozilla/5.0, AppleWebKit/537.36, Chrome/58.0.3029.110, Safari/537.3", "curl/7.46.0"}
3336
var uuids = []string{"550e8400-e29b-41d4-a716-446655440000", "123e4567-e89b-12d3-a456-426614174000", "9b2c3d4e-5f6a-7b8c-9d0e-1f2a3b4c5d6e"}
37+
var vpcActions = []string{"ACCEPT", "REJECT"}
3438
var wafActions = []string{"ALLOW", "BLOCK", "COUNT"}
3539
var wafRuleTypes = []string{"REGULAR", "RATE_BASED", "GROUP"}
3640
var wafSampleHTTPSourceNames = []string{"ALB", "CloudFront", "API Gateway"}
41+
3742
// ipPrefix contains example public IP address prefixes, representing geo-distributed or commonly used public IP ranges.
3843
var ipPrefix = []int{1, 8, 31, 41, 91, 123, 179, 201, 210, 250}
3944

45+
func randomDomain() string {
46+
return sampleDomains[rand.Intn(len(sampleDomains))]
47+
}
48+
4049
func iso8601Now() string {
4150
return time.Now().UTC().Format("2006-01-02T15:04:05.000000Z")
4251
}
@@ -45,15 +54,35 @@ func unixSeconds(delay int) int64 {
4554
return time.Now().Unix() + int64(delay)
4655
}
4756

57+
// randomProcessingTime returns a random float as string between 0.500 and 1.499
58+
func randomProcessingTime() float32 {
59+
return (0.5 + rand.Float32()*1000) / 1000
60+
}
61+
62+
func randomStatus() string {
63+
return statuses[rand.Intn(len(statuses))]
64+
}
65+
66+
func randomBytesSize() int {
67+
return rand.Intn(5000-200) + 200
68+
}
69+
4870
func randomSchema() string {
49-
return schema[rand.Intn(len(schema))]
71+
return httpSchema[rand.Intn(len(httpSchema))]
72+
}
73+
74+
func sslProtocol() string {
75+
return sslProtocols[rand.Intn(len(sslProtocols))]
76+
}
77+
78+
func randomSSLCipher() string {
79+
return sslCiphers[rand.Intn(len(sslCiphers))]
5080
}
5181

5282
func randomVPCAction() string {
5383
return vpcActions[rand.Intn(len(vpcActions))]
5484
}
5585

56-
// randomIP from 172.16.101.0/24
5786
func randomIP() string {
5887
return fmt.Sprintf("%d.%d.%d.%d",
5988
ipPrefix[rand.Intn(len(ipPrefix))],
@@ -63,9 +92,9 @@ func randomIP() string {
6392
)
6493
}
6594

66-
// randomPort in range 58080 to 62000
95+
// randomPort in range 58080 to 59090
6796
func randomPort() int {
68-
return rand.Intn(62000-58080) + 58080
97+
return rand.Intn(59090-58080) + 58080
6998
}
7099

71100
func ctUserIdentity() UserIdentity {

0 commit comments

Comments
 (0)