greybel-mock-environment 1.2.5 → 1.2.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.
@@ -145,7 +145,7 @@ var MockEnvironment = /** @class */ (function () {
145
145
  var currentPath = computer.getHome(user);
146
146
  var programPath = new types_1.File({
147
147
  name: 'myprogramm',
148
- permissions: '---------',
148
+ permissions: '----------',
149
149
  owner: 'root',
150
150
  isProtected: true,
151
151
  type: types_1.FileType.Bin
@@ -1,4 +1,4 @@
1
- import { PermissionMap, PermissionSegment } from '../utils';
1
+ import { Permissions, PermissionSegment } from './permission';
2
2
  import { Library } from './library';
3
3
  import { User } from './user';
4
4
  import { Version } from './version';
@@ -36,7 +36,7 @@ export interface EntityOptions {
36
36
  export declare class Entity {
37
37
  parent?: Entity;
38
38
  name: string;
39
- permissions: string;
39
+ permissions: Permissions;
40
40
  owner: string;
41
41
  group: string;
42
42
  isFolder?: boolean;
@@ -47,7 +47,6 @@ export declare class Entity {
47
47
  getPath(): string[] | null;
48
48
  getEntity(_path: string[]): Entity | null;
49
49
  copy(): Entity;
50
- parsePermissions(): PermissionMap;
51
50
  getPermissions(user: User, groups: Map<string, Set<string>>): PermissionSegment;
52
51
  }
53
52
  export interface FileOptions extends Omit<EntityOptions, 'isFolder'> {
@@ -25,6 +25,17 @@ var __assign = (this && this.__assign) || function () {
25
25
  };
26
26
  return __assign.apply(this, arguments);
27
27
  };
28
+ var __values = (this && this.__values) || function(o) {
29
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
30
+ if (m) return m.call(o);
31
+ if (o && typeof o.length === "number") return {
32
+ next: function () {
33
+ if (o && i >= o.length) o = void 0;
34
+ return { value: o && o[i++], done: !o };
35
+ }
36
+ };
37
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
38
+ };
28
39
  var __read = (this && this.__read) || function (o, n) {
29
40
  var m = typeof Symbol === "function" && o[Symbol.iterator];
30
41
  if (!m) return o;
@@ -41,19 +52,9 @@ var __read = (this && this.__read) || function (o, n) {
41
52
  }
42
53
  return ar;
43
54
  };
44
- var __values = (this && this.__values) || function(o) {
45
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
46
- if (m) return m.call(o);
47
- if (o && typeof o.length === "number") return {
48
- next: function () {
49
- if (o && i >= o.length) o = void 0;
50
- return { value: o && o[i++], done: !o };
51
- }
52
- };
53
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
54
- };
55
55
  Object.defineProperty(exports, "__esModule", { value: true });
56
56
  exports.Folder = exports.File = exports.Entity = exports.FileType = exports.FILE_CONTENT_LIMIT = void 0;
57
+ var permission_1 = require("./permission");
57
58
  var library_1 = require("./library");
58
59
  var version_1 = require("./version");
59
60
  exports.FILE_CONTENT_LIMIT = 160000;
@@ -83,7 +84,7 @@ var Entity = /** @class */ (function () {
83
84
  function Entity(options, parent) {
84
85
  this.parent = parent;
85
86
  this.name = options.name;
86
- this.permissions = options.permissions;
87
+ this.permissions = new permission_1.Permissions({ permissions: options.permissions });
87
88
  this.owner = options.owner;
88
89
  this.group = options.group || options.owner;
89
90
  this.isFolder = options.isFolder;
@@ -108,53 +109,26 @@ var Entity = /** @class */ (function () {
108
109
  Entity.prototype.copy = function () {
109
110
  var newEntity = new Entity({
110
111
  name: this.name,
111
- permissions: this.permissions,
112
+ permissions: this.permissions.toString(),
112
113
  owner: this.owner,
113
114
  isFolder: this.isFolder
114
115
  });
115
116
  return newEntity;
116
117
  };
117
- Entity.prototype.parsePermissions = function () {
118
- var permSegments = this.permissions.substr(1).match(/.{1,3}/g);
119
- if (permSegments.length !== 3) {
120
- throw Error('Invalid permissions.');
121
- }
122
- var _a = __read(permSegments, 3), u = _a[0], g = _a[1], o = _a[2];
123
- return {
124
- u: {
125
- r: u[0] !== '-',
126
- w: u[1] !== '-',
127
- x: u[2] !== '-'
128
- },
129
- g: {
130
- r: g[0] !== '-',
131
- w: g[1] !== '-',
132
- x: g[2] !== '-'
133
- },
134
- o: {
135
- r: o[0] !== '-',
136
- w: o[1] !== '-',
137
- x: o[2] !== '-'
138
- }
139
- };
140
- };
141
118
  Entity.prototype.getPermissions = function (user, groups) {
142
119
  if (user.username === 'root') {
143
- return {
144
- r: true,
145
- w: true,
146
- x: true
147
- };
120
+ return new permission_1.PermissionSegment({
121
+ segment: 'rwx'
122
+ });
148
123
  }
149
- var parsedPermissions = this.parsePermissions();
150
124
  if (this.owner === user.username) {
151
- return parsedPermissions.u;
125
+ return this.permissions.u;
152
126
  }
153
127
  else if (groups.has(this.owner) &&
154
128
  groups.get(this.owner).has(user.username)) {
155
- return parsedPermissions.g;
129
+ return this.permissions.g;
156
130
  }
157
- return parsedPermissions.o;
131
+ return this.permissions.o;
158
132
  };
159
133
  return Entity;
160
134
  }());
@@ -175,7 +149,7 @@ var File = /** @class */ (function (_super) {
175
149
  File.prototype.copy = function () {
176
150
  var newEntity = new File({
177
151
  name: this.name,
178
- permissions: this.permissions,
152
+ permissions: this.permissions.toString(),
179
153
  owner: this.owner,
180
154
  content: this.content,
181
155
  type: this.type,
@@ -240,7 +214,7 @@ var Folder = /** @class */ (function (_super) {
240
214
  Folder.prototype.copy = function (recursive) {
241
215
  var newEntity = new Folder({
242
216
  name: this.name,
243
- permissions: this.permissions,
217
+ permissions: this.permissions.toString(),
244
218
  owner: this.owner,
245
219
  folders: recursive ? this.copyFolders() : new Map(),
246
220
  files: recursive ? this.copyFiles() : new Map()
@@ -15,3 +15,4 @@ export { Session } from './session';
15
15
  export { Switch } from './switch';
16
16
  export { User } from './user';
17
17
  export { Vulnerability, VulnerabilityAction, VulnerabilityActionList, VulnerabilityActionUser, VulnerabilityRequirementList, VulnerabilityRequirements } from './vulnerabilities';
18
+ export { PermissionUserType, PermissionSegment, Permissions } from './permission';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VulnerabilityRequirements = exports.VulnerabilityRequirementList = exports.VulnerabilityActionUser = exports.VulnerabilityActionList = exports.VulnerabilityAction = exports.Vulnerability = exports.User = exports.Switch = exports.Session = exports.ServiceList = exports.Service = exports.Router = exports.Process = exports.Port = exports.NetworkDevice = exports.NetCard = exports.MetaOwnerType = exports.MetaOwnerLevel = exports.MetaInfo = exports.MetaDeviceType = exports.Location = exports.Library = exports.FSEntity = exports.Folder = exports.FileType = exports.File = exports.EMailMessage = exports.EMail = exports.Device = exports.Computer = void 0;
3
+ exports.Permissions = exports.PermissionSegment = exports.PermissionUserType = exports.VulnerabilityRequirements = exports.VulnerabilityRequirementList = exports.VulnerabilityActionUser = exports.VulnerabilityActionList = exports.VulnerabilityAction = exports.Vulnerability = exports.User = exports.Switch = exports.Session = exports.ServiceList = exports.Service = exports.Router = exports.Process = exports.Port = exports.NetworkDevice = exports.NetCard = exports.MetaOwnerType = exports.MetaOwnerLevel = exports.MetaInfo = exports.MetaDeviceType = exports.Location = exports.Library = exports.FSEntity = exports.Folder = exports.FileType = exports.File = exports.EMailMessage = exports.EMail = exports.Device = exports.Computer = void 0;
4
4
  var computer_1 = require("./computer");
5
5
  Object.defineProperty(exports, "Computer", { enumerable: true, get: function () { return computer_1.Computer; } });
6
6
  var device_1 = require("./device");
@@ -48,3 +48,7 @@ Object.defineProperty(exports, "VulnerabilityActionList", { enumerable: true, ge
48
48
  Object.defineProperty(exports, "VulnerabilityActionUser", { enumerable: true, get: function () { return vulnerabilities_1.VulnerabilityActionUser; } });
49
49
  Object.defineProperty(exports, "VulnerabilityRequirementList", { enumerable: true, get: function () { return vulnerabilities_1.VulnerabilityRequirementList; } });
50
50
  Object.defineProperty(exports, "VulnerabilityRequirements", { enumerable: true, get: function () { return vulnerabilities_1.VulnerabilityRequirements; } });
51
+ var permission_1 = require("./permission");
52
+ Object.defineProperty(exports, "PermissionUserType", { enumerable: true, get: function () { return permission_1.PermissionUserType; } });
53
+ Object.defineProperty(exports, "PermissionSegment", { enumerable: true, get: function () { return permission_1.PermissionSegment; } });
54
+ Object.defineProperty(exports, "Permissions", { enumerable: true, get: function () { return permission_1.Permissions; } });
@@ -0,0 +1,35 @@
1
+ export declare enum PermissionUserType {
2
+ User = 0,
3
+ Group = 1,
4
+ Other = 2,
5
+ None = 3
6
+ }
7
+ export interface PermissionSegmentOptions {
8
+ segment: string;
9
+ userType?: string;
10
+ }
11
+ export declare class PermissionSegment {
12
+ x: boolean;
13
+ w: boolean;
14
+ r: boolean;
15
+ userType: PermissionUserType;
16
+ constructor(options: PermissionSegmentOptions);
17
+ parseSegment(segment: string): void;
18
+ getFlagByString(flagType: string): boolean;
19
+ setFlagByString(flagType: string, value: boolean): void;
20
+ toString(): string;
21
+ }
22
+ export interface PermissionsOptions {
23
+ permissions: string;
24
+ }
25
+ export declare class Permissions {
26
+ m: string;
27
+ u: PermissionSegment;
28
+ g: PermissionSegment;
29
+ o: PermissionSegment;
30
+ constructor(options: PermissionsOptions);
31
+ parse(permission: string): void;
32
+ getSegmentByString(type: string): PermissionSegment;
33
+ chmod(str: string): boolean;
34
+ toString(): string;
35
+ }
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ var __read = (this && this.__read) || function (o, n) {
3
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
4
+ if (!m) return o;
5
+ var i = m.call(o), r, ar = [], e;
6
+ try {
7
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
+ }
9
+ catch (error) { e = { error: error }; }
10
+ finally {
11
+ try {
12
+ if (r && !r.done && (m = i["return"])) m.call(i);
13
+ }
14
+ finally { if (e) throw e.error; }
15
+ }
16
+ return ar;
17
+ };
18
+ var __values = (this && this.__values) || function(o) {
19
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
20
+ if (m) return m.call(o);
21
+ if (o && typeof o.length === "number") return {
22
+ next: function () {
23
+ if (o && i >= o.length) o = void 0;
24
+ return { value: o && o[i++], done: !o };
25
+ }
26
+ };
27
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.Permissions = exports.PermissionSegment = exports.PermissionUserType = void 0;
31
+ var PermissionUserType;
32
+ (function (PermissionUserType) {
33
+ PermissionUserType[PermissionUserType["User"] = 0] = "User";
34
+ PermissionUserType[PermissionUserType["Group"] = 1] = "Group";
35
+ PermissionUserType[PermissionUserType["Other"] = 2] = "Other";
36
+ PermissionUserType[PermissionUserType["None"] = 3] = "None";
37
+ })(PermissionUserType = exports.PermissionUserType || (exports.PermissionUserType = {}));
38
+ function getUserType(userType) {
39
+ switch (userType) {
40
+ case 'u':
41
+ return PermissionUserType.User;
42
+ case 'g':
43
+ return PermissionUserType.Group;
44
+ case 'o':
45
+ return PermissionUserType.Other;
46
+ default:
47
+ return PermissionUserType.None;
48
+ }
49
+ }
50
+ var PermissionSegment = /** @class */ (function () {
51
+ function PermissionSegment(options) {
52
+ this.parseSegment(options.segment);
53
+ this.userType = getUserType(options.userType);
54
+ }
55
+ PermissionSegment.prototype.parseSegment = function (segment) {
56
+ if (!/^[r\-][w\-][x\-]$/.test(segment)) {
57
+ throw new Error('Invalid permission segment.');
58
+ }
59
+ var _a = __read(segment, 3), r = _a[0], w = _a[1], x = _a[2];
60
+ this.r = r !== '-';
61
+ this.w = w !== '-';
62
+ this.x = x !== '-';
63
+ };
64
+ PermissionSegment.prototype.getFlagByString = function (flagType) {
65
+ switch (flagType) {
66
+ case 'x':
67
+ return this.x;
68
+ case 'w':
69
+ return this.w;
70
+ case 'r':
71
+ return this.r;
72
+ default:
73
+ throw new Error('Invalid permission flag type.');
74
+ }
75
+ };
76
+ PermissionSegment.prototype.setFlagByString = function (flagType, value) {
77
+ switch (flagType) {
78
+ case 'x': {
79
+ this.x = value;
80
+ break;
81
+ }
82
+ case 'w': {
83
+ this.w = value;
84
+ break;
85
+ }
86
+ case 'r': {
87
+ this.r = value;
88
+ break;
89
+ }
90
+ default:
91
+ throw new Error('Invalid permission flag type.');
92
+ }
93
+ };
94
+ PermissionSegment.prototype.toString = function () {
95
+ return "".concat(this.r ? 'r' : '-').concat(this.w ? 'w' : '-').concat(this.x ? 'x' : '-');
96
+ };
97
+ return PermissionSegment;
98
+ }());
99
+ exports.PermissionSegment = PermissionSegment;
100
+ var Permissions = /** @class */ (function () {
101
+ function Permissions(options) {
102
+ this.parse(options.permissions);
103
+ }
104
+ Permissions.prototype.parse = function (permission) {
105
+ if (!/^[d\-]([r\-][w\-][x\-]){3}$/.test(permission)) {
106
+ throw new Error('Invalid permission.');
107
+ }
108
+ var _a = __read(permission, 1), m = _a[0];
109
+ var u = permission.slice(1, 4);
110
+ var g = permission.slice(4, 7);
111
+ var o = permission.slice(7, 10);
112
+ this.m = m;
113
+ this.u = new PermissionSegment({ segment: u, userType: 'u' });
114
+ this.g = new PermissionSegment({ segment: g, userType: 'g' });
115
+ this.o = new PermissionSegment({ segment: o, userType: 'o' });
116
+ };
117
+ Permissions.prototype.getSegmentByString = function (type) {
118
+ switch (type) {
119
+ case 'u':
120
+ return this.u;
121
+ case 'g':
122
+ return this.g;
123
+ case 'o':
124
+ return this.o;
125
+ default:
126
+ throw new Error('Invalid permission flag type.');
127
+ }
128
+ };
129
+ Permissions.prototype.chmod = function (str) {
130
+ var e_1, _a;
131
+ if (!/^[ugo][-+][rwx]{3}$/.test(str)) {
132
+ return false;
133
+ }
134
+ var _b = __read(str), type = _b[0], operator = _b[1], flags = _b.slice(2);
135
+ var segment = this.getSegmentByString(type);
136
+ try {
137
+ for (var flags_1 = __values(flags), flags_1_1 = flags_1.next(); !flags_1_1.done; flags_1_1 = flags_1.next()) {
138
+ var flag = flags_1_1.value;
139
+ segment.setFlagByString(flag, operator === '+');
140
+ }
141
+ }
142
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
143
+ finally {
144
+ try {
145
+ if (flags_1_1 && !flags_1_1.done && (_a = flags_1.return)) _a.call(flags_1);
146
+ }
147
+ finally { if (e_1) throw e_1.error; }
148
+ }
149
+ return true;
150
+ };
151
+ Permissions.prototype.toString = function () {
152
+ var parts = [this.u, this.g, this.o].map(function (seg) { return seg.toString(); });
153
+ return "".concat(this.m).concat(parts.join(''));
154
+ };
155
+ return Permissions;
156
+ }());
157
+ exports.Permissions = Permissions;
package/dist/utils.d.ts CHANGED
@@ -3,21 +3,7 @@ import { Service } from './types/service';
3
3
  import { User } from './types/user';
4
4
  export declare function isValidIp(target: string): boolean;
5
5
  export declare function isLanIp(target: string): boolean;
6
- export interface PermissionSegment {
7
- x: boolean;
8
- w: boolean;
9
- r: boolean;
10
- }
11
- export interface PermissionMap {
12
- u: PermissionSegment;
13
- g: PermissionSegment;
14
- o: PermissionSegment;
15
- }
16
6
  export declare function getServiceLibrary(service: Service): Library | null;
17
- export declare function getFlagTypeByString(flag: PermissionSegment, flagType: string): boolean;
18
- export declare function setFlagTypeByString(flag: PermissionSegment, flagType: string, value: boolean): void;
19
- export declare function getPermissionSegmentByString(flag: PermissionMap, userType: string): PermissionSegment;
20
- export declare function transformFlagsToString(permissionMap: PermissionMap): string;
21
7
  export declare function getUserPath(user: User): string;
22
8
  export declare function getTraversalPath(path: string | null, currentLocation?: string[]): null | string[];
23
9
  export declare function getRandom(min: number, max: number): number;
package/dist/utils.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getRandomInt = exports.getRandom = exports.getTraversalPath = exports.getUserPath = exports.transformFlagsToString = exports.getPermissionSegmentByString = exports.setFlagTypeByString = exports.getFlagTypeByString = exports.getServiceLibrary = exports.isLanIp = exports.isValidIp = void 0;
6
+ exports.getRandomInt = exports.getRandom = exports.getTraversalPath = exports.getUserPath = exports.getServiceLibrary = exports.isLanIp = exports.isValidIp = void 0;
7
7
  var random_seed_1 = __importDefault(require("random-seed"));
8
8
  var library_1 = require("./types/library");
9
9
  var service_1 = require("./types/service");
@@ -34,58 +34,6 @@ function getServiceLibrary(service) {
34
34
  return null;
35
35
  }
36
36
  exports.getServiceLibrary = getServiceLibrary;
37
- function getFlagTypeByString(flag, flagType) {
38
- switch (flagType) {
39
- case 'x':
40
- return flag.x;
41
- case 'w':
42
- return flag.w;
43
- case 'r':
44
- return flag.r;
45
- default:
46
- throw new Error('Invalid permission flag type.');
47
- }
48
- }
49
- exports.getFlagTypeByString = getFlagTypeByString;
50
- function setFlagTypeByString(flag, flagType, value) {
51
- switch (flagType) {
52
- case 'x': {
53
- flag.x = value;
54
- break;
55
- }
56
- case 'w': {
57
- flag.w = value;
58
- break;
59
- }
60
- case 'r': {
61
- flag.r = value;
62
- break;
63
- }
64
- default:
65
- throw new Error('Invalid permission flag type.');
66
- }
67
- }
68
- exports.setFlagTypeByString = setFlagTypeByString;
69
- function getPermissionSegmentByString(flag, userType) {
70
- switch (userType) {
71
- case 'u':
72
- return flag.u;
73
- case 'g':
74
- return flag.g;
75
- case 'o':
76
- return flag.o;
77
- default:
78
- throw new Error('Invalid permission user type.');
79
- }
80
- }
81
- exports.getPermissionSegmentByString = getPermissionSegmentByString;
82
- function transformFlagsToString(permissionMap) {
83
- var segments = [permissionMap.u, permissionMap.g, permissionMap.o].map(function (map) {
84
- return "".concat(map.r ? 'r' : '-').concat(map.w ? 'w' : '-').concat(map.x ? 'x' : '-');
85
- });
86
- return "d".concat(segments.join(''));
87
- }
88
- exports.transformFlagsToString = transformFlagsToString;
89
37
  function getUserPath(user) {
90
38
  switch (user.username) {
91
39
  case 'root':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-mock-environment",
3
- "version": "1.2.5",
3
+ "version": "1.2.6",
4
4
  "description": "Mock environment",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",