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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 22 | import org.onosproject.app.ApplicationAdminService; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.core.Application; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.security.SecurityAdminService; |
| 27 | import org.onosproject.security.SecurityUtil; |
| 28 | |
| 29 | import java.security.Permission; |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Application security policy review commands. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 37 | @Service |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 38 | @Command(scope = "onos", name = "review", |
| 39 | description = "Application security policy review interface") |
| 40 | public class ReviewCommand extends AbstractShellCommand { |
| 41 | |
| 42 | @Argument(index = 0, name = "name", description = "Application name", |
| 43 | required = true, multiValued = false) |
| 44 | String name = null; |
| 45 | |
| 46 | @Argument(index = 1, name = "accept", description = "Option to accept policy", |
| 47 | required = false, multiValued = false) |
| 48 | String accept = null; |
| 49 | |
| 50 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 51 | protected void doExecute() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 52 | ApplicationAdminService applicationAdminService = get(ApplicationAdminService.class); |
| 53 | ApplicationId appId = applicationAdminService.getId(name); |
| 54 | if (appId == null) { |
| 55 | print("No such application: %s", name); |
| 56 | return; |
| 57 | } |
| 58 | Application app = applicationAdminService.getApplication(appId); |
| 59 | SecurityAdminService smService = SecurityUtil.getSecurityService(); |
| 60 | if (smService == null) { |
| 61 | print("Security Mode is disabled"); |
| 62 | return; |
| 63 | } |
| 64 | if (accept == null) { |
| 65 | smService.review(appId); |
| 66 | printPolicy(smService, app); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 67 | } else if ("accept".equals(accept.trim())) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 68 | smService.acceptPolicy(appId); |
| 69 | printPolicy(smService, app); |
| 70 | } else { |
| 71 | print("Unknown command"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private void printPolicy(SecurityAdminService smService, Application app) { |
| 76 | print("\n*******************************"); |
| 77 | print(" SM-ONOS APP REVIEW "); |
| 78 | print("*******************************"); |
| 79 | |
| 80 | print("Application name: %s ", app.id().name()); |
| 81 | print("Application role: " + app.role()); |
| 82 | print("\nDeveloper specified permissions: "); |
| 83 | printMap(smService.getPrintableSpecifiedPermissions(app.id())); |
| 84 | print("\nPermissions granted: "); |
| 85 | printMap(smService.getPrintableGrantedPermissions(app.id())); |
| 86 | print("\nAdditional permissions requested on runtime (POLICY VIOLATIONS): "); |
| 87 | printMap(smService.getPrintableRequestedPermissions(app.id())); |
| 88 | print(""); |
| 89 | |
| 90 | } |
| 91 | private void printMap(Map<Integer, List<Permission>> assortedMap) { |
| 92 | for (Integer type : assortedMap.keySet()) { |
| 93 | switch (type) { |
| 94 | case 0: |
| 95 | for (Permission perm: assortedMap.get(0)) { |
| 96 | print("\t[APP PERMISSION] " + perm.getName()); |
| 97 | } |
| 98 | break; |
| 99 | case 1: |
| 100 | for (Permission perm: assortedMap.get(1)) { |
| 101 | print("\t[NB-ADMIN SERVICE] " + perm.getName() + "(" + perm.getActions() + ")"); |
| 102 | } |
| 103 | break; |
| 104 | case 2: |
| 105 | for (Permission perm: assortedMap.get(2)) { |
| 106 | print("\t[NB SERVICE] " + perm.getName() + "(" + perm.getActions() + ")"); |
| 107 | } |
| 108 | break; |
| 109 | case 3: |
| 110 | for (Permission perm: assortedMap.get(3)) { |
| 111 | print("\t[Other SERVICE] " + perm.getName() + "(" + perm.getActions() + ")"); |
| 112 | } |
| 113 | break; |
| 114 | case 4: |
| 115 | for (Permission perm: assortedMap.get(4)) { |
| 116 | print("\t[Other] " + perm.getClass().getSimpleName() + |
| 117 | " " + perm.getName() + " (" + perm.getActions() + ")"); |
| 118 | } |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 119 | break; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |