Skip to content

Commit

Permalink
feat: make upstream field optional part II (#47)
Browse files Browse the repository at this point in the history
Closes #43
Closes #46
  • Loading branch information
Demonsthere authored Apr 25, 2021
1 parent 31b6e67 commit abf890f
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 36 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/rule_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (rj RuleJSON) MarshalJSON() ([]byte, error) {
type Alias RuleJSON

return unescapedMarshal(&struct {
Upstream *UpstreamJSON `json:"upstream"`
Upstream *UpstreamJSON `json:"upstream,omitempty"`
Alias
}{
Upstream: &UpstreamJSON{
Expand Down
8 changes: 7 additions & 1 deletion api/v1alpha1/rule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ type RuleList struct {

// RuleSpec defines the desired state of Rule
type RuleSpec struct {
Upstream *Upstream `json:"upstream"`
// +kubebuilder:validation:Optional
// +optional
Upstream *Upstream `json:"upstream,omitempty"`
Match *Match `json:"match"`
Authenticators []*Authenticator `json:"authenticators,omitempty"`
Authorizer *Authorizer `json:"authorizer,omitempty"`
Expand Down Expand Up @@ -238,6 +240,10 @@ func (r Rule) ToRuleJSON() *RuleJSON {
ruleJSON.Mutators = []*Mutator{{noopHandler}}
}

if ruleJSON.Upstream == nil {
ruleJSON.Upstream = &Upstream{}
}

if ruleJSON.Upstream.PreserveHost == nil {
ruleJSON.Upstream.PreserveHost = &preserveHostDefault
}
Expand Down
44 changes: 43 additions & 1 deletion api/v1alpha1/rule_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ var (
"handler": "noop"
}
]
},
{
"upstream": {
"url": "",
"preserve_host": false
},
"id": "fooNoUpstream.default",
"match": {
"url": "http://my-app/some-route3",
"methods": [
"GET",
"POST"
]
},
"authenticators": [
{
"handler": "unauthorized"
}
],
"authorizer": {
"handler": "handler1",
"config": {
"key1": "val1"
}
},
"mutators": [
{
"handler": "noop"
}
]
}
]`

Expand Down Expand Up @@ -219,7 +249,19 @@ func TestToOathkeeperRules(t *testing.T) {
&Authorizer{h1},
nil)

list.Items = []Rule{*rule1, *rule2, *rule3}
rule4 := newRule(
"fooNoUpstream",
"default",
"",
"http://my-app/some-route3",
nil,
nil,
nil,
nil,
&Authorizer{h1},
nil)

list.Items = []Rule{*rule1, *rule2, *rule3, *rule4}

//when
raw, err := list.ToOathkeeperRules()
Expand Down
45 changes: 14 additions & 31 deletions config/crd/bases/oathkeeper.ory.sh_rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ spec:
description: Rule is the Schema for the rules API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
Expand All @@ -36,12 +32,10 @@ spec:
properties:
authenticators:
items:
description: Authenticator represents a handler that authenticates
provided credentials.
description: Authenticator represents a handler that authenticates provided credentials.
properties:
config:
description: Config configures the handler. Configuration keys
vary per handler.
description: Config configures the handler. Configuration keys vary per handler.
type: object
handler:
description: Name is the name of a handler
Expand All @@ -51,12 +45,10 @@ spec:
type: object
type: array
authorizer:
description: Authorizer represents a handler that authorizes the subject
("user") from the previously validated credentials making the request.
description: Authorizer represents a handler that authorizes the subject ("user") from the previously validated credentials making the request.
properties:
config:
description: Config configures the handler. Configuration keys vary
per handler.
description: Config configures the handler. Configuration keys vary per handler.
type: object
handler:
description: Name is the name of a handler
Expand All @@ -65,8 +57,7 @@ spec:
- handler
type: object
configMapName:
description: ConfigMapName points to the K8s ConfigMap that contains
these rules
description: ConfigMapName points to the K8s ConfigMap that contains these rules
maxLength: 253
minLength: 1
pattern: '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*'
Expand All @@ -75,27 +66,23 @@ spec:
description: Match defines the URL(s) that an access rule should match.
properties:
methods:
description: Methods represent an array of HTTP methods (e.g. GET,
POST, PUT, DELETE, ...)
description: Methods represent an array of HTTP methods (e.g. GET, POST, PUT, DELETE, ...)
items:
type: string
type: array
url:
description: URL is the URL that should be matched. It supports
regex templates.
description: URL is the URL that should be matched. It supports regex templates.
type: string
required:
- methods
- url
type: object
mutators:
items:
description: Mutator represents a handler that transforms the HTTP
request before forwarding it.
description: Mutator represents a handler that transforms the HTTP request before forwarding it.
properties:
config:
description: Config configures the handler. Configuration keys
vary per handler.
description: Config configures the handler. Configuration keys vary per handler.
type: object
handler:
description: Name is the name of a handler
Expand All @@ -105,17 +92,13 @@ spec:
type: object
type: array
upstream:
description: Upstream represents the location of a server where requests
matching a rule should be forwarded to.
description: Upstream represents the location of a server where requests matching a rule should be forwarded to.
properties:
preserveHost:
description: PreserveHost includes the host and port of the url
value if set to false. If true, the host and port of the ORY Oathkeeper
Proxy will be used instead.
description: PreserveHost includes the host and port of the url value if set to false. If true, the host and port of the ORY Oathkeeper Proxy will be used instead.
type: boolean
stripPath:
description: StripPath replaces the provided path prefix when forwarding
the requested URL to the upstream URL.
description: StripPath replaces the provided path prefix when forwarding the requested URL to the upstream URL.
type: string
url:
description: URL defines the target URL for incoming requests
Expand Down
18 changes: 18 additions & 0 deletions config/samples/oathkeeper_v1alpha1_rule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ spec:
---
apiVersion: oathkeeper.ory.sh/v1alpha1
kind: Rule
metadata:
name: sample-rule-no-upstream
namespace: test-ns-1
spec:
description: Sample rule
match:
methods: ["GET"]
url: <http|https>://foo.bar
authenticators:
- handler: anonymous
authorizer:
handler: allow
mutators:
- handler: noop
config: {}
---
apiVersion: oathkeeper.ory.sh/v1alpha1
kind: Rule
metadata:
name: sample-rule-2
namespace: test-ns-1
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ require (
github.com/bitly/go-simplejson v0.5.0
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/go-logr/logr v0.1.0
github.com/onsi/ginkgo v1.14.0
github.com/onsi/ginkgo v1.16.1
github.com/onsi/gomega v1.10.1
github.com/stretchr/testify v1.4.0
github.com/stretchr/testify v1.5.1
k8s.io/api v0.17.8
k8s.io/apimachinery v0.17.8
k8s.io/client-go v0.17.8
Expand Down
Loading

0 comments on commit abf890f

Please sign in to comment.