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.security; |
| 18 | |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 20 | import org.onlab.osgi.DefaultServiceDirectory; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onlab.osgi.ServiceNotFoundException; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | |
| 25 | /** |
| 26 | * Utility class to aid Security-Mode ONOS. |
| 27 | */ |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 28 | @Beta |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 29 | public final class SecurityUtil { |
| 30 | |
| 31 | protected static ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 32 | |
| 33 | private SecurityUtil() { |
| 34 | } |
| 35 | |
| 36 | public static boolean isSecurityModeEnabled() { |
| 37 | if (System.getSecurityManager() != null) { |
| 38 | try { |
| 39 | SecurityAdminService securityService = serviceDirectory.get(SecurityAdminService.class); |
| 40 | if (securityService != null) { |
| 41 | return true; |
| 42 | } |
| 43 | } catch (ServiceNotFoundException e) { |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | public static SecurityAdminService getSecurityService() { |
| 51 | if (System.getSecurityManager() != null) { |
| 52 | try { |
| 53 | SecurityAdminService securityService = serviceDirectory.get(SecurityAdminService.class); |
| 54 | if (securityService != null) { |
| 55 | return securityService; |
| 56 | } |
| 57 | } catch (ServiceNotFoundException e) { |
| 58 | return null; |
| 59 | } |
| 60 | } |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | public static boolean isAppSecured(ApplicationId appId) { |
| 65 | SecurityAdminService service = getSecurityService(); |
| 66 | if (service != null) { |
| 67 | if (!service.isSecured(appId)) { |
| 68 | System.out.println("\n*******************************"); |
| 69 | System.out.println(" SM-ONOS APP WARNING "); |
| 70 | System.out.println("*******************************"); |
| 71 | System.out.println(appId.name() + " has not been secured."); |
| 72 | System.out.println("Please review before activating."); |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | public static void register(ApplicationId appId) { |
| 79 | SecurityAdminService service = getSecurityService(); |
| 80 | if (service != null) { |
| 81 | service.register(appId); |
| 82 | } |
| 83 | } |
| 84 | } |