Skip to content

Commit

Permalink
topaz config info (#372)
Browse files Browse the repository at this point in the history
* add InfoConfigCmd

* return status code 0 if no args

* impl topaz config info

* refactor flow env as kong vars, via cc.*

* refactor map to struct to preserve field ordering
  • Loading branch information
gertd committed May 5, 2024
1 parent fb4ce80 commit 74cd948
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 25 deletions.
19 changes: 19 additions & 0 deletions cmd/topaz/main.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"

"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/aserto-dev/topaz/pkg/cli/cmd"
Expand Down Expand Up @@ -45,6 +46,7 @@ func main() {
kong.Name(x.AppName),
kong.Description(x.AppDescription),
kong.UsageOnError(),
kong.Exit(exit),
kong.ConfigureHelp(kong.HelpOptions{
NoAppSummary: false,
Summary: false,
Expand All @@ -64,6 +66,15 @@ func main() {
"container_tag": cc.ContainerTag(),
"container_platform": cc.ContainerPlatform(),
"container_name": cc.ContainerName(ctx.Config.Active.ConfigFile),
"directory_svc": cc.DirectorySvc(),
"directory_key": cc.DirectoryKey(),
"directory_token": cc.DirectoryToken(),
"authorizer_svc": cc.AuthorizerSvc(),
"authorizer_key": cc.AuthorizerKey(),
"authorizer_token": cc.AuthorizerToken(),
"tenant_id": cc.TenantID(),
"insecure": strconv.FormatBool(cc.Insecure()),
"no_check": strconv.FormatBool(cc.NoCheck()),
},
)
zerolog.SetGlobalLevel(logLevel(cli.LogLevel))
Expand Down Expand Up @@ -111,3 +122,11 @@ func checkDBFiles(topazDBDir string) (bool, error) {

return len(files) > 0, nil
}

// set status code to 0 when executing with no arguments, help only output.
func exit(rc int) {
if len(os.Args) == 1 {
os.Exit(0)
}
os.Exit(rc)
}
85 changes: 85 additions & 0 deletions pkg/cli/cc/client.go
@@ -0,0 +1,85 @@
package cc

import (
"os"
"strconv"
)

const (
defaultDirectorySvc = "localhost:9292"
defaultDirectoryKey = ""
defaultDirectoryToken = ""
defaultAuthorizerSvc = "localhost:8282"
defaultAuthorizerKey = ""
defaultAuthorizerToken = ""
defaultTenantID = ""
defaultInsecure = false
defaultNoCheck = false
)

func DirectorySvc() string {
if directorySvc := os.Getenv("TOPAZ_DIRECTORY_SVC"); directorySvc != "" {
return directorySvc
}
return defaultDirectorySvc
}

func DirectoryKey() string {
if directoryKey := os.Getenv("TOPAZ_DIRECTORY_KEY"); directoryKey != "" {
return directoryKey
}
return defaultDirectoryKey
}

func DirectoryToken() string {
if directoryToken := os.Getenv("TOPAZ_DIRECTORY_TOKEN"); directoryToken != "" {
return directoryToken
}
return defaultDirectoryToken
}

func AuthorizerSvc() string {
if authorizerSvc := os.Getenv("TOPAZ_AUTHORIZER_SVC"); authorizerSvc != "" {
return authorizerSvc
}
return defaultAuthorizerSvc
}

func AuthorizerKey() string {
if authorizerKey := os.Getenv("TOPAZ_AUTHORIZER_KEY"); authorizerKey != "" {
return authorizerKey
}
return defaultAuthorizerKey
}

func AuthorizerToken() string {
if authorizerToken := os.Getenv("TOPAZ_AUTHORIZER_TOKEN"); authorizerToken != "" {
return authorizerToken
}
return defaultAuthorizerToken
}

func TenantID() string {
if tenantID := os.Getenv("ASERTO_TENANT_ID"); tenantID != "" {
return tenantID
}
return defaultTenantID
}

func Insecure() bool {
if insecure := os.Getenv("TOPAZ_INSECURE"); insecure != "" {
if b, err := strconv.ParseBool(insecure); err == nil {
return b
}
}
return defaultInsecure
}

func NoCheck() bool {
if noCheck := os.Getenv("TOPAZ_NO_CHECK"); noCheck != "" {
if b, err := strconv.ParseBool(noCheck); err == nil {
return b
}
}
return defaultNoCheck
}
19 changes: 7 additions & 12 deletions pkg/cli/clients/authorizer_client.go
Expand Up @@ -2,6 +2,7 @@ package clients

import (
"context"
"fmt"

azc "github.com/aserto-dev/go-aserto/client"
"github.com/fullstorydev/grpcurl"
Expand All @@ -14,23 +15,17 @@ import (
"github.com/aserto-dev/topaz/pkg/cli/cc"
)

const (
localhostAuthorizer string = "localhost:8282"
EnvTopazAuthorizerSvc string = "TOPAZ_AUTHORIZER_SVC"
EnvTopazAuthorizerKey string = "TOPAZ_AUTHORIZER_KEY"
)

type AuthorizerConfig struct {
Host string `flag:"host" short:"H" env:"TOPAZ_AUTHORIZER_SVC" help:"authorizer service address"`
APIKey string `flag:"api-key" short:"k" env:"TOPAZ_AUTHORIZER_KEY" help:"authorizer API key"`
Token string `flag:"token" short:"t" env:"TOPAZ_AUTHORIZER_TOKEN" help:"authorizer OAuth2.0 token" hidden:""`
Insecure bool `flag:"insecure" short:"i" env:"INSECURE" help:"skip TLS verification"`
TenantID string `flag:"tenant-id" help:"" env:"ASERTO_TENANT_ID" `
Host string `flag:"host" short:"H" default:"${authorizer_svc}" env:"TOPAZ_AUTHORIZER_SVC" help:"authorizer service address"`
APIKey string `flag:"api-key" short:"k" default:"${authorizer_key}" env:"TOPAZ_AUTHORIZER_KEY" help:"authorizer API key"`
Token string `flag:"token" short:"t" default:"${authorizer_token}" env:"TOPAZ_AUTHORIZER_TOKEN" help:"authorizer OAuth2.0 token" hidden:""`
Insecure bool `flag:"insecure" short:"i" default:"${insecure}" env:"TOPAZ_INSECURE" help:"skip TLS verification"`
TenantID string `flag:"tenant-id" help:"" default:"${tenant_id}" env:"ASERTO_TENANT_ID" `
}

func NewAuthorizerClient(c *cc.CommonCtx, cfg *AuthorizerConfig) (authorizer.AuthorizerClient, error) {
if cfg.Host == "" {
cfg.Host = localhostAuthorizer
return nil, fmt.Errorf("no host specified")
}

if err := cfg.validate(); err != nil {
Expand Down
20 changes: 7 additions & 13 deletions pkg/cli/clients/directory_client.go
Expand Up @@ -2,6 +2,7 @@ package clients

import (
"context"
"fmt"

"github.com/aserto-dev/go-aserto/client"
dsc "github.com/aserto-dev/go-directory-cli/client"
Expand All @@ -13,24 +14,17 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

const (
localhostDirectory string = "localhost:9292"
EnvTopazDirectorySvc string = "TOPAZ_DIRECTORY_SVC"
EnvTopazDirectoryKey string = "TOPAZ_DIRECTORY_KEY"
)

type DirectoryConfig struct {
Host string `flag:"host" short:"H" env:"TOPAZ_DIRECTORY_SVC" help:"directory service address"`
APIKey string `flag:"api-key" short:"k" env:"TOPAZ_DIRECTORY_KEY" help:"directory API key"`
Token string `flag:"token" short:"t" env:"TOPAZ_DIRECTORY_TOKEN" help:"directory OAuth2.0 token" hidden:""`
Insecure bool `flag:"insecure" short:"i" env:"INSECURE" help:"skip TLS verification"`
TenantID string `flag:"tenant-id" help:"" env:"ASERTO_TENANT_ID" `
Host string `flag:"host" short:"H" default:"${directory_svc}" env:"TOPAZ_DIRECTORY_SVC" help:"directory service address"`
APIKey string `flag:"api-key" short:"k" default:"${directory_key}" env:"TOPAZ_DIRECTORY_KEY" help:"directory API key"`
Token string `flag:"token" short:"t" default:"${directory_token}" env:"TOPAZ_DIRECTORY_TOKEN" help:"directory OAuth2.0 token" hidden:""`
Insecure bool `flag:"insecure" short:"i" default:"${insecure}" env:"TOPAZ_INSECURE" help:"skip TLS verification"`
TenantID string `flag:"tenant-id" help:"" default:"${tenant_id}" env:"ASERTO_TENANT_ID" `
}

func NewDirectoryClient(c *cc.CommonCtx, cfg *DirectoryConfig) (*dsc.Client, error) {

if cfg.Host == "" {
cfg.Host = localhostDirectory
return nil, fmt.Errorf("no host specified")
}

if err := cfg.validate(); err != nil {
Expand Down
95 changes: 95 additions & 0 deletions pkg/cli/cmd/config.go
Expand Up @@ -2,13 +2,15 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/adrg/xdg"
"github.com/alecthomas/kong"
"github.com/aserto-dev/topaz/pkg/cc/config"
"github.com/aserto-dev/topaz/pkg/cli/cc"
Expand All @@ -22,6 +24,7 @@ type ConfigCmd struct {
List ListConfigCmd `cmd:"" help:"list configurations"`
Rename RenameConfigCmd `cmd:"" help:"rename configuration"`
Delete DeleteConfigCmd `cmd:"" help:"delete configuration"`
Info InfoConfigCmd `cmd:"" help:"display configuration information"`
}

var restrictedNamePattern = regexp.MustCompile(`^[a-z0-9][a-z0-9_.-]*$`)
Expand Down Expand Up @@ -243,3 +246,95 @@ func (cmd ListConfigCmd) Run(c *cc.CommonCtx) error {

return nil
}

type InfoConfigCmd struct{}

func (cmd InfoConfigCmd) Run(c *cc.CommonCtx) error {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
return enc.Encode(cmd.info(c))
}

type Info struct {
Environment struct {
Home string `json:"home"`
XdgConfigHome string `json:"xdg_config_home"`
XdgDataHome string `json:"xdg_data_home"`
} `json:"environment"`
Config struct {
TopazCfgDir string `json:"topaz_cfg_dir"`
TopazCertsDir string `json:"topaz_certs_dir"`
TopazDataDir string `json:"topaz_db_dir"`
TopazDir string `json:"topaz_dir"`
} `json:"config"`
Runtime struct {
ActiveConfigurationName string `json:"active_configuration_name"`
ActiveConfigurationFile string `json:"active_configuration_file"`
RunningConfigurationName string `json:"running_configuration_name"`
RunningConfigurationFile string `json:"running_configuration_file"`
RunningContainerName string `json:"running_container_name"`
TopazConfigFile string `json:"topaz_json"`
} `json:"runtime"`
Default struct {
ContainerRegistry string `json:"container_registry"`
ContainerImage string `json:"container_image"`
ContainerTag string `json:"container_tag"`
ContainerPlatform string `json:"container_platform"`
NoCheck bool `json:"topaz_no_check"`
} `json:"default"`
Directory struct {
DirectorySvc string `json:"topaz_directory_svc"`
DirectoryKey string `json:"topaz_directory_key"`
DirectoryToken string `json:"topaz_directory_token"`
Insecure bool `json:"topaz_insecure"`
TenantID string `json:"aserto_tenant_id"`
} `json:"directory"`
Authorizer struct {
AuthorizerSvc string `json:"topaz_authorizer_svc"`
AuthorizerKey string `json:"topaz_authorizer_key"`
AuthorizerToken string `json:"topaz_authorizer_token"`
Insecure bool `json:"topaz_insecure"`
TenantID string `json:"aserto_tenant_id"`
} `json:"authorizer"`
}

func (cmd InfoConfigCmd) info(c *cc.CommonCtx) *Info {
info := Info{}

info.Environment.Home = xdg.Home
info.Environment.XdgConfigHome = xdg.ConfigHome
info.Environment.XdgDataHome = xdg.DataHome

info.Config.TopazCfgDir = cc.GetTopazCfgDir()
info.Config.TopazCertsDir = cc.GetTopazCertsDir()
info.Config.TopazDataDir = cc.GetTopazDataDir()
info.Config.TopazDir = cc.GetTopazDir()

info.Runtime.ActiveConfigurationName = c.Config.Active.Config
info.Runtime.ActiveConfigurationFile = c.Config.Active.ConfigFile
info.Runtime.RunningConfigurationName = c.Config.Running.Config
info.Runtime.RunningConfigurationFile = c.Config.Running.ConfigFile
info.Runtime.RunningContainerName = c.Config.Running.ContainerName
info.Runtime.TopazConfigFile = filepath.Join(cc.GetTopazDir(), CLIConfigurationFile)

info.Default.ContainerRegistry = cc.ContainerRegistry()
info.Default.ContainerImage = cc.ContainerImage()
info.Default.ContainerTag = cc.ContainerTag()
info.Default.ContainerPlatform = cc.ContainerPlatform()
info.Default.NoCheck = cc.NoCheck()

info.Directory.DirectorySvc = cc.DirectorySvc()
info.Directory.DirectoryKey = cc.DirectoryKey()
info.Directory.DirectoryToken = cc.DirectoryToken()
info.Directory.Insecure = cc.Insecure()
info.Directory.TenantID = cc.TenantID()

info.Authorizer.AuthorizerSvc = cc.AuthorizerSvc()
info.Authorizer.AuthorizerKey = cc.AuthorizerKey()
info.Authorizer.AuthorizerToken = cc.AuthorizerToken()
info.Authorizer.Insecure = cc.Insecure()
info.Authorizer.TenantID = cc.TenantID()

return &info
}

0 comments on commit 74cd948

Please sign in to comment.