Access to Secrets mounted as Volume
This tutorial shows how to use Secrets mounted as volume with the Serverless Function. It's based on a simple Function in Python 3.9. The Function reads data from Secret and returns it.
Prerequisites
Before you start, make sure you have these tools installed:
- Kyma installed on a cluster
Steps
Follow these steps:
Export these variables:
Click to copyexport FUNCTION_NAME={FUNCTION_NAME}export NAMESPACE={FUNCTION_NAMESPACE}export DOMAIN={DOMAIN_NAME}export SECRET_NAME={SECRET_NAME}export SECRET_DATA_KEY={SECRET_DATA_KEY}export SECRET_MOUNT_PATH={SECRET_MOUNT_PATH}Create a Secret:
Click to copykubectl -n $NAMESPACE create secret generic $SECRET_NAME \--from-literal=$SECRET_DATA_KEY={SECRET_DATA_VALUE}Create your Function with
secretMounts
:Click to copycat <<EOF | kubectl apply -f -apiVersion: serverless.kyma-project.io/v1alpha2kind: Functionmetadata:name: $FUNCTION_NAMEnamespace: $NAMESPACEspec:runtime: python39source:inline:source: |from os import pathBASE_PATH = "$SECRET_MOUNT_PATH"DATA_PATH = path.join(BASE_PATH, "$SECRET_DATA_KEY")def main(event, context):with open(DATA_PATH, "r") as f:data = f.read()return datasecretMounts:- secretName: $SECRET_NAMEmountPath: $SECRET_MOUNT_PATHEOFNOTE: Read more about creating Functions.
Create an APIRule:
The following steps allow you to test the Function in action.
Click to copycat <<EOF | kubectl apply -f -apiVersion: gateway.kyma-project.io/v1beta1kind: APIRulemetadata:name: $FUNCTION_NAMEnamespace: $NAMESPACEspec:gateway: kyma-system/kyma-gatewayhost: $FUNCTION_NAME.$DOMAINrules:- path: /.*accessStrategies:- config: {}handler: noopmethods:- GET- POST- PUT- DELETEservice:name: $FUNCTION_NAMEport: 80EOFNOTE: Read more about exposing Functions.
Call Function:
Click to copycurl https://$FUNCTION_NAME.$DOMAINYou should get
{SECRET_DATA_VALUE}
as a result.Next steps:
Now you can edit the Secret and see if the Function returns the new value from the Secret.
To edit your Secret, use:
Click to copykubectl -n $NAMESPACE edit secret $SECRET_NAMETo encode values used in
data
from the Secret, usebase64
, for example:Click to copyecho -n '{NEW_SECRET_DATA_VALUE}' | base64Calling the Function again (using
curl
) must return{NEW_SECRET_DATA_VALUE}
. Note that the Secret propagation may take some time, and the call may initially return the old value.