Obtain VCAP_APPLICATION's application_name value on Spring's application.yml - cloud-foundry

I'm trying to set a logging pattern using 'logging.pattern.console' that needs to include the CloudFoundry's application name of a given application. I know that application names can be found as part of the VCAP_APPLICATION env variable with the 'application_name' key, and I can resolve env variables on Spring Cloud applications using the standard Spring placeholder notation, available on the application.yml file; but as the variable is a Json, I can't parse it nor use SpEL to obtain the requested value only.
Is there any other way to obtain the application name as set on the manifest.yml file in the application.yml?

If you are using Spring Boot, you can access the application name with the property vcap.application.name. You should be able to reference this anywhere that properties are available, like #Value annotations or in application.properties.
Spring Boot's CloudFoundryVcapEnvironmentPostProcessor takes the VCAP_SERVICES & VCAP_APPLICATION environment variables and makes them available as properties through Spring's Environment api. This should happen automatically, no config or work necessary.

Related

BigQuery - How to instantiate BigQueryTemplate without env variables? getting The Application Default Credentials are not available

I'm trying to instantiate BigQueryTemplate without the environment variable GOOGLE_APPLICATION_CREDENTIALS.
Steps tried:
Implemented CredentialsSupplier by instantiating Credentials and setting location to service account json file.
Instantiated Bean BigQuery using BigQueryOptions::newBuilder() and setting credentials and project id.
Instantiating Bean BigQueryTemplate using the BigQuery bean created in step 2.
spring-cloud-gcp-dependencies 3.4.0 version is used.
The application executing in VM (non-gcp env).
Another option I tried is adding below properties
spring.cloud.gcp.bigquery.dataset-name=datasetname
spring.cloud.gcp.bigquery.credentials.location=file:/path/to/json
spring.cloud.gcp.bigquery.project-id=project-id
I'm getting below error
com.google.cloud.spring.bigquery.core.BigQueryTemplate,
applog.mthd=lambda$writeJsonStream$0,
applog.line=299, applog.msg=Error:
The Application Default Credentials are not available.
They are available if running in Google Compute Engine.
Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials.
Please let me know if I have missed any thing.
Thanks in advance.

can not access environment variable specified by user provided service with java

I have created a user provided service as follows:
cf cups myservice -p '{"db": "text"}'
and I bind this service to my app, the service name is myservice.
When i use cf env command, i can see the message:
"user-provided": [{
"credentials":{
"db":"text"
},
"name":"myservice"
}]
but when i access this variable with java
System.getenv("cloud.services.myservice.db")
is null. Why can't I access the db value?
When you do cf env on your app, you see an environment variable named VCAP_SERVICES that contains a JSON data structure like you showed:
VCAP_SERVICES: {
"user-provided": [
{
"credentials":{ "db":"text" },
"name":"myservice"
}
]
}
Your application can retrieve this JSON structure with System.getenv("VCAP_SERVICES"). You can then parse the JSON returned from that call into a Map, for example, and retrieve the values needed.
There is no environment variable available to your app named cloud.services.myservice.db, so System.getenv("cloud.services.myservice.db") won't return anything useful.
Spring Boot parses the VCAP_SERVICES environment variable and creates Spring environment properties like cloud.services.myservice.credentials.db and vcap.services.myservice.credentials.db. These properties can't be retrieved with System.getenv() because they exist only in the Spring environment abstraction, not in the OS environment. This is described nicely in a Spring blog post. More details are in the Spring Boot javadoc.
Services In CloudFoundry are presented JSON blob in the VCAP_SERVICES environment variable.
In Java you will be able to get an object with all the services with:
JSONObject vcap = new JSONObject(System.getenv("VCAP_SERVICES"));
https://docs.run.pivotal.io/devguide/deploy-apps/environment-variable.html for more information on provided environment variables.

Is it possible to edit configuration nodes in a Node-Red flow?

