From 8ca46fa35da7b4ed2a0e9b128b5750d7b8b3f1a9 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Mon, 7 Apr 2014 17:52:45 +0100 Subject: [PATCH] Audit configurable through admin console --- .../META-INF/resources/admin/js/app.js | 15 ++++ .../resources/admin/js/controllers/realm.js | 63 +++++++++++++++- .../META-INF/resources/admin/js/loaders.js | 8 +++ .../META-INF/resources/admin/js/services.js | 10 +++ .../admin/partials/realm-audit-config.html | 71 +++++++++++++++++++ .../resources/admin/partials/realm-audit.html | 7 +- .../resources/admin/partials/realm-menu.html | 2 +- .../org/keycloak/audit/AuditProvider.java | 4 +- .../keycloak/audit/jpa/JpaAuditProvider.java | 8 +-- .../audit/mongo/MongoAuditProvider.java | 12 ++-- .../tests/AbstractAuditProviderTest.java | 24 ++++--- .../idm/RealmAuditRepresentation.java | 37 ++++++++++ .../idm/RealmRepresentation.java | 4 ++ .../java/org/keycloak/models/RealmModel.java | 8 +++ .../org/keycloak/models/jpa/RealmAdapter.java | 22 ++++++ .../models/jpa/entities/RealmEntity.java | 21 +++++- .../mongo/keycloak/adapters/RealmAdapter.java | 22 ++++++ .../mongo/keycloak/entities/RealmEntity.java | 20 ++++++ .../services/managers/AuditManager.java | 57 +++++++++++++++ .../managers/ModelToRepresentation.java | 16 +++++ .../services/managers/RealmManager.java | 10 +++ .../services/resources/RealmsResource.java | 25 +------ .../services/resources/SocialResource.java | 25 +------ .../resources/admin/RealmAdminResource.java | 28 ++++++++ .../admin/ServerInfoAdminResource.java | 23 ++++++ .../testsuite/account/AccountTest.java | 59 +++++++++------ 26 files changed, 509 insertions(+), 92 deletions(-) create mode 100755 admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-audit-config.html create mode 100755 core/src/main/java/org/keycloak/representations/idm/RealmAuditRepresentation.java create mode 100644 services/src/main/java/org/keycloak/services/managers/AuditManager.java diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/js/app.js b/admin-ui/src/main/resources/META-INF/resources/admin/js/app.js index f8464595b0..4802f1fcd1 100755 --- a/admin-ui/src/main/resources/META-INF/resources/admin/js/app.js +++ b/admin-ui/src/main/resources/META-INF/resources/admin/js/app.js @@ -141,6 +141,21 @@ module.config([ '$routeProvider', function($routeProvider) { return RealmLoader(); } }, + controller : 'RealmAuditEventsCtrl' + }) + .when('/realms/:realm/audit-settings', { + templateUrl : 'partials/realm-audit-config.html', + resolve : { + realm : function(RealmLoader) { + return RealmLoader(); + }, + serverInfo : function(ServerInfoLoader) { + return ServerInfoLoader(); + }, + auditConfig : function(RealmAuditLoader) { + return RealmAuditLoader(); + } + }, controller : 'RealmAuditCtrl' }) .when('/realms/:realm/auth-settings', { diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js b/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js index b0642b7018..dc54399968 100755 --- a/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js +++ b/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js @@ -1022,7 +1022,65 @@ module.controller('RealmAuthSettingsDetailCtrl', function($scope, $routeParams, }; }); -module.controller('RealmAuditCtrl', function($scope, RealmAudit, realm) { +module.controller('RealmAuditCtrl', function($scope, auditConfig, RealmAudit, RealmAuditEvents, realm, serverInfo, $location, Notifications, TimeUnit, Dialog) { + $scope.realm = realm; + + $scope.auditConfig = auditConfig; + + $scope.auditConfig.expirationUnit = TimeUnit.autoUnit(auditConfig.auditExpiration); + if ($scope.auditConfig.expirationUnit) { + $scope.auditConfig.expirationUnit = 'Hours'; + } + + $scope.auditConfig.auditExpiration = TimeUnit.toUnit(auditConfig.auditExpiration, $scope.auditConfig.expirationUnit); + $scope.$watch('auditConfig.expirationUnit', function(to, from) { + if ($scope.auditConfig.auditExpiration) { + $scope.auditConfig.auditExpiration = TimeUnit.convert($scope.auditConfig.auditExpiration, from, to); + } + }); + + $scope.auditListeners = serverInfo.auditListeners; + + var oldCopy = angular.copy($scope.auditConfig); + $scope.changed = false; + + $scope.$watch('auditConfig', function() { + if (!angular.equals($scope.auditConfig, oldCopy)) { + $scope.changed = true; + } + }, true); + + $scope.save = function() { + $scope.changed = false; + + var copy = angular.copy($scope.auditConfig) + delete copy['expirationUnit']; + + copy.auditExpiration = TimeUnit.toSeconds($scope.auditConfig.auditExpiration, $scope.auditConfig.expirationUnit); + + RealmAudit.update({ + id : realm.realm + }, copy, function () { + $location.url("/realms/" + realm.realm + "/audit-settings"); + Notifications.success("Your changes have been saved to the realm."); + }); + }; + + $scope.reset = function() { + $scope.auditConfig = angular.copy(oldCopy); + $scope.changed = false; + }; + + $scope.clearAudit = function() { + Dialog.confirmDelete($scope.realm.realm, 'audit events', function() { + RealmAuditEvents.remove({ id : $scope.realm.realm }, function() { + Notifications.success("The audit events has been cleared."); + }); + }); + }; +}); + +module.controller('RealmAuditEventsCtrl', function($scope, RealmAuditEvents, realm) { $scope.realm = realm; $scope.page = 0; @@ -1038,8 +1096,7 @@ module.controller('RealmAuditCtrl', function($scope, RealmAudit, realm) { delete $scope.query[i]; } } - console.debug($scope.query.first); - $scope.events = RealmAudit.query($scope.query); + $scope.events = RealmAuditEvents.query($scope.query); } $scope.firstPage = function() { diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/js/loaders.js b/admin-ui/src/main/resources/META-INF/resources/admin/js/loaders.js index 84f8dbce8d..e1e6a787df 100755 --- a/admin-ui/src/main/resources/META-INF/resources/admin/js/loaders.js +++ b/admin-ui/src/main/resources/META-INF/resources/admin/js/loaders.js @@ -47,6 +47,14 @@ module.factory('RealmLoader', function(Loader, Realm, $route, $q) { }); }); +module.factory('RealmAuditLoader', function(Loader, RealmAudit, $route, $q) { + return Loader.get(RealmAudit, function() { + return { + id : $route.current.params.realm + } + }); +}); + module.factory('UserListLoader', function(Loader, User, $route, $q) { return Loader.query(User, function() { return { diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js b/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js index eabc83a824..81af19b992 100755 --- a/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js +++ b/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js @@ -145,6 +145,16 @@ module.factory('Realm', function($resource) { module.factory('RealmAudit', function($resource) { return $resource(authUrl + '/rest/admin/realms/:id/audit', { id : '@realm' + }, { + update : { + method : 'PUT' + } + }); +}); + +module.factory('RealmAuditEvents', function($resource) { + return $resource(authUrl + '/rest/admin/realms/:id/audit/events', { + id : '@realm' }); }); diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-audit-config.html b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-audit-config.html new file mode 100755 index 0000000000..befe2dde89 --- /dev/null +++ b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-audit-config.html @@ -0,0 +1,71 @@ +
+
+ + + +
+ +

{{realm.realm}} Audit Config

+ +
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+ +
+ +
+
+
+ +
+
+ +
+
+
+
+ +
+ + +
+ +
+
+
+ +
+ + +
+
+
+
+ diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-audit.html b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-audit.html index f31ab363ae..01543200fb 100755 --- a/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-audit.html +++ b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-audit.html @@ -1,6 +1,11 @@
- + + +