Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/aserto-dev/topaz

go 1.27.4

replace github.com/aserto-dev/go-topaz-ui => github.com/wingmans/go-topaz-ui v0.0.0-20261115163309-0a5a8b990ad0

require (
github.com/Masterminds/semver/v3 v3.5.0
github.com/alecthomas/kong v1.17.0
Expand Down Expand Up @@ -71,6 +73,7 @@ require (
dario.cat/mergo v1.0.2 // indirect
github.com/Azure/go-ansiterm v0.0.0-20261006220739-8c912ac31dd4 // indirect
github.com/Microsoft/go-winio v0.6.3-0.20251027160822-ad3df93bed29 // indirect
github.com/a-h/templ v0.3.1118 // indirect
github.com/agnivade/levenshtein v1.2.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/Masterminds/semver/v3 v3.5.0 h1:kQceYJfbupGfZOKZQg0kou0DgAKhzDg2NZPAw
github.com/Masterminds/semver/v3 v3.5.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Microsoft/go-winio v0.6.3-0.20251027160822-ad3df93bed29 h1:0kQAzHq8vLs7Pptv+7TxjdETLf/nIqJpIB4oC6Ba4vY=
github.com/Microsoft/go-winio v0.6.3-0.20251027160822-ad3df93bed29/go.mod h1:ZWa7ssZJT30CCDGJ7fk/2SBTq9BIQrrVjrcss0UW2s0=
github.com/a-h/templ v0.3.1118 h1:rZv90g1xVb7ieNz4j5WK6yQePZfsg7j7fVvP8qVmeJI=
github.com/a-h/templ v0.3.1118/go.mod h1:ttKggixbDULrwkJzfEO5fa4IPGTKiRt3qP09zjPuCS4=
github.com/agnivade/levenshtein v1.2.2 h1:fI8RgiztYX1Nn5pBHHqQ9EMCCklhrmXqPRIedFmx5M4=
github.com/agnivade/levenshtein v1.2.2/go.mod h1:QVVI16kDrtSuwcpd0p1+xMC6Z/VfhtCyDIjcwga4/DU=
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
Expand Down Expand Up @@ -36,8 +38,6 @@ github.com/aserto-dev/go-directory/openapi v0.34.4 h1:nE8hnCd4H7d6uERiZc6JjbBU9g
github.com/aserto-dev/go-directory/openapi v0.34.4/go.mod h1:T702zy7cywSI4Ixq99VkM7Y8PrHGsYx9YrVa+glLkcI=
github.com/aserto-dev/go-directory/pkg v0.34.4 h1:tXj5694sk9OkGSRzmuiELNB8fNiWySN5skQr2hV7opY=
github.com/aserto-dev/go-directory/pkg v0.34.4/go.mod h1:Ajbnf6tL6kZbU6s0wLvLjaqLGKDm0TAOMWSNX/HDbvk=
github.com/aserto-dev/go-topaz-ui v0.1.26 h1:pndp1j5Gbf5Fl6gE5pkYfZuB4TNi4gIY3aJvpvZBLf4=
github.com/aserto-dev/go-topaz-ui v0.1.26/go.mod h1:qQ0kgjgvsLinUFrJtxdbxNl7GF2LJ4naTrRa/gSKPug=
github.com/aserto-dev/logger v0.0.9 h1:QH11l8937Sw+GAe2yvgpoLg70fqQvPrEufkXAmDUk0g=
github.com/aserto-dev/logger v0.0.9/go.mod h1:mMXq/bhdIKoOVsIZ2zJOqgjcc/jR2x14FhU0St/8AVQ=
github.com/authzen/access.go/api v1.0.3 h1:77bdFt50mP5Ay5alFEnEXNqOrwdo2L4VSWapRHTTZgM=
Expand Down Expand Up @@ -339,6 +339,8 @@ github.com/valyala/fastjson v1.6.10 h1:/yjJg8jaVQdYR3arGxPE2X5z89xrlhS0eGXdv+ADT
github.com/valyala/fastjson v1.6.10/go.mod h1:e6FubmQouUNP73jtMLmcbxS6ydWIpOfhz34TSfO3JaE=
github.com/vektah/gqlparser/v2 v2.5.64 h1:N32yM6hQ6Kd5RdjCCEJsUrbCebVoA9yLLMOIa88Mkk0=
github.com/vektah/gqlparser/v2 v2.5.64/go.mod h1:nILRSUbsWDTuf8hSmAgFxuYh0SaMZdkvl6JSEbkJX8g=
github.com/wingmans/go-topaz-ui v0.0.0-20261115163309-0a5a8b990ad0 h1:rKSD3Teg6be9Dd5kabhNDUFOyMFLY3xkaXjfrQTSImM=
github.com/wingmans/go-topaz-ui v0.0.0-20261115163309-0a5a8b990ad0/go.mod h1:5PHu9kWGxiolXyh9fdd/LDoxr3UPC4lTVkHeAo4FG8A=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
Expand Down Expand Up @@ -386,8 +388,8 @@ golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
golang.org/x/exp v0.0.0-20261109185158-a52e3aef5920 h1:qr+tk4P56rtbVVAJDyAr5LAOiaD/XhZCoRTr4SfLxAk=
golang.org/x/exp v0.0.0-20261109185158-a52e3aef5920/go.mod h1:J3U+E8RzytcGvwz+uJuAP4zULJ2TkAQBnmPZMexpdXs=
golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg=
golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs=
golang.org/x/mod v0.43.0 h1:MFuq5J6s35W9D3IjkwyWc9vHh0nENOrt5K8Iof4ofmg=
golang.org/x/mod v0.43.0/go.mod h1:4ec1JnuyBz5+FIVhg86UsA39YG8BpnrZdJYSNHmGzHY=
golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY=
golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI=
golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs=
Expand Down
4 changes: 1 addition & 3 deletions topaz/cmd/templates/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ func (cmd *InstallTemplateCmd) installTemplate(ctx context.Context, cfg *cc.Conf

// 9 - topaz console, launch console so the user start exploring the template artifacts
if !cmd.NoConsole {
command := topaz.ConsoleCmd{
ConsoleAddress: "https://localhost:8080/ui/directory",
}
command := topaz.ConsoleCmd{}
if err := command.Run(ctx); err != nil {
return err
}
Expand Down
97 changes: 90 additions & 7 deletions topaz/cmd/topaz/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,106 @@ package topaz

import (
"context"
"errors"
"fmt"
"strings"

"github.com/aserto-dev/topaz/pkg/config"
"github.com/aserto-dev/topaz/topaz/cc"
"github.com/cli/browser"
)

const (
consoleService = "console"
consoleInitialPath = "/ui/directory/model"
)

var openConsole = browser.OpenURL

type ConsoleCmd struct {
ConsoleAddress string `arg:"" default:"https://localhost:8080/ui/directory" help:"gateway address of the console service"`
ConsoleAddress string `arg:"" optional:"" help:"gateway address of the console service"`
}

func (cmd *ConsoleCmd) Run(_ context.Context) error {
consoleAddress := cmd.ConsoleAddress
if consoleAddress == "" {
cliCfg, err := cc.RequireConfig()
if err != nil {
return err
}

topazCfg, err := getSvcConfig(cliCfg)
if err != nil {
return err
}

consoleAddress, err = configuredConsoleAddress(topazCfg)
if err != nil {
return err
}
}

return openConsole(consoleURL(consoleAddress))
}

func (cmd *ConsoleCmd) Run(ctx context.Context) error {
if !strings.HasSuffix(cmd.ConsoleAddress, "/ui/directory") {
cmd.ConsoleAddress += "/ui/directory"
func getSvcConfig(cliCfg *cc.Config) (*config.Config, error) {
configFile := cliCfg.Running.ConfigFile
if configFile == "" {
configFile = cliCfg.Active.ConfigFile
}
if configFile == "" {
return nil, cc.ErrNoConfig
}

loader, err := config.LoadConfiguration(configFile)
if err != nil {
return nil, fmt.Errorf("load Topaz configuration %q: %w", configFile, err)
}

return loader.Configuration, nil
}

if !strings.HasPrefix(cmd.ConsoleAddress, "https://") {
cmd.ConsoleAddress = "https://" + cmd.ConsoleAddress
func configuredConsoleAddress(cfg *config.Config) (string, error) {
service, ok := cfg.APIConfig.Services[consoleService]
if !ok || service == nil {
return "", errors.New("console service is not configured")
}

return browser.OpenURL(cmd.ConsoleAddress)
address := service.Gateway.FQDN
if address == "" {
address = service.Gateway.ListenAddress
}
if address == "" {
return "", errors.New("console gateway address is not configured")
}

address = clientAddress(address)
if !strings.Contains(address, "://") {
scheme := "https://"
if !service.Gateway.Certs.HasCert() {
scheme = "http://"
}

address = scheme + address
}

return address, nil
}

func consoleURL(address string) string {
address = strings.TrimRight(strings.TrimSpace(address), "/")
if !strings.Contains(address, "://") {
address = "https://" + address
}
if !strings.Contains(address, "/ui/") {
address += consoleInitialPath
} else if strings.HasSuffix(address, "/ui/directory") {
address += "/model"
}

return address
}

func clientAddress(address string) string {
return strings.Replace(address, "0.0.0.0", "localhost", 1)
}
59 changes: 58 additions & 1 deletion topazd/app/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
"strings"

"github.com/aserto-dev/go-aserto"
console "github.com/aserto-dev/go-topaz-ui"
"github.com/aserto-dev/topaz/pkg/config"
"github.com/aserto-dev/topaz/topazd/app/handlers"
"github.com/aserto-dev/topaz/topazd/service/builder"
Expand All @@ -13,7 +15,9 @@ import (
)

const (
consoleService = "console"
consoleService = "console"
defaultAuthorizerAddress = "localhost:8282"
defaultDirectoryAddress = "localhost:9292"
)

type ConsoleService struct{}
Expand Down Expand Up @@ -44,6 +48,59 @@ func (e *ConsoleService) GetGatewayRegistration(port string, services ...string)
func (e *ConsoleService) Close() {
}

// PrepareUIConfig prepares the UI configuration for the console service. #!
func (e *ConsoleService) PrepareUIConfig(cfg *config.Config) *console.Config {
return &console.Config{
Authorizer: uiGRPCClientConfig(cfg.APIConfig.Services[authorizerService], defaultAuthorizerAddress, &cfg.Auth),
Directory: uiGRPCClientConfig(cfg.APIConfig.Services[readerService], defaultDirectoryAddress, &cfg.Auth),
}
}

func uiGRPCClientConfig(service *builder.API, defaultAddress string, auth *config.AuthnConfig) aserto.Config {
clientCfg := aserto.Config{
Address: defaultAddress,
APIKey: firstAPIKey(auth),
NoTLS: true,
}

if service == nil {
return clientCfg
}

clientCfg.Address = service.GRPC.FQDN
if clientCfg.Address == "" {
clientCfg.Address = service.GRPC.ListenAddress
}
if clientCfg.Address == "" {
clientCfg.Address = defaultAddress
}

clientCfg.Address = serviceAddress(clientCfg.Address)
clientCfg.NoTLS = !service.GRPC.Certs.HasCert()
if !clientCfg.NoTLS {
clientCfg.CACertPath = service.GRPC.Certs.CA
clientCfg.Insecure = true
}

return clientCfg
}

func firstAPIKey(auth *config.AuthnConfig) string {
if auth == nil {
return ""
}

if len(auth.Keys) > 0 {
return auth.Keys[0]
}

for key := range auth.APIKeys {
return key
}

return ""
}

func (e *ConsoleService) PrepareConfig(cfg *config.Config) *handlers.TopazCfg {
directoryServiceURL := serviceAddress("https://" + strings.Split(cfg.DirectoryResolver.Address, ":")[0])

Expand Down
16 changes: 12 additions & 4 deletions topazd/app/topaz.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"context"
"net/http"

cerr "github.com/aserto-dev/errors"
console "github.com/aserto-dev/go-topaz-ui"
Expand Down Expand Up @@ -197,9 +196,18 @@ func (e *Topaz) ConfigServices() error {
// config service.
server.Gateway.Mux.Handle("/api/v2/config", apiKeyAuthMiddleware.ConfigAuth(handlers.ConfigHandlerV2(consoleConfig), e.Configuration.Auth))
server.Gateway.Mux.HandleFunc("/api/v1/authorizers", handlers.AuthorizersHandler(consoleConfig))
// console service. depends on config service.
server.Gateway.Mux.Handle("/ui/", handlers.UIHandler(http.FS(console.FS)))
server.Gateway.Mux.Handle("/assets/", handlers.UIHandler(http.FS(console.FS)))

// key registration point for the console UI
closeConsole, err := console.RegisterHandlers(server.Gateway.Mux, consoleSvc.PrepareUIConfig(e.Configuration))
if err != nil {
return errors.Wrap(err, "failed to register console handlers")
}

server.Cleanup = append(server.Cleanup, func() {
if err := closeConsole(); err != nil {
e.Logger.Error().Err(err).Msg("failed to close console clients")
}
})
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions topazd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ func main() {

rootCmd.AddCommand(cmdRun)

_ = cmdRun.MarkFlagRequired("config-file")

rootCmd.AddCommand(
versionCmd,
)
Expand Down
40 changes: 39 additions & 1 deletion topazd/topaz_run.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package main

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/aserto-dev/topaz/pkg/config"
"github.com/aserto-dev/topaz/topaz/cc"
"github.com/aserto-dev/topaz/topazd/app"
"github.com/aserto-dev/topaz/topazd/app/directory"
"github.com/aserto-dev/topaz/topazd/app/topaz"
Expand All @@ -20,6 +25,10 @@ var (
debugService *debug.Server
)

var loadCLIConfig = func(ctx context.Context) (*cc.Config, error) {
return cc.NewConfig(ctx, true, filepath.Join(cc.GetTopazDir(), "topaz.json"))
}

var cmdRun = &cobra.Command{
Use: "run [args]",
Short: "Start Topaz authorization service",
Expand All @@ -28,7 +37,12 @@ var cmdRun = &cobra.Command{
}

func run(cmd *cobra.Command, args []string) error {
configPath := config.Path(flagRunConfigFile)
configFile, err := resolveRunConfigFile(cmd.Context(), flagRunConfigFile, flagRunDebug)
if err != nil {
return err
}

configPath := config.Path(configFile)

topazApp, cleanup, err := topaz.BuildApp(os.Stdout, os.Stderr, configPath, configOverrides)
if err != nil {
Expand Down Expand Up @@ -86,6 +100,30 @@ func run(cmd *cobra.Command, args []string) error {
return nil
}

func resolveRunConfigFile(ctx context.Context, configFile string, debug bool) (string, error) {
if configFile != "" {
return configFile, nil
}

if !debug {
return "", errors.New("config-file is required")
}

cliConfig, err := loadCLIConfig(ctx)
if err != nil {
return "", fmt.Errorf("load CLI configuration: %w", err)
}

if cliConfig.Running.ConfigFile != "" {
return cliConfig.Running.ConfigFile, nil
}
if cliConfig.Active.ConfigFile != "" {
return cliConfig.Active.ConfigFile, nil
}

return "", cc.ErrNoConfig
}

func configOverrides(cfg *config.Config) {
cfg.Command.Mode = config.CommandModeRun

Expand Down
Loading