Skip to content

Commit

Permalink
fix(oauth2): allow name override (#914)
Browse files Browse the repository at this point in the history
* fix(oauth2): allow name override

* fix
  • Loading branch information
Julien Bouquillon committed Feb 10, 2022
1 parent d96ad5e commit 177139a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 12 deletions.
21 changes: 19 additions & 2 deletions src/components/oauth2-proxy/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Array [
},
},
],
"image": "quay.io/oauth2-proxy/oauth2-proxy:v7.2.0",
"image": "quay.io/oauth2-proxy/oauth2-proxy:v7.2.1",
"livenessProbe": Object {
"failureThreshold": 6,
"httpGet": Object {
Expand Down Expand Up @@ -353,7 +353,7 @@ Array [
},
},
],
"image": "quay.io/oauth2-proxy/oauth2-proxy:v7.2.0",
"image": "quay.io/oauth2-proxy/oauth2-proxy:v7.2.1",
"livenessProbe": Object {
"failureThreshold": 6,
"httpGet": Object {
Expand Down Expand Up @@ -506,6 +506,23 @@ Array [
]
`;

exports[`should return proxy manifests with custom env (legacy syntax) 1`] = `
Array [
Object {
"name": "OAUTH2_PROXY_HTTP_ADDRESS",
"value": "0.0.0.0:4180",
},
Object {
"name": "CUSTOM_VAR",
"value": "HELLO",
},
Object {
"name": "OAUTH2_PROXY_REDIRECT_URL",
"value": "https://test-456.fabrique.social.gouv.fr/oauth2/callback",
},
]
`;

exports[`should return proxy manifests with custom env 1`] = `
Array [
Object {
Expand Down
67 changes: 65 additions & 2 deletions src/components/oauth2-proxy/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test("should return dev manifests", async () => {

jest.doMock("@socialgouv/kosko-charts/environments", () => environmentMock);

const manifests = await create({
const manifests = await create("proxy", {
upstream: "http://target:123",
});
expect(manifests).toMatchSnapshot();
Expand All @@ -72,7 +72,7 @@ test("should return dev manifests with custom config", async () => {

jest.doMock("@socialgouv/kosko-charts/environments", () => environmentMock);

const manifests = await create({
const manifests = await create("proxy", {
config: {
subDomainPrefix: "metabase-",
},
Expand All @@ -91,6 +91,40 @@ test("should return dev manifests with custom envFrom", async () => {

jest.doMock("@socialgouv/kosko-charts/environments", () => environmentMock);

const expectedEnvFrom = [
{
secretRef: { name: "another-secret" },
},
{
configMapRef: { name: "another-configmap" },
},
];
const manifests = await create("proxy", {
config: {
container: {
envFrom: expectedEnvFrom,
},
subDomainPrefix: "metabase-",
},
upstream: "http://target:123",
});

// @ts-ignore
const containerEnvFrom = manifests.find((m) => m.kind === "Deployment").spec
.template.spec.containers[0].envFrom;
expect(containerEnvFrom).toEqual(expectedEnvFrom);
});

test("should return dev manifests with custom envFrom (legacy syntax)", async () => {
process.env.GITHUB_SHA = "123";
process.env.GITHUB_REF = "456";
process.env.GITHUB_JOB = "777";
process.env.GITHUB_RUN_ID = "888";
process.env.GITHUB_REPOSITORY = "socialgouv/test";
process.env.SOCIALGOUV_BASE_DOMAIN = "fabrique.social.gouv.fr";

jest.doMock("@socialgouv/kosko-charts/environments", () => environmentMock);

const expectedEnvFrom = [
{
secretRef: { name: "another-secret" },
Expand Down Expand Up @@ -125,6 +159,35 @@ test("should return proxy manifests with custom env", async () => {

jest.doMock("@socialgouv/kosko-charts/environments", () => environmentMock);

const manifests = await create("proxy", {
config: {
container: {
env: [
{
name: "CUSTOM_VAR",
value: "HELLO",
},
],
},
},
upstream: "http://target:123",
});
//@ts-expect-error
const containerEnv = manifests.find((m) => m.kind === "Deployment").spec
.template.spec.containers[0].env;
expect(containerEnv).toMatchSnapshot();
});

test("should return proxy manifests with custom env (legacy syntax)", async () => {
process.env.GITHUB_SHA = "123";
process.env.GITHUB_REF = "456";
process.env.GITHUB_JOB = "777";
process.env.GITHUB_RUN_ID = "888";
process.env.GITHUB_REPOSITORY = "socialgouv/test";
process.env.SOCIALGOUV_BASE_DOMAIN = "fabrique.social.gouv.fr";

jest.doMock("@socialgouv/kosko-charts/environments", () => environmentMock);

const manifests = await create({
config: {
container: {
Expand Down
32 changes: 24 additions & 8 deletions src/components/oauth2-proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ interface ProxyParams {
config?: Partial<AppConfig>;
}

// renovate: datasource=docker depName=sosedoff/pgweb versioning=v7.2.0
const OAUTH2_PROXY_VERSION = "v7.2.0";
// renovate: datasource=docker depName=quay.io/oauth2-proxy/oauth2-proxy versioning=v7.2.1
const OAUTH2_PROXY_VERSION = "v7.2.1";

// load some YAML from user env
const loadEnvYaml = async (fileName: string) => {
Expand Down Expand Up @@ -51,19 +51,35 @@ expect these files :
- environments/[env]/oauth2-proxy-configmap.yaml
- environments/[env]/oauth2-proxy-sealed-secret.yaml
*/
export const create = async ({ upstream, config = {} }: ProxyParams) => {

export const create = async (
name: ProxyParams | string,
params?: ProxyParams
) => {
let { upstream, config = {} } = params ?? {};

// todo: remove legacy call support (without name)
//@ts-expect-error
if (!upstream && "upstream" in name) {
upstream = name.upstream;
config = name.config ?? {};
name = "proxy";
}

if (typeof name !== "string" || !upstream) {
return [];
}

// expect dedicated configmap
const configMap = (await loadEnvYaml(
"oauth2-proxy-configmap.yml"
)) as ConfigMap;
const configMap = (await loadEnvYaml(`${name}.configmap.yml`)) as ConfigMap;

// expect dedicated secret
const sealedSecret = (await loadEnvYaml(
"oauth2-proxy-sealed-secret.yml"
`${name}.sealed-secret.yml`
)) as SealedSecret;

// oauth2 containers
const manifests = await appCreate("proxy", {
const manifests = await appCreate(name, {
config: {
...config,
container: {
Expand Down

0 comments on commit 177139a

Please sign in to comment.