1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.PluginInfrastructure;
|
---|
6 | using HeuristicLab.Security.Contracts.BusinessObjects;
|
---|
7 | using HeuristicLab.Hive.Server.Core;
|
---|
8 | using System.Diagnostics;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Security.Core {
|
---|
11 | [Application("Security Test App", "Test Application for the Security Service", false)]
|
---|
12 | class SecurityCoreTest : ApplicationBase {
|
---|
13 |
|
---|
14 | SecurityManager manager = new SecurityManager();
|
---|
15 | PermissionManager permManager = new PermissionManager();
|
---|
16 |
|
---|
17 |
|
---|
18 | private void testGroups() {
|
---|
19 |
|
---|
20 | User user = new User();
|
---|
21 | user.Login = "anna";
|
---|
22 | user.SetHashedPassword("blubb");
|
---|
23 | user.Name = "Anna";
|
---|
24 | user = manager.AddNewUser(user);
|
---|
25 |
|
---|
26 | User user2 = new User();
|
---|
27 | user2.Login = "stefan";
|
---|
28 | user2.Name = "Stefan";
|
---|
29 |
|
---|
30 | UserGroup group = new UserGroup();
|
---|
31 | group.Name = "Students";
|
---|
32 |
|
---|
33 | UserGroup group2 = new UserGroup();
|
---|
34 | group2.Name = "Lazy Students";
|
---|
35 |
|
---|
36 | group.Members.Add(user);
|
---|
37 | group.Members.Add(group2);
|
---|
38 |
|
---|
39 | group2.Members.Add(user2);
|
---|
40 |
|
---|
41 | manager.AddNewUserGroup(group);
|
---|
42 |
|
---|
43 | group = manager.GetUserGroupByName("Students");
|
---|
44 | group2 = manager.GetUserGroupByName("Lazy Students");
|
---|
45 |
|
---|
46 | manager.RemoveUserGroup(group2.Id);
|
---|
47 |
|
---|
48 | group = manager.GetUserGroupByName("Students");
|
---|
49 | group2 = manager.GetUserGroupByName("Lazy Students");
|
---|
50 |
|
---|
51 | manager.RemoveUser(user.Id);
|
---|
52 |
|
---|
53 | group = manager.GetUserGroupByName("Students");
|
---|
54 | manager.RemoveUserGroup(group.Id);
|
---|
55 |
|
---|
56 | group = manager.GetUserGroupByName("Students");
|
---|
57 | manager.RemoveUser(user2.Id);
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void testPermissions() {
|
---|
61 |
|
---|
62 | Permission permission = new Permission();
|
---|
63 | permission.Name = "ADD_JOBS";
|
---|
64 | permission.Description = "Add jobs";
|
---|
65 | permission.Plugin = "TestPlugin";
|
---|
66 |
|
---|
67 | manager.AddPermission(permission);
|
---|
68 |
|
---|
69 | permission = manager.GetPermissionById(permission.Id);
|
---|
70 |
|
---|
71 | User user = new User();
|
---|
72 | user.Login = "anna";
|
---|
73 | user.SetHashedPassword("blubb");
|
---|
74 | user.Name = "Anna";
|
---|
75 | user = manager.AddNewUser(user);
|
---|
76 |
|
---|
77 | manager.GrantPermission(user.Id, permission.Id, Guid.Empty);
|
---|
78 |
|
---|
79 | Guid sessionId = permManager.Authenticate("anna", "blubb");
|
---|
80 |
|
---|
81 | bool hasPermission =
|
---|
82 | permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
|
---|
83 |
|
---|
84 | manager.RevokePermission(user.Id, permission.Id, Guid.Empty);
|
---|
85 |
|
---|
86 | hasPermission =
|
---|
87 | permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
|
---|
88 |
|
---|
89 | permManager.EndSession(sessionId);
|
---|
90 |
|
---|
91 | manager.RemoveUser(user.Id);
|
---|
92 |
|
---|
93 | manager.RemovePermission(permission.Id);
|
---|
94 |
|
---|
95 | permission = manager.GetPermissionById(permission.Id);
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void testPermissionsGroup() {
|
---|
99 |
|
---|
100 | User user = new User();
|
---|
101 | user.Login = "anna";
|
---|
102 | user.SetHashedPassword("blubb");
|
---|
103 | user.Name = "Anna";
|
---|
104 | user = manager.AddNewUser(user);
|
---|
105 |
|
---|
106 | User user2 = new User();
|
---|
107 | user2.Login = "stefan";
|
---|
108 | user2.SetHashedPassword("foo");
|
---|
109 | user2.Name = "Stefan";
|
---|
110 |
|
---|
111 | UserGroup group = new UserGroup();
|
---|
112 | group.Name = "Students";
|
---|
113 |
|
---|
114 | UserGroup group2 = new UserGroup();
|
---|
115 | group2.Name = "Lazy Students";
|
---|
116 |
|
---|
117 | group.Members.Add(user);
|
---|
118 | group.Members.Add(group2);
|
---|
119 |
|
---|
120 | group2.Members.Add(user2);
|
---|
121 |
|
---|
122 | manager.AddNewUserGroup(group);
|
---|
123 |
|
---|
124 | Permission permission = new Permission();
|
---|
125 | permission.Name = "ADD_JOBS";
|
---|
126 | permission.Description = "Add jobs";
|
---|
127 | permission.Plugin = "TestPlugin";
|
---|
128 |
|
---|
129 | manager.AddPermission(permission);
|
---|
130 |
|
---|
131 | manager.GrantPermission(group.Id, permission.Id, Guid.Empty);
|
---|
132 | manager.GrantPermission(group2.Id, permission.Id, Guid.Empty);
|
---|
133 |
|
---|
134 | Guid sessionId = permManager.Authenticate("anna", "blubb");
|
---|
135 |
|
---|
136 | bool hasPermission =
|
---|
137 | permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
|
---|
138 |
|
---|
139 | Guid sessionId2 = permManager.Authenticate("stefan", "foo");
|
---|
140 |
|
---|
141 | hasPermission =
|
---|
142 | permManager.CheckPermission(sessionId2, permission.Id, Guid.Empty);
|
---|
143 |
|
---|
144 | manager.RevokePermission(group.Id, permission.Id, Guid.Empty);
|
---|
145 |
|
---|
146 | hasPermission =
|
---|
147 | permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
|
---|
148 |
|
---|
149 | hasPermission =
|
---|
150 | permManager.CheckPermission(sessionId2, permission.Id, Guid.Empty);
|
---|
151 |
|
---|
152 | manager.RevokePermission(group2.Id, permission.Id, Guid.Empty);
|
---|
153 |
|
---|
154 | hasPermission =
|
---|
155 | permManager.CheckPermission(sessionId2, permission.Id, Guid.Empty);
|
---|
156 |
|
---|
157 | permManager.EndSession(sessionId);
|
---|
158 | permManager.EndSession(sessionId2);
|
---|
159 |
|
---|
160 | manager.RemoveUser(user.Id);
|
---|
161 | manager.RemoveUser(user2.Id);
|
---|
162 | manager.RemoveUserGroup(group.Id);
|
---|
163 | manager.RemoveUserGroup(group2.Id);
|
---|
164 |
|
---|
165 | manager.RemovePermission(permission.Id);
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | public void InsertClientUser() {
|
---|
170 | PermissionCollection pc = HivePermissions.GetPermissions();
|
---|
171 | Debug.WriteLine("Revoke permissions....");
|
---|
172 | try {
|
---|
173 | UserGroup group = manager.GetUserGroupByName("Projektmitglieder");
|
---|
174 | manager.RevokePermission(group.Id, pc[pc.Convert(HivePermissions.Usermanagement.Client.Read)].Id, Guid.Empty);
|
---|
175 | manager.RevokePermission(group.Id, pc[pc.Convert(HivePermissions.Usermanagement.ClientGroup.Read)].Id, Guid.Empty);
|
---|
176 | }
|
---|
177 | catch (Exception ex) {
|
---|
178 | Debug.WriteLine(ex.Message);
|
---|
179 | }
|
---|
180 | Debug.WriteLine("Remove user...");
|
---|
181 | try {
|
---|
182 | manager.RemoveUser(manager.GetUserByName("HIVE User").Id);
|
---|
183 | }
|
---|
184 | catch (Exception ex) {
|
---|
185 | Debug.WriteLine(ex.Message);
|
---|
186 | }
|
---|
187 | Debug.WriteLine("Remove group....");
|
---|
188 | try {
|
---|
189 | manager.RemoveUserGroup(manager.GetUserGroupByName("Projektmitglieder").Id);
|
---|
190 | }
|
---|
191 | catch (Exception ex) {
|
---|
192 | Debug.WriteLine(ex.Message);
|
---|
193 | }
|
---|
194 |
|
---|
195 | User user = new User();
|
---|
196 | user.Login = "hive";
|
---|
197 | user.SetHashedPassword("hive");
|
---|
198 | user.Name = "HIVE User";
|
---|
199 | Debug.WriteLine("Adding user...");
|
---|
200 | try {
|
---|
201 | user = manager.AddNewUser(user);
|
---|
202 | }
|
---|
203 | catch (Exception ex) {
|
---|
204 | Debug.WriteLine(ex.Message);
|
---|
205 | }
|
---|
206 |
|
---|
207 | UserGroup grp = new UserGroup();
|
---|
208 | grp.Name = "Projektmitglieder";
|
---|
209 | grp.Members.Add(user);
|
---|
210 | Debug.WriteLine("Adding group...");
|
---|
211 | try {
|
---|
212 | manager.AddNewUserGroup(grp);
|
---|
213 | }
|
---|
214 | catch (Exception ex) {
|
---|
215 | Debug.WriteLine(ex.Message);
|
---|
216 | }
|
---|
217 | Debug.WriteLine("Grant permissions...");
|
---|
218 | try {
|
---|
219 | manager.GrantPermission(grp.Id, pc[pc.Convert(HivePermissions.Usermanagement.Client.Read)].Id, Guid.Empty);
|
---|
220 | manager.GrantPermission(grp.Id, pc[pc.Convert(HivePermissions.Usermanagement.ClientGroup.Read)].Id, Guid.Empty);
|
---|
221 | }
|
---|
222 | catch (Exception ex) {
|
---|
223 | Debug.WriteLine(ex.Message);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | public void InsertSuperUser() {
|
---|
228 | Debug.WriteLine("Revoke permissions...");
|
---|
229 | try {
|
---|
230 | UserGroup group = manager.GetUserGroupByName("Administratoren");
|
---|
231 | foreach (Permission item in HivePermissions.GetPermissions()) {
|
---|
232 | manager.RevokePermission(group.Id, item.Id, Guid.Empty);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | catch (Exception ex) {
|
---|
236 | Debug.WriteLine(ex.Message);
|
---|
237 | }
|
---|
238 | Debug.WriteLine("Revoke permissions from admin...");
|
---|
239 | try {
|
---|
240 | Guid g = manager.GetUserByName("HIVE Admin").Id;
|
---|
241 | foreach (Permission item in HivePermissions.GetPermissions()) {
|
---|
242 | manager.RevokePermission(g, item.Id, Guid.Empty);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | catch (Exception ex) {
|
---|
246 | Debug.WriteLine(ex.Message);
|
---|
247 | }
|
---|
248 | Debug.WriteLine("Removing user...");
|
---|
249 | try {
|
---|
250 | manager.RemoveUser(manager.GetUserByName("HIVE Admin").Id);
|
---|
251 | }
|
---|
252 | catch (Exception ex) {
|
---|
253 | Debug.WriteLine(ex.Message);
|
---|
254 | }
|
---|
255 | Debug.WriteLine("Removing group...");
|
---|
256 | try {
|
---|
257 | manager.RemoveUserGroup(manager.GetUserGroupByName("Administratoren").Id);
|
---|
258 | }
|
---|
259 | catch (Exception ex) {
|
---|
260 | Debug.WriteLine(ex.Message);
|
---|
261 | }
|
---|
262 |
|
---|
263 | User user = new User();
|
---|
264 | user.Login = "admin";
|
---|
265 | user.SetHashedPassword("admin");
|
---|
266 | user.Name = "HIVE Admin";
|
---|
267 | User tmp = manager.AddNewUser(user);
|
---|
268 |
|
---|
269 | Debug.WriteLine("Grant ALL permissions to admin...");
|
---|
270 | foreach (Permission item in HivePermissions.GetPermissions()) {
|
---|
271 | try {
|
---|
272 | manager.GrantPermission(tmp.Id, item.Id, Guid.Empty);
|
---|
273 | }
|
---|
274 | catch (Exception ex) {
|
---|
275 | Debug.WriteLine(ex.Message);
|
---|
276 | }
|
---|
277 | }
|
---|
278 | Debug.WriteLine("Adding user...");
|
---|
279 | try {
|
---|
280 | user = manager.AddNewUser(user);
|
---|
281 | }
|
---|
282 | catch (Exception ex) {
|
---|
283 | Debug.WriteLine(ex.Message);
|
---|
284 | }
|
---|
285 |
|
---|
286 | UserGroup grp = new UserGroup();
|
---|
287 | grp.Name = "Administratoren";
|
---|
288 | grp.Members.Add(user);
|
---|
289 | Debug.WriteLine("Adding group...");
|
---|
290 | try {
|
---|
291 | manager.AddNewUserGroup(grp);
|
---|
292 | }
|
---|
293 | catch (Exception ex) {
|
---|
294 | Debug.WriteLine(ex.Message);
|
---|
295 | }
|
---|
296 | Debug.WriteLine("Adding permissions...");
|
---|
297 | //admins allowed to do everything ;)
|
---|
298 | foreach (Permission item in HivePermissions.GetPermissions()) {
|
---|
299 | try {
|
---|
300 | manager.GrantPermission(grp.Id, item.Id, Guid.Empty);
|
---|
301 | }
|
---|
302 | catch (Exception ex) {
|
---|
303 | Debug.WriteLine(ex.Message);
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | public void InsertHivePermissions() {
|
---|
309 | foreach (Permission item in HivePermissions.GetPermissions()) {
|
---|
310 | try {
|
---|
311 | manager.AddPermission(item);
|
---|
312 | }
|
---|
313 | catch (Exception ex) {
|
---|
314 | Debug.WriteLine(ex.Message);
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | public override void Run() {
|
---|
320 | //testPermissionsGroup();
|
---|
321 |
|
---|
322 | InsertHivePermissions();
|
---|
323 | InsertClientUser();
|
---|
324 | InsertSuperUser();
|
---|
325 | Debug.WriteLine("Complete!");
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|