Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Monitor user Groups and Templates scripts #52

Merged
merged 21 commits into from
Sep 26, 2024

Conversation

nshandra
Copy link
Contributor

@nshandra nshandra commented Jul 7, 2024

📌 References

Issue: Implementation of the highest risk (the ones in green) events logging into MSTeams

📝 Implementation

This script will compare the metadata of the monitored userGroups or user templates with the version stored in the datastore and generate a report of the changes. This report will be sent to the MS Teams channel set in the webhook config section. Then the new version of the metadata will be stored in the datastore.

Execution:

yarn install

yarn build

yarn start usermonitoring [run-user-groups-monitoring | user-templates-monitoring] --config-file config.json

# To get the debug logs and store them in a file use:
LOG_LEVEL=debug yarn start usermonitoring [run-user-groups-monitoring | user-templates-monitoring] --config-file config.json &> monitoring.log

Parameters:

  • --config-file: Connection and webhook config file.
  • -s | --set-datastore: Write monitored data to datastore, use in script setup. It assumes there is a monitoring config in d2-tools/[user-groups-monitoring | user-templates-monitoring]

Requirements:

A config file with the access info of the server and the message webhook details:

{
    "URL": {
        "username": "user",
        "password": "passwd",
        "server": "https://dhis.url/"
    },
    "WEBHOOK": {
        "ms_url": "http://webhook.url/",
        "proxy": "http://proxy.url/",
        "server_name": "INSTANCE_NAME"
    }
}

Sample user-groups-monitoring datastore:

{
  "lastExecution": "timestamp",
  "monitoredUserGroups": [{
    "id1.UserGroup data": "......"
  },
  {
    "id2.UserGroup data": "......"
  }],
  "groupsToMonitor": [
    "id1",
    "id2"
  ]
}

Sample user-templates-monitoring datastore:

{
  "lastExecution": "timestamp",
  "monitoredUserTemplates": [{
    "username1.template data": "......"
  },
  {
    "username2.template data": "......"
  }],
  "templatesToMonitor": [
    "username1.template",
    "username2.template"
  ]
}

This reports stores data into the d2-tools.[user-groups-monitoring | user-templates-monitoring] datastore. This key needs to be setup before the first run to get a correct report.
Its possible to leave monitoredUserGroups | monitoredUserTemplates empty and use the -s flag to populate it.

The report, potentially, has tree sections for each user group:

  • New entries: JSON with the properties that were unset or empty and changed.
  • Modified fields: This section has two JSONs, one showing the old values and one with the news.
  • User assignment changes: This section will show the users lost and added to the group or the template roles and group.

If a section is empty it will be omited.

@nshandra nshandra requested review from xurxodev and ifoche July 7, 2024 18:07
@nshandra nshandra self-assigned this Jul 7, 2024
@nshandra nshandra changed the title Feature: Monitor userGroups script Feature: Monitor user Groups and Templates scripts Sep 5, 2024
@nshandra nshandra requested review from tokland and removed request for xurxodev September 5, 2024 11:41
@nshandra nshandra marked this pull request as ready for review September 6, 2024 06:48
Copy link
Contributor

@tokland tokland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[code only review] Very close, cool to have tests. Mostly minor issues:

README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
src/scripts/commands/usermonitoring.ts Outdated Show resolved Hide resolved
src/scripts/commands/usermonitoring.ts Outdated Show resolved Hide resolved
typos and repeated lines in README
usermonitoring.ts => userMonitoring.ts
userMonitoring: config_file => configFile
UserGroup/TemplateDiff replace _Lost/Added to camel case
CompareUserTemplatesUseCase.specs move noChangesDiff to data folder
CompareUserGroupsUseCase.data add usersChanges to the user difference test
Rework User and UserGroup entities and D2Repositories.
Fix and improve some tests.
@nshandra nshandra requested a review from tokland September 23, 2024 08:26
Copy link
Contributor

@tokland tokland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some very minor details and we are ready to go

@@ -75,7 +73,7 @@ export class MonitorUserGroupsUseCase {
log.info(`Get user groups with ids: ${options.groupsToMonitor.join(", ")}`);

const getGroupsUseCase = new GetUserGroupsUseCase(this.userGroupRepository);
const compareUserGroupsUseCase = new CompareUserGroupsUseCase();
const compareUserGroupsUseCase = new CompareUserGroups();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can now remove UseCase from the name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry about that...


export class SaveUserGroupsMonitoringConfigUseCase {
constructor(private configRepository: UserGroupsMonitoringConfigRepository) {}

async execute(options: UserGroupsMonitoringOptions, monitoredUserGroups: UserGroup[]): Async<void> {
options.lastExecution = new GetLogFormatDateUseCase().execute(new Date());
options.monitoredUserGroups = monitoredUserGroups;
const new_options = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

casing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, there were some other snake case names that are changed too.

@@ -12,7 +12,7 @@ import {

describe("CompareUserGroupsUseCase", () => {
it("Should return empty array when comparing the same objects", () => {
const useCase = new CompareUserGroupsUseCase();
const useCase = new CompareUserGroups();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name

@@ -97,7 +97,7 @@ export class MonitorUserTemplatesUseCase {
log.info(`Get user groups with usernames: ${options.templatesToMonitor.join(", ")}`);

const getTemplatesUseCase = new GetUserTemplatesUseCase(this.usersRepository);
const compareUserTemplatesUseCase = new CompareUserTemplatesUseCase();
const compareUserTemplatesUseCase = new CompareUserTemplates();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name


export class SaveUserTemplatesMonitoringConfigUseCase {
constructor(private configRepository: UserTemplatesMonitoringConfigRepository) {}

async execute(options: UserTemplatesMonitoringOptions, monitoredUserTemplates: User[]): Async<void> {
options.lastExecution = new GetLogFormatDateUseCase().execute(new Date());
options.monitoredUserTemplates = monitoredUserTemplates;
const new_options = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

casing


beforeEach(() => {
useCase = new CompareUserTemplatesUseCase();
useCase = new CompareUserTemplates();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name

Simplify getLogFormatDate, naming and casing.
Fix userRoles repeating in userCredentials.
@nshandra nshandra requested a review from tokland September 24, 2024 19:31
Copy link
Contributor

@tokland tokland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good! 👍

@ifoche ifoche merged commit 141301b into development Sep 26, 2024
1 check passed
@ifoche ifoche deleted the feature/monitor_usergroups branch September 26, 2024 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants