Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test-multi-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: ${{ github.event_name == 'pull_request' && fromJSON('["22"]') || fromJSON('["16", "18", "20", "22", "24"]') }}
node-version: ${{ github.event_name == 'pull_request' && fromJSON('["22"]') || fromJSON('["18", "20", "22", "24"]') }}
rabbitmq-version:
- "3.9-management"
- "3.10-management"
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"url": "https://github.com/runmq/queue/issues"
},
"homepage": "https://github.com/runmq/queue#readme",
"engines": {
"node": ">=18"
},
"devDependencies": {
"@eslint/js": "^9.29.0",
"@faker-js/faker": "^9.9.0",
Expand All @@ -55,10 +58,10 @@
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^11.0.1",
"@semantic-release/release-notes-generator": "^11.0.7",
"@types/jest": "^29.5.12",
"@types/jest": "^30.0.0",
"@types/node": "^22.0.0",
"eslint": "^9.29.0",
"jest": "^29.7.0",
"jest": "^30.0.3",
"patch-package": "^8.0.1",
"semantic-release": "^24.2.0",
"ts-jest": "^29.4.0",
Expand All @@ -71,9 +74,6 @@
"ajv": "8.18.0",
"rabbitmq-client": "^5.0.0"
},
"engines": {
"node": ">=16"
},
"release": {
"branches": [
"main"
Expand Down
154 changes: 73 additions & 81 deletions src/core/management/RabbitMQManagementClient.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import * as http from "node:http";
import * as https from "node:https";
import {RunMQLogger} from "@src/core/logging/RunMQLogger";
import {RabbitMQManagementConfig} from "@src";
import {RabbitMQOperatorPolicy} from "@src/types";

interface ManagementResponse {
status: number;
ok: boolean;
body: string;
}

const REQUEST_TIMEOUT_MS = 10_000;

export class RabbitMQManagementClient {
constructor(
private config: RabbitMQManagementConfig,
Expand All @@ -23,69 +13,27 @@ export class RabbitMQManagementClient {
return `Basic ${credentials}`;
}

private request(
urlString: string,
method: string,
body?: unknown
): Promise<ManagementResponse> {
const url = new URL(urlString);
const lib = url.protocol === 'https:' ? https : http;
const payload = body !== undefined ? JSON.stringify(body) : undefined;

const headers: Record<string, string> = {
'Authorization': this.getAuthHeader()
};
if (payload !== undefined) {
headers['Content-Type'] = 'application/json';
headers['Content-Length'] = Buffer.byteLength(payload).toString();
}

return new Promise((resolve, reject) => {
const req = lib.request(
{
protocol: url.protocol,
hostname: url.hostname,
port: url.port || (url.protocol === 'https:' ? 443 : 80),
path: `${url.pathname}${url.search}`,
method,
headers,
timeout: REQUEST_TIMEOUT_MS
},
(res) => {
const chunks: Buffer[] = [];
res.on('data', (chunk: Buffer) => chunks.push(chunk));
res.on('end', () => {
const status = res.statusCode ?? 0;
resolve({
status,
ok: status >= 200 && status < 300,
body: Buffer.concat(chunks).toString('utf8')
});
});
res.on('error', reject);
}
);
req.on('timeout', () => {
req.destroy(new Error(`Request timed out after ${REQUEST_TIMEOUT_MS}ms`));
});
req.on('error', reject);
if (payload !== undefined) req.write(payload);
req.end();
});
}

public async createOrUpdateOperatorPolicy(vhost: string, policy: RabbitMQOperatorPolicy): Promise<boolean> {
try {
const url = `${this.config.url}/api/operator-policies/${vhost}/${encodeURIComponent(policy.name)}`;
const response = await this.request(url, 'PUT', {
pattern: policy.pattern,
definition: policy.definition,
priority: policy.priority || 0,
"apply-to": policy["apply-to"]

const response = await fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': this.getAuthHeader()
},
body: JSON.stringify({
pattern: policy.pattern,
definition: policy.definition,
priority: policy.priority || 0,
"apply-to": policy["apply-to"]
})
});

if (!response.ok) {
this.logger.error(`Failed to create operator policy: ${response.status} - ${response.body}`);
const error = await response.text();
this.logger.error(`Failed to create operator policy: ${response.status} - ${error}`);
return false;
}

Expand All @@ -100,17 +48,24 @@ export class RabbitMQManagementClient {
public async getOperatorPolicy(vhost: string, policyName: string): Promise<RabbitMQOperatorPolicy | null> {
try {
const url = `${this.config.url}/api/operator-policies/${vhost}/${encodeURIComponent(policyName)}`;
const response = await this.request(url, 'GET');

const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': this.getAuthHeader()
}
});

if (!response.ok) {
if (response.status === 404) {
return null;
}
this.logger.error(`Failed to get operator policy: ${response.status} - ${response.body}`);
const error = await response.text();
this.logger.error(`Failed to get operator policy: ${response.status} - ${error}`);
return null;
}

return JSON.parse(response.body);
return await response.json();
} catch (error) {
this.logger.error(`Error getting operator policy: ${error}`);
return null;
Expand All @@ -120,10 +75,17 @@ export class RabbitMQManagementClient {
public async deleteOperatorPolicy(vhost: string, policyName: string): Promise<boolean> {
try {
const url = `${this.config.url}/api/operator-policies/${vhost}/${encodeURIComponent(policyName)}`;
const response = await this.request(url, 'DELETE');

const response = await fetch(url, {
method: 'DELETE',
headers: {
'Authorization': this.getAuthHeader()
}
});

if (!response.ok && response.status !== 404) {
this.logger.error(`Failed to delete operator policy: ${response.status} - ${response.body}`);
const error = await response.text();
this.logger.error(`Failed to delete operator policy: ${response.status} - ${error}`);
return false;
}

Expand All @@ -138,7 +100,14 @@ export class RabbitMQManagementClient {
public async checkManagementPluginEnabled(): Promise<boolean> {
try {
const url = `${this.config.url}/api/overview`;
const response = await this.request(url, 'GET');

const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': this.getAuthHeader()
}
});

return response.ok;
} catch (error) {
this.logger.warn(`Management plugin not accessible: ${error}`);
Expand All @@ -159,10 +128,19 @@ export class RabbitMQManagementClient {
): Promise<boolean> {
try {
const url = `${this.config.url}/api/global-parameters/${encodeURIComponent(name)}`;
const response = await this.request(url, 'PUT', {value});

const response = await fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': this.getAuthHeader()
},
body: JSON.stringify({value})
});

if (!response.ok) {
this.logger.error(`Failed to set parameter ${name}: ${response.status} - ${response.body}`);
const error = await response.text();
this.logger.error(`Failed to set parameter ${name}: ${response.status} - ${error}`);
return false;
}

Expand All @@ -184,17 +162,24 @@ export class RabbitMQManagementClient {
): Promise<T | null> {
try {
const url = `${this.config.url}/api/global-parameters/${encodeURIComponent(name)}`;
const response = await this.request(url, 'GET');

const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': this.getAuthHeader()
}
});

if (!response.ok) {
if (response.status === 404) {
return null;
}
this.logger.error(`Failed to get parameter ${name}: ${response.status} - ${response.body}`);
const error = await response.text();
this.logger.error(`Failed to get parameter ${name}: ${response.status} - ${error}`);
return null;
}

const data = JSON.parse(response.body);
const data = await response.json();
return data.value as T;
} catch (error) {
this.logger.error(`Error getting parameter: ${error}`);
Expand All @@ -212,10 +197,17 @@ export class RabbitMQManagementClient {
): Promise<boolean> {
try {
const url = `${this.config.url}/api/global-parameters/${encodeURIComponent(name)}`;
const response = await this.request(url, 'DELETE');

const response = await fetch(url, {
method: 'DELETE',
headers: {
'Authorization': this.getAuthHeader()
}
});

if (!response.ok && response.status !== 404) {
this.logger.error(`Failed to delete parameter ${name}: ${response.status} - ${response.body}`);
const error = await response.text();
this.logger.error(`Failed to delete parameter ${name}: ${response.status} - ${error}`);
return false;
}

Expand All @@ -226,4 +218,4 @@ export class RabbitMQManagementClient {
return false;
}
}
}
}
Loading
Loading