Skip to content

Commit

Permalink
MAISTRA-2010 Fix validation of AuthorizationPolicy fields (maistra#207)
Browse files Browse the repository at this point in the history
It would previously detect request.regex.headers as invalid, even
though it is supported.

Cherry-pick of MAISTRA-1739 (maistra#157)
  • Loading branch information
dgn authored Nov 23, 2020
1 parent 02d54da commit 78d6a2c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
2 changes: 2 additions & 0 deletions pilot/pkg/security/authz/builder/testdata/all-fields-in.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ spec:
- key: "request.headers[X-header]"
values: ["header", "header-prefix-*", "*-suffix-header", "*"]
notValues: ["not-header", "not-header-prefix-*", "*-not-suffix-header", "*"]
- key: "request.regex.headers[X-header-regex]"
values: ["some.*value"]
- key: "source.ip"
values: ["10.10.10.10", "192.168.10.0/24"]
notValues: ["90.10.10.10", "90.168.10.0/24"]
Expand Down
7 changes: 7 additions & 0 deletions pilot/pkg/security/authz/builder/testdata/all-fields-out.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ typedConfig:
- header:
name: X-header
presentMatch: true
- orIds:
ids:
- header:
name: X-header-regex
safeRegexMatch:
googleRe2: {}
regex: some.*value
- orIds:
ids:
- sourceIp:
Expand Down
35 changes: 19 additions & 16 deletions pkg/config/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,23 @@ type JwksInfo struct {
}

const (
attrRequestHeader = "request.headers" // header name is surrounded by brackets, e.g. "request.headers[User-Agent]".
attrSrcIP = "source.ip" // supports both single ip and cidr, e.g. "10.1.2.3" or "10.1.0.0/16".
attrSrcNamespace = "source.namespace" // e.g. "default".
attrSrcPrincipal = "source.principal" // source identity, e,g, "cluster.local/ns/default/sa/productpage".
attrRequestPrincipal = "request.auth.principal" // authenticated principal of the request.
attrRequestAudiences = "request.auth.audiences" // intended audience(s) for this authentication information.
attrRequestPresenter = "request.auth.presenter" // authorized presenter of the credential.
attrRequestClaims = "request.auth.claims" // claim name is surrounded by brackets, e.g. "request.auth.claims[iss]".
attrDestIP = "destination.ip" // supports both single ip and cidr, e.g. "10.1.2.3" or "10.1.0.0/16".
attrDestPort = "destination.port" // must be in the range [0, 65535].
attrDestLabel = "destination.labels" // label name is surrounded by brackets, e.g. "destination.labels[version]".
attrDestName = "destination.name" // short service name, e.g. "productpage".
attrDestNamespace = "destination.namespace" // e.g. "default".
attrDestUser = "destination.user" // service account, e.g. "bookinfo-productpage".
attrConnSNI = "connection.sni" // server name indication, e.g. "www.example.com".
attrExperimental = "experimental.envoy.filters."
attrRequestHeader = "request.headers" // header name is surrounded by brackets, e.g. "request.headers[User-Agent]".
attrRequestHeaderRegex = "request.regex.headers" // header regex is surrounded by brackets, e.g. "request.regex.headers[X-Random-.*]".
attrSrcIP = "source.ip" // supports both single ip and cidr, e.g. "10.1.2.3" or "10.1.0.0/16".
attrSrcNamespace = "source.namespace" // e.g. "default".
attrSrcPrincipal = "source.principal" // source identity, e,g, "cluster.local/ns/default/sa/productpage".
attrRequestPrincipal = "request.auth.principal" // authenticated principal of the request.
attrRequestAudiences = "request.auth.audiences" // intended audience(s) for this authentication information.
attrRequestPresenter = "request.auth.presenter" // authorized presenter of the credential.
attrRequestClaims = "request.auth.claims" // claim name is surrounded by brackets, e.g. "request.auth.claims[iss]".
attrDestIP = "destination.ip" // supports both single ip and cidr, e.g. "10.1.2.3" or "10.1.0.0/16".
attrDestPort = "destination.port" // must be in the range [0, 65535].
attrDestLabel = "destination.labels" // label name is surrounded by brackets, e.g. "destination.labels[version]".
attrDestName = "destination.name" // short service name, e.g. "productpage".
attrDestNamespace = "destination.namespace" // e.g. "default".
attrDestUser = "destination.user" // service account, e.g. "bookinfo-productpage".
attrConnSNI = "connection.sni" // server name indication, e.g. "www.example.com".
attrExperimental = "experimental.envoy.filters."
)

// ParseJwksURI parses the input URI and returns the corresponding hostname, port, and whether SSL is used.
Expand Down Expand Up @@ -106,6 +107,8 @@ func ValidateAttribute(key string, values []string) error {
switch {
case hasPrefix(key, attrRequestHeader):
return validateMapKey(key)
case hasPrefix(key, attrRequestHeaderRegex):
return validateMapKey(key)
case isEqual(key, attrSrcIP):
return ValidateIPs(values)
case isEqual(key, attrSrcNamespace):
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/security/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func TestValidateCondition(t *testing.T) {
values: []string{"productpage"},
wantError: true,
},
{
key: "request.regex.headers[]",
values: []string{"some.*value"},
wantError: true,
},
{
key: "request.regex.headers[X-header-regex]",
values: []string{"some.*value"},
wantError: false,
},
{
key: "source.ip",
values: []string{"1.2.3.4", "5.6.7.0/24"},
Expand Down

0 comments on commit 78d6a2c

Please sign in to comment.