|
| 1 | +--- |
| 2 | +parser: v2 |
| 3 | +auto_validation: true |
| 4 | +time: 20 |
| 5 | +tags: [tutorial>beginner, software-product>sap-btp--cloud-foundry-runtime-and-environment] |
| 6 | +primary_tag: software-product>sap-btp--cloud-foundry-runtime-and-environment |
| 7 | +author_name: Beyhan Veli |
| 8 | +author_profile: https://github.com/beyhan |
| 9 | +--- |
| 10 | + |
| 11 | +# Delivering Service Binding Credentials to Your Application |
| 12 | +<!-- description --> Learn how Cloud Foundry provides service credentials to your application and how to configure the credential delivery method. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | +- **Groups:** [Create Your First App on Cloud Foundry](group.scp-3-first-app) |
| 16 | +- **Tutorials:** [Create a Service Instance in SAP BTP](btp-cockpit-instances) |
| 17 | +- **Tutorials:** [Install the Cloud Foundry Command Line Interface (CLI)](cp-cf-download-cli) |
| 18 | + |
| 19 | +## You will learn |
| 20 | +- What service binding is and why it is important in Cloud Foundry |
| 21 | +- The three credential delivery methods available in Cloud Foundry |
| 22 | +- How to create a service binding and bind it to your application |
| 23 | +- How to enable and use the file-based credential delivery option |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +### Introduction to Service Binding in Cloud Foundry |
| 28 | + |
| 29 | +Service binding is the process by which Cloud Foundry provisions credentials for a service instance and delivers them to your application at runtime. When you **bind** a service instance to your application, the platform generates credentials (such as URLs, usernames, passwords, certificates, or API keys) and makes them available inside the application container. Your application can then use these credentials to connect to the service without hardcoding any configuration. |
| 30 | + |
| 31 | +This approach follows the [twelve-factor app](https://12factor.net/backing-services) methodology, where backing services are treated as attached resources, configured through the environment rather than embedded in application code. The benefits include: |
| 32 | + |
| 33 | +- **Portability:** The same application code works across development, staging, and production environments because credentials are injected externally. |
| 34 | +- **Security:** Credentials are never stored in source code or configuration files that could be committed to version control. |
| 35 | +- **Flexibility:** You can swap or rotate service instances without changing your application code. |
| 36 | + |
| 37 | +Service binding credentials are usually unique for each application. Another application bound to the same service instance may receive different credentials. This behavior depends on the service implementation. |
| 38 | + |
| 39 | +> After binding or unbinding a service instance, you must **restage** or **re-push** your application for changes to take effect. |
| 40 | +
|
| 41 | +### Overview of Service Binding Delivery Options |
| 42 | + |
| 43 | +When a service instance is created and bound to your application, the credentials and metadata become available inside the application container. Cloud Foundry offers **three mutually exclusive** delivery methods. You must choose one, and your application must consume credentials using the method you select. |
| 44 | + |
| 45 | +**1. Default: `VCAP_SERVICES` environment variable** |
| 46 | + |
| 47 | +By default, credentials for all bound service instances are stored as a JSON object in the [VCAP_SERVICES](https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#VCAP-SERVICES) environment variable. Your application parses this JSON at startup to extract connection details. Helper libraries are available for many frameworks. |
| 48 | + |
| 49 | +> The `VCAP_SERVICES` environment variable has a maximum size of **130 KB**. If you bind many services or if your services provide large credential payloads, consider one of the alternative delivery methods below. |
| 50 | +
|
| 51 | +**2. File-based VCAP services** |
| 52 | + |
| 53 | +If the [file-based-vcap-services](https://v3-apidocs.cloudfoundry.org/version/3.216.0/index.html#the-app-feature-object) app feature is enabled, Cloud Foundry writes the same JSON content to a file inside the container and exposes the file path through the [VCAP_SERVICES_FILE_PATH](https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#VCAP-SERVICES-FILE-PATH) environment variable. Your application reads credentials from the file instead of the environment variable. |
| 54 | + |
| 55 | +By default, this method supports credential payloads up to **1 MB**, although the Cloud Foundry operator can set a different limit. This is significantly higher than the 130 KB limit of the environment variable method. The file-based method is a good choice if you have many service bindings or services that return large credential objects. |
| 56 | + |
| 57 | +**3. Service Binding for K8s** |
| 58 | + |
| 59 | +If the [service-binding-k8s](https://v3-apidocs.cloudfoundry.org/version/3.216.0/index.html#the-app-feature-object) app feature is enabled, Cloud Foundry exposes credentials as a directory tree of files, following the [servicebinding.io](https://servicebinding.io) specification. The [SERVICE_BINDING_ROOT](https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#SERVICE-BINDING-ROOT) environment variable points to the root folder of this file structure. |
| 60 | + |
| 61 | +Each binding gets its own subdirectory named after the service. Inside each directory, every credential field is saved as a separate file. Nested structures and lists are serialized as JSON strings. For example if you have `VCAP_SERVICES` content like: |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "foo": [ |
| 66 | + { |
| 67 | + "name": "foo", |
| 68 | + "credentials": { |
| 69 | + "host": "db.example.com", |
| 70 | + "port": "5432", |
| 71 | + "deeply": { "nested": "value" } |
| 72 | + } |
| 73 | + } |
| 74 | + ] |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +This produces the following file structure: |
| 79 | + |
| 80 | +``` |
| 81 | +$SERVICE_BINDING_ROOT/ |
| 82 | + foo/ |
| 83 | + name: foo |
| 84 | + host: db.example.com |
| 85 | + port: 5432 |
| 86 | + deeply: {"nested":"value"} |
| 87 | +``` |
| 88 | + |
| 89 | +This method also supports up to **1 MB** in total across all files. It is fully compatible with Kubernetes service binding conventions, making it ideal for applications that run on both Cloud Foundry and Kubernetes. |
| 90 | + |
| 91 | +> These three delivery methods are **mutually exclusive**. Enabling one disables the others. If the application cannot consume credentials via the selected method, it will not be able to connect to its bound services, which may cause downtime. |
| 92 | +
|
| 93 | +### Bind a Service Instance to Your Application |
| 94 | + |
| 95 | +Follow the [Create a Service Instance in SAP BTP](btp-cockpit-instances) tutorial to create a service instance in your Cloud Foundry environment. It does not matter which service you choose. Please name your instance `my-first-service-instance`. |
| 96 | + |
| 97 | +To bind this service instance to your [cf-nodejs](https://github.com/SAP-samples/cf-sample-app-nodejs.git) application, update your `manifest.yml` file as shown below: |
| 98 | + |
| 99 | +```yaml |
| 100 | +--- |
| 101 | +applications: |
| 102 | + - name: cf-nodejs |
| 103 | + memory: 192M |
| 104 | + instances: 1 |
| 105 | + services: |
| 106 | + - my-first-service-instance |
| 107 | +``` |
| 108 | +
|
| 109 | +Then push your application to pick up the new binding: |
| 110 | +
|
| 111 | +```bash |
| 112 | +cf push cf-nodejs |
| 113 | +``` |
| 114 | + |
| 115 | +Alternatively, you can create the service binding using the CF CLI. Run the following command to bind the `my-first-service-instance` service instance to your `cf-nodejs` application: |
| 116 | + |
| 117 | +```bash |
| 118 | +cf bind-service cf-nodejs my-first-service-instance |
| 119 | +``` |
| 120 | + |
| 121 | +You should see output similar to: |
| 122 | + |
| 123 | +``` |
| 124 | +Binding service instance my-first-service-instance to app cf-nodejs in org <my-org> / <my-space> test as <my-user>... |
| 125 | +OK |
| 126 | +
|
| 127 | +TIP: Use 'cf restage cf-nodejs' to ensure your env variable changes take effect |
| 128 | +``` |
| 129 | + |
| 130 | +Restage your application to pick up the new binding: |
| 131 | + |
| 132 | +```bash |
| 133 | +cf restage cf-nodejs |
| 134 | +``` |
| 135 | + |
| 136 | +After the restage or push, verify the binding by inspecting your application's environment: |
| 137 | + |
| 138 | +```bash |
| 139 | +cf env cf-nodejs |
| 140 | +``` |
| 141 | + |
| 142 | +Look for the `VCAP_SERVICES` section in the output. This section contains the credentials and metadata for all services bound to your application (since the default binding method is being used). |
| 143 | + |
| 144 | +### Configure the File-Based Service Binding Option |
| 145 | + |
| 146 | +By default, Cloud Foundry delivers credentials through the `VCAP_SERVICES` environment variable. If you need to work with larger credential payloads (beyond the 130 KB environment variable limit) or prefer credential access via file, you can enable the **file-based VCAP services** delivery method. |
| 147 | + |
| 148 | +Currently, the CF CLI does not support enabling Cloud Foundry application features. Therefore, to enable file-based credential delivery for your application, you need to use the Cloud Foundry APIs by running the following commands: |
| 149 | + |
| 150 | +```bash |
| 151 | +cf app cf-nodejs --guid # get the guid of your applicaiton |
| 152 | +cf curl -X PATCH "/v3/apps/<app-guid>/features/file-based-vcap-services" -d '{ "enabled": true }' # enable the "file-based-vcap-services" application feature |
| 153 | +``` |
| 154 | + |
| 155 | +After enabling this feature, restart your application for the change to take effect: |
| 156 | + |
| 157 | +```bash |
| 158 | +cf restart cf-nodejs |
| 159 | +``` |
| 160 | + |
| 161 | +Once the application is running, verify that the `VCAP_SERVICES_FILE_PATH` environment variable is set: |
| 162 | + |
| 163 | +```bash |
| 164 | +cf env cf-nodejs |
| 165 | +``` |
| 166 | + |
| 167 | +The value of `VCAP_SERVICES_FILE_PATH` should be a file path such as `/etc/cf-service-bindings/vcap_services`. To inspect the contents of this file, you will need to SSH into the application container. For instructions, see [Accessing your apps with SSH](https://docs.cloudfoundry.org/devguide/deploy-apps/ssh-apps.html). |
| 168 | + |
| 169 | +--- |
0 commit comments