bcgov / api-services-portal

API Services Portal provides a multi-tenant frontend integrating API Gateway and Authorization services from Kong CE and Keycloak.
https://api.gov.bc.ca
Apache License 2.0
22 stars 7 forks source link

My Access page giving a 500 error #161

Closed ikethecoder closed 2 years ago

ikethecoder commented 3 years ago

When I log in with my Github account and go to My Access in Test environment, I get this page:

Screen Shot 2021-07-30 at 3.45.52 PM.png

ikethecoder commented 3 years ago

In the logs, I get this, but no other details:

TypeError: Cannot read property 'id' of null
    at /app/dist/www/server/pages/devportal/access.js:2582:145
    at arrayAggregator (/app/node_modules/lodash/_arrayAggregator.js:17:32)
    at /app/node_modules/lodash/_createAggregator.js:19:12
    at AccessList (/app/dist/www/server/pages/devportal/access.js:2579:41)
ikethecoder commented 3 years ago

I think the issue is that there are ServiceAccess records with no ProductEnvironment, which in theory should not happen.

Why would there be records for:

select * from "ServiceAccess" where "productEnvironment" is null;

Well, records can exist if a user has used "Link Consumer to Namespace" or the Consumer was migrated during initial onboarding from the Gateway; in these cases, there is no Application or ProductEnvironment linked.

Jonesy commented 3 years ago

This might be a duplicate of #148, where I have added some filtering for if these sneak by the API.

ikethecoder commented 3 years ago

Root cause is that it is possible to get orphaned Environments (Environments that are not associated with any Products). This can occur when Products and Environments are updated via the Directory API.

This ticket will cleanup the orphaned records:

select * from "Environment" where product is null;

select * from "GatewayService" where "environment" in (select id from "Environment" where product is null);
update "GatewayService" set environment = null where "environment" in (select id from "Environment" where product is null);
delete from "Environment" where product is null;

And will fix the Directory API (Batch Feeder) to avoid such issues in the future.

There should never be a case where an Environment is not associated with a Product.

ikethecoder commented 2 years ago

This error can also occur when an Environment is deleted. In this case, existing ServiceAccess records get there productEnvironment attribute set to null.