@brickert/kerio-connect-api 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -72,8 +72,9 @@ export interface GenericResponse {
72
72
  }
73
73
  }
74
74
  result?: {
75
+ viewport?: Array;
75
76
  userDetails?: object;
76
- list?: Array<object> | Array<IPAddressGroupItem>;
77
+ list?: Array;
77
78
  totalItems?: number;
78
79
  errors?: Array<{
79
80
  code: number,
@@ -81,6 +82,7 @@ export interface GenericResponse {
81
82
  }>;
82
83
  result?: Array;
83
84
  token?: string;
85
+ detail?: string;
84
86
  }
85
87
  };
86
88
  }
@@ -104,6 +106,8 @@ export class Kerio extends WebClient {
104
106
 
105
107
  IPAddressGroups: IPAddressGroup;
106
108
  Session: Session;
109
+ Logs: Logs;
110
+ Domains: Domains;
107
111
 
108
112
  login(user: KerioUser): void;
109
113
 
@@ -243,4 +247,105 @@ export interface KerioInstanceUserResponse {
243
247
  roles: {
244
248
  userRole: string;
245
249
  }
250
+ }
251
+
252
+ export class Logs extends KerioModules {
253
+ /**
254
+ * Get logs from the specified log and number of most recent lines. Defaults to the last 5 security logs.
255
+ * @param logName
256
+ * @param line_count
257
+ * @returns An array of log lines structured with timestamp (DD/MMM/YYYY HH:MM:SS) in Kerio Connect's server timezone and the message
258
+ */
259
+ list(logName: logName, line_count: number): Promise<Array<LogLineObject>>;
260
+ }
261
+
262
+ export type logName = "config" | "debug" | "error" | "mail" | "security" | "spam" | "warning";
263
+
264
+ export interface LogLineObject {
265
+ timestamp: string;
266
+ message: string;
267
+ }
268
+
269
+ export class Domains extends KerioModules {
270
+ /**
271
+ * Get a Mapped Array of KerioDomainItems indexed by their ID
272
+ * @returns {Promise<Map<import('../index.d.ts').KerioDomainID, import('../index.d.ts').KerioDomainItem>>}
273
+ */
274
+ list(): Promise<Map<KerioDomainID, KerioDomainItem>>;
275
+ /**
276
+ * Get the DKIM DNS TXT record object for the domain name. TXT record name & value pair, selector name, and raw public key string.
277
+ * @param {string} domainName Name of the domain
278
+ * @returns {Promise<import('../index.d.ts').DKIMRecord>}
279
+ */
280
+ getDKIMDNSRecord(domainName: string): Promise<DKIMRecord>;
281
+ }
282
+
283
+ /**
284
+ * @example "keriodb://domain/1234-5678-901-2345"
285
+ */
286
+ export type KerioDomainID = string;
287
+
288
+ export interface KerioDomainItem {
289
+ id: KerioDomainID;
290
+ name: string;
291
+ description: string;
292
+ primary: boolean;
293
+ userMaxCount: number;
294
+ passwords: {
295
+ expiration: {
296
+ enabled: boolean;
297
+ days: number;
298
+ };
299
+ historyCount: number;
300
+ enforceComplexity: boolean;
301
+ minLength: number;
302
+ };
303
+ messages: {
304
+ outgoingLimit: {
305
+ enabled: boolean,
306
+ size: number;
307
+ units: string;
308
+ };
309
+ autoClean: {
310
+ deleted: {
311
+ enabled: boolean;
312
+ days: number;
313
+ };
314
+ junk: {
315
+ enabled: boolean;
316
+ days: number;
317
+ };
318
+ sent: {
319
+ enabled: boolean;
320
+ days: number;
321
+ };
322
+ other: {
323
+ enabled: boolean;
324
+ days: number;
325
+ };
326
+ };
327
+ deleteRecovery: {
328
+ enabled: boolean;
329
+ days: number;
330
+ }
331
+ };
332
+ DKIMenabled: boolean;
333
+ }
334
+
335
+ export interface DKIMRecord {
336
+ /**
337
+ * @example 'mail._domainkey.example.org'
338
+ */
339
+ name: string;
340
+ selector: string;
341
+ type: "TXT";
342
+ /**
343
+ * @example 'v=DKIM1; p=1234'
344
+ */
345
+ value: string;
346
+ /**
347
+ * The 'p=' section of the TXT record value
348
+ * @example 'abcdefg1234567'
349
+ */
350
+ raw_public_key: string;
246
351
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brickert/kerio-connect-api",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "An unofficial API wrapper for Kerio Connect Mail Server jsonrpc Admin API.",
5
5
  "keywords": [
6
6
  "kerio",
package/src/domains.js ADDED
@@ -0,0 +1,207 @@
1
+ import { KerioModules } from './modules.js';
2
+
3
+ export class Domains extends KerioModules {
4
+
5
+ /**
6
+ * Get a Mapped Array of KerioDomainItems indexed by their ID
7
+ * @returns {Promise<Map<import('../index.d.ts').KerioDomainID, import('../index.d.ts').KerioDomainItem>>}
8
+ */
9
+ async list() {
10
+ try {
11
+ if (!this.instance.logged_in) {
12
+
13
+ this.reset();
14
+
15
+ throw {
16
+ name: "KerioClientSessionError",
17
+ message: `Kerio session invalid. Try logging in again.`,
18
+ type: 'Kerio',
19
+ from: "Kerio.Domains.list"
20
+ }
21
+ }
22
+
23
+ let domain_list_response = await this.sessionedRequest({
24
+ http_method: 'POST',
25
+ api_method: 'Domains.get',
26
+ auth: {
27
+ cookie: this.instance.session_cookie,
28
+ token: this.instance.x_token
29
+ },
30
+ body_params: {
31
+ query: {
32
+ orderBy: [
33
+ {
34
+ columnName: "name",
35
+ direction: "Asc"
36
+ }
37
+ ]
38
+ }
39
+ }
40
+ });
41
+
42
+ let response_body = domain_list_response._body;
43
+
44
+ if (!response_body.result?.errors && !response_body?.error) {
45
+
46
+ var domainsListResult = new Map();
47
+
48
+ if (response_body.result.list.length > 0) {
49
+
50
+ response_body.result.list.forEach(domain => {
51
+ /**
52
+ * @type {import('../index.d.ts').KerioDomainItem}
53
+ */
54
+ var obj = {
55
+ id: domain.id,
56
+ name: domain.name,
57
+ description: domain.description,
58
+ primary: domain.isPrimary,
59
+ userMaxCount: domain.userMaxCount,
60
+ passwords: {
61
+ expiration: {
62
+ enabled: domain.passwordExpirationEnabled,
63
+ days: domain.passwordExpirationEnabled
64
+ },
65
+ historyCount: domain.passwordHistoryCount,
66
+ enforceComplexity: domain.passwordComplexityEnabled,
67
+ minLength: domain.passwordMinimumLength
68
+ },
69
+ messages: {
70
+ outgoingLimit: {
71
+ enabled: domain.outgoingMessageLimit.isActive,
72
+ size: domain.outgoingMessageLimit.limit.value,
73
+ units: domain.outgoingMessageLimit.limit.units
74
+ },
75
+ autoClean: {
76
+ deleted: {
77
+ enabled: domain.deletedItems.isEnabled,
78
+ days: domain.deletedItems.days
79
+ },
80
+ junk: {
81
+ enabled: domain.junkEmail.isEnabled,
82
+ days: domain.junkEmail.days
83
+ },
84
+ sent: {
85
+ enabled: domain.sentItems.isEnabled,
86
+ days: domain.sentItems.days
87
+ },
88
+ other: {
89
+ enabled: domain.autoDelete.isEnabled,
90
+ days: domain.autoDelete.days
91
+ }
92
+ },
93
+ deleteRecovery: {
94
+ enabled: domain.keepForRecovery.isEnabled,
95
+ days: domain.keepForRecovery.days
96
+ }
97
+ },
98
+ DKIMenabled: domain.isDkimEnabled
99
+ }
100
+
101
+ domainsListResult.set(domain.id, obj);
102
+ });
103
+ }
104
+
105
+ this.instance.logger.debug({
106
+ name: "KerioDomainsList",
107
+ message: `listed ${domainsListResult.size} domains entities`,
108
+ type: 'Kerio',
109
+ from: "Kerio.Domains.list"
110
+ });
111
+
112
+ return domainsListResult;
113
+
114
+ } else {
115
+ throw {
116
+ name: "KerioRequestError",
117
+ message: `Error occured while fetching results from API method 'Domains.get' for all domains`,
118
+ type: 'Kerio',
119
+ from: "Kerio.Domains.list"
120
+ }
121
+ }
122
+
123
+ } catch (e) {
124
+ this.instance.logger.error(e);
125
+ throw e;
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Get the DKIM DNS TXT record object for the domain name. TXT record name & value pair, selector name, and raw public key string.
131
+ * @param {string} domainName Name of the domain
132
+ * @returns {Promise<import('../index.d.ts').DKIMRecord>}
133
+ */
134
+ async getDKIMDNSRecord(domainName = null) {
135
+ try {
136
+
137
+ if (!this.instance.logged_in) {
138
+
139
+ this.reset();
140
+
141
+ throw {
142
+ name: "KerioClientSessionError",
143
+ message: `Kerio session invalid. Try logging in again.`,
144
+ type: 'Kerio',
145
+ from: "Kerio.Domains.getDKIMDNSRecord"
146
+ }
147
+ }
148
+
149
+ if (!domainName) {
150
+ throw {
151
+ name: "KerioDKIMDomainNameError",
152
+ message: `Invalid Domain Name while processing API method 'Domains.getDkimDnsRecord' for domain '${domainName}'`,
153
+ type: 'Kerio',
154
+ from: "Kerio.Domains.getDKIMDNSRecord"
155
+ }
156
+ }
157
+
158
+ let dkim_record_response = await this.sessionedRequest({
159
+ http_method: 'POST',
160
+ api_method: 'Domains.getDkimDnsRecord',
161
+ auth: {
162
+ cookie: this.instance.session_cookie,
163
+ token: this.instance.x_token
164
+ },
165
+ body_params: {
166
+ domain: domainName
167
+ }
168
+ });
169
+
170
+ let response_body = dkim_record_response._body;
171
+
172
+ if (!response_body.result?.errors && !response_body?.error) {
173
+
174
+ let _record = response_body.result.detail;
175
+ let name = _record.split("\n")[0].split(":")[1].trim();
176
+ let value = _record.split("\n")[1].split("value:")[1].trim().split(";").join("; ");
177
+ let raw_key = value.split("; p=")[1];
178
+
179
+ this.instance.logger.debug({
180
+ name: "KerioDKIMDomainRecord",
181
+ message: `A DKIM record was obtained. Excerpt of: ${raw_key.substring(0, 10)}`,
182
+ type: "Kerio",
183
+ from: "Kerio.Domains.getDKIMDNSRecord"
184
+ });
185
+
186
+ return {
187
+ name,
188
+ selector: name.split(".")[0],
189
+ type: "TXT",
190
+ value,
191
+ raw_public_key: raw_key
192
+ }
193
+
194
+ } else {
195
+ throw {
196
+ name: "KerioRequestError",
197
+ message: `Error occured while fetching results from API method 'Domains.getDkimDnsRecord' for domain '${domainName}'`,
198
+ type: 'Kerio',
199
+ from: "Kerio.Domains.getDKIMDNSRecord"
200
+ }
201
+ }
202
+ } catch (e) {
203
+ this.instance.logger.error(e);
204
+ throw e;
205
+ }
206
+ }
207
+ }
@@ -358,6 +358,13 @@ export class IPAddressGroup extends KerioModules {
358
358
  }
359
359
  }
360
360
 
361
+ /**
362
+ * Remove IPAddressGroupItem(s) of type 'host' by the description name from this group name. TAKE CARE as this will remove all items upto the specified max limit that share the same name. You may limit number of items to remove. They are sorted by their unique ID.
363
+ * @param {string} description Name of the group item(s)
364
+ * @param {string} group_name Name of the IPAddressGroup string to remove from
365
+ * @param {number} remove_limit Max number of group items to remove upto. Specifying -1 will remove ALL by the description name
366
+ * @returns Returns an array of host IP addresses that were removed
367
+ */
361
368
  async removeHostFromIPAddressGroupByDesc(description = null, group_name = null, remove_limit = 1) {
362
369
  try {
363
370
 
package/src/kerio.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { IPAddressGroup } from "./ipaddressgroups.js";
2
+ import { Logs } from "./logs.js";
2
3
  import { Session } from "./session.js";
4
+ import { Domains } from "./domains.js";
3
5
  import { WebClient } from "./client.js";
4
6
  import { CronJob } from 'cron';
5
7
  import pino from 'pino';
@@ -20,8 +22,9 @@ export class Kerio extends WebClient {
20
22
  this.userAgent = userAgent;
21
23
 
22
24
  this.IPAddressGroups = new IPAddressGroup(this);
23
-
25
+ this.Logs = new Logs(this);
24
26
  this.Session = new Session(this);
27
+ this.Domains = new Domains(this);
25
28
 
26
29
  this.logger = pino({
27
30
  level: "debug",
package/src/logs.js ADDED
@@ -0,0 +1,107 @@
1
+ import { KerioModules } from './modules.js';
2
+
3
+ /**
4
+ * @type {import('../index.d.ts').Logs}
5
+ */
6
+ export class Logs extends KerioModules {
7
+
8
+ /**
9
+ * Get logs from the specified log and number of most recent lines. Defaults to the last 5 security logs.
10
+ * @param {import('../index.d.ts').logName} logName
11
+ * @param {number} line_count
12
+ * @returns {Promise<Array<import('../index.d.ts').LogLineObject>>} An array of log lines structured with timestamp (DD/MMM/YYYY HH:MM:SS) in Kerio Connect's server timezone and the message
13
+ */
14
+ async list(logName = "security", line_count = 5) {
15
+ try {
16
+
17
+ if (!this.instance.logged_in) {
18
+
19
+ this.reset();
20
+
21
+ throw {
22
+ name: "KerioClientSessionError",
23
+ message: `Kerio session invalid. Try logging in again.`,
24
+ type: 'Kerio',
25
+ from: "Kerio.Logs.list"
26
+ }
27
+ }
28
+
29
+ if (!Number.isInteger(line_count) && line_count < 0) {
30
+ throw {
31
+ name: "KerioLogListCountError",
32
+ message: `Invalid log line count while processing API method 'Logs.get' for log '${logName}'`,
33
+ type: 'Kerio',
34
+ from: "Kerio.Logs.list"
35
+ }
36
+ }
37
+
38
+ if (!["config", "debug", "error", "mail", "security", "spam", "warning"].includes(logName)) {
39
+ throw {
40
+ name: "KerioLogNameError",
41
+ message: `Invalid log name while processing API method 'Logs.get' for log '${logName}'`,
42
+ type: 'Kerio',
43
+ from: "Kerio.Logs.list"
44
+ }
45
+ }
46
+
47
+ let log_list_response = await this.sessionedRequest({
48
+ http_method: 'POST',
49
+ api_method: 'Logs.get',
50
+ auth: {
51
+ cookie: this.instance.session_cookie,
52
+ token: this.instance.x_token
53
+ },
54
+ body_params: {
55
+ logName: logName,
56
+ fromLine : -1,
57
+ countLines: line_count
58
+ }
59
+ });
60
+
61
+ let response_body = log_list_response._body;
62
+
63
+ if (!response_body.result?.errors && !response_body?.error) {
64
+
65
+ var logLinesResult = new Array();
66
+
67
+ if (response_body.result.viewport.length > 0) {
68
+
69
+ response_body.result.viewport.forEach(line => {
70
+
71
+ let timestamp_string = line.content.split("]")[0];
72
+ timestamp_string = timestamp_string.substring(1, timestamp_string.length).trim();
73
+
74
+ let message = line.content.split("]")[1].trim();
75
+
76
+ logLinesResult.push({
77
+ timestamp: timestamp_string,
78
+ message
79
+ });
80
+
81
+ });
82
+ }
83
+
84
+ this.instance.logger.debug({
85
+ name: "KerioLogsList",
86
+ message: `listed ${logLinesResult.length} log lines from Log '${logName}'`,
87
+ type: 'Kerio',
88
+ from: "Kerio.Logs.list"
89
+ });
90
+
91
+ return logLinesResult;
92
+
93
+ } else {
94
+ throw {
95
+ name: "KerioRequestError",
96
+ message: `Error occured while fetching results from API method 'Logs.get' for log '${logName}'`,
97
+ type: 'Kerio',
98
+ from: "Kerio.Logs.list"
99
+ }
100
+ }
101
+
102
+ } catch (e) {
103
+ this.instance.logger.error(e);
104
+ throw e;
105
+ }
106
+ }
107
+ }