Skip to content

Commit 1deb328

Browse files
authored
Initial beta release
1 parent 283696a commit 1deb328

File tree

4,010 files changed

+1144524
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,010 files changed

+1144524
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/.idea
2+
/build
3+
4+
.DS_Store
5+
/cloudflarebeat
6+
/cloudflarebeat.test
7+
*.pyc
8+
cloudflarebeat.local.yml
9+
/cloudflarelogs.state
10+
*.state

.travis.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
sudo: required
2+
dist: trusty
3+
services:
4+
- docker
5+
6+
language: go
7+
8+
go:
9+
- 1.6
10+
11+
os:
12+
- linux
13+
- osx
14+
15+
env:
16+
matrix:
17+
- TARGETS="check"
18+
- TARGETS="testsuite"
19+
20+
global:
21+
# Cross-compile for amd64 only to speed up testing.
22+
- GOX_FLAGS="-arch amd64"
23+
24+
addons:
25+
apt:
26+
packages:
27+
- python-virtualenv
28+
29+
before_install:
30+
# Redo the travis setup but with the elastic/libbeat path. This is needed so the package path is correct
31+
- mkdir -p $HOME/gopath/src/github.com/hartfordfive/cloudflarebeat/
32+
- rsync -az ${TRAVIS_BUILD_DIR}/ $HOME/gopath/src/github.com/hartfordfive/cloudflarebeat/
33+
- export TRAVIS_BUILD_DIR=$HOME/gopath/src/github.com/hartfordfive/cloudflarebeat/
34+
- cd $HOME/gopath/src/github.com/hartfordfive/cloudflarebeat/
35+
36+
install:
37+
- true
38+
39+
script:
40+
- make $TARGETS
41+
42+
after_success:
43+
# Copy full.cov to coverage.txt because codecov.io requires this file

CONTRIBUTING.md

Whitespace-only changes.

Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
BEATNAME=cloudflarebeat
2+
BEAT_DIR=github.com/hartfordfive/cloudflarebeat
3+
SYSTEM_TESTS=false
4+
TEST_ENVIRONMENT=false
5+
ES_BEATS?=./vendor/github.com/elastic/beats
6+
GOPACKAGES=$(shell glide novendor)
7+
PREFIX?=.
8+
9+
# Path to the libbeat Makefile
10+
-include $(ES_BEATS)/libbeat/scripts/Makefile
11+
12+
# Initial beat setup
13+
.PHONY: setup
14+
setup: copy-vendor
15+
make update
16+
17+
# Copy beats into vendor directory
18+
.PHONY: copy-vendor
19+
copy-vendor:
20+
mkdir -p vendor/github.com/elastic/
21+
cp -R ${GOPATH}/src/github.com/elastic/beats vendor/github.com/elastic/
22+
rm -rf vendor/github.com/elastic/beats/.git
23+
24+
.PHONY: git-init
25+
git-init:
26+
git init
27+
git add README.md CONTRIBUTING.md
28+
git commit -m "Initial commit"
29+
git add LICENSE
30+
git commit -m "Add the LICENSE"
31+
git add .gitignore
32+
git commit -m "Add git settings"
33+
git add .
34+
git reset -- .travis.yml
35+
git commit -m "Add cloudflarebeat"
36+
git add .travis.yml
37+
git commit -m "Add Travis CI"
38+
39+
# This is called by the beats packer before building starts
40+
.PHONY: before-build
41+
before-build:

