diff --git a/api/v1alpha1/json.go b/api/v1alpha1/json.go new file mode 100644 index 0000000..5755f2a --- /dev/null +++ b/api/v1alpha1/json.go @@ -0,0 +1,31 @@ +package v1alpha1 + +import ( + "bytes" + "encoding/json" +) + +func unescapedMarshalIndent(in interface{}, prefix, indent string) ([]byte, error) { + var b bytes.Buffer + enc := json.NewEncoder(&b) + enc.SetEscapeHTML(false) + enc.SetIndent(prefix, indent) + if err := enc.Encode(in); err != nil { + return nil, err + } + + result := b.Bytes() + return result[:len(result)-1], nil +} + +func unescapedMarshal(in interface{}) ([]byte, error) { + var b bytes.Buffer + enc := json.NewEncoder(&b) + enc.SetEscapeHTML(false) + if err := enc.Encode(in); err != nil { + return nil, err + } + + result := b.Bytes() + return result[:len(result)-1], nil +} diff --git a/api/v1alpha1/rule_json.go b/api/v1alpha1/rule_json.go index 0403fc7..c6e3b95 100644 --- a/api/v1alpha1/rule_json.go +++ b/api/v1alpha1/rule_json.go @@ -1,7 +1,5 @@ package v1alpha1 -import "encoding/json" - // RuleJson is a representation of an Oathkeeper rule. type RuleJSON struct { ID string `json:"id"` @@ -13,7 +11,7 @@ func (rj RuleJSON) MarshalJSON() ([]byte, error) { type Alias RuleJSON - return json.Marshal(&struct { + return unescapedMarshal(&struct { Upstream *UpstreamJSON `json:"upstream"` Alias }{ diff --git a/api/v1alpha1/rule_types.go b/api/v1alpha1/rule_types.go index ae85f12..e5f0295 100644 --- a/api/v1alpha1/rule_types.go +++ b/api/v1alpha1/rule_types.go @@ -16,11 +16,12 @@ limitations under the License. package v1alpha1 import ( - "encoding/json" "fmt" - "github.com/ory/oathkeeper-maester/internal/validation" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + + "github.com/ory/oathkeeper-maester/internal/validation" ) const ( @@ -139,7 +140,7 @@ func (rl RuleList) ToOathkeeperRules() ([]byte, error) { rules[i] = rl.Items[i].ToRuleJSON() } - return json.MarshalIndent(rules, "", " ") + return unescapedMarshalIndent(rules, "", " ") } // FilterNotValid filters out Rules which doesn't pass validation due to being not processed yet or due to negative result of validation. It returns a list of Rules which passed validation successfully. diff --git a/api/v1alpha1/rule_types_test.go b/api/v1alpha1/rule_types_test.go index cfa0137..0b8a75d 100644 --- a/api/v1alpha1/rule_types_test.go +++ b/api/v1alpha1/rule_types_test.go @@ -191,7 +191,7 @@ func TestToOathkeeperRules(t *testing.T) { newStringPtr("/api/v1"), nil, newBoolPtr(true), - []*Authenticator{&Authenticator{h1}}, + []*Authenticator{{h1}}, nil, []*Mutator{{h1}, {h2}}) @@ -203,7 +203,7 @@ func TestToOathkeeperRules(t *testing.T) { nil, nil, newBoolPtr(false), - []*Authenticator{&Authenticator{h1}, {h2}}, + []*Authenticator{{h1}, {h2}}, nil, nil)