Skip to content

Commit

Permalink
IS-1488: Added unleash api token file (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
vetlesolgaard authored Aug 29, 2023
1 parent a43940e commit 019e628
Show file tree
Hide file tree
Showing 23 changed files with 214 additions and 201 deletions.
3 changes: 2 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ SYFOVEILEDER_AAD_APP_CLIENT_ID=dev-fss.teamsykefravr.syfoveileder
SYFOVEILEDER_HOST=syfoveileder.dev.intern.nav.no
ESYFOVARSEL_AAD_APP_CLIENT_ID=dev-gcp.team-esyfo.esyfovarsel
ESYFOVARSEL_HOST=http://esyfovarsel.intern.dev.no

UNLEASH_SERVER_API_URL=https://teamsykefravr-unleash-api.nav.cloud.nais.io/api
UNLEASH_SERVER_API_TOKEN=kjempesikkert-token
LOG_LEVEL=debug
28 changes: 28 additions & 0 deletions .github/workflows/unleash-apitoken.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Deploy unleash api-tokens
on:
push:
branches:
- master
paths:
- '.github/workflows/unleash-apitokens.yaml'
- '.nais/unleash/unleash-apitoken-dev.yaml'
- '.nais/unleash/unleash-apitoken-prod.yaml'
jobs:
apply-apitoken-unleash:
name: Apply ApiToken for Unleash to cluster
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Deploy to dev
uses: nais/deploy/actions/deploy@v1
env:
APIKEY: ${{ secrets.NAIS_DEPLOY_APIKEY }}
CLUSTER: dev-gcp
RESOURCE: .nais/unleash/unleash-apitoken-dev.yaml
- name: Deploy to prod
uses: nais/deploy/actions/deploy@v1
env:
APIKEY: ${{ secrets.NAIS_DEPLOY_APIKEY }}
CLUSTER: prod-gcp
RESOURCE: .nais/unleash/unleash-apitoken-prod.yaml
2 changes: 2 additions & 0 deletions .nais/naiserator-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ spec:
external:
- host: "login.microsoftonline.com"
- host: "unleash.nais.io"
- host: "teamsykefravr-unleash-api.nav.cloud.nais.io"
- host: "internarbeidsflatedecorator-q1.dev-fss-pub.nais.io"
- host: "modiacontextholder-q1.dev-fss-pub.nais.io"
- host: "ereg-services-q1.dev-fss-pub.nais.io"
Expand Down Expand Up @@ -78,6 +79,7 @@ spec:
envFrom:
- secret: syfomodiaperson-session-key
- secret: syfomodiaperson-redis-password
- secret: syfomodiaperson-unleash-api-token
ingresses:
- "https://syfomodiaperson.intern.dev.nav.no"
env:
Expand Down
2 changes: 2 additions & 0 deletions .nais/naiserator-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ spec:
external:
- host: "login.microsoftonline.com"
- host: "unleash.nais.io"
- host: "teamsykefravr-unleash-api.nav.cloud.nais.io"
- host: "internarbeidsflatedecorator.prod-fss-pub.nais.io"
- host: "modiacontextholder.prod-fss-pub.nais.io"
- host: "ereg-services.prod-fss-pub.nais.io"
Expand Down Expand Up @@ -78,6 +79,7 @@ spec:
envFrom:
- secret: syfomodiaperson-session-key
- secret: syfomodiaperson-redis-password
- secret: syfomodiaperson-unleash-api-token
ingresses:
- "https://syfomodiaperson.intern.nav.no"
env:
Expand Down
14 changes: 14 additions & 0 deletions .nais/unleash/unleash-apitoken-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: unleash.nais.io/v1
kind: ApiToken
metadata:
name: syfomodiaperson
namespace: teamsykefravr
labels:
team: teamsykefravr
spec:
unleashInstance:
apiVersion: unleash.nais.io/v1
kind: RemoteUnleash
name: teamsykefravr
secretName: syfomodiaperson-unleash-api-token
environment: development
14 changes: 14 additions & 0 deletions .nais/unleash/unleash-apitoken-prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: unleash.nais.io/v1
kind: ApiToken
metadata:
name: syfomodiaperson
namespace: teamsykefravr
labels:
team: teamsykefravr
spec:
unleashInstance:
apiVersion: unleash.nais.io/v1
kind: RemoteUnleash
name: teamsykefravr
secretName: syfomodiaperson-unleash-api-token
environment: production
6 changes: 3 additions & 3 deletions mock/mockEndepunkter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import { mockIspengestopp } from "./ispengestopp/mockIspengestopp";
import { mockIsdialogmote } from "./isdialogmote/mockIsdialogmote";
import { mockIsdialogmotekandidat } from "./isdialogmotekandidat/mockIsdialogmotekandidat";
import { mockSyfoveileder } from "./syfoveileder/mockSyfoveileder";
import { mockUnleash } from "./unleash/mockUnleash";
import { mockUnleashEndpoint } from "./unleashMocks";
import { mockIsdialogmelding } from "./isdialogmelding/mockIsdialogmelding";
import { mockIsaktivitetskrav } from "./isaktivitetskrav/mockIsaktivitetskrav";
import { mockIsbehandlerdialog } from "./isbehandlerdialog/mockIsbehandlerdialog";
import { mockEsyfovarsel } from "./esyfovarsel/mockEsyfovarsel";

const express = require("express");
import express from "express";

const mockEndepunkter = (server: any) => {
server.use(express.json());
Expand Down Expand Up @@ -49,7 +49,7 @@ const mockEndepunkter = (server: any) => {
mockIsdialogmotekandidat,
mockIsdialogmelding,
mockSyfoveileder,
mockUnleash,
mockUnleashEndpoint,
].forEach((func) => {
func(server);
});
Expand Down
14 changes: 0 additions & 14 deletions mock/unleash/mockUnleash.ts

This file was deleted.

8 changes: 0 additions & 8 deletions mock/unleash/unleashMock.ts

This file was deleted.

20 changes: 20 additions & 0 deletions mock/unleashMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { UNLEASH_ROOT } from "@/apiConstants";
import { ToggleNames } from "@/data/unleash/unleash_types";
import express from "express";

export const mockUnleashEndpoint = (server: any) => {
server.get(
`${UNLEASH_ROOT}/toggles`,
(req: express.Request, res: express.Response) => {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(mockUnleashResponse));
}
);
};

export const mockUnleashResponse = Object.values(ToggleNames).reduce(
(accumulator, toggleName) => {
return { ...accumulator, [toggleName]: true };
},
{}
);
20 changes: 8 additions & 12 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import helmet = require("helmet");
import path = require("path");
import prometheus = require("prom-client");

import * as Config from "./server/config";
import { getOpenIdClient, getOpenIdIssuer } from "./server/authUtils";
import { setupProxy } from "./server/proxy";
import { setupSession } from "./server/session";

import unleashRoutes = require("./server/routes/unleashRoutes");
import Config = require("./server/config");
import unleash = require("./server/unleash");

// Prometheus metrics
const collectDefaultMetrics = prometheus.collectDefaultMetrics;
Expand Down Expand Up @@ -65,18 +64,15 @@ const setupServer = async () => {

server.use("/static", express.static(DIST_DIR));

server.post(
server.get(
"/unleash/toggles",
redirectIfUnauthorized,
(req: express.Request, res: express.Response) => {
const toggles = req.body.toggles;
const unleashToggles = unleashRoutes.unleashToggles(
toggles,
req.query.valgtEnhet,
req.query.userId
(req: express.Request, res: express.Response<unleash.Toggles>) => {
const togglesResponse = unleash.getToggles(
req.query.veilederId,
req.query.enhetId
);

res.status(200).send(unleashToggles);
res.status(200).send(togglesResponse);
}
);

Expand Down
7 changes: 7 additions & 0 deletions server/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dotenv = require("dotenv");

dotenv.config();

const hasEnvVar = (name: any) => {
Expand Down Expand Up @@ -313,10 +314,16 @@ export const redis = {
}),
};

export const unleash: { serverApiUrl: string; serverApiToken: string } = {
serverApiUrl: envVar({ name: "UNLEASH_SERVER_API_URL" }),
serverApiToken: envVar({ name: "UNLEASH_SERVER_API_TOKEN" }),
};

module.exports = {
auth: auth,
isDev: isDev,
isProd: isProd,
redis: redis,
unleash: unleash,
server: server,
};
102 changes: 0 additions & 102 deletions server/routes/unleashRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,102 +0,0 @@
import unleashClient = require("unleash-client");

const { initialize, Strategy } = unleashClient;

class ByDevEnhet extends Strategy {
constructor() {
super("byDevEnhet");
}

isEnabled(parameters: any, context: any) {
if (!context.valgtEnhet) {
return false;
}

const valgtEnhetMatches =
parameters.enheter.indexOf(context.valgtEnhet) !== -1;

return valgtEnhetMatches && process.env.NAIS_CONTEXT === "dev";
}
}

class ByProdEnhet extends Strategy {
constructor() {
super("byProdEnhet");
}

isEnabled(parameters: any, context: any) {
if (!context.valgtEnhet) {
return false;
}

const valgtEnhetMatches =
parameters.enheter.indexOf(context.valgtEnhet) !== -1;

return valgtEnhetMatches && process.env.NAIS_CONTEXT === "prod";
}
}

class ByUserId extends Strategy {
constructor() {
super("byUserId");
}

isEnabled(parameters: any, context: any) {
if (!context.user) {
return false;
}

return parameters.user.indexOf(context.user) !== -1;
}
}

class ByEnvironmentToggle extends Strategy {
constructor() {
super("byEnvironmentToggle");
}

isEnabled(parameters: any) {
return (
(parameters.dev === "true" && process.env.NAIS_CONTEXT === "dev") ||
(parameters.prod === "true" && process.env.NAIS_CONTEXT === "prod")
);
}
}

const unleash = initialize({
url: "https://unleash.nais.io/api/",
appName: "syfomodiaperson",
environment: process.env.NAIS_CONTEXT,
strategies: [
new ByDevEnhet(),
new ByUserId(),
new ByProdEnhet(),
new ByEnvironmentToggle(),
],
});

export const unleashToggles = (toggles: any, valgtEnhet: any, userId: any) => {
return {
"syfo.dialogmote.virksomhetinput": unleash.isEnabled(
"syfo.dialogmote.virksomhetinput",
{
valgtEnhet: valgtEnhet,
user: userId,
}
),
"syfo.motebehov.tilbakemelding": unleash.isEnabled(
"syfo.motebehov.tilbakemelding",
{
valgtEnhet: valgtEnhet,
user: userId,
}
),
"syfo.behandlerdialog.meldingfranav": unleash.isEnabled(
"syfo.behandlerdialog.meldingfranav",
{
valgtEnhet: valgtEnhet,
user: userId,
}
),
};
};
Loading

0 comments on commit 019e628

Please sign in to comment.