Skip to content

Commit

Permalink
Update directory configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
carabasdaniel committed Jun 9, 2023
1 parent e11a2bf commit cb801ef
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 33 deletions.
44 changes: 37 additions & 7 deletions directory/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
package directory

import (
grpcc "github.com/aserto-dev/go-aserto/client"
"github.com/aserto-dev/go-edge-ds/pkg/directory"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)

type Config struct {
EdgeConfig directory.Config `json:"edge"`
Remote struct {
Addr string `json:"address"`
Key string `json:"api_key"`
Insecure bool `json:"insecure"`
TenantID string `json:"tenant_id"`
} `json:"remote"`
Config map[string]interface{} `json:"config"`
}

func (cfg *Config) ToRemoteConfig() (*grpcc.Config, error) {
grpcCfg := grpcc.Config{}
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &grpcCfg,
TagName: "json",
})
if err != nil {
return nil, errors.Wrap(err, "error decoding file decision logger config")
}
err = dec.Decode(cfg.Config)
if err != nil {
return nil, errors.Wrap(err, "error decoding file decision logger config")
}

return &grpcCfg, nil
}

func (cfg *Config) ToEdgeConfig() (*directory.Config, error) {
edgeCfg := directory.Config{}
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &edgeCfg,
TagName: "json",
})
if err != nil {
return nil, errors.Wrap(err, "error decoding file decision logger config")
}
err = dec.Decode(cfg.Config)
if err != nil {
return nil, errors.Wrap(err, "error decoding file decision logger config")
}
return &edgeCfg, nil
}
18 changes: 13 additions & 5 deletions pkg/app/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ type Authorizer struct {

// Start starts all services required by the engine.
func (e *Authorizer) Start() error {
if (strings.Contains(e.Configuration.Directory.Remote.Addr, "localhost") || strings.Contains(e.Configuration.Directory.Remote.Addr, "0.0.0.0")) &&
e.Configuration.Directory.EdgeConfig.DBPath != "" {
addr := strings.Split(e.Configuration.Directory.Remote.Addr, ":")
remoteConfig, err := e.Configuration.Directory.ToRemoteConfig()
if err != nil {
return err
}
edgeConfig, err := e.Configuration.Directory.ToEdgeConfig()
if err != nil {
return err
}
if (strings.Contains(remoteConfig.Address, "localhost") || strings.Contains(remoteConfig.Address, "0.0.0.0")) &&
edgeConfig.DBPath != "" {
addr := strings.Split(remoteConfig.Address, ":")
if len(addr) != 2 {
return errors.Errorf("invalid remote address - should contain <host>:<port>")
}
Expand All @@ -38,7 +46,7 @@ func (e *Authorizer) Start() error {
}

edge, err := edgeServer.NewEdgeServer(
e.Configuration.Directory.EdgeConfig,
*edgeConfig,
&e.Configuration.API.GRPC.Certs,
addr[0],
port,
Expand All @@ -51,7 +59,7 @@ func (e *Authorizer) Start() error {
e.Server.RegisterServer("edgeDirServer", edge.Start, edge.Stop)
}

err := e.Server.Start(e.Context)
err = e.Server.Start(e.Context)
if err != nil {
return errors.Wrap(err, "failed to start engine server")
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/app/directory/simple_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,35 @@ import (
grpcc "github.com/aserto-dev/go-aserto/client"
ds2 "github.com/aserto-dev/go-directory/aserto/directory/reader/v2"

"github.com/aserto-dev/topaz/directory"
"github.com/aserto-dev/topaz/resolvers"
"github.com/rs/zerolog"
)

type Resolver struct {
logger *zerolog.Logger
cfg *directory.Config
cfg *grpcc.Config
dirConn *grpcc.Connection
}

var _ resolvers.DirectoryResolver = &Resolver{}

func NewResolver(logger *zerolog.Logger, cfg *directory.Config) resolvers.DirectoryResolver {
func NewResolver(logger *zerolog.Logger, cfg *grpcc.Config) resolvers.DirectoryResolver {
return &Resolver{
logger: logger,
cfg: cfg,
}
}

func connect(logger *zerolog.Logger, cfg *directory.Config) (*grpcc.Connection, error) {
logger.Debug().Str("tenant-id", cfg.Remote.TenantID).Str("addr", cfg.Remote.Addr).Str("apiKey", cfg.Remote.Key).Bool("insecure", cfg.Remote.Insecure).Msg("GetDS")
func connect(logger *zerolog.Logger, cfg *grpcc.Config) (*grpcc.Connection, error) {
logger.Debug().Str("tenant-id", cfg.TenantID).Str("addr", cfg.Address).Str("apiKey", cfg.APIKey).Bool("insecure", cfg.Insecure).Msg("GetDS")

ctx := context.Background()

conn, err := grpcc.NewConnection(ctx,
grpcc.WithAddr(cfg.Remote.Addr),
grpcc.WithAPIKeyAuth(cfg.Remote.Key),
grpcc.WithTenantID(cfg.Remote.TenantID),
grpcc.WithInsecure(cfg.Remote.Insecure),
grpcc.WithAddr(cfg.Address),
grpcc.WithAPIKeyAuth(cfg.APIKey),
grpcc.WithTenantID(cfg.TenantID),
grpcc.WithInsecure(cfg.Insecure),
)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/tests/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (

func TestWithMissingIdentity(t *testing.T) {
harness := atesting.SetupOnline(t, func(cfg *config.Config) {
cfg.Directory.EdgeConfig.DBPath = atesting.AssetAcmeEBBFilePath()
cfg.Directory.Remote.Addr = "localhost:12345"
cfg.Directory.Config["db_path"] = atesting.AssetAcmeEBBFilePath()
cfg.Directory.Config["address"] = "localhost:12345"
})
defer harness.Cleanup()

Expand Down
3 changes: 1 addition & 2 deletions pkg/app/tests/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ var _ = Describe("Engine GRPC Server", func() {

BeforeEach(func() {
h = testing.SetupOffline(ginkgoT, func(cfg *config.Config) {
cfg.Directory.EdgeConfig.DBPath = testing.AssetAcmeEBBFilePath()
cfg.OPA.LocalBundles.Paths = []string{testing.AssetLocalBundle()}
cfg.Directory.Config["db_path"] = testing.AssetAcmeEBBFilePath()
})
})

Expand Down
3 changes: 1 addition & 2 deletions pkg/app/tests/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (

func TestPolicy(t *testing.T) {
harness := atesting.SetupOnline(t, func(cfg *config.Config) {
cfg.Directory.EdgeConfig.DBPath = atesting.AssetAcmeEBBFilePath()
cfg.Directory.Remote.Addr = "localhost:12346"
cfg.Directory.Config["db_path"] = atesting.AssetAcmeEBBFilePath()
})
defer harness.Cleanup()

Expand Down
2 changes: 1 addition & 1 deletion pkg/app/tests/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var _ = Describe("Engine Runtime", func() {

BeforeEach(func() {
h = testing.SetupOffline(ginkgoT, func(cfg *config.Config) {
cfg.Directory.EdgeConfig.DBPath = testing.AssetAcmeEBBFilePath()
cfg.Directory.Config["db_path"] = testing.AssetAcmeEBBFilePath()
cfg.OPA.LocalBundles.Paths = []string{testing.AssetLocalBundle()}
})
})
Expand Down
6 changes: 5 additions & 1 deletion pkg/app/topaz/directory_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ func DirectoryResolver(
logger *zerolog.Logger,
cfg *config.Config) resolvers.DirectoryResolver {

return directory.NewResolver(logger, &cfg.Directory)
dirCfg, err := cfg.Directory.ToRemoteConfig()
if err != nil {
logger.Error().Err(err).Msg("cannot configure directory resolver")
}
return directory.NewResolver(logger, dirCfg)
}
3 changes: 1 addition & 2 deletions pkg/testing/assets/config-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ logging:
log_level: debug

directory_service:
edge:
config:
db_path: /tmp/edgeds.db
seed_metadata: false
remote:
address: "localhost:9292"
insecure: true

Expand Down
3 changes: 1 addition & 2 deletions pkg/testing/assets/config-online.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ logging:
log_level: debug

directory_service:
edge:
config:
db_path: /tmp/edgeds.db
seed_metadata: false
remote:
insecure: true

api:
Expand Down

0 comments on commit cb801ef

Please sign in to comment.