@mindconnect/mindconnect-nodejs
Version:
NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)
594 lines • 36.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const sdk_1 = require("../../api/sdk");
const utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
let color = (0, command_utils_1.getColor)("magenta");
let groupColor = (0, command_utils_1.getColor)("green");
let roleColor = (0, command_utils_1.getColor)("yellow");
exports.default = (program) => {
program
.command("identity-management")
.alias("iam")
.option("-m, --mode [list|create|assign|remove|delete|info]", "Mode can be list | create | assign | remove | delete | info", "list")
.option("-u, --user [user]", "user name")
.option("-g, --group [group]", "user group")
.option("-r, --role [role]", "user role")
.option("-s, --subtenant <subtenant>", "subtenant for user")
.option("-m, --membergroup [membergroup]", "member group")
.option("-l, --memberrole [memberrole]", "member role")
.option("-t, --meta", "include meta information (ids, login details etc.)")
.option("-w, --raw", "don't automatically preceed group names with mdsp_usergroup or role with mdsp_customrole")
.option("-k, --passkey <passkey>", "passkey")
.option("-v, --verbose", "verbose output")
.description(color("manage mindsphere users, groups and roles *"))
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
checkRequiredParameters(options);
const sdk = (0, command_utils_1.getSdk)(options);
color = (0, command_utils_1.adjustColor)(color, options);
(0, command_utils_1.homeDirLog)(options.verbose, color);
(0, command_utils_1.proxyLog)(options.verbose, color);
const iam = sdk.GetIdentityManagementClient();
const tenantManagement = sdk.GetTenantManagementClient();
const users = () => __awaiter(void 0, void 0, void 0, function* () { return yield getAllUsers(iam, options); });
const groups = () => __awaiter(void 0, void 0, void 0, function* () { return yield getAllGroups(iam, options); });
const roles = () => __awaiter(void 0, void 0, void 0, function* () { return yield getAllRoles(iam, options); });
// * list and info commands
(options.mode === "list" || options.mode === "info") &&
options.user &&
printUsers(yield users(), yield groups(), yield roles(), options);
(options.mode === "list" || options.mode === "info") &&
options.group &&
printGroups(yield users(), yield groups(), yield roles(), options);
(options.mode === "list" || options.mode === "info") &&
options.role &&
printRoles(yield users(), yield groups(), yield roles(), options);
// * create commands
options.mode === "create" && options.user && (yield createUser(iam, tenantManagement, options));
options.mode === "create" && options.group && (yield createGroup(iam, options));
options.mode === "create" && options.role && (yield createRole(iam, options));
// * delete commands
options.mode === "delete" && options.user && (yield deleteUser(iam, options));
options.mode === "delete" && options.group && (yield deleteGroup(iam, options));
options.mode === "delete" && options.role && (yield deleteRole(iam, options));
// * user assign commands (like in settings app)
options.mode === "assign" &&
options.user &&
options.group &&
(yield assignUserToGroup(iam, options));
options.mode === "assign" && options.user && options.role && (yield assignUserToRole(iam, options));
// * user remove commands (like in settings app)
options.mode === "remove" &&
options.user &&
options.group &&
(yield removeUserFromGroup(iam, options));
options.mode === "remove" &&
options.role &&
options.user &&
(yield removeRoleFromUser(iam, options));
// * group assign commands (like in settings app)
options.mode === "assign" &&
options.group &&
options.membergroup &&
(yield addMemberGroupToGroup(iam, options));
options.mode === "assign" && options.group && options.role && (yield addRoleToGroup(iam, options));
// * group remove commands (like in settings app)
options.mode === "remove" &&
options.group &&
options.membergroup &&
(yield removeMemberGroupFromGroup(iam, options));
options.mode === "remove" &&
options.group &&
options.role &&
(yield removeRoleFromGroup(iam, options));
// * role assign commands (like in settings app)
options.mode === "assign" &&
options.role &&
options.memberrole &&
(yield addMemberRoleToRole(iam, options));
// * role remove commands (like in settings app)
options.mode === "remove" &&
options.role &&
options.memberrole &&
(yield removeMemberRoleFromRole(iam, options));
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
console.log(`\n Example:\n`);
console.log(` mdsp iam --mode list --user \t\t list all ${color("users")}`);
console.log(` mdsp iam --mode list --user [user] \t list all ${color("users")} which contain ${color("[user]")} in the login name`);
console.log(` mdsp iam --mode list --group \t\t list all ${groupColor("groups")}`);
console.log(` mdsp iam --mode list --group [group] \t list all ${groupColor("groups")} which contain ${groupColor("[group]")} in the name`);
console.log(` mdsp iam --mode list --role \t\t list all ${groupColor("roles")}`);
console.log(` mdsp iam --mode list --role [role] \t list all ${roleColor("roles")} which contain ${roleColor("[role]")} in the name`);
console.log(`\n mdsp iam --mode info --user [user] --meta \t get all infos about ${color("users")} which contain ${color("[user]")} in the login name`);
console.log(` mdsp iam --mode info --group [group] --meta \t get all infos about ${groupColor("groups")} which contain ${groupColor("[group]")} in the name`);
console.log(` mdsp iam --mode info --role [role] --meta \t get all infos about ${roleColor("roles")} which contain ${roleColor("[role]")} in the name`);
console.log(`\n mdsp iam --mode create|delete --user <user> \t create or delete ${color("user")}`);
console.log(` mdsp iam --mode create|delete --group <group> create or delete ${groupColor("group")}`);
console.log(` mdsp iam --mode create|delete --role <role> \t create or delete ${roleColor("role")}`);
console.log(` mdsp iam --mode create --user <user> --subtenant subtenant \t create ${color("user")} in specified subtenant`);
console.log(`\n mdsp iam --mode assign --user <user> --group <group> \t assign ${color("user")} to user group ${groupColor("group")}`);
console.log(` mdsp iam --mode assign --user <user> --role <role> \t assign ${roleColor("role")} to user ${color("user")}`);
console.log(` mdsp iam --mode assign --user <user> --group <group> \t assign ${color("user")} to user group ${groupColor("group")}`);
console.log(` mdsp iam --mode assign --group <group> --membergroup <membergroup> \t assign ${groupColor("membergroup")} to user group ${groupColor("group")}`);
console.log(` mdsp iam --mode assign --group <group> --role <role> \t\t\t assign ${roleColor("role")} to user group ${groupColor("group")}`);
console.log(` mdsp iam --mode assign --role <role> --memberrole <memberrole> \t assign ${roleColor("member role")} to custom role ${roleColor("role")}`);
console.log(`\n mdsp iam --mode remove --user <user> --group <group> \t remove ${color("user")} from user group ${groupColor("group")}`);
console.log(` mdsp iam --mode remove --user <user> --role <role> \t remove ${roleColor("role")} from user ${color("user")}`);
console.log(` mdsp iam --mode remove --user <user> --group <group> \t remove ${color("user")} from user group ${groupColor("group")}`);
console.log(` mdsp iam --mode remove --group <group> --membergroup <membergroup> \t remove ${groupColor("membergroup")} from user group ${groupColor("group")}`);
console.log(` mdsp iam --mode remove --group <group> --role <role> \t\t\t remove ${roleColor("role")} from user group ${groupColor("group")}`);
console.log(` mdsp iam --mode remove --role <role> --memberrole <memberrole> \t remove ${roleColor("member role")} from custom role ${roleColor("role")}`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function printUsers(users, groups, roles, options) {
if (typeof options.user === "string") {
users = users.filter((user) => { var _a; return (_a = user.userName) === null || _a === void 0 ? void 0 : _a.includes(options.user); });
}
users.forEach((user) => {
printUser(user, groups, roles, options);
});
console.log(`\n${color(users.length)} users found`);
}
function printGroups(users, groups, roles, options) {
const selected = typeof options.group === "string"
? groups.filter((group) => { var _a; return (_a = group.displayName) === null || _a === void 0 ? void 0 : _a.includes(options.group); })
: groups;
selected.forEach((group) => {
prettyPrintMembers(group, users, groups, roles, options);
options.meta && (0, command_utils_1.printObjectInfo)("Meta:", group.meta || {}, options, ["lastModified"], color, 0);
});
console.log(`\n${groupColor(selected.length)} groups found`);
}
function printRoles(users, groups, roles, options) {
const selected = typeof options.role === "string" ? roles.filter((role) => { var _a; return (_a = role.displayName) === null || _a === void 0 ? void 0 : _a.includes(options.role); }) : roles;
selected.forEach((role) => {
prettyPrintMembers(role, users, groups, roles, options);
options.meta && (0, command_utils_1.printObjectInfo)("Meta:", role.meta || {}, options, ["lastModified"], color, 0);
});
console.log(`\n${roleColor(selected.length)} roles found.`);
}
function prettyPrintMembers(group, users, allGroups, allRoles, options) {
const members = group.members || [];
const assignedUsers = members.filter((x) => x.type === sdk_1.IdentityManagementModels.ScimGroupMember.TypeEnum.USER);
const assignedGroups = members.filter((x) => x.type === sdk_1.IdentityManagementModels.ScimGroupMember.TypeEnum.GROUP);
const assignedRoles = allRoles
.filter((role) => { var _a; return (_a = role.members) === null || _a === void 0 ? void 0 : _a.map((y) => y.value).includes(group.id); })
.map((role) => role.displayName);
const groupOrRoleColor = options.role ? roleColor : groupColor;
console.log(`${groupOrRoleColor(group.displayName)} [${assignedUsers.length + " users"} , ${groupColor(assignedGroups.length + " subgroups")}, ${roleColor(assignedRoles.length + " roles")}]`);
if (options.mode === "info") {
console.log("Users:");
assignedUsers.forEach((member) => {
var _a;
console.log(`\t${(_a = users.find((x) => x.id === member.value)) === null || _a === void 0 ? void 0 : _a.userName}`);
});
console.log(groupColor("Groups:"));
assignedGroups.forEach((member) => {
var _a;
console.log(`\t${(_a = allGroups.find((x) => x.id === member.value)) === null || _a === void 0 ? void 0 : _a.displayName}`);
});
console.log(roleColor("Roles:"));
assignedRoles.forEach((role) => {
console.log(`\t${roleColor(role)}`);
});
}
}
function getAllUsers(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const users = [];
let startIndex = 1;
let count = 500;
let userPage;
do {
userPage = yield iam.GetUsers({ startIndex: startIndex, count: 500, sortBy: "userName" });
users.push(...userPage.resources);
startIndex += count;
} while (startIndex < (userPage.totalResults || 1));
return users;
});
}
function findInMembers(roleId, groups, roles) {
const ids = [];
const groupsandroles = groups.concat(roles);
const foundGroups = groupsandroles.filter((x) => x.id === roleId);
if (foundGroups.length > 0) {
foundGroups.forEach((group) => {
if (group.members) {
group.members.forEach((member) => {
if (member.type === sdk_1.IdentityManagementModels.ScimGroupMember.TypeEnum.GROUP) {
ids.push(member.value);
}
});
}
});
}
const result = groupsandroles.filter((x) => ids.includes(x.id)).map((x) => x.displayName);
return result.filter((value, index, self) => self.indexOf(value) === index);
}
function getAllGroups(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
let filter = `displayName sw "mdsp_usergroup"`;
return getGroupsWithFilter(iam, filter);
});
}
function getAllRoles(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
let filter = `(displayName sw "mdsp:" or displayName sw "mdsp_customrole:")`;
return getGroupsWithFilter(iam, filter);
});
}
function getGroupsWithFilter(iam, filterString) {
return __awaiter(this, void 0, void 0, function* () {
const groups = [];
let startIndex = 1;
let count = 500;
let groupPage;
do {
groupPage = yield iam.GetGroups({
filter: filterString,
startIndex: startIndex,
count: 500,
});
groups.push(...groupPage.resources);
startIndex += count;
} while (startIndex < (groupPage.totalResults || 1));
const result = groups.sort((x, y) => {
return x.displayName.localeCompare(y.displayName);
});
return result;
});
}
function printUser(user, allGroups, allRoles, options) {
var _a, _b, _c, _d, _e;
const groups = (_a = user.groups) === null || _a === void 0 ? void 0 : _a.filter((x) => x.display === "mdsp:core:TenantAdmin");
const userColor = groups && groups.length ? color : (x) => x;
const admin = groups && groups.length > 0 ? color("*") : "-";
const userGroups = (_b = user.groups) === null || _b === void 0 ? void 0 : _b.filter((x) => x.display.startsWith("mdsp_usergroup:"));
const userRoles = (_c = user.groups) === null || _c === void 0 ? void 0 : _c.filter((x) => !x.display.startsWith("mdsp_usergroup:"));
let userInfo = `${admin} ${userColor(user.userName)} (${((_d = user.name) === null || _d === void 0 ? void 0 : _d.familyName) || "<empty>"}, ${((_e = user.name) === null || _e === void 0 ? void 0 : _e.givenName) || "<empty>"}) ${user.active ? "" : color("inactive")} [${groupColor(((userGroups === null || userGroups === void 0 ? void 0 : userGroups.length) || 0) + " groups")}, ${roleColor(((userRoles === null || userRoles === void 0 ? void 0 : userRoles.length) || 0) + " roles")}]`;
userInfo += ` Last Login: ${user.lastLogonTime ? (0, command_utils_1.humanReadableDate)(new Date(user.lastLogonTime)) : color("never")}`;
console.log(userInfo);
if (options.mode == "info") {
delete user.groups;
console.log(`${groupColor("Groups")}:`);
userGroups === null || userGroups === void 0 ? void 0 : userGroups.forEach((group) => {
console.log(`\t${groupColor(group.display + " " + group.type)}`);
});
console.log(`${roleColor("Roles")}:`);
userRoles === null || userRoles === void 0 ? void 0 : userRoles.forEach((role) => {
console.log(`\t${roleColor(role.display)} ${role.type === sdk_1.IdentityManagementModels.Group.TypeEnum.DIRECT
? roleColor("(DIRECT)")
: roleColor("(INDIRECT) via:") + JSON.stringify(findInMembers(role.value, allGroups, allRoles))}`);
});
options.meta && (0, command_utils_1.printObjectInfo)("Meta:", user || {}, options, ["lastModified"], color, 0);
}
}
function createUser(iam, tm, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const newUser = { userName: options.user };
if (options.subtenant && typeof options.subtenant === "string") {
const subtenants = yield tm.GetSubtenants();
const subtenant = (_a = subtenants.content) === null || _a === void 0 ? void 0 : _a.find((x) => x.displayName === options.subtenant);
if (subtenant) {
newUser.subtenants = [{ id: subtenant.id }];
}
}
const user = yield iam.PostUser(newUser);
console.log(`user with username ${color(user.userName)} created`);
(0, command_utils_1.verboseLog)(JSON.stringify(user, null, 2), options.verbose);
});
}
function createRole(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const name = normalize(options.role, options);
const role = yield iam.PostGroup({ displayName: name, description: `created using CLI` });
console.log(`custom role with displayName ${roleColor(role.displayName)} created`);
(0, command_utils_1.verboseLog)(JSON.stringify(role, null, 2), options.verbose);
});
}
function createGroup(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const name = normalize(options.group, options);
const group = yield iam.PostGroup({ displayName: name, description: `created using CLI` });
console.log(`group with displayName ${groupColor(group.displayName)} created`);
(0, command_utils_1.verboseLog)(JSON.stringify(group, null, 2), options.verbose);
});
}
function normalize(name, options) {
if (options.group) {
if (!options.raw && !name.startsWith("mdsp_usergroup:")) {
name = `mdsp_usergroup:${name}`;
}
}
else if (options.role) {
if (!options.raw && !name.startsWith("mdsp_customrole:") && !name.startsWith("mdsp:")) {
if (["Developer", "DeveloperAdmin", "StandardUser", "SubTenatnUser", "TenantAdmin"].includes(name)) {
name = "mdsp:core:" + name;
}
else {
name = `mdsp_customrole:${name}`;
}
}
}
return name;
}
function deleteUser(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const users = yield iam.GetUsers({ filter: `userName eq "${options.user}"` });
if (users.totalResults === 1) {
const deletedUser = yield iam.DeleteUser(users.resources[0].id);
console.log(`user with username ${color(users.resources[0].userName)} deleted`);
(0, command_utils_1.verboseLog)(JSON.stringify(deletedUser, null, 2), options.verbose);
}
else {
(0, utils_1.throwError)(`found ${color(users.totalResults)} users users but expected 1 `);
}
});
}
function deleteGroup(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const groups = yield iam.GetGroups({ filter: `displayName eq "${normalize(options.group, options)}"` });
if (groups.totalResults === 1) {
const deletedGroup = yield iam.DeleteGroup(groups.resources[0].id);
console.log(`group ${groupColor(groups.resources[0].displayName)} deleted`);
(0, command_utils_1.verboseLog)(JSON.stringify(deletedGroup, null, 2), options.verbose);
}
else {
(0, utils_1.throwError)(`found ${color(groups.totalResults)} groups but expected 1 (--mode delete command expects the full name of the group in --group parameter) `);
}
});
}
function deleteRole(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const roles = yield iam.GetGroups({ filter: `displayName eq "${normalize(options.role, options)}"` });
if (roles.totalResults === 1) {
const deleteRole = yield iam.DeleteGroup(roles.resources[0].id);
console.log(`role ${roleColor(roles.resources[0].displayName)} deleted`);
(0, command_utils_1.verboseLog)(JSON.stringify(deleteRole, null, 2), options.verbose);
}
else {
(0, utils_1.throwError)(`found ${color(roles.totalResults)} roles but expected 1 (--mode delete command expects the full name of the role in --role parameter) `);
}
});
}
function assignUserToGroup(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const users = yield iam.GetUsers({ filter: `userName eq "${options.user}"` });
const groups = yield iam.GetGroups({ filter: `displayName eq "${normalize(options.group, options)}"` });
if (users.totalResults !== 1) {
(0, utils_1.throwError)(`found ${color(users.totalResults)} users but expected 1 (you have to use the full --user name for ${options.mode} command)`);
}
if (groups.totalResults !== 1) {
(0, utils_1.throwError)(`found ${groupColor(groups.totalResults)} groups but expected 1 (you have to use the full --group name for ${options.mode} command)`);
}
const assigned = yield iam.PostGroupMember(groups.resources[0].id, {
type: sdk_1.IdentityManagementModels.ScimGroupMember.TypeEnum.USER,
value: users.resources[0].id,
});
console.log(`assigned user ${color(options.user)} to ${groupColor(normalize(options.group, options))}`);
(0, command_utils_1.verboseLog)(JSON.stringify(assigned, null, 2), options.verbose);
});
}
function assignUserToRole(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const users = yield iam.GetUsers({ filter: `userName eq "${options.user}"` });
const roles = yield iam.GetGroups({ filter: `displayName eq "${normalize(options.role, options)}"` });
if (users.totalResults !== 1) {
(0, utils_1.throwError)(`found ${color(users.totalResults)} users but expected 1 (you have to use the full --user name for --mode ${options.mode} command)`);
}
if (roles.totalResults !== 1) {
(0, utils_1.throwError)(`found ${roleColor(roleColor.totalResults)} roles but expected 1 (you have to use the full --role name for --mode ${options.mode} command)`);
}
const assigned = yield iam.PostGroupMember(roles.resources[0].id, {
type: sdk_1.IdentityManagementModels.ScimGroupMember.TypeEnum.USER,
value: users.resources[0].id,
});
console.log(`assigned role ${roleColor(normalize(options.role, options))} to user ${color(options.user)}`);
(0, command_utils_1.verboseLog)(JSON.stringify(assigned, null, 2), options.verbose);
});
}
function removeUserFromGroup(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const users = yield iam.GetUsers({ filter: `userName eq "${options.user}"` });
const groups = yield iam.GetGroups({ filter: `displayName eq "${normalize(options.group, options)}"` });
if (users.totalResults !== 1) {
(0, utils_1.throwError)(`found ${color(users.totalResults)} users but expected 1 (you have to use the full --user name for ${options.mode} command)`);
}
if (groups.totalResults !== 1) {
(0, utils_1.throwError)(`found ${groupColor(groups.totalResults)} groups but expected 1 (you have to use the full --group name for ${options.mode} command)`);
}
const removed = yield iam.DeleteGroupMember(groups.resources[0].id, users.resources[0].id);
console.log(`removed user ${color(options.user)} from ${groupColor(normalize(options.group, options))}`);
(0, command_utils_1.verboseLog)(JSON.stringify(removed, null, 2), options.verbose);
});
}
function removeRoleFromUser(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const users = yield iam.GetUsers({ filter: `userName eq "${options.user}"` });
const roles = yield iam.GetGroups({ filter: `displayName eq "${normalize(options.role, options)}"` });
if (users.totalResults !== 1) {
(0, utils_1.throwError)(`found ${color(users.totalResults)} users but expected 1 (you have to use the full --user name for ${options.mode} command)`);
}
if (roles.totalResults !== 1) {
(0, utils_1.throwError)(`found ${groupColor(roles.totalResults)} groups but expected 1 (you have to use the full --group name for ${options.mode} command)`);
}
const removed = yield iam.DeleteGroupMember(roles.resources[0].id, users.resources[0].id);
console.log(`removed role ${roleColor(normalize(options.role, options))} from user ${color(options.user)}`);
(0, command_utils_1.verboseLog)(JSON.stringify(removed, null, 2), options.verbose);
});
}
function addMemberGroupToGroup(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const groups = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.group, { group: options.group })}"`,
});
const memberGroups = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.membergroup, { group: options.membergroup })}"`,
});
if (memberGroups.totalResults !== 1) {
(0, utils_1.throwError)(`found ${color(memberGroups.totalResults)} member groups but expected 1 (you have to use the full --membergroup name for ${options.mode} command)`);
}
if (groups.totalResults !== 1) {
(0, utils_1.throwError)(`found ${groupColor(groups.totalResults)} groups but expected 1 (you have to use the full --group name for ${options.mode} command)`);
}
const assigned = yield iam.PostGroupMember(groups.resources[0].id, {
type: sdk_1.IdentityManagementModels.ScimGroupMember.TypeEnum.GROUP,
value: memberGroups.resources[0].id,
});
console.log(`assigned memberGroup ${groupColor(normalize(options.membergroup, { group: options.membergroup }))} to ${groupColor(normalize(options.group, { group: options.group }))}`);
(0, command_utils_1.verboseLog)(JSON.stringify(assigned, null, 2), options.verbose);
});
}
function removeMemberGroupFromGroup(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const groups = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.group, { group: options.group })}"`,
});
const memberGroups = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.membergroup, { group: options.membergroup })}"`,
});
if (memberGroups.totalResults !== 1) {
(0, utils_1.throwError)(`found ${color(memberGroups.totalResults)} member groups but expected 1 (you have to use the full --membergroup name for ${options.mode} command)`);
}
if (groups.totalResults !== 1) {
(0, utils_1.throwError)(`found ${groupColor(groups.totalResults)} groups but expected 1 (you have to use the full --group name for ${options.mode} command)`);
}
const assigned = yield iam.DeleteGroupMember(groups.resources[0].id, memberGroups.resources[0].id);
console.log(`removed memberGroup ${groupColor(normalize(options.membergroup, { group: options.membergroup }))} from ${groupColor(normalize(options.group, { group: options.group }))}`);
(0, command_utils_1.verboseLog)(JSON.stringify(assigned, null, 2), options.verbose);
});
}
function addRoleToGroup(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const groups = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.group, { group: options.group })}"`,
});
const roles = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.role, { role: options.role })}"`,
});
if (roles.totalResults !== 1) {
(0, utils_1.throwError)(`found ${roleColor(roles.totalResults)} roles but expected 1 (you have to use the full --role name for ${options.mode} command)`);
}
if (groups.totalResults !== 1) {
(0, utils_1.throwError)(`found ${groupColor(groups.totalResults)} groups but expected 1 (you have to use the full --group name for ${options.mode} command)`);
}
const assigned = yield iam.PostGroupMember(roles.resources[0].id, {
type: sdk_1.IdentityManagementModels.ScimGroupMember.TypeEnum.GROUP,
value: groups.resources[0].id,
});
console.log(`assigned role ${roleColor(normalize(options.role, { role: options.role }))} to group ${groupColor(normalize(options.group, { group: options.group }))}`);
(0, command_utils_1.verboseLog)(JSON.stringify(assigned, null, 2), options.verbose);
});
}
function removeRoleFromGroup(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const groups = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.group, { group: options.group })}"`,
});
const roles = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.role, { role: options.role })}"`,
});
if (roles.totalResults !== 1) {
(0, utils_1.throwError)(`found ${roleColor(roles.totalResults)} roles but expected 1 (you have to use the full --role name for ${options.mode} command)`);
}
if (groups.totalResults !== 1) {
(0, utils_1.throwError)(`found ${groupColor(groups.totalResults)} groups but expected 1 (you have to use the full --group name for ${options.mode} command)`);
}
const assigned = yield iam.DeleteGroupMember(roles.resources[0].id, groups.resources[0].id);
console.log(`removed role ${roleColor(normalize(options.role, { role: options.role }))} from group ${groupColor(normalize(options.group, { group: options.group }))}`);
(0, command_utils_1.verboseLog)(JSON.stringify(assigned, null, 2), options.verbose);
});
}
function addMemberRoleToRole(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const roles = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.role, { role: options.role })}"`,
});
const memberRoles = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.memberrole, { role: options.role })}"`,
});
if (memberRoles.totalResults !== 1) {
(0, utils_1.throwError)(`found ${roleColor(memberRoles.totalResults)} member roles but expected 1 (you have to use the full --memberrole name for ${options.mode} command)`);
}
if (roles.totalResults !== 1) {
(0, utils_1.throwError)(`found ${roleColor(roles.totalResults)} roles but expected 1 (you have to use the full --role name for ${options.mode} command)`);
}
const assigned = yield iam.PostGroupMember(memberRoles.resources[0].id, {
type: sdk_1.IdentityManagementModels.ScimGroupMember.TypeEnum.GROUP,
value: roles.resources[0].id,
});
console.log(`assigned member role ${roleColor(normalize(options.memberrole, { group: options.memberrole }))} to ${roleColor(normalize(options.role, { group: options.role }))}`);
(0, command_utils_1.verboseLog)(JSON.stringify(assigned, null, 2), options.verbose);
});
}
function removeMemberRoleFromRole(iam, options) {
return __awaiter(this, void 0, void 0, function* () {
const roles = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.role, { role: options.role })}"`,
});
const memberRoles = yield iam.GetGroups({
filter: `displayName eq "${normalize(options.memberrole, { role: options.role })}"`,
});
if (memberRoles.totalResults !== 1) {
(0, utils_1.throwError)(`found ${roleColor(memberRoles.totalResults)} member roles but expected 1 (you have to use the full --memberrole name for ${options.mode} command)`);
}
if (roles.totalResults !== 1) {
(0, utils_1.throwError)(`found ${roleColor(roles.totalResults)} roles but expected 1 (you have to use the full --role name for ${options.mode} command)`);
}
const removed = yield iam.DeleteGroupMember(memberRoles.resources[0].id, roles.resources[0].id);
console.log(`removed member role ${roleColor(normalize(options.memberrole, { group: options.memberrole }))} from ${roleColor(normalize(options.role, { group: options.role }))}`);
(0, command_utils_1.verboseLog)(JSON.stringify(removed, null, 2), options.verbose);
});
}
function checkRequiredParameters(options) {
!(["list", "create", "assign", "remove", "delete", "info"].indexOf(options.mode) >= 0) &&
(0, utils_1.throwError)(`invalid mode ${options.mode} (must be list, creete, assign, remove, delete, info)`);
["list", "info"].forEach((x) => {
options.mode === x &&
!options.user &&
!options.group &&
!options.role &&
(0, utils_1.throwError)(`you have to specify either --user [user] or --group [group] or --role [role] for mdsp iam --mode ${x} command`);
});
["create", "delete"].forEach((x) => {
options.mode === x &&
!(typeof options.user === "string") &&
!(typeof options.group === "string") &&
!(typeof options.role === "string") &&
(0, utils_1.throwError)(`you have to specify either --user [user] or --group [group] or --role [role] for mdsp iam --mode ${x} command`);
});
["create", "delete", "assign", "remove"].forEach((x) => {
options.mode === x &&
options.user &&
options.user === true &&
(0, utils_1.throwError)(`you have to specify full user name for iam --mode ${x} command`);
options.mode === x &&
options.group &&
options.group === true &&
(0, utils_1.throwError)(`you have to specify full group name for iam --mode ${x} command`);
options.mode === x &&
options.role &&
options.role === true &&
(0, utils_1.throwError)(`you have to specify full role name for iam --mode ${x} command`);
});
}
//# sourceMappingURL=identity.js.map