blob: 0ebd2836905dc125141f65fa7e775195a28c885c [file] [log] [blame]
Changhoon Yoonb856b812015-08-10 03:47:19 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Changhoon Yoonb856b812015-08-10 03:47:19 +09003 *
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
17package org.onosproject.cli.security;
18
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.app.ApplicationAdminService;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.core.Application;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.security.SecurityAdminService;
26import org.onosproject.security.SecurityUtil;
27
28import java.security.Permission;
29import java.util.List;
30import 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")
38public 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 Halla3fcf672017-03-28 16:53:22 -070065 } else if ("accept".equals(accept.trim())) {
Changhoon Yoonb856b812015-08-10 03:47:19 +090066 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 Milkey4fd3ceb2015-12-10 14:43:08 -0800117 break;
Changhoon Yoonb856b812015-08-10 03:47:19 +0900118 default:
119 break;
120 }
121 }
122 }
123}