Skip to content

Commit

Permalink
Fixes #161 adds stdin support for policy load (#181)
Browse files Browse the repository at this point in the history
* Fixes #161 adds stdin support for policy load

* resolve code smell
  • Loading branch information
infamousjoeg committed Nov 23, 2022
1 parent 339a795 commit 7a3c77a
Showing 1 changed file with 56 additions and 5 deletions.
61 changes: 56 additions & 5 deletions cmd/conjur.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
Expand All @@ -17,6 +18,8 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

const stdinErrMsg = "Failed to read from stdin."

var (
// Account conjur account
Account string
Expand Down Expand Up @@ -49,7 +52,16 @@ var (
InspectResources bool
)

func loadPolicy(policyBranch string, policyFilePath string, policyMode conjurapi.PolicyMode) {
func isInputFromPipe() bool {
stat, _ := os.Stdin.Stat()
return (stat.Mode() & os.ModeCharDevice) == 0
}

func loadPolicyFile(policyBranch string, policyFilePath string, policyMode conjurapi.PolicyMode) {
if policyFilePath == "" {
log.Fatal("Policy file path is required")
}

client, _, err := conjur.GetConjurClient()
if err != nil {
log.Fatalf("Failed to initialize conjur client. %s", err)
Expand All @@ -67,6 +79,19 @@ func loadPolicy(policyBranch string, policyFilePath string, policyMode conjurapi
prettyprint.PrintJSON(response)
}

func loadPolicyPipe(policyBranch, policyContent string, policyMode conjurapi.PolicyMode) {
client, _, err := conjur.GetConjurClient()
if err != nil {
log.Fatalf("Failed to initialize conjur client. %s", err)
}

response, err := client.LoadPolicy(policyMode, policyBranch, strings.NewReader(policyContent))
if err != nil {
log.Fatalf("Failed to load policy. %v. %s", response, err)
}
prettyprint.PrintJSON(response)
}

func removeFile(path string) {
err := os.Remove(path)
if err != nil {
Expand Down Expand Up @@ -209,7 +234,16 @@ var conjurAppendPolicyCmd = &cobra.Command{
Example Usage:
$ cybr conjur append-policy --branch root --file ./path/to/root.yml`,
Run: func(cmd *cobra.Command, args []string) {
loadPolicy(PolicyBranch, PolicyFilePath, conjurapi.PolicyModePost)
if isInputFromPipe() {
// Read from stdin
policy, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("%s %s", stdinErrMsg, err)
}
loadPolicyPipe(PolicyBranch, string(policy), conjurapi.PolicyModePost)
} else {
loadPolicyFile(PolicyBranch, PolicyFilePath, conjurapi.PolicyModePost)
}
},
}

Expand All @@ -222,7 +256,16 @@ var conjurUpdatePolicyCmd = &cobra.Command{
Example Usage:
$ cybr conjur update-policy --branch root --file ./path/to/root.yml`,
Run: func(cmd *cobra.Command, args []string) {
loadPolicy(PolicyBranch, PolicyFilePath, conjurapi.PolicyModePatch)
if isInputFromPipe() {
// Read from stdin
policy, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("%s %s", stdinErrMsg, err)
}
loadPolicyPipe(PolicyBranch, string(policy), conjurapi.PolicyModePut)
} else {
loadPolicyFile(PolicyBranch, PolicyFilePath, conjurapi.PolicyModePatch)
}
},
}

Expand All @@ -235,7 +278,16 @@ var conjurReplacePolicyCmd = &cobra.Command{
Example Usage:
$ cybr conjur replace-policy --branch root --file ./path/to/root.yml`,
Run: func(cmd *cobra.Command, args []string) {
loadPolicy(PolicyBranch, PolicyFilePath, conjurapi.PolicyModePut)
if isInputFromPipe() {
// Read from stdin
policy, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("%s %s", stdinErrMsg, err)
}
loadPolicyPipe(PolicyBranch, string(policy), conjurapi.PolicyModePut)
} else {
loadPolicyFile(PolicyBranch, PolicyFilePath, conjurapi.PolicyModePut)
}
},
}

Expand Down Expand Up @@ -429,7 +481,6 @@ func init() {
conjurAppendPolicyCmd.Flags().StringVarP(&PolicyBranch, "branch", "b", "", "The policy branch in which policy is being loaded")
conjurAppendPolicyCmd.MarkFlagRequired("branch")
conjurAppendPolicyCmd.Flags().StringVarP(&PolicyFilePath, "file", "f", "", "The policy file that will be loaded into the branch")
conjurAppendPolicyCmd.MarkFlagRequired("file")

// update-policy
conjurUpdatePolicyCmd.Flags().StringVarP(&PolicyBranch, "branch", "b", "", "The policy branch in which policy is being loaded")
Expand Down

0 comments on commit 7a3c77a

Please sign in to comment.