blob: 1d7e32c700ef840bd0803d56b2e1f7dd6221c2a2 [file] [log] [blame]
Changhoon Yoonb856b812015-08-10 03:47:19 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
Changhoon Yoonb856b812015-08-10 03:47:19 +090022import org.onosproject.app.ApplicationAdminService;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.core.Application;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.security.SecurityAdminService;
27import org.onosproject.security.SecurityUtil;
28
29import java.security.Permission;
30import java.util.List;
31import java.util.Map;
32
33
34/**
35 * Application security policy review commands.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Service
Changhoon Yoonb856b812015-08-10 03:47:19 +090038@Command(scope = "onos", name = "review",
39 description = "Application security policy review interface")
40public 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 Milkeyd84f89b2018-08-17 14:54:17 -070051 protected void doExecute() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090052 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 Halla3fcf672017-03-28 16:53:22 -070067 } else if ("accept".equals(accept.trim())) {
Changhoon Yoonb856b812015-08-10 03:47:19 +090068 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 Milkey4fd3ceb2015-12-10 14:43:08 -0800119 break;
Changhoon Yoonb856b812015-08-10 03:47:19 +0900120 default:
121 break;
122 }
123 }
124 }
125}