2018-01-31 16:53:07 +00:00
/ *
Copyright 2017 The Kubernetes Authors .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package sslcipher
import (
2023-07-22 03:32:07 +00:00
"regexp"
2021-08-21 20:42:00 +00:00
networking "k8s.io/api/networking/v1"
2018-01-31 16:53:07 +00:00
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
2023-07-22 03:32:07 +00:00
"k8s.io/ingress-nginx/internal/ingress/errors"
2018-01-31 16:53:07 +00:00
"k8s.io/ingress-nginx/internal/ingress/resolver"
)
2023-07-22 03:32:07 +00:00
const (
sslPreferServerCipherAnnotation = "ssl-prefer-server-ciphers"
sslCipherAnnotation = "ssl-ciphers"
)
2023-08-31 07:36:48 +00:00
// Should cover something like "ALL:!aNULL:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP"
2024-01-05 14:50:34 +00:00
// (?:@STRENGTH) is included twice so it can appear before or after @SECLEVEL=n
var regexValidSSLCipher = regexp . MustCompile ( ` ^(?:(?:[A-Za-z0-9!:+\-])*(?:@STRENGTH)*(?:@SECLEVEL=[0-5])*(?:@STRENGTH)*)*$ ` )
2023-07-22 03:32:07 +00:00
var sslCipherAnnotations = parser . Annotation {
Group : "backend" ,
Annotations : parser . AnnotationFields {
sslPreferServerCipherAnnotation : {
Validator : parser . ValidateBool ,
Scope : parser . AnnotationScopeIngress ,
Risk : parser . AnnotationRiskLow ,
Documentation : ` The following annotation will set the ssl_prefer_server_ciphers directive at the server level .
2024-05-28 18:37:30 +00:00
This configuration specifies that server ciphers should be preferred over client ciphers when using the TLS protocols . ` ,
2023-07-22 03:32:07 +00:00
} ,
sslCipherAnnotation : {
2023-08-31 07:36:48 +00:00
Validator : parser . ValidateRegex ( regexValidSSLCipher , true ) ,
2023-07-22 03:32:07 +00:00
Scope : parser . AnnotationScopeIngress ,
Risk : parser . AnnotationRiskLow ,
Documentation : ` Using this annotation will set the ssl_ciphers directive at the server level. This configuration is active for all the paths in the host. ` ,
} ,
} ,
}
2018-01-31 16:53:07 +00:00
type sslCipher struct {
2023-07-22 03:32:07 +00:00
r resolver . Resolver
annotationConfig parser . Annotation
2018-01-31 16:53:07 +00:00
}
2020-05-13 02:40:56 +00:00
// Config contains the ssl-ciphers & ssl-prefer-server-ciphers configuration
2020-05-11 08:31:08 +00:00
type Config struct {
SSLCiphers string
SSLPreferServerCiphers string
}
2018-01-31 16:53:07 +00:00
// NewParser creates a new sslCipher annotation parser
func NewParser ( r resolver . Resolver ) parser . IngressAnnotation {
2023-07-22 03:32:07 +00:00
return sslCipher {
r : r ,
annotationConfig : sslCipherAnnotations ,
}
2018-01-31 16:53:07 +00:00
}
// Parse parses the annotations contained in the ingress rule
2020-05-11 08:31:08 +00:00
// used to add ssl-ciphers & ssl-prefer-server-ciphers to the server name
2019-06-09 22:49:59 +00:00
func ( sc sslCipher ) Parse ( ing * networking . Ingress ) ( interface { } , error ) {
2020-05-11 08:31:08 +00:00
config := & Config { }
var err error
var sslPreferServerCiphers bool
2023-07-22 03:32:07 +00:00
sslPreferServerCiphers , err = parser . GetBoolAnnotation ( sslPreferServerCipherAnnotation , ing , sc . annotationConfig . Annotations )
2020-05-11 08:31:08 +00:00
if err != nil {
config . SSLPreferServerCiphers = ""
} else {
if sslPreferServerCiphers {
config . SSLPreferServerCiphers = "on"
} else {
config . SSLPreferServerCiphers = "off"
}
}
2023-07-22 03:32:07 +00:00
config . SSLCiphers , err = parser . GetStringAnnotation ( sslCipherAnnotation , ing , sc . annotationConfig . Annotations )
if err != nil && ! errors . IsInvalidContent ( err ) && ! errors . IsMissingAnnotations ( err ) {
return config , err
}
2020-05-11 08:31:08 +00:00
return config , nil
2018-01-31 16:53:07 +00:00
}
2023-07-22 03:32:07 +00:00
func ( sc sslCipher ) GetDocumentation ( ) parser . AnnotationFields {
return sc . annotationConfig . Annotations
}
2023-08-31 07:36:48 +00:00
func ( sc sslCipher ) Validate ( anns map [ string ] string ) error {
maxrisk := parser . StringRiskToRisk ( sc . r . GetSecurityConfiguration ( ) . AnnotationsRiskLevel )
2023-07-22 03:32:07 +00:00
return parser . CheckAnnotationRisk ( anns , maxrisk , sslCipherAnnotations . Annotations )
}