Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"test": "jest"
},
"dependencies": {
"chalk": "4.1.2"
"chalk": "4.1.2",
"undici": "^5.4.0"
},
"publishConfig": {
"access": "public"
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createFetch } from './fetch';
export type ContextConfig = {
personalAccessToken?: string;
environment?: string;
tlsRejectUnauthorized?: boolean;
};

export type ServiceAccessToken = {
Expand All @@ -25,6 +26,7 @@ export type Subscriptions = {
export class Context {
private personalAccessToken?: string;
private environment: string;
private _tlsRejectUnauthorized: boolean;

constructor(config?: ContextConfig) {
if (!config?.personalAccessToken && !process.env.OSC_ACCESS_TOKEN) {
Expand All @@ -37,6 +39,8 @@ export class Context {
? config.personalAccessToken
: process.env.OSC_ACCESS_TOKEN;
this.environment = config?.environment ? config.environment : 'prod';
this._tlsRejectUnauthorized =
config?.tlsRejectUnauthorized !== false ? true : false;
}

getPersonalAccessToken() {
Expand All @@ -47,6 +51,10 @@ export class Context {
return this.environment;
}

get tlsRejectUnauthorized() {
return this._tlsRejectUnauthorized;
}

async getServiceAccessToken(serviceId: string): Promise<string> {
const serviceUrl = new URL(
`https://catalog.svc.${this.environment}.osaas.io/mysubscriptions`
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ jest.mock('./fetch', () => ({
createFetch: jest.fn()
}));

describe('Context tlsRejectUnauthorized', () => {
test('defaults to true when not specified', () => {
const ctx = new Context({ personalAccessToken: 'dummy' });
expect(ctx.tlsRejectUnauthorized).toBe(true);
});

test('stores false when explicitly set to false', () => {
const ctx = new Context({
personalAccessToken: 'dummy',
tlsRejectUnauthorized: false
});
expect(ctx.tlsRejectUnauthorized).toBe(false);
});

test('stores true when explicitly set to true', () => {
const ctx = new Context({
personalAccessToken: 'dummy',
tlsRejectUnauthorized: true
});
expect(ctx.tlsRejectUnauthorized).toBe(true);
});
});

describe('Core functionalities', () => {
afterAll(() => {
jest.clearAllMocks();
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,17 @@ export async function getInstanceHealth(
'health'
);

const { status } = await createFetch<{ status: string }>(healthUrl, {
method: 'GET',
headers: {
'x-jwt': `Bearer ${token}`
}
});
const { status } = await createFetch<{ status: string }>(
healthUrl,
{
method: 'GET',
headers: {
'x-jwt': `Bearer ${token}`
}
},
undefined,
context.tlsRejectUnauthorized
);
return status;
}

Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,24 @@ const defaultErrorFactory: ErrorFactory = async (response) => {
export const createFetch = async <T>(
url: string | URL,
options?: RequestInit,
errorFactory: ErrorFactory = defaultErrorFactory
errorFactory: ErrorFactory = defaultErrorFactory,
tlsRejectUnauthorized = true
): Promise<T> => {
try {
Log().debug(`${options?.method}: ${url}`);
const response = await fetch(url, { ...options });
const fetchOptions: RequestInit = { ...options };
if (!tlsRejectUnauthorized) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { Agent } = require('undici') as {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Agent: new (opts: { connect: { rejectUnauthorized: boolean } }) => any;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(fetchOptions as any).dispatcher = new Agent({
connect: { rejectUnauthorized: false }
});
}
const response = await fetch(url, fetchOptions);

Log().debug(
response.status + ': ' + response.statusText + ': ' + response.ok
Expand Down
Loading