Skip to content

Commit

Permalink
Merge pull request #5 from ssttehrani/snappcloud
Browse files Browse the repository at this point in the history
refactor: add http auth support to contour
  • Loading branch information
ssttehrani authored Feb 26, 2024
2 parents c0005e5 + 5f826fc commit fe82257
Show file tree
Hide file tree
Showing 23 changed files with 2,096 additions and 143 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ container: ## Build the Contour container image
--build-arg "BUILD_GOEXPERIMENT=$(BUILD_GOEXPERIMENT)" \
$(DOCKER_BUILD_LABELS) \
$(shell pwd) \
--platform linux/amd64 \
--tag $(IMAGE):$(VERSION)

push: ## Push the Contour container image to the Docker registry
Expand Down
16 changes: 8 additions & 8 deletions apis/projectcontour/v1/httpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ type AuthorizationServer struct {
// +kubebuilder:default=grpc
ServiceAPIType AuthorizationServiceAPIType `json:"serviceAPIType,omitempty"`

// HttpAuthorizationServerSettings defines configurations for interacting with an external HTTP authorization server.
// HTTPServerSettings defines configurations for interacting with an external HTTP authorization server.
//
// +optional
HttpServerSettings *HttpAuthorizationServerSettings `json:"httpSettings,omitempty"`
HTTPServerSettings *HTTPAuthorizationServerSettings `json:"httpSettings,omitempty"`

// AuthPolicy sets a default authorization policy for client requests.
// This policy will be used unless overridden by individual routes.
Expand Down Expand Up @@ -287,8 +287,8 @@ type AuthorizationServer struct {
WithRequestBody *AuthorizationServerBufferSettings `json:"withRequestBody,omitempty"`
}

// HttpAuthorizationServerSettings defines configurations for interacting with an external HTTP authorization server.
type HttpAuthorizationServerSettings struct {
// HTTPAuthorizationServerSettings defines configurations for interacting with an external HTTP authorization server.
type HTTPAuthorizationServerSettings struct {
// PathPrefix Sets a prefix to the value of authorization request header Path.
//
// +optional
Expand All @@ -307,21 +307,21 @@ type HttpAuthorizationServerSettings struct {
// Note that in addition to the the user’s supplied matchers, Host, Method, Path, Content-Length, and Authorization are additionally included in the list.
//
// +optional
AllowedAuthorizationHeaders []HttpAuthorizationServerAllowedHeaders `json:"allowedAuthorizationHeaders,omitempty"`
AllowedAuthorizationHeaders []HTTPAuthorizationServerAllowedHeaders `json:"allowedAuthorizationHeaders,omitempty"`

// AllowedUpstreamHeaders specifies authorization response headers that will be added to the original client request.
// Note that coexistent headers will be overridden.
//
// +optional
AllowedUpstreamHeaders []HttpAuthorizationServerAllowedHeaders `json:"allowedUpstreamHeaders,omitempty"`
AllowedUpstreamHeaders []HTTPAuthorizationServerAllowedHeaders `json:"allowedUpstreamHeaders,omitempty"`
}

// HttpAuthorizationServerAllowedHeaders specifies how to conditionally match against allowed headers
// HTTPAuthorizationServerAllowedHeaders specifies how to conditionally match against allowed headers
// in the context of HTTP authorization. It includes options such as Exact, Prefix, Suffix,
// Contains, and IgnoreCase to customize header matching criteria. However, regex support
// is intentionally excluded to simplify the user experience and prevent potential issues.
// One of Prefix, Exact, Suffix or Contains must be provided.
type HttpAuthorizationServerAllowedHeaders struct {
type HTTPAuthorizationServerAllowedHeaders struct {
// Exact specifies a string that the header name must be equal to.
//
// +optional
Expand Down
45 changes: 45 additions & 0 deletions apis/projectcontour/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions apis/projectcontour/v1alpha1/extensionservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type ExtensionServiceTarget struct {
// ExtensionServiceSpec defines the desired state of an ExtensionService resource.
type ExtensionServiceSpec struct {
// Services specifies the set of Kubernetes Service resources that
// receive GRPC extension API requests.
// receive extension API requests.
// If no weights are specified for any of the entries in
// this array, traffic will be spread evenly across all the
// services.
Expand All @@ -77,15 +77,15 @@ type ExtensionServiceSpec struct {
UpstreamValidation *contour_api_v1.UpstreamValidation `json:"validation,omitempty"`

// Protocol may be used to specify (or override) the protocol used to reach this Service.
// Values may be h2 or h2c. If omitted, protocol-selection falls back on Service annotations.
// Values may be h2, h2c or http/1.1. If omitted, protocol-selection falls back on Service annotations.
//
// +optional
// +kubebuilder:validation:Enum=h1;h2;h2c
// +kubebuilder:validation:Enum=http/1.1;h2;h2c
Protocol *string `json:"protocol,omitempty"`

// The policy for load balancing GRPC service requests. Note that the
// The policy for load balancing service requests. Note that the
// `Cookie` and `RequestHash` load balancing strategies cannot be used
// here.
// here for GRPC service requests.
//
// +optional
LoadBalancerPolicy *contour_api_v1.LoadBalancerPolicy `json:"loadBalancerPolicy,omitempty"`
Expand Down
32 changes: 16 additions & 16 deletions cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,10 +861,6 @@ func (s *Server) setupGlobalExternalAuthentication(contourConfiguration contour_
context = contourConfiguration.GlobalExternalAuthorization.AuthPolicy.Context
}

if contourConfiguration.GlobalExternalAuthorization.ServiceAPIType == contour_api_v1.AuthorizationHTTPService && contourConfiguration.GlobalExternalAuthorization.HttpServerSettings == nil {
return nil, fmt.Errorf("Spec.globalExtAuth.HttpServerSettings is not set and it is required for http type")
}

// Not required due to Kubernetes API server validation.
//
// if auth.ServiceAPIType == contour_api_v1.AuthorizationHTTPService && auth.HttpServerSettings.ServerURI == "" {
Expand All @@ -885,24 +881,28 @@ func (s *Server) setupGlobalExternalAuthentication(contourConfiguration contour_
globalExternalAuthConfig.ServiceAPIType = contour_api_v1.AuthorizationGRPCService
case contour_api_v1.AuthorizationHTTPService:
globalExternalAuthConfig.ServiceAPIType = contour_api_v1.AuthorizationHTTPService
globalExternalAuthConfig.HttpPathPrefix = contourConfiguration.GlobalExternalAuthorization.HttpServerSettings.PathPrefix
// globalExternalAuthConfig.HttpServerURI = contourConfiguration.GlobalExternalAuthorization.HttpServerSettings.ServerURI

if contourConfiguration.GlobalExternalAuthorization.HttpServerSettings.AllowedAuthorizationHeaders != nil {
if err := dag.ExternalAuthAllowedHeadersValid(contourConfiguration.GlobalExternalAuthorization.HttpServerSettings.AllowedAuthorizationHeaders); err != nil {
return nil, err
}
if contourConfiguration.GlobalExternalAuthorization.HTTPServerSettings != nil {
globalExternalAuthConfig.HTTPPathPrefix = contourConfiguration.GlobalExternalAuthorization.HTTPServerSettings.PathPrefix

globalExternalAuthConfig.HttpAllowedAuthorizationHeaders = contourConfiguration.GlobalExternalAuthorization.HttpServerSettings.AllowedAuthorizationHeaders
}
// globalExternalAuthConfig.HttpServerURI = contourConfiguration.GlobalExternalAuthorization.HttpServerSettings.ServerURI

if contourConfiguration.GlobalExternalAuthorization.HttpServerSettings.AllowedUpstreamHeaders != nil {
if err := dag.ExternalAuthAllowedHeadersValid(contourConfiguration.GlobalExternalAuthorization.HttpServerSettings.AllowedUpstreamHeaders); err != nil {
if len(contourConfiguration.GlobalExternalAuthorization.HTTPServerSettings.AllowedAuthorizationHeaders) > 0 {
if err := dag.ExternalAuthAllowedHeadersValid(contourConfiguration.GlobalExternalAuthorization.HTTPServerSettings.AllowedAuthorizationHeaders); err != nil {
return nil, err
}

return nil, err
globalExternalAuthConfig.HTTPAllowedAuthorizationHeaders = contourConfiguration.GlobalExternalAuthorization.HTTPServerSettings.AllowedAuthorizationHeaders
}

globalExternalAuthConfig.HttpAllowedUpstreamHeaders = contourConfiguration.GlobalExternalAuthorization.HttpServerSettings.AllowedUpstreamHeaders
if len(contourConfiguration.GlobalExternalAuthorization.HTTPServerSettings.AllowedUpstreamHeaders) > 0 {
if err := dag.ExternalAuthAllowedHeadersValid(contourConfiguration.GlobalExternalAuthorization.HTTPServerSettings.AllowedUpstreamHeaders); err != nil {

return nil, err
}

globalExternalAuthConfig.HTTPAllowedUpstreamHeaders = contourConfiguration.GlobalExternalAuthorization.HTTPServerSettings.AllowedUpstreamHeaders
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/contour/servecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,15 @@ func (ctx *serveContext) convertToContourConfigurationSpec() contour_api_v1alpha
Name: nsedName.Name,
Namespace: nsedName.Namespace,
},
ServiceAPIType: ctx.Config.GlobalExternalAuthorization.ServiceAPIType,
ResponseTimeout: ctx.Config.GlobalExternalAuthorization.ResponseTimeout,
FailOpen: ctx.Config.GlobalExternalAuthorization.FailOpen,
}

if ctx.Config.GlobalExternalAuthorization.HTTPServerSettings != nil {
globalExtAuth.HTTPServerSettings = ctx.Config.GlobalExternalAuthorization.HTTPServerSettings
}

if ctx.Config.GlobalExternalAuthorization.AuthPolicy != nil {
globalExtAuth.AuthPolicy = &contour_api_v1.AuthorizationPolicy{
Disabled: ctx.Config.GlobalExternalAuthorization.AuthPolicy.Disabled,
Expand Down
38 changes: 19 additions & 19 deletions examples/contour/01-crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,8 @@ spec:
authorization to Contour external authorization.
type: boolean
httpSettings:
description: HttpAuthorizationServerSettings defines configurations
for interacting with an external HTTP authorization server.
description: HTTPServerSettings defines configurations for interacting
with an external HTTP authorization server.
properties:
allowedAuthorizationHeaders:
description: AllowedAuthorizationHeaders specifies client
Expand All @@ -642,7 +642,7 @@ spec:
Host, Method, Path, Content-Length, and Authorization are
additionally included in the list.
items:
description: HttpAuthorizationServerAllowedHeaders specifies
description: HTTPAuthorizationServerAllowedHeaders specifies
how to conditionally match against allowed headers in
the context of HTTP authorization. It includes options
such as Exact, Prefix, Suffix, Contains, and IgnoreCase
Expand Down Expand Up @@ -679,7 +679,7 @@ spec:
response headers that will be added to the original client
request. Note that coexistent headers will be overridden.
items:
description: HttpAuthorizationServerAllowedHeaders specifies
description: HTTPAuthorizationServerAllowedHeaders specifies
how to conditionally match against allowed headers in
the context of HTTP authorization. It includes options
such as Exact, Prefix, Suffix, Contains, and IgnoreCase
Expand Down Expand Up @@ -4387,8 +4387,8 @@ spec:
from internal authorization to Contour external authorization.
type: boolean
httpSettings:
description: HttpAuthorizationServerSettings defines configurations
for interacting with an external HTTP authorization server.
description: HTTPServerSettings defines configurations for
interacting with an external HTTP authorization server.
properties:
allowedAuthorizationHeaders:
description: AllowedAuthorizationHeaders specifies client
Expand All @@ -4397,7 +4397,7 @@ spec:
matchers, Host, Method, Path, Content-Length, and Authorization
are additionally included in the list.
items:
description: HttpAuthorizationServerAllowedHeaders specifies
description: HTTPAuthorizationServerAllowedHeaders specifies
how to conditionally match against allowed headers
in the context of HTTP authorization. It includes
options such as Exact, Prefix, Suffix, Contains, and
Expand Down Expand Up @@ -4436,7 +4436,7 @@ spec:
client request. Note that coexistent headers will be
overridden.
items:
description: HttpAuthorizationServerAllowedHeaders specifies
description: HTTPAuthorizationServerAllowedHeaders specifies
how to conditionally match against allowed headers
in the context of HTTP authorization. It includes
options such as Exact, Prefix, Suffix, Contains, and
Expand Down Expand Up @@ -5083,9 +5083,9 @@ spec:
resource.
properties:
loadBalancerPolicy:
description: The policy for load balancing GRPC service requests.
Note that the `Cookie` and `RequestHash` load balancing strategies
cannot be used here.
description: The policy for load balancing service requests. Note
that the `Cookie` and `RequestHash` load balancing strategies cannot
be used here for GRPC service requests.
properties:
requestHashPolicies:
description: RequestHashPolicies contains a list of hash policies
Expand Down Expand Up @@ -5151,10 +5151,10 @@ spec:
type: object
protocol:
description: Protocol may be used to specify (or override) the protocol
used to reach this Service. Values may be h2 or h2c. If omitted,
protocol-selection falls back on Service annotations.
used to reach this Service. Values may be h2, h2c or http/1.1. If
omitted, protocol-selection falls back on Service annotations.
enum:
- h1
- http/1.1
- h2
- h2c
type: string
Expand All @@ -5168,7 +5168,7 @@ spec:
type: string
services:
description: Services specifies the set of Kubernetes Service resources
that receive GRPC extension API requests. If no weights are specified
that receive extension API requests. If no weights are specified
for any of the entries in this array, traffic will be spread evenly
across all the services. Otherwise, traffic is balanced proportionally
to the Weight field in each entry.
Expand Down Expand Up @@ -7410,8 +7410,8 @@ spec:
from internal authorization to Contour external authorization.
type: boolean
httpSettings:
description: HttpAuthorizationServerSettings defines configurations
for interacting with an external HTTP authorization server.
description: HTTPServerSettings defines configurations for
interacting with an external HTTP authorization server.
properties:
allowedAuthorizationHeaders:
description: AllowedAuthorizationHeaders specifies client
Expand All @@ -7420,7 +7420,7 @@ spec:
matchers, Host, Method, Path, Content-Length, and Authorization
are additionally included in the list.
items:
description: HttpAuthorizationServerAllowedHeaders specifies
description: HTTPAuthorizationServerAllowedHeaders specifies
how to conditionally match against allowed headers
in the context of HTTP authorization. It includes
options such as Exact, Prefix, Suffix, Contains, and
Expand Down Expand Up @@ -7459,7 +7459,7 @@ spec:
client request. Note that coexistent headers will be
overridden.
items:
description: HttpAuthorizationServerAllowedHeaders specifies
description: HTTPAuthorizationServerAllowedHeaders specifies
how to conditionally match against allowed headers
in the context of HTTP authorization. It includes
options such as Exact, Prefix, Suffix, Contains, and
Expand Down
Loading

0 comments on commit fe82257

Please sign in to comment.