Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.cli.security; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.app.ApplicationAdminService; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.core.Application; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.security.SecurityAdminService; |
| 26 | import org.onosproject.security.SecurityUtil; |
| 27 | |
| 28 | import java.security.Permission; |
| 29 | import java.util.List; |
| 30 | import java.util.Map; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Application security policy review commands. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "review", |
| 37 | description = "Application security policy review interface") |
| 38 | public class ReviewCommand extends AbstractShellCommand { |
| 39 | |
| 40 | @Argument(index = 0, name = "name", description = "Application name", |
| 41 | required = true, multiValued = false) |
| 42 | String name = null; |
| 43 | |
| 44 | @Argument(index = 1, name = "accept", description = "Option to accept policy", |
| 45 | required = false, multiValued = false) |
| 46 | String accept = null; |
| 47 | |
| 48 | @Override |
| 49 | protected void execute() { |
| 50 | ApplicationAdminService applicationAdminService = get(ApplicationAdminService.class); |
| 51 | ApplicationId appId = applicationAdminService.getId(name); |
| 52 | if (appId == null) { |
| 53 | print("No such application: %s", name); |
| 54 | return; |
| 55 | } |
| 56 | Application app = applicationAdminService.getApplication(appId); |
| 57 | SecurityAdminService smService = SecurityUtil.getSecurityService(); |
| 58 | if (smService == null) { |
| 59 | print("Security Mode is disabled"); |
| 60 | return; |
| 61 | } |
| 62 | if (accept == null) { |
| 63 | smService.review(appId); |
| 64 | printPolicy(smService, app); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 65 | } else if ("accept".equals(accept.trim())) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 66 | smService.acceptPolicy(appId); |
| 67 | printPolicy(smService, app); |
| 68 | } else { |
| 69 | print("Unknown command"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private void printPolicy(SecurityAdminService smService, Application app) { |
| 74 | print("\n*******************************"); |
| 75 | print(" SM-ONOS APP REVIEW "); |
| 76 | print("*******************************"); |
| 77 | |
| 78 | print("Application name: %s ", app.id().name()); |
| 79 | print("Application role: " + app.role()); |
| 80 | print("\nDeveloper specified permissions: "); |
| 81 | printMap(smService.getPrintableSpecifiedPermissions(app.id())); |
| 82 | print("\nPermissions granted: "); |
| 83 | printMap(smService.getPrintableGrantedPermissions(app.id())); |
| 84 | print("\nAdditional permissions requested on runtime (POLICY VIOLATIONS): "); |
| 85 | printMap(smService.getPrintableRequestedPermissions(app.id())); |
| 86 | print(""); |
| 87 | |
| 88 | } |
| 89 | private void printMap(Map<Integer, List<Permission>> assortedMap) { |
| 90 | for (Integer type : assortedMap.keySet()) { |
| 91 | switch (type) { |
| 92 | case 0: |
| 93 | for (Permission perm: assortedMap.get(0)) { |
| 94 | print("\t[APP PERMISSION] " + perm.getName()); |
| 95 | } |
| 96 | break; |
| 97 | case 1: |
| 98 | for (Permission perm: assortedMap.get(1)) { |
| 99 | print("\t[NB-ADMIN SERVICE] " + perm.getName() + "(" + perm.getActions() + ")"); |
| 100 | } |
| 101 | break; |
| 102 | case 2: |
| 103 | for (Permission perm: assortedMap.get(2)) { |
| 104 | print("\t[NB SERVICE] " + perm.getName() + "(" + perm.getActions() + ")"); |
| 105 | } |
| 106 | break; |
| 107 | case 3: |
| 108 | for (Permission perm: assortedMap.get(3)) { |
| 109 | print("\t[Other SERVICE] " + perm.getName() + "(" + perm.getActions() + ")"); |
| 110 | } |
| 111 | break; |
| 112 | case 4: |
| 113 | for (Permission perm: assortedMap.get(4)) { |
| 114 | print("\t[Other] " + perm.getClass().getSimpleName() + |
| 115 | " " + perm.getName() + " (" + perm.getActions() + ")"); |
| 116 | } |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 117 | break; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |