- APIs created with WSO2 k8s API Operator can be secured by defining security with Security kind. It supports basic, JWT and Oauth2 security types.
Note:
- When a Security custom resource refers a secret, you need to make sure the namespace of the secret is same as the namespace of that Security custom resource. When an API refers a Security custom resource in swagger definition under security keyword you need to make sure that the namespace of the Security custom resource is same as the namespace that the API belongs to.
- Create a secret with the certificate
>> kubectl create secret generic <SECRET_NAME> -n <NAMESPACE> --from-file=<PATH_TO_CERT>
- The namespace of the secret should be the namespace of the Security custom resource.
- Create a security with Security kind. Include the name of the secret created in step (1) in certificate field
apiVersion: <VERSION> kind: Security metadata: name: <SECURITY_NAME> namespace: <NAMESPACE> spec: type: JWT securityConfig: - issuer: <ISSUER> audience: <AUDIENCE> certificate: <NAME_OF_THE_SECRET_CREATED_IN_STEP_1>
-
Create a secret with the certificate
>> kubectl create secret generic <SECRET_NAME> -n <NAMESPACE> --from-file=<PATH_TO_CERT>
- The namespace of the secret should be the namespace of the Security custom resource.
-
Create a secret with user credentials
apiVersion: v1 kind: Secret metadata: name: <SECRET_NAME> type: Opaque data: username: <BASE64_ENCODED_USER_NAME> password: <BASE64_ENCODED_PASSWORD>
-
Create a security with Security kind. Include the name of the secret created in step (1) in certificate field and name of the secret created in step (2) in credentials field.
apiVersion: <VERSION> kind: Security metadata: name: <SECURITY_NAME> namespace: <NAMESPACE> spec: type: Oauth securityConfig: - certificate: <NAME_OF_THE_SECRET_CREATED_IN_STEP_1> endpoint: <ENDPOINT> credentials: <NAME_OF_THE_SECRET_CREATED_IN_STEP_2>
-
Create a secret with user credentials
apiVersion: v1 kind: Secret metadata: name: <SECRET_NAME> type: Opaque data: username: <BASE64_ENCODED_USER_NAME> password: <BASE64_ENCODED_PASSWORD>
-
Create a security with Security kind. Include the name of the secret created in step (1) in credentials field.
apiVersion: <version> kind: Security metadata: name: <SECURITY_NAME> namespace: <NAMESPACE> spec: type: basic securityConfig: - credentials: <NAME_OF_THE_SECRET_CREATED_IN_STEP_1>
Security can be defined in swagger definition under security keyword in both API and resource levels. Define the property scopes for OAuth2 security scheme.
-
Defining security in API level**
security: - petstorebasic: [] - oauthtest: - read
-
Defining security in resource level**
paths: "/pet/findByStatus": get: security: - basicauth: - read:pets - write:pets - petstorebasic: []
Sample Security definitions can be find in here.