Skip to content

Commit

Permalink
remove TOML representation of policies
Browse files Browse the repository at this point in the history
This commit removes the TOML marshaling/unmarshaling
of policies. Now, a policy can only be marshaled
as JSON.

This commit finally removes the TOML dependency.
  • Loading branch information
Andreas Auernhammer authored and harshavardhana committed Apr 29, 2020
1 parent 8f259c0 commit 3cd0009
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 148 deletions.
7 changes: 3 additions & 4 deletions cmd/kes/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import (

const identityCmdUsage = `usage: %s <command>
assign Assign a policy to an identity such that the policy
applies to the identity.
list List all identities present at the key server.
forget Forget an identity such that no policy applies to it.
assign Assign an identity to a policy.
list List identities at the KES server.
forget Forget an identity.
-h, --help Show list of command-line options
`
Expand Down
41 changes: 15 additions & 26 deletions cmd/kes/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
"github.com/minio/kes"
)

const policyCmdUsage = `Manage named key policies.
const policyCmdUsage = `Manage named KES policies.
usage: %s <command>
add Add a named policy to the policy set.
add Add a new named policy.
show Download and print a named policy.
list List all named policies.
list List named policies.
delete Delete a named policy.
-h, --help Show list of command-line options
Expand Down Expand Up @@ -54,11 +54,10 @@ func policy(args []string) error {
}
}

