This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.Questo contenuto non è disponibile nella lingua selezionata.
Chapter 10. Security
10.1. Configuring JSON Web Token authentication for Knative services Copia collegamentoCollegamento copiato negli appunti!
OpenShift Serverless does not currently have user-defined authorization features. To add user-defined authorization to your deployment, you must integrate OpenShift Serverless with Red Hat OpenShift Service Mesh, and then configure JSON Web Token (JWT) authentication and sidecar injection for Knative services.
10.1.1. Using JSON Web Token authentication with Service Mesh 2.x and OpenShift Serverless Copia collegamentoCollegamento copiato negli appunti!
You can use JSON Web Token (JWT) authentication with Knative services by using Service Mesh 2.x and OpenShift Serverless. To do this, you must create authentication requests and policies in the application namespace that is a member of the ServiceMeshMemberRoll
object. You must also enable sidecar injection for the service.
Adding sidecar injection to pods in system namespaces, such as knative-serving
and knative-serving-ingress
, is not supported when Kourier is enabled.
If you require sidecar injection for pods in these namespaces, see the OpenShift Serverless documentation on Integrating Service Mesh with OpenShift Serverless natively.
Prerequisites
- You have installed the OpenShift Serverless Operator, Knative Serving, and Red Hat OpenShift Service Mesh on your cluster.
-
Install the OpenShift CLI (
oc
). - You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in OpenShift Container Platform.
Procedure
Add the
sidecar.istio.io/inject="true"
annotation to your service:Example service
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
Service
resource:oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
RequestAuthentication
resource in each serverless application namespace that is a member in theServiceMeshMemberRoll
object:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
RequestAuthentication
resource:oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Allow access to the
RequestAuthenticaton
resource from system pods for each serverless application namespace that is a member in theServiceMeshMemberRoll
object, by creating the followingAuthorizationPolicy
resource:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
AuthorizationPolicy
resource:oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For each serverless application namespace that is a member in the
ServiceMeshMemberRoll
object, create the followingAuthorizationPolicy
resource:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
AuthorizationPolicy
resource:oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
If you try to use a
curl
request to get the Knative service URL, it is denied:Example command
curl http://hello-example-1-default.apps.mycluster.example.com/
$ curl http://hello-example-1-default.apps.mycluster.example.com/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
RBAC: access denied
RBAC: access denied
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify the request with a valid JWT.
Get the valid JWT token:
TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.8/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.8/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Access the service by using the valid token in the
curl
request header:curl -H "Authorization: Bearer $TOKEN" http://hello-example-1-default.apps.example.com
$ curl -H "Authorization: Bearer $TOKEN" http://hello-example-1-default.apps.example.com
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The request is now allowed:
Example output
Hello OpenShift!
Hello OpenShift!
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.1.2. Using JSON Web Token authentication with Service Mesh 1.x and OpenShift Serverless Copia collegamentoCollegamento copiato negli appunti!
You can use JSON Web Token (JWT) authentication with Knative services by using Service Mesh 1.x and OpenShift Serverless. To do this, you must create a policy in the application namespace that is a member of the ServiceMeshMemberRoll
object. You must also enable sidecar injection for the service.
Adding sidecar injection to pods in system namespaces, such as knative-serving
and knative-serving-ingress
, is not supported when Kourier is enabled.
If you require sidecar injection for pods in these namespaces, see the OpenShift Serverless documentation on Integrating Service Mesh with OpenShift Serverless natively.
Prerequisites
- You have installed the OpenShift Serverless Operator, Knative Serving, and Red Hat OpenShift Service Mesh on your cluster.
-
Install the OpenShift CLI (
oc
). - You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in OpenShift Container Platform.
Procedure
Add the
sidecar.istio.io/inject="true"
annotation to your service:Example service
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
Service
resource:oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a policy in a serverless application namespace which is a member in the
ServiceMeshMemberRoll
object, that only allows requests with valid JSON Web Tokens (JWT):ImportantThe paths
/metrics
and/healthz
must be included inexcludedPaths
because they are accessed from system pods in theknative-serving
namespace.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
Policy
resource:oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
If you try to use a
curl
request to get the Knative service URL, it is denied:curl http://hello-example-default.apps.mycluster.example.com/
$ curl http://hello-example-default.apps.mycluster.example.com/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Origin authentication failed.
Origin authentication failed.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify the request with a valid JWT.
Get the valid JWT token:
TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Access the service by using the valid token in the
curl
request header:curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"
$ curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The request is now allowed:
Example output
Hello OpenShift!
Hello OpenShift!
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.2. Configuring a custom domain for a Knative service Copia collegamentoCollegamento copiato negli appunti!
Knative services are automatically assigned a default domain name based on your cluster configuration. For example, <service_name>-<namespace>.example.com
. You can customize the domain for your Knative service by mapping a custom domain name that you own to a Knative service.
You can do this by creating a DomainMapping
resource for the service. You can also create multiple DomainMapping
resources to map multiple domains and subdomains to a single service.
10.2.1. Creating a custom domain mapping Copia collegamentoCollegamento copiato negli appunti!
You can customize the domain for your Knative service by mapping a custom domain name that you own to a Knative service. To map a custom domain name to a custom resource (CR), you must create a DomainMapping
CR that maps to an Addressable target CR, such as a Knative service or a Knative route.
Prerequisites
- The OpenShift Serverless Operator and Knative Serving are installed on your cluster.
-
Install the OpenShift CLI (
oc
). - You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in OpenShift Container Platform.
You have created a Knative service and control a custom domain that you want to map to that service.
NoteYour custom domain must point to the IP address of the OpenShift Container Platform cluster.
Procedure
Create a YAML file containing the
DomainMapping
CR in the same namespace as the target CR you want to map to:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example service domain mapping
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example route domain mapping
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
DomainMapping
CR as a YAML file:oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.2.2. Creating a custom domain mapping by using the Knative CLI Copia collegamentoCollegamento copiato negli appunti!
You can customize the domain for your Knative service by mapping a custom domain name that you own to a Knative service. You can use the Knative (kn
) CLI to create a DomainMapping
custom resource (CR) that maps to an Addressable target CR, such as a Knative service or a Knative route.
Prerequisites
- The OpenShift Serverless Operator and Knative Serving are installed on your cluster.
You have created a Knative service or route, and control a custom domain that you want to map to that CR.
NoteYour custom domain must point to the DNS of the OpenShift Container Platform cluster.
-
You have installed the Knative (
kn
) CLI. - You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in OpenShift Container Platform.
Procedure
Map a domain to a CR in the current namespace:
kn domain create <domain_mapping_name> --ref <target_name>
$ kn domain create <domain_mapping_name> --ref <target_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example command
kn domain create example.com --ref example-service
$ kn domain create example.com --ref example-service
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
--ref
flag specifies an Addressable target CR for domain mapping.If a prefix is not provided when using the
--ref
flag, it is assumed that the target is a Knative service in the current namespace.Map a domain to a Knative service in a specified namespace:
kn domain create <domain_mapping_name> --ref <ksvc:service_name:service_namespace>
$ kn domain create <domain_mapping_name> --ref <ksvc:service_name:service_namespace>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example command
kn domain create example.com --ref ksvc:example-service:example-namespace
$ kn domain create example.com --ref ksvc:example-service:example-namespace
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Map a domain to a Knative route:
kn domain create <domain_mapping_name> --ref <kroute:route_name>
$ kn domain create <domain_mapping_name> --ref <kroute:route_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example command
kn domain create example.com --ref kroute:example-route
$ kn domain create example.com --ref kroute:example-route
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.2.3. Securing a service with a custom domain by using a TLS certificate Copia collegamentoCollegamento copiato negli appunti!
After you have configured a custom domain for a Knative service, you can use a TLS certificate to secure the mapped service. To do this, you must create a Kubernetes TLS secret, and then update the DomainMapping
CR to use the TLS secret that you have created.
Prerequisites
-
You configured a custom domain for a Knative service and have a working
DomainMapping
CR. - You have a TLS certificate from your Certificate Authority provider or a self-signed certificate.
-
You have obtained the
cert
andkey
files from your Certificate Authority provider, or a self-signed certificate. -
Install the OpenShift CLI (
oc
).
Procedure
Create a Kubernetes TLS secret:
oc create secret tls <tls_secret_name> --cert=<path_to_certificate_file> --key=<path_to_key_file>
$ oc create secret tls <tls_secret_name> --cert=<path_to_certificate_file> --key=<path_to_key_file>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you are using Red Hat OpenShift Service Mesh as the ingress for your OpenShift Serverless installation, label the Kubernetes TLS secret with the following:
“networking.internal.knative.dev/certificate-uid": “<value>”
“networking.internal.knative.dev/certificate-uid": “<value>”
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you are using a third-party secret provider such as cert-manager, you can configure your secret manager to label the Kubernetes TLS secret automatically. Cert-manager users can use the secret template offered to automatically generate secrets with the correct label. In this case, secret filtering is done based on the key only, but this value can carry useful information such as the certificate ID that the secret contains.
NoteThe {cert-manager-operator} is a Technology Preview feature. For more information, see the Installing the {cert-manager-operator} documentation.
Update the
DomainMapping
CR to use the TLS secret that you have created:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the
DomainMapping
CR status isTrue
, and that theURL
column of the output shows the mapped domain with the schemehttps
:oc get domainmapping <domain_name>
$ oc get domainmapping <domain_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME URL READY REASON example.com https://example.com True
NAME URL READY REASON example.com https://example.com True
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: If the service is exposed publicly, verify that it is available by running the following command:
curl https://<domain_name>
$ curl https://<domain_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the certificate is self-signed, skip verification by adding the
-k
flag to thecurl
command.