From c088fd2ddede581a4ba70cc917e179d41688096e Mon Sep 17 00:00:00 2001 From: arekkas Date: Thu, 14 Dec 2017 18:10:51 +0100 Subject: [PATCH] cmd: Adds a dedicated command for importing policies Closes #701 --- cmd/cli/handler_policy.go | 40 +++++++++++++++++++++++++++------------ cmd/policies_import.go | 31 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 cmd/policies_import.go diff --git a/cmd/cli/handler_policy.go b/cmd/cli/handler_policy.go index 53109debfc6..96c895dd00e 100644 --- a/cmd/cli/handler_policy.go +++ b/cmd/cli/handler_policy.go @@ -49,22 +49,37 @@ func newPolicyHandler(c *config.Config) *PolicyHandler { } } -func (h *PolicyHandler) CreatePolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) ImportPolicy(cmd *cobra.Command, args []string) { + if len(args) == 0 { + fmt.Println(cmd.UsageString()) + return + } + m := h.newPolicyManager(cmd) - if files, _ := cmd.Flags().GetStringSlice("files"); len(files) > 0 { - for _, path := range files { - reader, err := os.Open(path) - pkg.Must(err, "Could not open file %s: %s", path, err) + for _, path := range args { + reader, err := os.Open(path) + pkg.Must(err, "Could not open file %s: %s", path, err) - var p hydra.Policy - err = json.NewDecoder(reader).Decode(&p) - pkg.Must(err, "Could not parse JSON: %s", err) + var p hydra.Policy + err = json.NewDecoder(reader).Decode(&p) + pkg.Must(err, "Could not parse JSON: %s", err) - _, response, err := m.CreatePolicy(p) - checkResponse(response, err, http.StatusCreated) - fmt.Printf("Imported policy %s from %s.\n", p.Id, path) - } + _, response, err := m.CreatePolicy(p) + checkResponse(response, err, http.StatusCreated) + fmt.Printf("Imported policy %s from %s.\n", p.Id, path) + } + + return +} + +func (h *PolicyHandler) CreatePolicy(cmd *cobra.Command, args []string) { + m := h.newPolicyManager(cmd) + + if files, _ := cmd.Flags().GetStringSlice("files"); len(files) > 0 { + fmt.Println("Importing policies using the -f flag is deprecated and will be removed in the future.") + fmt.Println(`Please use "hydra policies import" instead.`) + h.ImportPolicy(cmd, files) return } @@ -74,6 +89,7 @@ func (h *PolicyHandler) CreatePolicy(cmd *cobra.Command, args []string) { resources, _ := cmd.Flags().GetStringSlice("resources") actions, _ := cmd.Flags().GetStringSlice("actions") isAllow, _ := cmd.Flags().GetBool("allow") + if len(subjects) == 0 || len(resources) == 0 || len(actions) == 0 { fmt.Println(cmd.UsageString()) fmt.Println("") diff --git a/cmd/policies_import.go b/cmd/policies_import.go new file mode 100644 index 00000000000..0f4b881e4e3 --- /dev/null +++ b/cmd/policies_import.go @@ -0,0 +1,31 @@ +// Copyright © 2017 Aeneas Rekkas +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "github.com/spf13/cobra" +) + +// policiesImportCmd represents the import command +var policiesImportCmd = &cobra.Command{ + Use: "import [...]", + Short: "Import policies from JSON files", + Run: cmdHandler.Policies.ImportPolicy, +} + +func init() { + policiesCmd.AddCommand(policiesImportCmd) + +}