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

Add more security actions logs #1563

Merged
merged 20 commits into from Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 54 additions & 11 deletions lib/api/controllers/security.js
Expand Up @@ -304,6 +304,7 @@ class SecurityController extends NativeController {
description,
{ creatorId, refresh, apiKeyId });

this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on user "${userId}."`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Here request.context.user is an object, IMHO it's better to print only the ID
(This remark apply everywhere)

return apiKey.serialize({ includeToken: true });
}

Expand Down Expand Up @@ -534,7 +535,10 @@ class SecurityController extends NativeController {
refresh: getRefresh(request)
}
)
.then(role => formatProcessing.serializeRole(role));
.then(role => {
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on role "${role._id}."`);
return formatProcessing.serializeRole(role);
});
}

/**
Expand All @@ -556,7 +560,10 @@ class SecurityController extends NativeController {
refresh: getRefresh(request)
}
)
.then(role => formatProcessing.serializeRole(role));
.then(role => {
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on role "${role._id}."`);
return formatProcessing.serializeRole(role);
});
}

/**
Expand All @@ -571,7 +578,11 @@ class SecurityController extends NativeController {
const options = { refresh: getRefresh(request) };

return this.kuzzle.repositories.role.load(request.input.resource._id)
.then(role => this.kuzzle.repositories.role.delete(role, options));
.then(role =>
{
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action} on role "${role._id}."`);
return this.kuzzle.repositories.role.delete(role, options);
});
}

/**
Expand Down Expand Up @@ -640,7 +651,9 @@ class SecurityController extends NativeController {
refresh: getRefresh(request)
}
)
.then(profile => formatProcessing.serializeProfile(profile)
.then(profile => {
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on profile "${profile._id}."`);
return formatProcessing.serializeProfile(profile);}
);
}

Expand All @@ -665,8 +678,10 @@ class SecurityController extends NativeController {
refresh: getRefresh(request)
}
)
.then(profile => formatProcessing.serializeProfile(profile)
);
.then(profile => {
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on profile "${profile._id}."`);
return formatProcessing.serializeProfile(profile);
});
}

/**
Expand All @@ -681,7 +696,10 @@ class SecurityController extends NativeController {
const options = { refresh: getRefresh(request) };

return this.kuzzle.repositories.profile.load(request.input.resource._id)
.then(profile => this.kuzzle.repositories.profile.delete(profile, options));
.then(profile => {
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on profile "${profile._id}."`);
return this.kuzzle.repositories.profile.delete(profile, options);
});
}

/**
Expand Down Expand Up @@ -829,6 +847,7 @@ class SecurityController extends NativeController {

await this.kuzzle.repositories.user.delete(user, options);

this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on user "${userId}."`);
return {
_id: userId
};
Expand All @@ -855,6 +874,7 @@ class SecurityController extends NativeController {
const pojoUser = request.input.body.content;
pojoUser._id = request.input.resource._id || uuid();

this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on user "${pojoUser._id}."`);
return persistUser(this.kuzzle, request, pojoUser);
}

Expand All @@ -880,6 +900,7 @@ class SecurityController extends NativeController {

pojoUser.profileIds = this.kuzzle.config.security.restrictedProfileIds;

this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on user "${pojoUser._id}."`);
return persistUser(this.kuzzle, request, pojoUser);
}

Expand Down Expand Up @@ -912,7 +933,10 @@ class SecurityController extends NativeController {
Object.assign(currentUserPojo, pojo));
})
.then(user => this.kuzzle.repositories.user.persist(user, options))
.then(updatedUser => formatProcessing.serializeUser(updatedUser));
.then(updatedUser => {
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on user "${updatedUser._id}."`);
return formatProcessing.serializeUser(updatedUser);
});
}

/**
Expand Down Expand Up @@ -961,6 +985,7 @@ class SecurityController extends NativeController {
updatedUser,
options);

this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on user "${createdUser._id}."`);
return formatProcessing.serializeUser(createdUser);
}

Expand All @@ -985,7 +1010,10 @@ class SecurityController extends NativeController {
.then(profile => this.kuzzle.repositories.profile.validateAndSaveProfile(
_.extend(profile, request.input.body),
options))
.then(updatedProfile => formatProcessing.serializeProfile(updatedProfile));
.then(updatedProfile => {
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on profile "${updatedProfile._id}."`);
return formatProcessing.serializeProfile(updatedProfile);
});
}

/**
Expand All @@ -1009,7 +1037,10 @@ class SecurityController extends NativeController {
.then(role => this.kuzzle.repositories.role.validateAndSaveRole(
_.extend(role, request.input.body),
options))
.then(updatedRole => formatProcessing.serializeRole(updatedRole));
.then(updatedRole => {
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on role "${updatedRole._id}."`);
return formatProcessing.serializeRole(updatedRole);
});
}

/**
Expand Down Expand Up @@ -1049,6 +1080,7 @@ class SecurityController extends NativeController {
.then(() => response);
}

this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}".`);
return response;
});
}
Expand Down Expand Up @@ -1153,6 +1185,7 @@ class SecurityController extends NativeController {
strategy,
'create');

this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on user "${id}."`);
return createMethod(request, request.input.body, id, strategy);
});
}
Expand Down Expand Up @@ -1188,6 +1221,7 @@ class SecurityController extends NativeController {
strategy,
'update');

this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on user "${id}."`);
return updateMethod(request, request.input.body, id, strategy);
});
}
Expand Down Expand Up @@ -1247,7 +1281,10 @@ class SecurityController extends NativeController {
'delete');

return deleteMethod(request, request.input.resource._id, request.input.args.strategy)
.then(() => ({acknowledged: true}));
.then(() => {
this.kuzzle.log.info(`[SECURITY] User "${request.context.user}" applied action "${request.input.action}" on user "${request.input.resource._id}."`);
return {acknowledged: true};
});
}

/**
Expand Down Expand Up @@ -1442,6 +1479,12 @@ function mDelete (kuzzle, type, request) {
errorsManager.get('services', 'storage', 'incomplete_delete', errors));
}

if (ids.length > 1000) {
kuzzle.log.info(`[SECURITY] User "${request.context.user}" deleted the following ${type}s ${ids.slice(0, 1000).join(', ')}... (${ids.length - 1000} more users deleted)."`);
}
else {
kuzzle.log.info(`[SECURITY] User "${request.context.user}" deleted the following ${type}s ${ids.join(', ')}."`);
}
return ids;
});
}
Expand Down
11 changes: 7 additions & 4 deletions lib/api/funnel.js
Expand Up @@ -676,7 +676,8 @@ class Funnel {
if (asError) {
callback(error, request);
this.handleErrorDump(error);
} else {
}
else {
callback(null, request);
}

Expand All @@ -701,20 +702,22 @@ class Funnel {
if (this[cachedItem.executor](cachedItem.request, cachedItem.callback) === -1) {
// no slot found again. We stop here and try next time
break;
} else {
}
else {
this.requestsCacheQueue.shift();
}
}
}

if (this.requestsCacheQueue.length > 0) {
setTimeout(() => this._playCachedRequests(), 0);
} else {
}
else {
const now = Date.now();
// No request remaining in cache => stop the background task and return to normal behavior
this.overloaded = false;

if (this.overloadWarned
if ( this.overloadWarned
&& (this.lastOverloadTime === 0 || this.lastOverloadTime < now - 500)
) {
this.overloadWarned = false;
Expand Down
2 changes: 2 additions & 0 deletions test/api/controllers/security/profiles.test.js
Expand Up @@ -490,6 +490,7 @@ describe('Test: security controller - profiles', () => {

describe('#deleteProfile', () => {
it('should return an object with on deleteProfile call', () => {
kuzzle.repositories.profile.load.resolves({ _id: 'test' });
kuzzle.repositories.profile.delete.resolves({_id: 'test'});

return securityController.deleteProfile(new Request({_id: 'test'}))
Expand All @@ -501,6 +502,7 @@ describe('Test: security controller - profiles', () => {

it('should reject with an error in case of error', () => {
const error = new Error('Mocked error');
kuzzle.repositories.profile.load.resolves({ _id: 'test' });
kuzzle.repositories.profile.delete.rejects(error);

return should(securityController.deleteProfile(new Request({_id: 'test'}))).be.rejectedWith(error);
Expand Down
5 changes: 3 additions & 2 deletions test/api/controllers/security/roles.test.js
Expand Up @@ -251,7 +251,7 @@ describe('Test: security controller - roles', () => {

describe('#deleteRole', () => {
it('should return response with on deleteRole call', done => {
const role = {my: 'role'};
const role = {_id: 'role'};

kuzzle.repositories.role.load.resolves(role);
kuzzle.repositories.role.delete.resolves();
Expand All @@ -270,8 +270,9 @@ describe('Test: security controller - roles', () => {
});

it('should forward refresh option', () => {
const role = {my: 'role'};
const role = {_id: 'role'};

kuzzle.repositories.role.load.resolves(role);
kuzzle.repositories.role.getRoleFromRequest.resolves(role);
kuzzle.repositories.role.delete.resolves();

Expand Down