Skip to content

Commit

Permalink
Merge pull request #108 from openbankingnigeria/kong-fxi
Browse files Browse the repository at this point in the history
fix upstream and plugins.
  • Loading branch information
the-wunmi authored Jan 26, 2024
2 parents 3e34ea1 + f3b7c60 commit a36c5fb
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 91 deletions.
3 changes: 2 additions & 1 deletion apps/server/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/node_modules
**/dist
**/dist
**/certs
128 changes: 90 additions & 38 deletions apps/server/src/apis/apis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ export class APIService {
name: route.name,
enabled: route.enabled,
upstream: new GETAPIUpstreamResponseDTO({
host: gatewayService?.host || null,
protocol: gatewayService?.protocol || null,
port: gatewayService?.port || null,
path: gatewayService?.path || null,
url: gatewayService
? `${gatewayService.protocol}://${gatewayService.host}:${
gatewayService.port || ''
Expand Down Expand Up @@ -153,15 +149,28 @@ export class APIService {
}

let gatewayService = null;
let gatewayRoutes = null;
let gatewayRoute = null;
let plugin = null;
if (route.serviceId) {
gatewayService = await this.kongService.getService(
environment,
route.serviceId,
);
gatewayRoutes = await this.kongService.getServiceRoutes(
}
if (route.routeId) {
gatewayRoute = await this.kongRouteService.getRoute(
environment,
route.serviceId,
route.routeId,
);
}
if (gatewayRoute?.id) {
const plugins = await this.kongRouteService.getPlugins(
environment,
gatewayRoute?.id!,
);

plugin = plugins.data.find(
(plugin) => plugin.name === KONG_PLUGINS.REQUEST_TRANSFORMER,
);
}

Expand All @@ -174,19 +183,28 @@ export class APIService {
name: route.name,
enabled: route.enabled,
upstream: new GETAPIUpstreamResponseDTO({
host: gatewayService?.host || null,
protocol: gatewayService?.protocol || null,
port: gatewayService?.port || null,
path: gatewayService?.path || null,
url: gatewayService
? `${gatewayService.protocol}://${gatewayService.host}:${
gatewayService.port || ''
}${gatewayService.path || ''}`
: null,
method: plugin?.config.http_method || null,
headers: plugin?.config?.add?.headers?.map((header: string) => {
const [key, ...value] = header.split(':')
return { key, value: value.join(':') }
}) || [],
querystring: plugin?.config?.add?.querystring?.map((header: string) => {
const [key, ...value] = header.split(':')
return { key, value: value.join(':') }
}) || [],
body: plugin?.config?.add?.body?.map((header: string) => {
const [key, ...value] = header.split(':')
return { key, value: value.join(':') }
}) || []
}),
downstream: new GETAPIDownstreamResponseDTO({
paths: gatewayRoutes?.data[0]?.paths || [],
methods: gatewayRoutes?.data[0]?.methods || [],
paths: gatewayRoute?.paths || [],
methods: gatewayRoute?.methods || [],
}),
}),
);
Expand Down Expand Up @@ -250,7 +268,7 @@ export class APIService {
) {
const { name, enabled } = data;
const { paths, methods } = data.downstream;
const { url } = data.upstream;
const { url, method, headers, querystring, body } = data.upstream;

const collection = await this.collectionRepository.findOne({
where: { id: data.collectionId },
Expand Down Expand Up @@ -346,6 +364,30 @@ export class APIService {
},
);

if (headers || querystring || body || method) {
await this.kongRouteService.updateOrCreatePlugin(
environment,
gatewayRoute.id,
{
name: KONG_PLUGINS.REQUEST_TRANSFORMER,
enabled: true,
config: {
http_method: method?.toUpperCase(),
remove: {
headers: headers?.map(h => `${h.key}:${h.value}`),
querystring: querystring?.map(h => `${h.key}:${h.value}`),
body: body?.map(h => `${h.key}:${h.value}`),
},
add: {
headers: headers?.map(h => `${h.key}:${h.value}`),
querystring: querystring?.map(h => `${h.key}:${h.value}`),
body: body?.map(h => `${h.key}:${h.value}`),
}
}
},
);
}

// TODO emit event

return ResponseFormatter.success(
Expand All @@ -356,13 +398,7 @@ export class APIService {
enabled: createdRoute.enabled,
upstream: new GETAPIUpstreamResponseDTO({
...data.upstream,
host: gatewayService.host,
protocol: gatewayService.protocol,
port: gatewayService.port,
path: gatewayService.path,
url: `${gatewayService.protocol}://${gatewayService.host}:${
gatewayService.port || ''
}${gatewayService.path || ''}`,
url: `${gatewayService.protocol}://${gatewayService.host}:${gatewayService.port || ''}${gatewayService.path || ''}`,
}),
downstream: new GETAPIDownstreamResponseDTO(data.downstream),
}),
Expand Down Expand Up @@ -526,7 +562,7 @@ export class APIService {
) {
const { name, enabled } = data;
const { paths, methods } = data.downstream;
const { url } = data.upstream;
const { url, method, headers, querystring, body } = data.upstream;

const route = await this.routeRepository.findOne({
where: { id: routeId, environment },
Expand Down Expand Up @@ -618,6 +654,30 @@ export class APIService {
);
}

if (method || headers || querystring || body) {
await this.kongRouteService.updateOrCreatePlugin(
environment,
gatewayRoute.id,
{
name: KONG_PLUGINS.REQUEST_TRANSFORMER,
enabled: true,
config: {
http_method: method?.toUpperCase(),
remove: {
headers: headers?.map(h => `${h.key}:${h.value}`),
querystring: querystring?.map(h => `${h.key}:${h.value}`),
body: body?.map(h => `${h.key}:${h.value}`),
},
add: {
headers: headers?.map(h => `${h.key}:${h.value}`),
querystring: querystring?.map(h => `${h.key}:${h.value}`),
body: body?.map(h => `${h.key}:${h.value}`),
}
}
},
);
}

// TODO emit event

return ResponseFormatter.success(
Expand All @@ -628,13 +688,7 @@ export class APIService {
enabled: route.enabled,
upstream: new GETAPIUpstreamResponseDTO({
...data.upstream,
host: gatewayService.host,
protocol: gatewayService.protocol,
port: gatewayService.port,
path: gatewayService.path,
url: `${gatewayService.protocol}://${gatewayService.host}:${
gatewayService.port || ''
}${gatewayService.path || ''}`,
url: `${gatewayService.protocol}://${gatewayService.host}:${gatewayService.port || ''}${gatewayService.path || ''}`,
}),
downstream: new GETAPIDownstreamResponseDTO(data.downstream),
}),
Expand Down Expand Up @@ -938,15 +992,17 @@ export class APIService {

for (const route of routes) {
let gatewayService = null;
let gatewayRoutes = null;
let gatewayRoute = null;
if (route.serviceId) {
gatewayService = await this.kongService.getService(
environment,
route.serviceId,
);
gatewayRoutes = await this.kongService.getServiceRoutes(
}
if (route.routeId) {
gatewayRoute = await this.kongRouteService.getRoute(
environment,
route.serviceId,
route.routeId,
);
}

Expand All @@ -956,19 +1012,15 @@ export class APIService {
name: route.name,
enabled: route.enabled,
upstream: new GETAPIUpstreamResponseDTO({
host: gatewayService?.host || null,
protocol: gatewayService?.protocol || null,
port: gatewayService?.port || null,
path: gatewayService?.path || null,
url: gatewayService
? `${gatewayService.protocol}://${gatewayService.host}:${
gatewayService.port || ''
}${gatewayService.path || ''}`
: null,
}),
downstream: new GETAPIDownstreamResponseDTO({
paths: gatewayRoutes?.data[0]?.paths || [],
methods: gatewayRoutes?.data[0]?.methods || [],
paths: gatewayRoute?.paths || [],
methods: gatewayRoute?.methods || [],
}),
}),
);
Expand Down
69 changes: 64 additions & 5 deletions apps/server/src/apis/dto/index.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ class CreateAPIUpstreamDTO {
@IsNotEmpty()
@IsUrl()
url: string;

@IsOptional()
@IsString()
@IsEnum(HTTP_METHODS)
method?: string;

@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => KVDTO)
headers?: KVDTO[];

@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => KVDTO)
querystring?: KVDTO[];

@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => KVDTO)
body?: KVDTO[];
}

