Skip to content

Commit

Permalink
Disable local path for apply
Browse files Browse the repository at this point in the history
  • Loading branch information
hdkshingala committed Oct 12, 2023
1 parent 0cb2fa0 commit 1870d01
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/aperturectl/cmd/blueprints/dynamic-values.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Provides a dynamic values file for a given Aperture Blueprint that can be then u
if valuesFile == "" {
return fmt.Errorf("--output-file must be provided")
}
_, _, blueprintsDir, err := pull(blueprintsURI, blueprintsVersion)
_, _, blueprintsDir, err := pull(blueprintsURI, blueprintsVersion, true)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/aperturectl/cmd/blueprints/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ aperturectl blueprints generate --values-file=rate-limiting.yaml --apply`,
}

for _, vFile := range valuesFiles {
_, err := Generate(vFile, overrideBlueprintsURI, overrideBlueprintsVersion, updatedOutputDir)
_, err := Generate(vFile, overrideBlueprintsURI, overrideBlueprintsVersion, updatedOutputDir, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -123,7 +123,7 @@ aperturectl blueprints generate --values-file=rate-limiting.yaml --apply`,
}

// Generate generates Aperture Policy related resources from Aperture Blueprint.
func Generate(valuesFile string, overrideBlueprintsURI string, overrideBlueprintsVersion string, outputDir string) (map[string]any, error) {
func Generate(valuesFile string, overrideBlueprintsURI string, overrideBlueprintsVersion string, outputDir string, localAllowed bool) (map[string]any, error) {
_, err := os.Stat(valuesFile)
if err != nil {
log.Info().Msgf("Error reading values file: %s", err.Error())
Expand Down Expand Up @@ -173,7 +173,7 @@ func Generate(valuesFile string, overrideBlueprintsURI string, overrideBlueprint
}

// pull
_, blueprintsURIRoot, blueprintsDir, err := pull(blueprintsURI, blueprintsVersion)
_, blueprintsURIRoot, blueprintsDir, err := pull(blueprintsURI, blueprintsVersion, localAllowed)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/aperturectl/cmd/blueprints/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ aperturectl blueprints list --version latest
aperturectl blueprints list --all`,
RunE: func(cmd *cobra.Command, args []string) error {
blueprintsCacheRoot, blueprintsURIRoot, _, err := pull(blueprintsURI, blueprintsVersion)
blueprintsCacheRoot, blueprintsURIRoot, _, err := pull(blueprintsURI, blueprintsVersion, true)
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/aperturectl/cmd/blueprints/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var pullCmd = &cobra.Command{
aperturectl blueprints pull --version latest`,
RunE: func(cmd *cobra.Command, args []string) error {
_, _, _, err := pull(blueprintsURI, blueprintsVersion)
_, _, _, err := pull(blueprintsURI, blueprintsVersion, true)
if err != nil {
return err
}
Expand All @@ -38,7 +38,7 @@ aperturectl blueprints pull --version latest`,
}

// Pull pulls the blueprints from the given URI and version.
func pull(blueprintsURI string, blueprintsVersion string) (string, string, string, error) {
func pull(blueprintsURI string, blueprintsVersion string, localAllowed bool) (string, string, string, error) {
userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", "", "", err
Expand All @@ -65,6 +65,9 @@ func pull(blueprintsURI string, blueprintsVersion string) (string, string, strin
// uri can be a file or url
// first detect if it's a local path
if _, err = os.Stat(blueprintsURI); err == nil {
if !localAllowed {
return blueprintsCacheRoot, "", "", errors.New("local paths are not allowed as blueprints URI")
}
// path exists
blueprintsURI, err = filepath.Abs(blueprintsURI)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/aperturectl/cmd/blueprints/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ aperturectl blueprints remove --all`,
// RemoveRunE is the RunE function executed by the remove command.
func RemoveRunE(cmd *cobra.Command, args []string) error {
skipPull = true
blueprintsCacheRoot, blueprintsURIRoot, _, err := pull(blueprintsURI, blueprintsVersion)
blueprintsCacheRoot, blueprintsURIRoot, _, err := pull(blueprintsURI, blueprintsVersion, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/aperturectl/cmd/blueprints/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Provides a values file for a given Aperture Blueprint that can be then used to g
if valuesFile == "" {
return fmt.Errorf("--output-file must be provided")
}
_, _, blueprintsDir, err := pull(blueprintsURI, blueprintsVersion)
_, _, blueprintsDir, err := pull(blueprintsURI, blueprintsVersion, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/aperturectl/cmd/cloud/blueprints/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var BlueprintsApplyCmd = &cobra.Command{
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
valuesMap, err := blueprints.Generate(valuesFile, "", "", "")
valuesMap, err := blueprints.Generate(valuesFile, "", "", "", false)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/aperturectl/cmd/cloud/blueprints/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func init() {
BlueprintsDeleteCmd.Flags().StringVar(&name, "name", "", "Name of blueprint to get")
BlueprintsDeleteCmd.Flags().StringVar(&name, "policy-name", "", "Delete Blueprint by Policy Name")
}

// BlueprintsDeleteCmd is the command to delete a blueprint from the Cloud Controller.
Expand All @@ -21,7 +21,7 @@ var BlueprintsDeleteCmd = &cobra.Command{
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if name == "" {
return fmt.Errorf("--name is required")
return fmt.Errorf("--policy-name is required")
}
return nil
},
Expand Down
14 changes: 11 additions & 3 deletions cmd/aperturectl/cmd/cloud/blueprints/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package blueprints
import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"

cloudv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/cloud/v1"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd/utils"
)

func init() {
BlueprintsGetCmd.Flags().StringVar(&name, "name", "", "Name of blueprint to get")
BlueprintsGetCmd.Flags().StringVar(&name, "policy-name", "", "Get Blueprint by Policy Name")
}

// BlueprintsGetCmd is the command to get a blueprint from the Cloud Controller.
Expand All @@ -21,7 +23,7 @@ var BlueprintsGetCmd = &cobra.Command{
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if name == "" {
return fmt.Errorf("--name is required")
return fmt.Errorf("--policy-name is required")
}
return nil
},
Expand All @@ -36,7 +38,13 @@ var BlueprintsGetCmd = &cobra.Command{
fmt.Printf("Name: %s\n", getResponse.GetBlueprint().GetBlueprintsName())
fmt.Printf("Version: %s\n", getResponse.GetBlueprint().GetVersion())
fmt.Printf("Policy Name: %s\n", getResponse.GetBlueprint().GetPolicyName())
fmt.Printf("Values: \n%s\n", getResponse.GetBlueprint().GetValues())

yamlString, err := utils.GetYAMLString(getResponse.GetBlueprint().GetValues())
if err != nil {
return err
}
yamlString = strings.ReplaceAll(yamlString, "\n", "\n\t")
fmt.Printf("Values: \n\t%s\n", yamlString)

return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/aperturectl/cmd/cloud/blueprints/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var BlueprintsListCmd = &cobra.Command{

w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
for _, blueprint := range listResponse.GetBlueprints() {
fmt.Fprintf(w, "%s\n", blueprint.GetBlueprintsName())
fmt.Fprintf(w, "%s\n", blueprint.GetPolicyName())
}

w.Flush()
Expand Down
2 changes: 1 addition & 1 deletion cmd/aperturectl/cmd/utils/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
func PullSource(dir, uri string) error {
d := deps.Parse("", uri)
if d == nil {
return errors.New("unable to parse URI1: " + uri)
return errors.New("unable to parse URI: " + uri)
}

// read d and based on source write uri to uriFilename
Expand Down
14 changes: 14 additions & 0 deletions cmd/aperturectl/cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,17 @@ func URIToRawContentURL(uri string) string {
}
return ""
}

func GetYAMLString(bytes []byte) (string, error) {
var data map[string]interface{}
err := yaml.Unmarshal(bytes, &data)
if err != nil {
return "", err
}

yamlString, err := yaml.Marshal(data)
if err != nil {
return "", err
}
return string(yamlString), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ aperturectl cloud blueprints delete [flags]
### Options

```
-h, --help help for delete
--name string Name of blueprint to get
-h, --help help for delete
--policy-name string Delete Blueprint by Policy Name
```

### Options inherited from parent commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ aperturectl cloud blueprints get [flags]
### Options

```
-h, --help help for get
--name string Name of blueprint to get
-h, --help help for get
--policy-name string Get Blueprint by Policy Name
```

### Options inherited from parent commands
Expand Down

0 comments on commit 1870d01

Please sign in to comment.