2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.admin;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
29 import javax.servlet.ServletConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.annotation.WebInitParam;
32 import javax.servlet.annotation.WebServlet;
33 import javax.servlet.http.HttpServlet;
36 import org.eclipse.jgit.api.Git;
37 import org.eclipse.jgit.api.errors.GitAPIException;
38 import org.eclipse.jgit.lib.Repository;
39 import org.eclipse.jgit.lib.StoredConfig;
40 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
41 import org.hibernate.Session;
42 import org.hibernate.SessionFactory;
43 import org.openecomp.policy.rest.XACMLRest;
44 import org.openecomp.policy.rest.XACMLRestProperties;
45 import org.openecomp.policy.rest.dao.UserInfoDao;
46 import org.openecomp.policy.rest.jpa.UserInfo;
47 import org.openecomp.policy.rest.util.Webapps;
48 import org.openecomp.policy.xacml.api.pap.PAPPolicyEngine;
49 import org.springframework.beans.factory.annotation.Autowired;
51 import com.att.research.xacml.util.XACMLProperties;
52 import com.google.common.base.Splitter;
56 public class XacmlAdminUI extends HttpServlet implements PAPNotificationBroadcaster.PAPNotificationBroadcastListener{
58 private static final long serialVersionUID = 1L;
62 private PAPPolicyEngine papEngine;
63 private static Path repositoryPath;
64 private static Repository repository;
67 UserInfoDao userInfoDao;
70 SessionFactory sessionfactory;
72 @WebServlet(value = "/policy#/*", description = "XACML Admin Console", asyncSupported = true, loadOnStartup = 1, initParams = { @WebInitParam(name = "XACML_PROPERTIES_NAME", value = "xacml.admin.properties", description = "The location of the properties file holding configuration information.") })
73 public static class Servlet extends HttpServlet {
74 private static final long serialVersionUID = -5274600248961852835L;
77 public void init(ServletConfig servletConfig) throws ServletException {
78 super.init(servletConfig);
80 // Common initialization
82 XACMLRest.xacmlInit(servletConfig);
84 // Initialize GIT repository.
86 XacmlAdminUI.initializeGitRepository();
89 // The webapps Action and Config are read when getActionHome or getConfigHome are called
92 } catch (Exception e) {
93 throw new ServletException(e);
100 public void destroy() {
101 if (XacmlAdminUI.repository != null) {
102 XacmlAdminUI.repository.close();
108 private static void initializeGitRepository() throws ServletException {
111 XacmlAdminUI.repositoryPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_REPOSITORY));
112 } catch (Exception e) {
113 XACMLProperties.reloadProperties();
114 XacmlAdminUI.repositoryPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_REPOSITORY));
116 FileRepositoryBuilder builder = new FileRepositoryBuilder();
118 XacmlAdminUI.repository = builder.setGitDir(XacmlAdminUI.repositoryPath.toFile()).readEnvironment().findGitDir().setBare().build();
119 if (Files.notExists(XacmlAdminUI.repositoryPath)|| Files.notExists(Paths.get(XacmlAdminUI.repositoryPath.toString(), "HEAD"))) {
121 // Create it if it doesn't exist. As a bare repository
122 XacmlAdminUI.repository.create();
124 // Add the magic file so remote works.
126 Path daemon = Paths.get(XacmlAdminUI.repositoryPath.toString(), "git-daemon-export-ok");
127 Files.createFile(daemon);
129 } catch (IOException e) {
130 throw new ServletException(e.getMessage(), e.getCause());
133 // Make sure the workspace directory is created
135 Path workspace = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_WORKSPACE));
136 workspace = workspace.toAbsolutePath();
137 if (Files.notExists(workspace)) {
139 Files.createDirectory(workspace);
140 } catch (IOException e) {
141 throw new ServletException(e.getMessage(), e.getCause());
145 // Create the user workspace directory
147 workspace = Paths.get(workspace.toString(), "admin");
149 if (Files.notExists(workspace)) {
151 Files.createDirectory(workspace);
152 } catch (IOException e) {
153 throw new ServletException(e.getMessage(), e.getCause());
157 // Get the path to where the repository is going to be
159 Path gitPath = Paths.get(workspace.toString(), XacmlAdminUI.repositoryPath.getFileName().toString());
160 if (Files.notExists(gitPath)) {
162 Files.createDirectory(gitPath);
163 } catch (IOException e) {
164 throw new ServletException(e.getMessage(), e.getCause());
168 // Initialize the domain structure
171 String domain = XacmlAdminUI.getDomain();
172 if (domain != null) {
173 for (String part : Splitter.on(':').trimResults().split(domain)) {
177 Path subdir = Paths.get(gitPath.toString(), part);
178 if (Files.notExists(subdir)) {
180 Files.createDirectory(subdir);
181 Files.createFile(Paths.get(subdir.toString(), ".svnignore"));
182 } catch (IOException e) {
183 throw new ServletException(e.getMessage(), e.getCause());
189 Files.createFile(Paths.get(workspace.toString(), ".svnignore"));
191 } catch (IOException e) {
192 throw new ServletException(e.getMessage(), e.getCause());
197 // These are the sequence of commands that must be done initially to
198 // finish setting up the remote bare repository.
200 Git git = Git.init().setDirectory(gitPath.toFile()).setBare(false).call();
201 git.add().addFilepattern(base).call();
202 git.commit().setMessage("Initialize Bare Repository").call();
203 StoredConfig config = git.getRepository().getConfig();
204 config.setString("remote", "origin", "url", XacmlAdminUI.repositoryPath.toAbsolutePath().toString());
205 config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
207 git.push().setRemote("origin").add("master").call();
209 * This will not work unless
210 * git.push().setRemote("origin").add("master").call(); is called
211 * first. Otherwise it throws an exception. However, if the push()
212 * is called then calling this function seems to add nothing.
214 * git.branchCreate().setName("master")
215 * .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
216 * .setStartPoint("origin/master").setForce(true).call();
218 } catch (GitAPIException | IOException e) {
219 throw new ServletException(e.getMessage(), e.getCause());
223 public UserInfo getUserNameFromUserInfoTable(String createdBy){
224 String loginId = createdBy;
226 Session session = sessionfactory.openSession();
227 user = session.load(UserInfo.class, loginId);
228 return (UserInfo) user;
232 public void updateAllGroups() {
236 public PAPPolicyEngine getPapEngine() {
240 public void setPapEngine(PAPPolicyEngine papEngine) {
241 this.papEngine = papEngine;
244 public static String getConfigHome() {
245 return Webapps.getConfigHome();
248 public static String getDomain() {
249 return XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_DOMAIN, "urn");
252 // get the repository path from property file
253 public static Path getRepositoryPath() {
254 if(repositoryPath == null){
256 initializeGitRepository();
257 } catch (ServletException e) {
261 return repositoryPath;