Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Remove cloudRunUrl functionality - we always go via the load balancer…
Browse files Browse the repository at this point in the history
… now (#120)

* Remove cloudRunUrl functionality - we always go via the load balancer now
  • Loading branch information
varney authored Dec 22, 2023
1 parent 2a5eca7 commit be6bdd9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .c8rc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"test/helpers",
"test-data"
],
"statements": "68",
"statements": "67",
"branches": "97",
"functions": "40",
"lines": "68"
"lines": "67"
}
3 changes: 1 addition & 2 deletions config/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"gcpConfigs": {
"credentials": {
"url": "https://bn-credentials-test.bnu.bn.nr",
"audience": "credentials-test-audience",
"cloudRunUrl": "https://bn-credentials-test.app.run"
"audience": "credentials-test-audience"
}
},
"gcpProxy": {
Expand Down
11 changes: 4 additions & 7 deletions lib/utils/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ async function performRequest(method, params) {
async function buildHeaders(params, headers = {}) {
// We have enabled the possibility to send in a different gcp config to use
const conf = params.gcpConfig || config.gcpProxy;
// We will firsthand use the cloudRunUrl
// This so we go directly to the cloud run url without going through a LB out on the web.
const audienceOrUrl = conf.cloudRunUrl || conf.audience;
let gcpHeader = {};
if (!params.baseUrl && audienceOrUrl) gcpHeader = await getGcpAuthHeaders(audienceOrUrl);
if (!params.baseUrl && conf.audience) gcpHeader = await getGcpAuthHeaders(conf.audience);
headers = { ...headers, ...gcpHeader };
const defaults = {
accept: "application/json",
Expand Down Expand Up @@ -155,12 +152,12 @@ function withAssertion(verb, fn, params) {
}

function getUrl(params) {
// Enable the usage of sent in gcpConfig and cloudRunUrl.
// Enable the usage of sent in gcpConfig.
const conf = params.gcpConfig || config.gcpProxy;
// If we send in a baseUrl, always use that. Then look if we should call the cloudRun url
// If we send in a baseUrl, always use that.
// If not then call the loadbalancer url.
if (params.baseUrl) return `${params.baseUrl}${params.path}`;
return `${conf.cloudRunUrl || conf.url}${params.path}`;
return `${conf.url}${params.path}`;
}

export default backends;
36 changes: 9 additions & 27 deletions test/unit/utils/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ import config from "exp-config";
import nock from "nock";
import urlencode from "urlencode";

import clone from "../../helpers/clone.js";
import fakeApiInit from "../../helpers/fake-api.js";
import http from "../../../lib/utils/http.js";
import * as fakeGcpAuth from "../../helpers/fake-gcp-auth.js";

const fakeApi = fakeApiInit();
const credentialsFakeApi = fakeApiInit(config.gcpConfigs.credentials.cloudRunUrl);
const credentialsLoadBalancerFakeApi = fakeApiInit(config.gcpConfigs.credentials.url);
const credentialsFakeApi = fakeApiInit(config.gcpConfigs.credentials.url);

describe("http", () => {
beforeEach(() => {
fakeApi.reset();
credentialsFakeApi.reset();
credentialsLoadBalancerFakeApi.reset();
fakeGcpAuth.reset();
});

Expand Down Expand Up @@ -236,36 +233,36 @@ describe("http", () => {
});
});

describe("call other teams gcp with cloudrun url because we live in GCP, with sent-in gcpConfig", () => {
describe("call other teams gcp with audience because we live in GCP, with sent-in gcpConfig", () => {
beforeEach(fakeGcpAuth.authenticated);
after(fakeGcpAuth.reset);
const correlationId = "http-gcp-config";
const gcpConfig = config.gcpConfigs.credentials;

it("should do get-requests", async () => {
credentialsFakeApi.get("/credentials/some/path").reply(200, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.cloudRunUrl}`);
credentialsFakeApi.get("/credentials/some/path").reply(200, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.audience}`);
const result = await http.get({ path: "/credentials/some/path", gcpConfig, correlationId });
result.body.should.eql({ ok: true });
});

it("should fail on 500", (done) => {
credentialsFakeApi.get("/credentials/some/path").reply(500, { ok: false }).matchHeader("authorization", `Bearer ${gcpConfig.cloudRunUrl}`);
credentialsFakeApi.get("/credentials/some/path").reply(500, { ok: false }).matchHeader("authorization", `Bearer ${gcpConfig.audience}`);
http.asserted
.get({ path: "/credentials/some/path", gcpConfig, correlationId })
.then(() => done("should not come here"))
.catch(() => done());
});

it("should throw on 404", (done) => {
credentialsFakeApi.get("/credentials/some/path").reply(404, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.cloudRunUrl}`);
credentialsFakeApi.get("/credentials/some/path").reply(404, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.audience}`);
http.asserted
.get({ path: "/credentials/some/path", gcpConfig, correlationId })
.then(() => done("should not come here"))
.catch(() => done());
});

it("should do delete-requests", async () => {
credentialsFakeApi.delete("/credentials/some/path").reply(200, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.cloudRunUrl}`);
credentialsFakeApi.delete("/credentials/some/path").reply(200, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.audience}`);
const result = await http.asserted.del({ path: "/credentials/some/path", gcpConfig, correlationId });
result.should.eql({ ok: true });
});
Expand All @@ -275,7 +272,7 @@ describe("http", () => {
credentialsFakeApi[method.toLowerCase()]("/credentials/some/path", (body) => {
body.should.eql({ correlationId });
return true;
}).reply(200, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.cloudRunUrl}`);
}).reply(200, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.audience}`);
const result = await http.asserted[method.toLowerCase()]({
path: "/credentials/some/path",
gcpConfig,
Expand All @@ -290,7 +287,7 @@ describe("http", () => {
credentialsFakeApi[method.toLowerCase()]("/credentials/some/path", (body) => {
body.should.eql({ correlationId });
return true;
}).reply(code, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.cloudRunUrl}`);
}).reply(code, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.audience}`);
const result = await http.asserted[method.toLowerCase()]({
path: "/credentials/some/path",
gcpConfig,
Expand All @@ -302,28 +299,13 @@ describe("http", () => {
});

it("should throw on 404", (done) => {
credentialsFakeApi[method.toLowerCase()]("/credentials/some/path").reply(404, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.cloudRunUrl}`);
credentialsFakeApi[method.toLowerCase()]("/credentials/some/path").reply(404, { ok: true }).matchHeader("authorization", `Bearer ${gcpConfig.audience}`);
http.asserted[method.toLowerCase()]({ path: "/credentials/some/path", gcpConfig, correlationId })
.then(() => done("should not come here"))
.catch(() => done());
});
});
});

describe("call other teams gcp with audience, because we don't live in GCP, with sent-in gcpConfig", () => {
before(credentialsLoadBalancerFakeApi.reset);
beforeEach(fakeGcpAuth.authenticated);
after(fakeGcpAuth.reset);
const correlationId = "http-gcp-config";
const gcpConfig = clone(config.gcpConfigs.credentials);
delete gcpConfig.cloudRunUrl;

it("should do get-requests", async () => {
credentialsLoadBalancerFakeApi.get("/credentials/some/path").reply(200, { ok: true });
const result = await http.get({ path: "/credentials/some/path", gcpConfig, correlationId });
result.body.should.eql({ ok: true });
});
});

afterEach(fakeApi.reset);
});

0 comments on commit be6bdd9

Please sign in to comment.