In Node-Red, I'm using some Amazon Web Services nodes (from module node-red-node-aws), and I would like to read some configuration settings from a file (e.g. the access key ID & the secret key for the S3 nodes), but I can't find a way to set everything up dynamically, as this configuration has to be made in a config node, which can't be used in a flow.
Is there a way to do this in Node-Red?
Thanks!
Unless a node implementation specifically allows for dynamic configuration, this is not something that Node-RED does generically.
One approach I have seen is to have a flow update itself using the admin REST API into the runtime - see https://nodered.org/docs/api/admin/methods/post/flows/
That requires you to first GET the current flow configuration, modify the flow definition with the desired values and then post it back.
That approach is not suitable in all cases; the config node still only has a single active configuration.
Another approach, if the configuration is statically held in a file, is to insert them into your flow configuration before starting Node-RED - ie, have a place-holding config node configuration in the flow that you insert the credentials into.
Finally, you can use environment variables: if you set the configuration node's property to be something like $(MY_AWS_CREDS), then the runtime will substitute that environment variable on start-up.
You can update your package.json start script to start Node-RED with your desired credentials as environment variables:
"scripts": {
"start": "AWS_SECRET_ACCESS_KEY=<SECRET_KEY> AWS_ACCESS_KEY_ID=<KEY_ID> ./node_modules/.bin/node-red -s ./settings.js"
}
This worked perfect for me when using the node-red-contrib-aws-dynamodbnode. Just leave the credentials in the node blank and they get picked up from your environment variables.

Loopback - Setting Up environment specific configuration

Hi can I get some help with setting environment specific configuration.
I have two files for datasource
server/datasources.json
server/datasources.test.json
I use the script "SET NODE_ENV=test && mocha test/**/*.test.js" on WIndows to run my test cases and set the node environment to test.
Loopback does not load server/datasource.test.json instead the datasource from server/datasource.json is loaded.
I have confirmend the environment using process.env.NODE_ENV which logs "test
I have tried to change server/datasource.json to server/datasource.local.json, But then I get an error
WARNING: Main config file "datasources.json" is missing.
I dont understand what I am doing wrong.Am I supposed to create all the config files for the test environment like *.test.json.
Or is there a different config file where I have to define envrionment specific files.
Please check this repo https://github.com/dhruv004/sample-loopback-example
From the code If you run npm run test It loads data from local.json which is the data source for development environment.It should load data from test.json(datasource for test environment)
Looking on your repository, I can see this note from LoopBack documentation particulary relevant for you:
A LoopBack application can load multiple configuration files, that can potentially conflict with each other. The value set by the file with the highest priority will always take effect. The priorities are:
Environment-specific configuration, based on the value of NODE_ENV; for example, server/config.staging.json.
Local configuration file; for example, server/config.local.json.
Default configuration file; for example, server/config.json.
In your model-config.json all models have datasource set to db so in your case LoopBack application loads first datasources.test.json. It cannot find datasource db there (only testdb), so it falls back to datasources.json. There it finds datasource db and it uses it. Try renaming testdb in datasources.test.json to db and it will take a precedense.

Property configuration AEM Day lib

We are using the lib
"Day Commons Library - HTTP Client 3.1 Bundling
(com.day.commons.osgi.wrapper.commons-httpclient)"
in our AEM 6.0 system.
We'd like to change the property http.protocol.cookie-policy of this lib to another value. (It's the cookie policy https://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/cookie/CookiePolicy.html).
What is the best way to do that?
The property is not visible in the OSGi console, there are only other values of this lib configurable (like HTTP Proxy User etc.)
According to https://docs.adobe.com/docs/en/aem/6-0/deploy/configuring/configuring-osgi.html
one could create a configuration in
/apps/system/config
But how can we ensure that the library reloads exactly this configuration when we restart the bundle/service in OSGi console?
Does the entry in JCR require to be named like the class
org.apache.commons.httpclient.cookie.CookiePolicy
and the value in the jcr:content has to be of type String and has to contain the other cookie policy, e.g. BROWSER_COMPATIBILITY, correct?
How can we determine later if the value was loaded? The problem is: the lib is not our code, we can't simply add a log line.
The httpclient osgi bundle loads only select properties defined in metatype.xml (This is an example. not the actual config used in day common httpclient). so your config will not be used.
You should be able to use setCookiePolicy method of org.apache.commons.httpclient.params.HttpMethodParams.