From 41dfd48117760a36e21ada03f7ffcd1cd7790b76 Mon Sep 17 00:00:00 2001 From: Gert Drapers Date: Tue, 25 Oct 2022 12:41:39 -0700 Subject: [PATCH 1/4] topaz cli remote --- pkg/cli/clients/directory_client.go | 39 +++++++++++++++++++++-------- pkg/cli/cmd/backup.go | 5 ++-- pkg/cli/cmd/importer.go | 5 ++-- pkg/cli/cmd/load.go | 5 ++-- pkg/cli/cmd/restore.go | 5 ++-- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/pkg/cli/clients/directory_client.go b/pkg/cli/clients/directory_client.go index 8a4412e3..e4df8889 100644 --- a/pkg/cli/clients/directory_client.go +++ b/pkg/cli/clients/directory_client.go @@ -1,29 +1,48 @@ package clients import ( - asertogoClient "github.com/aserto-dev/aserto-go/client" + grpcClient "github.com/aserto-dev/aserto-go/client" "github.com/aserto-dev/go-directory-cli/client" "github.com/aserto-dev/topaz/pkg/cli/cc" - "github.com/google/uuid" ) const localhostDirectory = "localhost:9292" -func NewDirectoryClient(c *cc.CommonCtx, address string) (*client.Client, error) { +func NewDirectoryClient(c *cc.CommonCtx, cfg *Config) (*client.Client, error) { - if address == "" { - address = localhostDirectory + if cfg.Host == "" { + cfg.Host = localhostDirectory } - conn, err := asertogoClient.NewConnection( - c.Context, asertogoClient.WithInsecure(true), - asertogoClient.WithAddr(address), - asertogoClient.WithSessionID(uuid.NewString()), - ) + opts := []grpcClient.ConnectionOption{ + grpcClient.WithAddr(cfg.Host), + grpcClient.WithInsecure(cfg.Insecure), + } + + if cfg.APIKey != "" { + opts = append(opts, grpcClient.WithAPIKeyAuth(cfg.APIKey)) + } + + if cfg.SessionID != "" { + opts = append(opts, grpcClient.WithSessionID(cfg.SessionID)) + } + if cfg.TenantID != "" { + opts = append(opts, grpcClient.WithTenantID(cfg.TenantID)) + } + + conn, err := grpcClient.NewConnection(c.Context, opts...) if err != nil { return nil, err } return client.New(conn, c.UI) } + +type Config struct { + Host string `flag:"host" short:"H" help:"" env:"TOPAZ_DIRECTORY_SVC" default:"localhost:9292"` + APIKey string `flag:"api-key" short:"k" help:"" env:"TOPAZ_DIRECTORY_KEY"` + Insecure bool `flag:"insecure" short:"i" help:""` + SessionID string `flag:"session-id" help:""` + TenantID string `flag:"tenant-id" help:""` +} diff --git a/pkg/cli/cmd/backup.go b/pkg/cli/cmd/backup.go index 5a0d79b9..57d791c8 100644 --- a/pkg/cli/cmd/backup.go +++ b/pkg/cli/cmd/backup.go @@ -12,11 +12,12 @@ import ( type BackupCmd struct { File string `arg:"" default:"backup.tar.gz" help:"absolute file path to make backup to"` + clients.Config } const defaultFileName = "backup.tar.gz" -func (cmd BackupCmd) Run(c *cc.CommonCtx) error { +func (cmd *BackupCmd) Run(c *cc.CommonCtx) error { if running, err := dockerx.IsRunning(dockerx.Topaz); !running || err != nil { if err != nil { return err @@ -25,7 +26,7 @@ func (cmd BackupCmd) Run(c *cc.CommonCtx) error { return nil } - dirClient, err := clients.NewDirectoryClient(c, "") + dirClient, err := clients.NewDirectoryClient(c, &cmd.Config) if err != nil { return err } diff --git a/pkg/cli/cmd/importer.go b/pkg/cli/cmd/importer.go index 920bfc66..94de22cc 100644 --- a/pkg/cli/cmd/importer.go +++ b/pkg/cli/cmd/importer.go @@ -12,9 +12,10 @@ import ( type ImportCmd struct { Directory string `short:"d" required:"" help:"directory containing .json data"` + clients.Config } -func (cmd ImportCmd) Run(c *cc.CommonCtx) error { +func (cmd *ImportCmd) Run(c *cc.CommonCtx) error { if running, err := dockerx.IsRunning(dockerx.Topaz); !running || err != nil { if err != nil { return err @@ -29,7 +30,7 @@ func (cmd ImportCmd) Run(c *cc.CommonCtx) error { return err } - dirClient, err := clients.NewDirectoryClient(c, "") + dirClient, err := clients.NewDirectoryClient(c, &cmd.Config) if err != nil { return err } diff --git a/pkg/cli/cmd/load.go b/pkg/cli/cmd/load.go index 0da2d7c7..c58eb19b 100644 --- a/pkg/cli/cmd/load.go +++ b/pkg/cli/cmd/load.go @@ -12,11 +12,12 @@ import ( type LoadCmd struct { File string `arg:"" default:"manifest.yaml" help:"absolute path to manifest file"` + clients.Config } var defaultManifestName = "manifest.yaml" -func (cmd LoadCmd) Run(c *cc.CommonCtx) error { +func (cmd *LoadCmd) Run(c *cc.CommonCtx) error { if running, err := dockerx.IsRunning(dockerx.Topaz); !running || err != nil { if err != nil { return err @@ -25,7 +26,7 @@ func (cmd LoadCmd) Run(c *cc.CommonCtx) error { return nil } - dirClient, err := clients.NewDirectoryClient(c, "") + dirClient, err := clients.NewDirectoryClient(c, &cmd.Config) if err != nil { return err } diff --git a/pkg/cli/cmd/restore.go b/pkg/cli/cmd/restore.go index 6bfec0b1..1f5a00cf 100644 --- a/pkg/cli/cmd/restore.go +++ b/pkg/cli/cmd/restore.go @@ -12,9 +12,10 @@ import ( type RestoreCmd struct { File string `arg:"" default:"backup.tar.gz" help:"absolute file path to local backup tarball"` + clients.Config } -func (cmd RestoreCmd) Run(c *cc.CommonCtx) error { +func (cmd *RestoreCmd) Run(c *cc.CommonCtx) error { if running, err := dockerx.IsRunning(dockerx.Topaz); !running || err != nil { if err != nil { return err @@ -23,7 +24,7 @@ func (cmd RestoreCmd) Run(c *cc.CommonCtx) error { return nil } - dirClient, err := clients.NewDirectoryClient(c, "") + dirClient, err := clients.NewDirectoryClient(c, &cmd.Config) if err != nil { return err } From 4b797815e4895d39895e003c18d16a4d74bc8553 Mon Sep 17 00:00:00 2001 From: Gert Drapers Date: Tue, 25 Oct 2022 13:22:37 -0700 Subject: [PATCH 2/4] enable session-id for restore and import --- pkg/cli/cmd/importer.go | 2 ++ pkg/cli/cmd/restore.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/pkg/cli/cmd/importer.go b/pkg/cli/cmd/importer.go index 94de22cc..4af2468e 100644 --- a/pkg/cli/cmd/importer.go +++ b/pkg/cli/cmd/importer.go @@ -8,6 +8,7 @@ import ( "github.com/aserto-dev/topaz/pkg/cli/clients" "github.com/aserto-dev/topaz/pkg/cli/dockerx" "github.com/fatih/color" + "github.com/google/uuid" ) type ImportCmd struct { @@ -30,6 +31,7 @@ func (cmd *ImportCmd) Run(c *cc.CommonCtx) error { return err } + cmd.Config.SessionID = uuid.NewString() dirClient, err := clients.NewDirectoryClient(c, &cmd.Config) if err != nil { return err diff --git a/pkg/cli/cmd/restore.go b/pkg/cli/cmd/restore.go index 1f5a00cf..d4018141 100644 --- a/pkg/cli/cmd/restore.go +++ b/pkg/cli/cmd/restore.go @@ -8,6 +8,7 @@ import ( "github.com/aserto-dev/topaz/pkg/cli/clients" "github.com/aserto-dev/topaz/pkg/cli/dockerx" "github.com/fatih/color" + "github.com/google/uuid" ) type RestoreCmd struct { @@ -24,6 +25,8 @@ func (cmd *RestoreCmd) Run(c *cc.CommonCtx) error { return nil } + cmd.Config.SessionID = uuid.NewString() + dirClient, err := clients.NewDirectoryClient(c, &cmd.Config) if err != nil { return err From 490a74e91a74efee03d959f9705ca78ac6a506b2 Mon Sep 17 00:00:00 2001 From: Gert Drapers Date: Tue, 25 Oct 2022 15:05:20 -0700 Subject: [PATCH 3/4] deps --- go.mod | 4 ++-- go.sum | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 926f17d6..f4b27c16 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/aserto-dev/topaz go 1.17 -// replace github.com/slok/go-http-metrics => github.com/aserto-dev/go-http-metrics v0.10.1-0.20220201161800-936358c78a53 +replace github.com/aserto-dev/go-directory-cli => ../go-directory-cli require ( github.com/alecthomas/kong v0.6.1 @@ -12,7 +12,7 @@ require ( github.com/aserto-dev/errors v0.0.2 github.com/aserto-dev/go-authorizer v0.20.1 github.com/aserto-dev/go-directory v0.20.1 - github.com/aserto-dev/go-directory-cli v0.20.1 + github.com/aserto-dev/go-directory-cli v0.20.3 github.com/aserto-dev/go-edge-ds v0.20.1 github.com/aserto-dev/go-http-metrics v0.10.1-20221024-1 github.com/aserto-dev/logger v0.0.2 diff --git a/go.sum b/go.sum index 5fda8f08..34511eef 100644 --- a/go.sum +++ b/go.sum @@ -309,8 +309,6 @@ github.com/aserto-dev/go-authorizer v0.20.1 h1:01ry1AmhOhhGVeWyyvTVTnoCG6lYjmY5u github.com/aserto-dev/go-authorizer v0.20.1/go.mod h1:RTpBixDT2WIPOkXcewCXG3NxOWDt22yiXMb+qvdxucM= github.com/aserto-dev/go-directory v0.20.1 h1:tOoZPUeNqtiuy/kd/24oju0aTTug2tt6dx4qnLFxbJA= github.com/aserto-dev/go-directory v0.20.1/go.mod h1:gjg6wZezLGXJj1LBEXaJUS9kpOnaDWeFUYhDG1TAkTY= -github.com/aserto-dev/go-directory-cli v0.20.1 h1:okRavhuGxkBmQmoizkX8EyqIJxunXSbiaqN0YyFWmNg= -github.com/aserto-dev/go-directory-cli v0.20.1/go.mod h1:g2n0U4JN/q51T6dmninbDT0Q7/4JLiaJns+lor//7dc= github.com/aserto-dev/go-edge-ds v0.20.1 h1:lc0E5tBb2Rm/3pa6PQc7RBmBhQktrdyo8ZTLxD2mi0Y= github.com/aserto-dev/go-edge-ds v0.20.1/go.mod h1:AFn7Tt4ShTV+RDPPGGB3LLtUZwB5MVR8IPCFxXYj4+k= github.com/aserto-dev/go-grpc v0.8.0/go.mod h1:hog+zu5jE3+Y6t7m5PIBlavosmbd2qdF0BGZ9ueWK74= From a66b998da1dae3c579279e70be35668dd188ab12 Mon Sep 17 00:00:00 2001 From: Gert Drapers Date: Tue, 25 Oct 2022 15:09:02 -0700 Subject: [PATCH 4/4] deps --- go.mod | 2 -- go.sum | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index f4b27c16..7e3baa69 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/aserto-dev/topaz go 1.17 -replace github.com/aserto-dev/go-directory-cli => ../go-directory-cli - require ( github.com/alecthomas/kong v0.6.1 github.com/aserto-dev/aserto-go v0.8.14-0.20221018123009-a56db85a7ed3 diff --git a/go.sum b/go.sum index 34511eef..42e4770b 100644 --- a/go.sum +++ b/go.sum @@ -309,6 +309,8 @@ github.com/aserto-dev/go-authorizer v0.20.1 h1:01ry1AmhOhhGVeWyyvTVTnoCG6lYjmY5u github.com/aserto-dev/go-authorizer v0.20.1/go.mod h1:RTpBixDT2WIPOkXcewCXG3NxOWDt22yiXMb+qvdxucM= github.com/aserto-dev/go-directory v0.20.1 h1:tOoZPUeNqtiuy/kd/24oju0aTTug2tt6dx4qnLFxbJA= github.com/aserto-dev/go-directory v0.20.1/go.mod h1:gjg6wZezLGXJj1LBEXaJUS9kpOnaDWeFUYhDG1TAkTY= +github.com/aserto-dev/go-directory-cli v0.20.3 h1:bQR0H0UEzLt9ahr7VcGorZ+q8le8tr2OYPzkUWK8sC0= +github.com/aserto-dev/go-directory-cli v0.20.3/go.mod h1:g2n0U4JN/q51T6dmninbDT0Q7/4JLiaJns+lor//7dc= github.com/aserto-dev/go-edge-ds v0.20.1 h1:lc0E5tBb2Rm/3pa6PQc7RBmBhQktrdyo8ZTLxD2mi0Y= github.com/aserto-dev/go-edge-ds v0.20.1/go.mod h1:AFn7Tt4ShTV+RDPPGGB3LLtUZwB5MVR8IPCFxXYj4+k= github.com/aserto-dev/go-grpc v0.8.0/go.mod h1:hog+zu5jE3+Y6t7m5PIBlavosmbd2qdF0BGZ9ueWK74=