const addPolicyCmdUsage = `Adds a named policy to the policy set of the key server.
const addPolicyCmdUsage = `Adds a named policy to the policy set of the KES server.
It reads a TOML or JSON encoded policy from the specified file
and adds it to the policy set of the key server. The policy will
be available under the specified policy name.
It reads a JSON encoded policy from the specified file and
adds it to the policy set of the KES server.
usage: %s <policy> <file>
Expand Down Expand Up @@ -89,29 +88,25 @@ func addPolicy(args []string) error {
if err != nil {
return fmt.Errorf("Cannot read policy file '%s': %v", args[1], err)
}

var policy kes.Policy
if err = policy.UnmarshalTOML(data); err != nil {
if err = policy.UnmarshalJSON(data); err != nil {
return fmt.Errorf("Policy file contains neither valid TOML nor valid JSON")
}
if err = policy.UnmarshalJSON(data); err != nil {
return fmt.Errorf("Policy file is invalid JSON: %v", err)
}

if err := client.WritePolicy(args[0], &policy); err != nil {
if err = client.WritePolicy(args[0], &policy); err != nil {
return fmt.Errorf("Failed to add policy '%s': %v", args[0], err)
}
return nil
}

const showPolicyCmdUsage = `Downloads and prints key policies.
const showPolicyCmdUsage = `Downloads and prints KES policies.
It prints the policy definition of a named policy to STDOUT.
By default, the policy definition is printed in a human-readable
format to a terminal or as TOML to a UNIX pipe / file.
format to a terminal or as JSON to a UNIX pipe / file.
usage: %s <policy>
--json Encode policy as JSON instead of TOML.
-k, --insecure Skip X.509 certificate validation during TLS handshake
-h, --help Show list of command-line options
Expand All @@ -123,9 +118,7 @@ func showPolicy(args []string) error {
fmt.Fprintf(cli.Output(), showPolicyCmdUsage, cli.Name())
}

var formatJSON bool
var insecureSkipVerify bool
cli.BoolVar(&formatJSON, "json", false, "")
cli.BoolVar(&insecureSkipVerify, "k", false, "Skip X.509 certificate validation during TLS handshake")
cli.BoolVar(&insecureSkipVerify, "insecure", false, "Skip X.509 certificate validation during TLS handshake")

Expand All @@ -151,15 +144,11 @@ func showPolicy(args []string) error {
if err != nil {
return fmt.Errorf("Failed to fetch policy '%s': %v", args[0], err)
}
switch {
case isTerm(os.Stdout) && !formatJSON:
if isTerm(os.Stdout) {
fmt.Println(policy.String())
case formatJSON:
} else {
output, _ := policy.MarshalJSON()
os.Stdout.Write(output)
default:
output, _ := policy.MarshalTOML()
os.Stdout.Write(output)
}
return nil
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/fatih/color v1.7.0
github.com/hashicorp/vault/api v1.0.4
github.com/kr/pretty v0.1.0 // indirect
github.com/pelletier/go-toml v1.6.0
github.com/secure-io/sio-go v0.3.0
github.com/stretchr/testify v1.4.0 // indirect
golang.org/x/crypto v0.0.0-20191117063200-497ca9f6d64f
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
28 changes: 0 additions & 28 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"net/http"
"path"
"strings"

"github.com/pelletier/go-toml"
)

type Policy struct {
Expand Down Expand Up @@ -61,32 +59,6 @@ func (p *Policy) UnmarshalJSON(b []byte) error {
return nil
}

func (p Policy) MarshalTOML() ([]byte, error) {
type PolicyTOML struct {
Patterns []string `toml:"paths"`
}
return toml.Marshal(PolicyTOML{
Patterns: p.patterns,
})
}

func (p *Policy) UnmarshalTOML(b []byte) error {
var policyTOML struct {
Patterns []string `toml:"paths"`
}

if err := toml.Unmarshal(b, &policyTOML); err != nil {
return err
}
for _, pattern := range policyTOML.Patterns {
if _, err := path.Match(pattern, pattern); err != nil {
return err
}
}
p.patterns = policyTOML.Patterns
return nil
}

func (p *Policy) String() string {
var builder strings.Builder
fmt.Fprintln(&builder, "[")
Expand Down
87 changes: 0 additions & 87 deletions policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,93 +142,6 @@ func TestPolicyUnmarshalJSON(t *testing.T) {
}
}

var policyMarshalTOMLTests = []struct {
Policy *Policy
Output string
}{
{ // 0
Policy: mustNewPolicy(),
Output: `paths = []` + "\n",
},
{ // 1
Policy: mustNewPolicy("/v1/key/create/*", "/v1/key/delete/*"),
Output: `paths = ["/v1/key/create/*","/v1/key/delete/*"]` + "\n",
},
{ // 2
Policy: mustNewPolicy("/v1/key/create/*", "/v1/key/delete/*", "/v1/key/generate/my-key"),
Output: `paths = ["/v1/key/create/*","/v1/key/delete/*","/v1/key/generate/my-key"]` + "\n",
},
}

func TestPolicyMarshalTOML(t *testing.T) {
for i, test := range policyMarshalTOMLTests {
out, err := test.Policy.MarshalTOML()
if err != nil {
t.Fatalf("Test %d: %v", i, err)
}

output := string(out)
if output != test.Output {
t.Fatalf("Test %d: got %s - want %s", i, out, test.Output)
}
}
}

var policyUnmarshalTOMLTests = []struct {
Source string
Policy *Policy
Err error
}{
{ // 0
Source: `paths = []`,
Policy: mustNewPolicy(),
Err: nil,
},
{ // 1
Source: `paths = [""]`,
Policy: mustNewPolicy(""),
Err: nil,
},
{ // 2
Source: `paths = ["/v1/key/create/*","/v1/key/delete/*"]`,
Policy: mustNewPolicy("/v1/key/create/*", "/v1/key/delete/*"),
Err: nil,
},
{ // 3
Source: `paths = ["/v1/key/create/*","/v1/key/delete/*","/v1/key/generate/my-key"]`,
Policy: mustNewPolicy("/v1/key/create/*", "/v1/key/delete/*", "/v1/key/generate/my-key"),
Err: nil,
},
{ // 4
Source: `paths = ["/v1/key/create/*","/v1/key/delete/*","/v1/key/generate/my-key[0-]"]`,
Policy: mustNewPolicy("/v1/key/create/*", "/v1/key/delete/*", "/v1/key/generate/my-key"),
Err: path.ErrBadPattern,
},
}

func TestPolicyUnmarshalTOML(t *testing.T) {
for i, test := range policyUnmarshalTOMLTests {
var policy Policy
err := policy.UnmarshalTOML([]byte(test.Source))
if err != test.Err {
t.Fatalf("Test %d: got error %v - want error %v", i, err, test.Err)
}
if err == nil {
if len(policy.patterns) != len(test.Policy.patterns) {
t.Fatalf("Test %d: policy differs in paths: got %d - want %d", i, len(policy.patterns), len(test.Policy.patterns))
}

sort.Strings(policy.patterns)
sort.Strings(test.Policy.patterns)
for j := range policy.patterns {
if policy.patterns[j] != test.Policy.patterns[j] {
t.Fatalf("Test %d: policy path %d does not match: got %s - want %s", i, j, policy.patterns[j], test.Policy.patterns[j])
}
}
}
}
}

var policyStringTests = []struct {
Policy *Policy
Output string
Expand Down

0 comments on commit 3cd0009

Please sign in to comment.