Skip to content
Merged
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
40 changes: 28 additions & 12 deletions cmd/cli/handler_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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("")
Expand Down
31 changes: 31 additions & 0 deletions cmd/policies_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright © 2017 Aeneas Rekkas <aeneas+oss@aeneas.io>
//
// 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 <path/to/file.json> [<path/to/other/file.json>...]",
Short: "Import policies from JSON files",
Run: cmdHandler.Policies.ImportPolicy,
}

func init() {
policiesCmd.AddCommand(policiesImportCmd)

}