Skip to content

Commit

Permalink
Merge pull request #53 from flanksource/git-files
Browse files Browse the repository at this point in the history
feat: add support for github in file scrapping
  • Loading branch information
moshloop authored Jun 1, 2022
2 parents 20e65bb + e76f5d6 commit c124816
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 54 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*.json
*.json
.vscode
_DS_Store
.bin/
vendor
confighub
confighub
scraped/
7 changes: 4 additions & 3 deletions api/v1/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package v1

// File ...
type File struct {
ID string `json:"id"`
Type string `json:"type"`
Glob []string `json:"path"`
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
URL string `json:"url,omitempty"`
Paths []string `json:"paths,omitempty"`
}
1 change: 1 addition & 0 deletions api/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ScrapeResult struct {
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
ID string `json:"id,omitempty"`
Source string `json:"source,omitempty"`
Config interface{} `json:"config,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ type ConfigScraper struct {

// IsEmpty ...
func (c ConfigScraper) IsEmpty() bool {
return len(c.AWS) == 0
return len(c.AWS) == 0 && len(c.File) == 0
}
6 changes: 6 additions & 0 deletions db/migrations/6_source_alter.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- +goose Up
-- +goose StatementBegin
---
ALTER TABLE config_item ADD source text;

-- +goose StatementEnd
1 change: 1 addition & 0 deletions db/models/config_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ConfigItem struct {
Network *string `gorm:"column:network;default:null" json:"network,omitempty" toml:"network" yaml:"network,omitempty"`
Subnet *string `gorm:"column:subnet;default:null" json:"subnet,omitempty" toml:"subnet" yaml:"subnet,omitempty"`
Config *string `gorm:"column:config;default:null" json:"config,omitempty" toml:"config" yaml:"config,omitempty"`
Source *string `gorm:"column:source;default:null" json:"source,omitempty" toml:"source" yaml:"source,omitempty"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"`
}
Expand Down
18 changes: 13 additions & 5 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,28 @@ func NewConfigItemFromResult(result v1.ScrapeResult) models.ConfigItem {
Network: &result.Network,
Subnet: &result.Subnet,
Name: &result.Name,
Source: &result.Source,
}
}

// Update creates or update a configuartion with config changes
func Update(ctx v1.ScrapeContext, results []v1.ScrapeResult) error {
// boil.DebugMode = true
for _, result := range results {
data, err := json.Marshal(result.Config)
if err != nil {
return errors.Wrapf(err, "Unable to marshal: %v", result.Config)
var dataStr string
switch data := result.Config.(type) {
case string:
dataStr = data
case []byte:
dataStr = string(data)
default:
bytes, err := json.Marshal(data)
if err != nil {
return errors.Wrapf(err, "Unable to marshal: %v", result.Config)
}
dataStr = string(bytes)
}

ci := NewConfigItemFromResult(result)
dataStr := string(data)
ci.Config = &dataStr

existing, err := GetConfigItem(result.ID)
Expand Down
6 changes: 4 additions & 2 deletions filesystem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func (fm *fileFinder) Find(path string) ([]string, error) {
}

// Read ...
func (fm *fileFinder) Read(match string) ([]byte, error) {
return os.ReadFile(match)
func (fm *fileFinder) Read(match string) ([]byte, string, error) {
content, err := os.ReadFile(match)
filename := filepath.Base(match)
return content, filename, err
}
2 changes: 1 addition & 1 deletion filesystem/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package filesystem
// Finder defines the contract for any file system finder to impletent
type Finder interface {
Find(string) ([]string, error)
Read(string) ([]byte, error)
Read(string) ([]byte, string, error)
}
6 changes: 6 additions & 0 deletions fixtures/file-git.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
file:
- type: kind
id: metadata.name
url: github.com/flanksource/canary-checker
paths:
- fixtures/minimal/**.yaml
5 changes: 5 additions & 0 deletions fixtures/file-local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
file:
- type: aws[0].region
id: aws[0].region
paths:
- fixtures/test.yaml
File renamed without changes.
6 changes: 6 additions & 0 deletions fixtures/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
aws:
- region: eu-west-1
compliance: true
patch_states: true
patch_details: true
inventory: true
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/flanksource/commons v1.5.11
github.com/flanksource/kommons v0.26.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-getter v1.6.1
github.com/henvic/httpretty v0.0.6
github.com/jackc/pgx/v4 v4.14.1
github.com/labstack/echo/v4 v4.6.3
Expand All @@ -32,6 +33,7 @@ require (
gorm.io/gorm v1.23.2
k8s.io/apimachinery v0.20.4
k8s.io/client-go v11.0.0+incompatible
sigs.k8s.io/yaml v1.2.0
)

require (
Expand All @@ -56,6 +58,7 @@ require (
github.com/aws/smithy-go v1.11.2 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/cheggaaa/pb v1.0.27 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
Expand Down Expand Up @@ -90,8 +93,7 @@ require (
github.com/hairyhenderson/toml v0.3.1-0.20191004034452-2a4f3b6160f2 // indirect
github.com/hashicorp/consul/api v1.10.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-getter v1.4.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v0.12.0 // indirect
github.com/hashicorp/go-immutable-radix v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
Expand Down Expand Up @@ -122,12 +124,14 @@ require (
github.com/joho/godotenv v1.3.0 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
github.com/klauspost/compress v1.11.2 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/mailru/easyjson v0.7.0 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.8 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
Expand Down Expand Up @@ -158,7 +162,7 @@ require (
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
Expand All @@ -184,7 +188,6 @@ require (
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
sigs.k8s.io/kustomize v2.0.3+incompatible // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.0.2 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)

replace (
Expand Down
14 changes: 11 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/checkpoint-restore/go-criu/v5 v5.0.0/go.mod h1:cfwC0EG7HMUenopBsUf9d89JlCLQIfgVcNsNN0t6T2M=
github.com/cheggaaa/pb v1.0.27 h1:wIkZHkNfC7R6GI5w7l/PdAdzXzlrbcI3p8OAlnkTsnc=
github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -466,10 +467,12 @@ github.com/hashicorp/consul/sdk v0.8.0/go.mod h1:GBvyrGALthsZObzUGsfgHZQDXjg4lOj
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-getter v1.4.1 h1:3A2Mh8smGFcf5M+gmcv898mZdrxpseik45IpcyISLsA=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-getter v1.4.1/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY=
github.com/hashicorp/go-getter v1.6.1 h1:NASsgP4q6tL94WH6nJxKWj8As2H/2kop/bB1d8JMyRY=
github.com/hashicorp/go-getter v1.6.1/go.mod h1:IZCrswsZPeWv9IkVnLElzRU/gz/QPi6pZHn4tv6vbwA=
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
github.com/hashicorp/go-hclog v0.8.0/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
Expand Down Expand Up @@ -626,6 +629,8 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.11.2 h1:MiK62aErc3gIiVEtyzKfeOHgW7atJb5g/KNX5m3c2nQ=
github.com/klauspost/compress v1.11.2/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -685,6 +690,7 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0=
github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
Expand Down Expand Up @@ -909,6 +915,7 @@ github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljT
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
Expand Down Expand Up @@ -1203,8 +1210,9 @@ golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210902050250-f475640dd07b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e h1:w36l2Uw3dRan1K3TyXriXvY+6T56GNmlKGcqiQUJDfM=
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
2 changes: 1 addition & 1 deletion scrapers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
// All is the scrappers registry
var All = []v1.Scraper{
aws.Scraper{},
file.JSONScrapper{},
file.FileScrapper{},
}
Loading

0 comments on commit c124816

Please sign in to comment.