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 | [ClassInfo(Name = "Security Test App",
|
---|
12 | Description = "Test Application for the Security Service",
|
---|
13 | AutoRestart = false)]
|
---|
14 | class SecurityCoreTest : ApplicationBase {
|
---|
15 |
|
---|
16 | SecurityManager manager = new SecurityManager();
|
---|
17 | PermissionManager permManager = new PermissionManager();
|
---|
18 |
|
---|
19 |
|
---|
20 | private void testGroups() {
|
---|
21 |
|
---|
22 | User user = new User();
|
---|
23 | user.Login = "anna";
|
---|
24 | user.SetHashedPassword("blubb");
|
---|
25 | user.Name = "Anna";
|
---|
26 | user = manager.AddNewUser(user);
|
---|
27 |
|
---|
28 | User user2 = new User();
|
---|
29 | user2.Login = "stefan";
|
---|
30 | user2.Name = "Stefan";
|
---|
31 |
|
---|
32 | UserGroup group = new UserGroup();
|
---|
33 | group.Name = "Students";
|
---|
34 |
|
---|
35 | UserGroup group2 = new UserGroup();
|
---|
36 | group2.Name = "Lazy Students";
|
---|
37 |
|
---|
38 | group.Members.Add(user);
|
---|
39 | group.Members.Add(group2);
|
---|
40 |
|
---|
41 | group2.Members.Add(user2);
|
---|
42 |
|
---|
43 | manager.AddNewUserGroup(group);
|
---|
44 |
|
---|
45 | group = manager.GetUserGroupByName("Students");
|
---|
46 | group2 = manager.GetUserGroupByName("Lazy Students");
|
---|
47 |
|
---|
48 | manager.RemoveUserGroup(group2.Id);
|
---|
49 |
|
---|
50 | group = manager.GetUserGroupByName("Students");
|
---|
51 | group2 = manager.GetUserGroupByName("Lazy Students");
|
---|
52 |
|
---|
53 | manager.RemoveUser(user.Id);
|
---|
54 |
|
---|
55 | group = manager.GetUserGroupByName("Students");
|
---|
56 | manager.RemoveUserGroup(group.Id);
|
---|
57 |
|
---|
58 | group = manager.GetUserGroupByName("Students");
|
---|
59 | manager.RemoveUser(user2.Id);
|
---|
60 | }
|
---|
61 |
|
---|
62 | private void testPermissions() {
|
---|
63 |
|
---|
64 | Permission permission = new Permission();
|
---|
65 | permission.Name = "ADD_JOBS";
|
---|
66 | permission.Description = "Add jobs";
|
---|
67 | permission.Plugin = "TestPlugin";
|
---|
68 |
|
---|
69 | manager.AddPermission(permission);
|
---|
70 |
|
---|
71 | permission = manager.GetPermissionById(permission.Id);
|
---|
72 |
|
---|
73 | User user = new User();
|
---|
74 | user.Login = "anna";
|
---|
75 | user.SetHashedPassword("blubb");
|
---|
76 | user.Name = "Anna";
|
---|
77 | user = manager.AddNewUser(user);
|
---|
78 |
|
---|
79 | manager.GrantPermission(user.Id, permission.Id, Guid.Empty);
|
---|
80 |
|
---|
81 | Guid sessionId = permManager.Authenticate("anna", "blubb");
|
---|
82 |
|
---|
83 | bool hasPermission =
|
---|
84 | permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
|
---|
85 |
|
---|
86 | manager.RevokePermission(user.Id, permission.Id, Guid.Empty);
|
---|
87 |
|
---|
88 | hasPermission =
|
---|
89 | permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
|
---|
90 |
|
---|
91 | permManager.EndSession(sessionId);
|
---|
92 |
|
---|
93 | manager.RemoveUser(user.Id);
|
---|
94 |
|
---|
95 | manager.RemovePermission(permission.Id);
|
---|
96 |
|
---|
97 | permission = manager.GetPermissionById(permission.Id);
|
---|
98 | }
|
---|
99 |
|
---|
100 | private void testPermissionsGroup() {
|
---|
101 |
|
---|
102 | User user = new User();
|
---|
103 | user.Login = "anna";
|
---|
104 | user.SetHashedPassword("blubb");
|
---|
105 | user.Name = "Anna";
|
---|
106 | user = manager.AddNewUser(user);
|
---|
107 |
|
---|
108 | User user2 = new User();
|
---|
109 | user2.Login = "stefan";
|
---|
110 | user2.SetHashedPassword("foo");
|
---|
111 | user2.Name = "Stefan";
|
---|
112 |
|
---|
113 | UserGroup group = new UserGroup();
|
---|
114 | group.Name = "Students";
|
---|
115 |
|
---|
116 | UserGroup group2 = new UserGroup();
|
---|
117 | group2.Name = "Lazy Students";
|
---|
118 |
|
---|
119 | group.Members.Add(user);
|
---|
120 | group.Members.Add(group2);
|
---|
121 |
|
---|
122 | group2.Members.Add(user2);
|
---|
123 |
|
---|
124 | manager.AddNewUserGroup(group);
|
---|
125 |
|
---|
126 | Permission permission = new Permission();
|
---|
127 | permission.Name = "ADD_JOBS";
|
---|
128 | permission.Description = "Add jobs";
|
---|
129 | permission.Plugin = "TestPlugin";
|
---|
130 |
|
---|
131 | manager.AddPermission(permission);
|
---|
132 |
|
---|
133 | manager.GrantPermission(group.Id, permission.Id, Guid.Empty);
|
---|
134 | manager.GrantPermission(group2.Id, permission.Id, Guid.Empty);
|
---|
135 |
|
---|
136 | Guid sessionId = permManager.Authenticate("anna", "blubb");
|
---|
137 |
|
---|
138 | bool hasPermission =
|
---|
139 | permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
|
---|
140 |
|
---|
141 | Guid sessionId2 = permManager.Authenticate("stefan", "foo");
|
---|
142 |
|
---|
143 | hasPermission =
|
---|
144 | permManager.CheckPermission(sessionId2, permission.Id, Guid.Empty);
|
---|
145 |
|
---|
146 | manager.RevokePermission(group.Id, permission.Id, Guid.Empty);
|
---|
147 |
|
---|
148 | hasPermission =
|
---|
149 | permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
|
---|
150 |
|
---|
151 | hasPermission =
|
---|
152 | permManager.CheckPermission(sessionId2, permission.Id, Guid.Empty);
|
---|
153 |
|
---|
154 | manager.RevokePermission(group2.Id, permission.Id, Guid.Empty);
|
---|
155 |
|
---|
156 | hasPermission =
|
---|
157 | permManager.CheckPermission(sessionId2, permission.Id, Guid.Empty);
|
---|
158 |
|
---|
159 | permManager.EndSession(sessionId);
|
---|
160 | permManager.EndSession(sessionId2);
|
---|
161 |
|
---|
162 | manager.RemoveUser(user.Id);
|
---|
163 | manager.RemoveUser(user2.Id);
|
---|
164 | manager.RemoveUserGroup(group.Id);
|
---|
165 | manager.RemoveUserGroup(group2.Id);
|
---|
166 |
|
---|
167 | manager.RemovePermission(permission.Id);
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | public void InsertClientUser() {
|
---|
172 | PermissionCollection pc = HivePermissions.GetPermissions();
|
---|
173 | Debug.WriteLine("Revoke permissions....");
|
---|
174 | try {
|
---|
175 | UserGroup group = manager.GetUserGroupByName("Projektmitglieder");
|
---|
176 | manager.RevokePermission(group.Id, pc[pc.Convert(HivePermissions.Usermanagement.Client.Read)].Id, Guid.Empty);
|
---|
177 | manager.RevokePermission(group.Id, pc[pc.Convert(HivePermissions.Usermanagement.ClientGroup.Read)].Id, Guid.Empty);
|
---|
178 | }
|
---|
179 | catch (Exception ex) {
|
---|
180 | Debug.WriteLine(ex.Message);
|
---|
181 | }
|
---|
182 | Debug.WriteLine("Remove user...");
|
---|
183 | try {
|
---|
184 | manager.RemoveUser(manager.GetUserByName("HIVE User").Id);
|
---|
185 | }
|
---|
186 | catch (Exception ex) {
|
---|
187 | Debug.WriteLine(ex.Message);
|
---|
188 | }
|
---|
189 | Debug.WriteLine("Remove group....");
|
---|
190 | try {
|
---|
191 | manager.RemoveUserGroup(manager.GetUserGroupByName("Projektmitglieder").Id);
|
---|
192 | }
|
---|
193 | catch (Exception ex) {
|
---|
194 | Debug.WriteLine(ex.Message);
|
---|
195 | }
|
---|
196 |
|
---|
197 | User user = new User();
|
---|
198 | user.Login = "hive";
|
---|
199 | user.SetHashedPassword("hive");
|
---|
200 | user.Name = "HIVE User";
|
---|
201 | Debug.WriteLine("Adding user...");
|
---|
202 | try {
|
---|
203 | user = manager.AddNewUser(user);
|
---|
204 | }
|
---|
205 | catch (Exception ex) {
|
---|
206 | Debug.WriteLine(ex.Message);
|
---|
207 | }
|
---|
208 |
|
---|
209 | UserGroup grp = new UserGroup();
|
---|
210 | grp.Name = "Projektmitglieder";
|
---|
211 | grp.Members.Add(user);
|
---|
212 | Debug.WriteLine("Adding group...");
|
---|
213 | try {
|
---|
214 | manager.AddNewUserGroup(grp);
|
---|
215 | }
|
---|
216 | catch (Exception ex) {
|
---|
217 | Debug.WriteLine(ex.Message);
|
---|
218 | }
|
---|
219 | Debug.WriteLine("Grant permissions...");
|
---|
220 | try {
|
---|
221 | manager.GrantPermission(grp.Id, pc[pc.Convert(HivePermissions.Usermanagement.Client.Read)].Id, Guid.Empty);
|
---|
222 | manager.GrantPermission(grp.Id, pc[pc.Convert(HivePermissions.Usermanagement.ClientGroup.Read)].Id, Guid.Empty);
|
---|
223 | }
|
---|
224 | catch (Exception ex) {
|
---|
225 | Debug.WriteLine(ex.Message);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | public void InsertSuperUser() {
|
---|
230 | Debug.WriteLine("Revoke permissions...");
|
---|
231 | try {
|
---|
232 | UserGroup group = manager.GetUserGroupByName("Administratoren");
|
---|
233 | foreach (Permission item in HivePermissions.GetPermissions()) {
|
---|
234 | manager.RevokePermission(group.Id, item.Id, Guid.Empty);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | catch (Exception ex) {
|
---|
238 | Debug.WriteLine(ex.Message);
|
---|
239 | }
|
---|
240 | Debug.WriteLine("Revoke permissions from admin...");
|
---|
241 | try {
|
---|
242 | Guid g = manager.GetUserByName("HIVE Admin").Id;
|
---|
243 | foreach (Permission item in HivePermissions.GetPermissions()) {
|
---|
244 | manager.RevokePermission(g, item.Id, Guid.Empty);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | catch (Exception ex) {
|
---|
248 | Debug.WriteLine(ex.Message);
|
---|
249 | }
|
---|
250 | Debug.WriteLine("Removing user...");
|
---|
251 | try {
|
---|
252 | manager.RemoveUser(manager.GetUserByName("HIVE Admin").Id);
|
---|
253 | }
|
---|
254 | catch (Exception ex) {
|
---|
255 | Debug.WriteLine(ex.Message);
|
---|
256 | }
|
---|
257 | Debug.WriteLine("Removing group...");
|
---|
258 | try {
|
---|
259 | manager.RemoveUserGroup(manager.GetUserGroupByName("Administratoren").Id);
|
---|
260 | }
|
---|
261 | catch (Exception ex) {
|
---|
262 | Debug.WriteLine(ex.Message);
|
---|
263 | }
|
---|
264 |
|
---|
265 | User user = new User();
|
---|
266 | user.Login = "admin";
|
---|
267 | user.SetHashedPassword("admin");
|
---|
268 | user.Name = "HIVE Admin";
|
---|
269 | Debug.WriteLine("Grant ALL permissions to admin...");
|
---|
270 | foreach (Permission item in HivePermissions.GetPermissions()) {
|
---|
271 | try {
|
---|
272 | manager.GrantPermission(user.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 | }
|
---|