README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Cloudflarebeat
2+
3+
Welcome to Cloudflarebeat.
4+
5+
Ensure that this folder is at the following location:
6+
`${GOPATH}/github.com/hartfordfive`
7+
8+
## Getting Started with Cloudflarebeat
9+
10+
### Requirements
11+
12+
* [Golang](https://golang.org/dl/) 1.7
13+
14+
### Init Project
15+
To get running with Cloudflarebeat and also install the
16+
dependencies, run the following command:
17+
18+
```
19+
make setup
20+
```
21+
22+
It will create a clean git history for each major step. Note that you can always rewrite the history if you wish before pushing your changes.
23+
24+
To push Cloudflarebeat in the git repository, run the following commands:
25+
26+
```
27+
git remote set-url origin https://github.com/hartfordfive/cloudflarebeat
28+
git push origin master
29+
```
30+
31+
For further development, check out the [beat developer guide](https://www.elastic.co/guide/en/beats/libbeat/current/new-beat.html).
32+
33+
### Build
34+
35+
To build the binary for Cloudflarebeat run the command below. This will generate a binary
36+
in the same directory with the name cloudflarebeat.
37+
38+
```
39+
make
40+
```
41+
42+
43+
### Run
44+
45+
To run Cloudflarebeat with debugging output enabled, run:
46+
47+
```
48+
./cloudflarebeat -c cloudflarebeat.yml -e -d "*"
49+
```
50+
51+
For details of command line options, view the following links:
52+
53+
- https://www.elastic.co/guide/en/beats/libbeat/master/config-file-format-cli.html
54+
- https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-command-line.html
55+
56+
57+
## Cloudflarebeat specific configuration options
58+
59+
- `cloudflarebeat.period` : The period at which the cloudflare logs will be fetched. (Default value is 1800s/30mins which is the default suggested by the Enterprise Log Share API documentation page.)
60+
- `cloudflarebeat.api_key` : The API key of the user account (mandatory)
61+
- `cloudflarebeat.email` : The email address of the user account (mandatory)
62+
- `cloudflarebeat.zone_tag` : The zone tag of the domain for which you want to access the enterpise logs (mandatory)
63+
- `cloudflarebeat.state_file_storage_type` : The type of storage for the state file, either `disk`, `s3`, or `consul`, which keeps track of the current progress. (Defau)
64+
- `cloudflarebeat.aws_access_key` : The user AWS access key, if S3 storage selected.
65+
- `cloudflarebeat.aws_secret_access_key` : The user AWS secret access key, if S3 storage selected.
66+
67+
## Filtering out specific logs and/or log properties
68+
69+
70+
### Test
71+
72+
To test Cloudflarebeat, run the following command:
73+
74+
```
75+
make testsuite
76+
```
77+
78+
alternatively:
79+
```
80+
make unit-tests
81+
make system-tests
82+
make integration-tests
83+
make coverage-report
84+
```
85+
86+
The test coverage is reported in the folder `./build/coverage/`
87+
88+
### Update
89+
90+
Each beat has a template for the mapping in elasticsearch and a documentation for the fields
91+
which is automatically generated based on `etc/fields.yml`.
92+
To generate etc/cloudflarebeat.template.json and etc/cloudflarebeat.asciidoc
93+
94+
```
95+
make update
96+
```
97+
98+
99+
### Cleanup
100+
101+
To clean Cloudflarebeat source code, run the following commands:
102+
103+
```
104+
make fmt
105+
make simplify
106+
```
107+
108+
To clean up the build directory and generated artifacts, run:
109+
110+
```
111+
make clean
112+
```
113+
114+
115+
### Clone
116+
117+
To clone Cloudflarebeat from the git repository, run the following commands:
118+
119+
```
120+
mkdir -p ${GOPATH}/github.com/hartfordfive
121+
cd ${GOPATH}/github.com/hartfordfive
122+
git clone https://github.com/hartfordfive/cloudflarebeat
123+
```
124+
125+
126+
For further development, check out the [beat developer guide](https://www.elastic.co/guide/en/beats/libbeat/current/new-beat.html).
127+
128+
129+
## Packaging
130+
131+
The beat frameworks provides tools to crosscompile and package your beat for different platforms. This requires [docker](https://www.docker.com/) and vendoring as described above. To build packages of your beat, run the following command:
132+
133+
```
134+
make package
135+
```
136+
137+
This will fetch and create all images required for the build process. The hole process to finish can take several minutes.

beater/cloudflarebeat.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package beater
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
"github.com/elastic/beats/libbeat/beat"
9+
"github.com/elastic/beats/libbeat/common"
10+
"github.com/elastic/beats/libbeat/logp"
11+
"github.com/elastic/beats/libbeat/publisher"
12+
13+
"github.com/hartfordfive/cloudflarebeat/cloudflare"
14+
"github.com/hartfordfive/cloudflarebeat/config"
15+
)
16+
17+
type Cloudflarebeat struct {
18+
done chan struct{}
19+
config config.Config
20+
client publisher.Client
21+
}
22+
23+
// Creates beater
24+
func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
25+
config := config.DefaultConfig
26+
if err := cfg.Unpack(&config); err != nil {
27+
return nil, fmt.Errorf("Error reading config file: %v", err)
28+
}
29+
30+
bt := &Cloudflarebeat{
31+
done: make(chan struct{}),
32+
config: config,
33+
}
34+
return bt, nil
35+
}
36+
37+
func (bt *Cloudflarebeat) Run(b *beat.Beat) error {
38+
logp.Info("cloudflarebeat is running! Hit CTRL-C to stop it.")
39+
40+
bt.client = b.Publisher.Connect()
41+
ticker := time.NewTicker(bt.config.Period)
42+
counter := 1
43+
44+
// Initialize the Cloudflare client here
45+
cc := cloudflare.NewClient(map[string]interface{}{
46+
"api_key": bt.config.APIKey,
47+
"email": bt.config.Email,
48+
"debug": bt.config.Debug,
49+
"exclude": bt.config.Exclude,
50+
})
51+
52+
sf := cloudflare.NewStateFile("cloudflarebeat.state", bt.config.StateFileStorageType)
53+
logp.Info("Initializing state file 'cloudflarebeats.state' with storage type '" + bt.config.StateFileStorageType + "'")
54+
err := sf.Initialize()
55+
56+
if err != nil {
57+
logp.Err("Could not load statefile: %s", err.Error())
58+
os.Exit(1)
59+
}
60+
61+
if sf.GetLastStartTS() != 0 {
62+
logp.Info("Start time loaded from state file: %s", time.Unix(int64(sf.GetLastStartTS()), 0).Format(time.RFC3339))
63+
logp.Info(" End time loaded from state file: %s", time.Unix(int64(sf.GetLastEndTS()), 0).Format(time.RFC3339))
64+
}
65+
66+
var timeStart, timeEnd, timeNow int
67+
68+
for {
69+
select {
70+
case <-bt.done:
71+
return nil
72+
case <-ticker.C:
73+
}
74+
75+
timeNow = int(time.Now().UTC().Unix())
76+
77+
if sf.GetLastStartTS() != 0 {
78+
timeStart = sf.GetLastEndTS() + 1 // last time start
79+
timeEnd = sf.GetLastEndTS() + (30 * 60) // to 30 minutes later, minus 1 second
80+
} else {
81+
timeStart = timeNow - (30 * 60) // last end TS as per statefile
82+
timeEnd = timeNow // to 1 second ago
83+
}
84+
85+
logp.Info("Next request start time: %s", time.Unix(int64(timeStart), 0).Format(time.RFC3339))
86+
logp.Info(" Next request end Time: %s", time.Unix(int64(timeEnd), 0).Format(time.RFC3339))
87+
88+
logs, err := cc.GetLogRangeFromTimestamp(map[string]interface{}{
89+
"zone_tag": bt.config.ZoneTag,
90+
"time_start": timeStart,
91+
"time_end": timeEnd,
92+
//"count": 10,
93+
})
94+
95+
if err != nil {
96+
logp.Err("GetLogRangeFromTimestamp: %s", err.Error())
97+
sf.UpdateLastRequestTS(timeNow)
98+
} else {
99+
bt.client.PublishEvents(logs)
100+
logp.Info("Total events sent this period: %d", len(logs))
101+
// Now need to update the disk-based state file that keeps track of the current state
102+
sf.UpdateLastStartTS(timeStart)
103+
sf.UpdateLastEndTS(timeEnd)
104+
sf.UpdateLastCount(len(logs))
105+
sf.UpdateLastRequestTS(timeNow)
106+
}
107+
108+
err = sf.Save()
109+
if err != nil {
110+
logp.Err("Could not persist state file to storage: %s", err.Error())
111+
}
112+
113+
counter++
114+
}
115+
116+
}
117+
118+
func (bt *Cloudflarebeat) Stop() {
119+
bt.client.Close()
120+
close(bt.done)
121+
}

0 commit comments

Comments
 (0)