cds-dbm icon indicating copy to clipboard operation
cds-dbm copied to clipboard

Credentials in default-env.json

Open T1mey opened this issue 3 years ago • 6 comments

Hi,

I'll try running

npx cds-dbm deploy -create-db --load-via delta

but (in SAP BAS) the default-env.json is not picked up. Here's my file (values replaced)

{
	"VCAP_SERVICES": {
		"postgresql-db": [
			{
				"label": "postgresql-db",
				"provider": null,
				"plan": "standard",
				"name": "wp-postgres-tool",
				"tags": [
					"relational",
					"database"
				],
				"instance_guid": "guid",
				"instance_name": "wp-postgres-tool",
				"binding_guid": "guid",
				"binding_name": null,
				"credentials": {
					"username": "user",
					"password": "pass",
					"hostname": "db.com",
					"dbname": "xxx",
					"port": "4071",
					"uri": "postgres://url",
				"syslog_drain_url": null,
				"volume_mounts": []
			}
		],

It tries to connect to localhost...

TypeError: Cannot read property 'username' of undefined
    at getCredentialsForClient (/home/user/projects/onboarding-tool-cap/node_modules/cds-dbm/dist/adapter/PostgresAdapter.js:53:26)

T1mey avatar Nov 09 '21 13:11 T1mey

I have the same issue but locally, when checking issue https://github.com/sapmentors/cds-pg/issues/142, the issue is not localhost but the tag relational instead of plain to test locally I added in @sap/cds-compiler/lib/sql-indentifier.js the relation sqlDialect, but on BTP that is not an option...

bartdp86 avatar Mar 17 '22 15:03 bartdp86

Maybe @sjvans can help here. I've worked around this problem by setting the User-Provided Variables CDS_CONFIG to:

{
  "requires": {
    "database": {
      "dialect": "plain",
      "impl": "cds-pg",
      "model": "csn.json",
      "credentials": {
        "username": "user",
        "password": "pass",
        "hostname": "host",
        "dbname": "db",
        "port": "3285",
        "uri": "postgres://user:pass@host:3285/db",
        "sslcert": "cert",
        "sslrootcert": "cert"
      }
    }
  }
}

But that is of course not the solution. We either need the SAP BTP PostgreSQL Hyperscaler Service to set the Tag "plain" or that CAP would also search for the tags relational or database that are set in the service binding:

		"postgresql-db": [
			{
				"label": "postgresql-db",
				"provider": null,
				"plan": "trial",
				"name": "pg-beershop-database",
				"tags": [
					"relational",
					"database"
				],

gregorwolf avatar Mar 19 '22 23:03 gregorwolf

OK, thanks to the comment https://github.com/sapmentors/cds-pg/issues/263#issuecomment-1017514535 from @sebastianesch regarding the service lookup of CAP I've added:

        "vcap": {
          "name": "pg-beershop-database"
        }

to main/package.json#L91. I've tested also kind and set it to postgresql-db as this is the label of the service. But that did not produce a match. I think that is a bug in the CAP lookup for services. CC: @sjvans

gregorwolf avatar Mar 20 '22 00:03 gregorwolf

At the moment the service from VCAP is only picked up when either the tag is 'db' - as this is the service name from the cds.requires section (which is merged with the cds-pg section) AND/OR 'plain', the dialect from the cds-pg section.

You should be able to set the tags for your service instance either in your MTA descriptor or via the CF CLI.

sebastianesch avatar Mar 20 '22 14:03 sebastianesch

Hi @sebastianesch thank you for the tip. I've added now

      service-tags:
        - plain

to the PostgreSQL Service section of main/mta.yaml#L113. Now the credential lookup works without any issues.

gregorwolf avatar Mar 20 '22 17:03 gregorwolf

Hi @gregorwolf , In my case I am creating the database service using cockpit. In the mta.yaml I am using below code.

resources:
  - name: xxx-xxxx-xxxxxxxx-database
    type: org.cloudfoundry.existing-service

How do I update 'plain' service-tags. Can you please help?

harshadporwal47 avatar Sep 04 '22 13:09 harshadporwal47