Skip to content

Commit de92a33

Browse files
committed
Importers communicate with GraphKB backend over http with authentication.
Importers do not connect directly to DB anymore but instead send their updates over http with authentication. Authentication is handled with tokens whitelisted in the backend to allow an importer to inject data into the graph.
1 parent 0150446 commit de92a33

26 files changed

+515
-381
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ go:
55

66
script:
77
- go test -v ./...
8+
- pushd web && npm ci && npm run build && popd
89
- go build -o go-graphkb cmd/go-graphkb/main.go
910
- go build -o source-csv cmd/source-csv/main.go

config.yml renamed to cmd/go-graphkb/config.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ mariadb:
77
database: graphkb
88

99
sources:
10-
csv:
11-
data: ./examples/example-data.csv
10+
csv-employees: token_auth_for_csv_importer

cmd/go-graphkb/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,18 @@ func flush(cmd *cobra.Command, args []string) {
107107
}
108108

109109
func listen(cmd *cobra.Command, args []string) {
110-
server.StartServer(Database, Database)
110+
eventBus := make(chan knowledge.SourceSubGraphUpdates)
111+
listener := knowledge.NewGraphUpdater(Database, Database)
112+
113+
listener.Listen(eventBus)
114+
115+
if err := Database.InitializeSchema(); err != nil {
116+
log.Fatal(err)
117+
}
118+
119+
server.StartServer(Database, Database, eventBus)
120+
121+
close(eventBus)
111122
}
112123

113124
func read(cmd *cobra.Command, args []string) {

cmd/importer-csv/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
graphkb:
2+
url: "http://localhost:8090"
3+
auth_token: "token_auth_for_csv_importer"
4+
5+
path: "../../examples/example-data.csv"

cmd/source-csv/main.go renamed to cmd/importer-csv/main.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@ import (
1313
// ConfigPath string
1414
var ConfigPath string
1515

16-
// SourceName string
17-
var SourceName string
18-
1916
func onInit() {
2017
viper.SetConfigFile(ConfigPath)
2118
viper.SetConfigType("yaml")
2219

2320
if err := viper.ReadInConfig(); err != nil {
2421
panic(fmt.Errorf("Cannot read configuration file from %s", ConfigPath))
2522
}
26-
27-
fmt.Println("Using config file:", viper.ConfigFileUsed())
2823
}
2924

3025
func main() {
@@ -35,18 +30,14 @@ func main() {
3530
rootCmd := &cobra.Command{
3631
Use: "source-csv [opts]",
3732
Run: func(cmd *cobra.Command, args []string) {
38-
if err := importer.Start(SourceName, sources.NewCSVSource(), nil); err != nil {
33+
if err := importer.Start(sources.NewCSVSource(), nil); err != nil {
3934
log.Fatal(err)
4035
}
4136
},
4237
}
4338

44-
rootCmd.PersistentFlags().StringVar(&ConfigPath, "config", "config.yml", "Provide the path to the configuration file (required)")
45-
rootCmd.PersistentFlags().StringVar(&SourceName, "source-name", "", "Provide a unique source name")
46-
47-
if err := cobra.MarkFlagRequired(rootCmd.PersistentFlags(), "source-name"); err != nil {
48-
log.Fatal(err)
49-
}
39+
rootCmd.PersistentFlags().StringVar(&ConfigPath, "config", "config.yml",
40+
"Provide the path to the configuration file (required)")
5041

5142
if err := rootCmd.Execute(); err != nil {
5243
log.Fatal(err)

importer/importer.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package importer
22

33
import (
44
"fmt"
5-
"log"
6-
7-
"github.com/clems4ever/go-graphkb/internal/database"
85
"github.com/clems4ever/go-graphkb/internal/knowledge"
96
"github.com/clems4ever/go-graphkb/internal/sources"
107
"github.com/spf13/viper"
@@ -14,35 +11,24 @@ type ImporterOptions struct {
1411
CacheGraph bool
1512
}
1613

17-
func Start(name string, source sources.Source, options *ImporterOptions) error {
18-
dbName := viper.GetString("mariadb.database")
19-
if dbName == "" {
20-
return fmt.Errorf("Please provide database_name option in your configuration file")
14+
func Start(source sources.Source, options *ImporterOptions) error {
15+
url := viper.GetString("graphkb.url")
16+
if url == "" {
17+
return fmt.Errorf("Please provide graphkb URL in configuration file")
2118
}
22-
mariaDatabase := database.NewMariaDB(
23-
viper.GetString("mariadb.username"),
24-
viper.GetString("mariadb.password"),
25-
viper.GetString("mariadb.host"),
26-
dbName)
27-
28-
observableSource := sources.NewObservableSource(source)
29-
30-
eventBus := make(chan knowledge.SourceSubGraphUpdates)
31-
listener := knowledge.NewSourceListener(mariaDatabase, mariaDatabase)
3219

33-
closeC := listener.Listen(eventBus)
34-
35-
if err := mariaDatabase.InitializeSchema(); err != nil {
36-
log.Fatal(err)
20+
authToken := viper.GetString("graphkb.auth_token")
21+
if authToken == "" {
22+
return fmt.Errorf("Please provide a graphkb auth token to communicate with GraphKB")
3723
}
3824

39-
emitter := knowledge.NewGraphEmitter(name, eventBus, mariaDatabase)
40-
if err := observableSource.Start(emitter); err != nil {
41-
return err
42-
}
25+
observableSource := sources.NewObservableSource(source)
26+
api := knowledge.NewGraphAPI(url, authToken)
27+
graphImporter := knowledge.NewGraphImporter(api)
4328

44-
close(eventBus)
45-
<-closeC
29+
if err := observableSource.Start(graphImporter); err != nil {
30+
return fmt.Errorf("Unable to start importer: %v", err)
31+
}
4632

4733
return nil
4834
}

internal/database/mariadb.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ func (m *MariaDB) Query(ctx context.Context, query *query.QueryIL) (*knowledge.G
444444
}
445445

446446
func (m *MariaDB) SaveSchema(ctx context.Context, sourceName string, schema schema.SchemaGraph) error {
447-
b, err := schema.ToJSON()
447+
b, err := json.Marshal(schema)
448448
if err != nil {
449449
return fmt.Errorf("Unable to json encode schema: %v", err)
450450
}
@@ -470,7 +470,8 @@ func (m *MariaDB) LoadSchema(ctx context.Context, sourceName string) (schema.Sch
470470
}
471471

472472
graph := schema.NewSchemaGraph()
473-
if err := graph.FromJSON([]byte(rawJson)); err != nil {
473+
err := json.Unmarshal([]byte(rawJson), &graph)
474+
if err != nil {
474475
return schema.NewSchemaGraph(), err
475476
}
476477

internal/knowledge/emitter.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)