export class CreateAPIDto {
Expand Down Expand Up @@ -83,10 +106,43 @@ class UpdateAPIDownstreamDTO {
methods: string[];
}

class KVDTO {
@Expose()
@IsString()
key: string;

@Expose()
@IsString()
value: string;
}

class UpdateAPIUpstreamDTO {
@IsNotEmpty()
@IsUrl()
url: string;

@IsOptional()
@IsString()
@IsEnum(HTTP_METHODS)
method?: string;

@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => KVDTO)
headers?: KVDTO[];

@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => KVDTO)
querystring?: KVDTO[];

@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => KVDTO)
body?: KVDTO[];
}

export class UpdateAPIDto {
Expand Down Expand Up @@ -137,19 +193,22 @@ export class GETAPIUpstreamResponseDTO {
}

@Expose()
host: string | null;
url: string | null;

@Expose()
protocol: string | null;
method?: string;

@Expose()
port: number | null;
@Type(() => KVDTO)
headers?: KVDTO[];

@Expose()
path: string | null;
@Type(() => KVDTO)
querystring?: KVDTO[];

@Expose()
url: string | null;
@Type(() => KVDTO)
body?: KVDTO[];
}

export class GetAPIResponseDTO {
Expand Down
2 changes: 0 additions & 2 deletions apps/server/src/settings/settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,6 @@ export class SettingsService {
(consumerPlugin) => consumerPlugin.name === KONG_PLUGINS.IP_RESTRICTION,
);

console.log({ consumerIPRestriction });

return ResponseFormatter.success(
'IP Restriction retrieved successfully',
new IPRestrictionResponse({
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ export class SetupService {
data = cjson.decode(data)
${response?.[0]?.body && (
`
if kong.service.response.get_status() == nil then
return
end
if data then
data = cjson.encode(${jsonToLua(response?.[0]?.body)})
kong.response.set_raw_body(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ export interface Consumer {
} | null;
}

export interface ListRequest {
size?: number;
offset?: number;
tags?: string;
}

export interface ListResponse<T> {
data: T[];
next: string;
}

export interface ListPluginsRequest extends Omit<ListRequest, 'tags'> {}

export interface ListConsumersResponse extends ListResponse<Consumer> {}
export interface ListConsumerKeysResponse extends ListResponse<ConsumerKey> {}
Loading

0 comments on commit a36c5fb

Please sign in to comment.