Skip to content

Commit

Permalink
Merge pull request #102 from mindstand/driver_update
Browse files Browse the repository at this point in the history
Driver update
  • Loading branch information
nikitawootten authored May 12, 2022
2 parents a8ccfb6 + cb8a672 commit 9f5f9bb
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 282 deletions.
11 changes: 8 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package gogm

import (
"crypto/tls"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -53,9 +54,13 @@ type Config struct {

Realm string `yaml:"realm" json:"realm" mapstructure:"realm"`

// these security configurations will be ignored if the protocol does not contain +s
UseSystemCertPool bool `yaml:"use_system_cert_pool" mapstructure:"use_system_cert_pool"`
CAFileLocation string `yaml:"ca_file_location" mapstructure:"ca_file_location"`
// deprecated: in favor of tls config
//these security configurations will be ignored if the protocol does not contain +s
UseSystemCertPool bool `yaml:"use_system_cert_pool" mapstructure:"use_system_cert_pool"`
// deprecated: in favor of tls config
CAFileLocation string `yaml:"ca_file_location" mapstructure:"ca_file_location"`

TLSConfig *tls.Config `yaml:"tls_config" mapstructure:"tls_config"`

// Index Strategy defines the index strategy for GoGM
IndexStrategy IndexStrategy `yaml:"index_strategy" json:"index_strategy" mapstructure:"index_strategy"`
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mindstand/gogm/v2

go 1.13
go 1.16

require (
github.com/adam-hanna/arrayOperations v0.2.6
Expand All @@ -11,7 +11,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/kr/pretty v0.1.0 // indirect
github.com/mindstand/go-cypherdsl v0.2.0
github.com/neo4j/neo4j-go-driver/v4 v4.3.3
github.com/neo4j/neo4j-go-driver/v4 v4.4.2-0.20220317151800-1a19fb114732
github.com/opentracing/opentracing-go v1.2.0
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/stretchr/objx v0.2.0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ github.com/dchest/siphash v1.2.1 h1:4cLinnzVJDKxTCl9B01807Yiy+W7ZzVHj/KIroQRvT4=
github.com/dchest/siphash v1.2.1/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
Expand Down Expand Up @@ -44,16 +45,26 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mindstand/go-cypherdsl v0.2.0 h1:/B6A8DhWk2RksdJxruy3+ii3Hvrr5JU+2vL3/oJMLrI=
github.com/mindstand/go-cypherdsl v0.2.0/go.mod h1:swzbrSTuq3CRgFglg3aVThG9GBQmHXz6AY81q9mRMto=
github.com/mindstand/neo4j-go-driver/v4 v4.3.4-0.20220222141004-33255dc85550 h1:K5JAVs3c5c2n1+jzLejM1vNd8qD8wgLvIyE4WD5tcu8=
github.com/mindstand/neo4j-go-driver/v4 v4.3.4-0.20220222141004-33255dc85550/go.mod h1:HQcN7yHl5H0hJKaYuF3TBH9m/Ym0C0ua4NHzKUr9uSc=
github.com/mindstand/neo4j-go-driver/v4 v4.4.0-beta1.0.20220222145400-d3c6fddab3cc h1:PY2MtEh34XJ/r4rHgxqnsby9lpmijQLWVcC7h0s+pyU=
github.com/mindstand/neo4j-go-driver/v4 v4.4.0-beta1.0.20220222145400-d3c6fddab3cc/go.mod h1:NexOfrm4c317FVjekrhVV8pHBXgtMG5P6GeweJWCyo4=
github.com/mindstand/neo4j-go-driver/v4 v4.4.0-beta1.0.20220314193855-8ea056066b14 h1:+C3FJ2je/N1/ejuLCg6W2/NM5V5D4T6NC6Pf2bAe8t4=
github.com/mindstand/neo4j-go-driver/v4 v4.4.0-beta1.0.20220314193855-8ea056066b14/go.mod h1:NexOfrm4c317FVjekrhVV8pHBXgtMG5P6GeweJWCyo4=
github.com/neo4j/neo4j-go-driver/v4 v4.3.3 h1:QwM0IN1L6q1+N9cNqjv9Pmj4J4qCVauczQZdFsDafv8=
github.com/neo4j/neo4j-go-driver/v4 v4.3.3/go.mod h1:G+DuMWSR9Auvbm6tk+fHNIegnfswAsmXgP/ibvwOY2Q=
github.com/neo4j/neo4j-go-driver/v4 v4.4.2-0.20220317151800-1a19fb114732 h1:nHwy/xxNFLe9MrQy0ub1JCyrh2p/bAW79uFDX3exjHQ=
github.com/neo4j/neo4j-go-driver/v4 v4.4.2-0.20220317151800-1a19fb114732/go.mod h1:NexOfrm4c317FVjekrhVV8pHBXgtMG5P6GeweJWCyo4=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.14.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -104,6 +115,8 @@ golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5IqFdQieMLwwCJokV2pc=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
71 changes: 46 additions & 25 deletions gogm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package gogm

import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
Expand Down Expand Up @@ -97,23 +98,28 @@ func NewContext(ctx context.Context, config *Config, pkStrategy *PrimaryKeyStrat
func (g *Gogm) init(ctx context.Context) error {
err := g.validate()
if err != nil {
return err
return fmt.Errorf("failed to validate config, %w", err)
}

err = g.parseOgmTypes()
if err != nil {
return err
return fmt.Errorf("failed to parse ogm types, %w", err)
}

g.logger.Debug("establishing neo connection")

err = g.initDriver(ctx)
if err != nil {
return err
return fmt.Errorf("failed to initialize driver, %w", err)
}

g.logger.Debug("initializing indices")
return g.initIndex(ctx)
err = g.initIndex(ctx)
if err != nil {
return fmt.Errorf("failed to init indices, %w", err)
}

return nil
}

func (g *Gogm) validate() error {
Expand Down Expand Up @@ -147,7 +153,7 @@ func (g *Gogm) parseOgmTypes() error {
name := reflect.TypeOf(t).Elem().Name()
dc, err := getStructDecoratorConfig(g, t, g.mappedRelations)
if err != nil {
return err
return fmt.Errorf("failed to get structDecoratorConfig for %s, %w", name, err)
}

g.logger.Debugf("mapped type %s", name)
Expand All @@ -166,27 +172,39 @@ func (g *Gogm) parseOgmTypes() error {
}

func (g *Gogm) initDriver(ctx context.Context) error {
var certPool *x509.CertPool
isEncrypted := strings.Contains(g.config.Protocol, "+s")

if isEncrypted {
if g.config.UseSystemCertPool {
var err error
certPool, err = x509.SystemCertPool()
if err != nil {
return fmt.Errorf("failed to get system cert pool")
}
} else {
certPool = x509.NewCertPool()
if g.config.TLSConfig == nil {
g.config.TLSConfig = &tls.Config{}
}

// handle deprecated config support
if g.config.CAFileLocation != "" {
g.logger.Debugf("loading ca file at location `%s`", g.config.CAFileLocation)
bytes, err := ioutil.ReadFile(g.config.CAFileLocation)
if err != nil {
return fmt.Errorf("failed to open ca file, %w", err)
}
g.logger.Debugf("successfully loaded ca file")

var certPool *x509.CertPool
if g.config.UseSystemCertPool {
g.logger.Debug("loading system cert pool")
var err error
certPool, err = x509.SystemCertPool()
if err != nil {
return fmt.Errorf("failed to get system cert pool")
}
g.logger.Debug("successfully loaded system cert pool")
} else {
certPool = x509.NewCertPool()
}

certPool.AppendCertsFromPEM(bytes)
if !certPool.AppendCertsFromPEM(bytes) {
return errors.New("failed to load CA into cert pool")
}
g.config.TLSConfig.RootCAs = certPool
}
}

Expand All @@ -198,7 +216,9 @@ func (g *Gogm) initDriver(ctx context.Context) error {
neoConf.MaxConnectionPoolSize = g.config.PoolSize

if isEncrypted {
neoConf.RootCAs = certPool
if g.config.TLSConfig.RootCAs != nil {
neoConf.RootCAs = g.config.TLSConfig.RootCAs
}
}
}

Expand Down Expand Up @@ -231,7 +251,9 @@ func (g *Gogm) initDriver(ctx context.Context) error {
}

func (g *Gogm) initDriverRoutine(neoConfig func(neoConf *neo4j.Config), doneChan chan error) {
driver, err := neo4j.NewDriver(g.config.ConnectionString(), neo4j.BasicAuth(g.config.Username, g.config.Password, g.config.Realm), neoConfig)
connStr := g.config.ConnectionString()
g.logger.Debugf("connection string: %s\n", connStr)
driver, err := neo4j.NewDriver(connStr, neo4j.BasicAuth(g.config.Username, g.config.Password, g.config.Realm), neoConfig)
if err != nil {
doneChan <- fmt.Errorf("failed to create driver, %w", err)
return
Expand All @@ -249,21 +271,20 @@ func (g *Gogm) initDriverRoutine(neoConfig func(neoConf *neo4j.Config), doneChan
// get neoversion
sess := driver.NewSession(neo4j.SessionConfig{
AccessMode: neo4j.AccessModeRead,
// DatabaseName: "neo4j",
})

res, err := sess.Run("return 1", nil)
if err != nil {
doneChan <- err
doneChan <- fmt.Errorf("failed to run test query, %w", err)
return
} else if err = res.Err(); err != nil {
doneChan <- err
doneChan <- fmt.Errorf("failed to run test query, %w", err)
return
}

sum, err := res.Consume()
if err != nil {
doneChan <- err
doneChan <- fmt.Errorf("failed to consume test query, %w", err)
return
}

Expand All @@ -278,27 +299,27 @@ func (g *Gogm) initIndex(ctx context.Context) error {
g.logger.Debug("dropping all known indexes")
err := dropAllIndexesAndConstraints(ctx, g)
if err != nil {
return err
return fmt.Errorf("failed to drop all known indexes, %w", err)
}

g.logger.Debug("creating all mapped indexes")
err = createAllIndexesAndConstraints(ctx, g, g.mappedTypes)
if err != nil {
return err
return fmt.Errorf("failed t create all indexes and constraints, %w", err)
}

g.logger.Debug("verifying all indexes")
err = verifyAllIndexesAndConstraints(ctx, g, g.mappedTypes)
if err != nil {
return err
return fmt.Errorf("failed to verify all indexes and contraints, %w", err)
}
return nil
case VALIDATE_INDEX:
g.logger.Debug("chose VALIDATE_INDEX strategy")
g.logger.Debug("verifying all indexes")
err := verifyAllIndexesAndConstraints(ctx, g, g.mappedTypes)
if err != nil {
return err
return fmt.Errorf("failed to verify all indexes and contraints, %w", err)
}
return nil
case IGNORE_INDEX:
Expand Down
44 changes: 37 additions & 7 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,62 @@ package gogm

import (
"context"
"fmt"
"github.com/cornelk/hashmap"
)

//drops all known indexes
func dropAllIndexesAndConstraints(ctx context.Context, gogm *Gogm) error {
if gogm.boltMajorVersion >= 4 {
return dropAllIndexesAndConstraintsV4(ctx, gogm)
for _, db := range gogm.config.TargetDbs {
err := dropAllIndexesAndConstraintsV4(ctx, gogm, db)
if err != nil {
return fmt.Errorf("failed to drop indexes and constraints for db %s on db version 4+, %w", db, err)
}
}
} else {
err := dropAllIndexesAndConstraintsV3(ctx, gogm)
if err != nil {
return fmt.Errorf("failed to drop indexes and constraints on db version 3, %w", err)
}
}

return dropAllIndexesAndConstraintsV3(ctx, gogm)
return nil
}

//creates all indexes
func createAllIndexesAndConstraints(ctx context.Context, gogm *Gogm, mappedTypes *hashmap.HashMap) error {
if gogm.boltMajorVersion >= 4 {
return createAllIndexesAndConstraintsV4(ctx, gogm, mappedTypes)
for _, db := range gogm.config.TargetDbs {
err := createAllIndexesAndConstraintsV4(ctx, gogm, mappedTypes, db)
if err != nil {
return fmt.Errorf("failed to create indexes and constraints for db %s on db version 4+, %w", db, err)
}
}
} else {
err := createAllIndexesAndConstraintsV3(ctx, gogm, mappedTypes)
if err != nil {
return fmt.Errorf("failed to create indexes and constraints on db version 3, %w", err)
}
}

return createAllIndexesAndConstraintsV3(ctx, gogm, mappedTypes)
return nil
}

//verifies all indexes
func verifyAllIndexesAndConstraints(ctx context.Context, gogm *Gogm, mappedTypes *hashmap.HashMap) error {
if gogm.boltMajorVersion >= 4 {
return verifyAllIndexesAndConstraintsV4(ctx, gogm, mappedTypes)
for _, db := range gogm.config.TargetDbs {
err := verifyAllIndexesAndConstraintsV4(ctx, gogm, mappedTypes, db)
if err != nil {
return fmt.Errorf("failed to verify indexes and constraints for db %s on db version 4+, %w", db, err)
}
}
} else {
err := verifyAllIndexesAndConstraintsV3(ctx, gogm, mappedTypes)
if err != nil {
return fmt.Errorf("failed to verify all indexes and contraints on db version 3, %w", err)
}
}

return verifyAllIndexesAndConstraintsV3(ctx, gogm, mappedTypes)
return nil
}
Loading

0 comments on commit 9f5f9bb

Please sign in to comment.