Skip to content

Commit

Permalink
Refactored checks into functions and added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jensalm committed Jan 8, 2025
1 parent 309eeb7 commit 159fac2
Show file tree
Hide file tree
Showing 31 changed files with 686 additions and 116 deletions.
4 changes: 3 additions & 1 deletion internal/ascode/cache_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package ascode

import (
"errors"
log "github.com/sirupsen/logrus"
"github.com/michaelquigley/pfxlog"
"reflect"
)

type CacheGetter func(id string) (interface{}, error)

var log = pfxlog.Logger()

func GetItemFromCache(c map[string]interface{}, key string, fn CacheGetter) (interface{}, error) {
if key == "" {
return nil, errors.New("key is null, can't resolve from cache or get it from source")
Expand Down
7 changes: 5 additions & 2 deletions internal/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import "io"
/*
Extends the standard FPrintF with overwriting the current line because it has the `\u001B[2K`
*/
func FPrintFReusingLine(writer io.Writer, format string, a ...any) (n int, err error) {
return fmt.Fprintf(writer, "\u001B[2K"+format, a)
func FPrintfReusingLine(writer io.Writer, format string, a ...any) (n int, err error) {
return fmt.Fprintf(writer, "\u001B[2K"+format+"\r", a...)
}
func FPrintflnReusingLine(writer io.Writer, format string, a ...any) (n int, err error) {
return FPrintfReusingLine(writer, format+"\n", a...)
}
45 changes: 15 additions & 30 deletions ziti/cmd/ascode/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,15 @@ func (d *Exporter) Execute(input []string) error {

result := map[string]interface{}{}

all := slices.Contains(args, "all") || len(args) == 0
if all ||
slices.Contains(args, "ca") ||
slices.Contains(args, "certificate-authority") {
if d.IsCertificateAuthorityExportRequired(args) {
log.Debug("Processing Certificate Authorities")
cas, err := d.GetCertificateAuthorities()
if err != nil {
return err
}
result["certificateAuthorities"] = cas
}
if all ||
slices.Contains(args, "identity") {
if d.IsIdentityExportRequired(args) {
log.Debug("Processing Identities")
identities, err := d.GetIdentities()
if err != nil {
Expand All @@ -178,90 +174,79 @@ func (d *Exporter) Execute(input []string) error {
result["identities"] = identities
}

if all ||
slices.Contains(args, "edge-router") ||
slices.Contains(args, "er") {
if d.IsEdgeRouterExportRequired(args) {
log.Debug("Processing Edge Routers")
routers, err := d.GetEdgeRouters()
if err != nil {
return err
}
result["edgeRouters"] = routers
}
if all ||
slices.Contains(args, "service") {
if d.IsServiceExportRequired(args) {
log.Debug("Processing Services")
services, err := d.GetServices()
if err != nil {
return err
}
result["services"] = services
}
if all ||
slices.Contains(args, "config") {
if d.IsConfigExportRequired(args) {
log.Debug("Processing Configs")
configs, err := d.GetConfigs()
if err != nil {
return err
}
result["configs"] = configs
}
if all ||
slices.Contains(args, "config-type") {
if d.IsConfigTypeExportRequired(args) {
log.Debug("Processing Config Types")
configTypes, err := d.GetConfigTypes()
if err != nil {
return err
}
result["configTypes"] = configTypes
}
if all ||
slices.Contains(args, "service-policy") {
if d.IsServicePolicyExportRequired(args) {
log.Debug("Processing Service Policies")
servicePolicies, err := d.GetServicePolicies()
if err != nil {
return err
}
result["servicePolicies"] = servicePolicies
}
if all ||
slices.Contains(args, "edge-router-policy") {
if d.IsEdgeRouterExportRequired(args) {
log.Debug("Processing Edge Router Policies")
routerPolicies, err := d.GetRouterPolicies()
routerPolicies, err := d.GetEdgeRouterPolicies()
if err != nil {
return err
}
result["edgeRouterPolicies"] = routerPolicies
}
if all ||
slices.Contains(args, "service-edge-router-policy") {
if d.IsServiceEdgeRouterPolicyExportRequired(args) {
log.Debug("Processing Service Edge Router Policies")
serviceRouterPolicies, err := d.GetServiceEdgeRouterPolicies()
if err != nil {
return err
}
result["serviceEdgeRouterPolicies"] = serviceRouterPolicies
}
if all ||
slices.Contains(args, "external-jwt-signer") {
if d.IsExtJwtSignerExportRequired(args) {
log.Debug("Processing External JWT Signers")
externalJwtSigners, err := d.GetExternalJwtSigners()
if err != nil {
return err
}
result["externalJwtSigners"] = externalJwtSigners
}
if all ||
slices.Contains(args, "auth-policy") {
if d.IsAuthPolicyExportRequired(args) {
log.Debug("Processing Auth Policies")
authPolicies, err := d.GetAuthPolicies()
if err != nil {
return err
}
result["authPolicies"] = authPolicies
}
if all ||
slices.Contains(args, "posture-check") {
if d.IsPostureCheckExportRequired(args) {
log.Debug("Processing Posture Checks")
postureChecks, err := d.GetPostureChecks()
if err != nil {
Expand Down Expand Up @@ -304,7 +289,7 @@ func (d *Exporter) getEntities(entityName string, count ClientCount, list Client
more := true
for more {
resp, err := list(&offset, &limit)
_, _ = internal.FPrintFReusingLine(d.loginOpts.Err, "Reading %d/%d %s\r", offset, totalCount, entityName)
_, _ = internal.FPrintfReusingLine(d.loginOpts.Err, "Reading %d/%d %s", offset, totalCount, entityName)
if err != nil {
return nil, errors.Join(errors.New("error reading "+entityName), err)
}
Expand All @@ -323,7 +308,7 @@ func (d *Exporter) getEntities(entityName string, count ClientCount, list Client
offset += limit
}

_, _ = internal.FPrintFReusingLine(d.loginOpts.Err, "Read %d %s\r\n", len(result), entityName)
_, _ = internal.FPrintflnReusingLine(d.loginOpts.Err, "Read %d %s", len(result), entityName)

return result, nil

Expand Down
6 changes: 6 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_auth_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ import (
"github.com/openziti/edge-api/rest_management_api_client/external_jwt_signer"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/ziti/internal/ascode"
"slices"
)

func (d Exporter) IsAuthPolicyExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "auth-policy")
}

func (d Exporter) GetAuthPolicies() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
7 changes: 7 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_certificate_authorities.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ package exporter
import (
"github.com/openziti/edge-api/rest_management_api_client/certificate_authority"
"github.com/openziti/edge-api/rest_model"
"slices"
)

func (d Exporter) IsCertificateAuthorityExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "ca") ||
slices.Contains(args, "certificate-authority")
}

func (d Exporter) GetCertificateAuthorities() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
5 changes: 5 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"slices"
)

func (d Exporter) IsConfigTypeExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "config-type")
}

func (d Exporter) GetConfigTypes() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
6 changes: 6 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ import (
"github.com/openziti/edge-api/rest_management_api_client/config"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/ziti/internal/ascode"
"slices"
)

func (d Exporter) IsConfigExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "config")
}

func (d Exporter) GetConfigs() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
8 changes: 7 additions & 1 deletion ziti/cmd/ascode/exporter/exporter_edgerouter_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ package exporter
import (
"github.com/openziti/edge-api/rest_management_api_client/edge_router_policy"
"github.com/openziti/edge-api/rest_model"
"slices"
)

func (d Exporter) GetRouterPolicies() ([]map[string]interface{}, error) {
func (d Exporter) IsEdgeRouterPolicyExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "edge-router-policy")
}

func (d Exporter) GetEdgeRouterPolicies() ([]map[string]interface{}, error) {

return d.getEntities(
"EdgeRouterPolicies",
Expand Down
7 changes: 7 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_edgerouters.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ package exporter
import (
"github.com/openziti/edge-api/rest_management_api_client/edge_router"
"github.com/openziti/edge-api/rest_model"
"slices"
)

func (d Exporter) IsEdgeRouterExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "edge-router") ||
slices.Contains(args, "er")
}

func (d Exporter) GetEdgeRouters() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
7 changes: 7 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_external_jwt_signers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ package exporter
import (
"github.com/openziti/edge-api/rest_management_api_client/external_jwt_signer"
"github.com/openziti/edge-api/rest_model"
"slices"
)

func (d Exporter) IsExtJwtSignerExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "ext-jwt-signer") ||
slices.Contains(args, "external-jwt-signer")
}

func (d Exporter) GetExternalJwtSigners() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
6 changes: 6 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_identities.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import (
"github.com/openziti/edge-api/rest_management_api_client/identity"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/ziti/internal/ascode"
"slices"
)

func (d Exporter) IsIdentityExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "identity")
}

func (d Exporter) GetIdentities() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
6 changes: 6 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_posture_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ package exporter

import (
"github.com/openziti/edge-api/rest_management_api_client/posture_checks"
"golang.org/x/exp/slices"
)

func (d Exporter) IsPostureCheckExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "posture-check")
}

func (d Exporter) GetPostureChecks() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ package exporter
import (
"github.com/openziti/edge-api/rest_management_api_client/service_edge_router_policy"
"github.com/openziti/edge-api/rest_model"
"slices"
)

func (d Exporter) IsServiceEdgeRouterPolicyExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "service-edge-router-policy")
}

func (d Exporter) GetServiceEdgeRouterPolicies() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
6 changes: 6 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_service_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ package exporter
import (
"github.com/openziti/edge-api/rest_management_api_client/service_policy"
"github.com/openziti/edge-api/rest_model"
"slices"
)

func (d Exporter) IsServicePolicyExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "service-policy")
}

func (d Exporter) GetServicePolicies() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
6 changes: 6 additions & 0 deletions ziti/cmd/ascode/exporter/exporter_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import (
"github.com/openziti/edge-api/rest_management_api_client/service"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/ziti/internal/ascode"
"slices"
)

func (d Exporter) IsServiceExportRequired(args []string) bool {
return slices.Contains(args, "all") || len(args) == 0 || // explicit all or nothing specified
slices.Contains(args, "service")
}

func (d Exporter) GetServices() ([]map[string]interface{}, error) {

return d.getEntities(
Expand Down
Loading

0 comments on commit 159fac2

Please sign in to comment.