2 * Copyright (c) 2014, 2016 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.aaa.api;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.HashMap;
13 import java.util.List;
15 import org.opendaylight.aaa.api.model.Domain;
16 import org.opendaylight.aaa.api.model.Grant;
17 import org.opendaylight.aaa.api.model.Grants;
18 import org.opendaylight.aaa.api.model.Role;
19 import org.opendaylight.aaa.api.model.User;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * StoreBuilder is triggered during feature installation by
25 * <code>AAAIDMLightModule.createInstance()</code>. StoreBuilder is responsible
26 * for initializing the IIDMStore with initial default user account
27 * information. By default, the following users are created:
33 * <p>By default, the following domain is created:
38 * <p>By default, the following grants are created:
40 * <li>admin with admin role on sdn</li>
41 * <li>admin with user role on sdn</li>
44 * @author peter.mellquist@hp.com
45 * @author saichler@cisco.com
46 * @author Michael Vorburger.ch - some refactoring, for new CLI tool
48 public class StoreBuilder {
50 private static final Logger LOG = LoggerFactory.getLogger(StoreBuilder.class);
52 private final IIDMStore store;
54 public StoreBuilder(IIDMStore store) {
59 * Initialize IIDMStore with the default domain and the 'user' and 'admin' roles, if needed.
60 * This does not create any default user entries (because they are an inherent security risk).
62 * @param domainID ID (same as name) of the "authentication domain"
63 * @return ID of the just newly created Domain, or null if no new one had to be created
64 * @throws IDMStoreException for issues coming from the IIDMStore
66 public String initDomainAndRolesWithoutUsers(String domainID) throws IDMStoreException {
67 LOG.info("Checking if default entries must be created in IDM store");
69 // Check whether the default domain exists. If it exists, then do not
70 // create default data in the store.
71 // TODO Address the fact that someone may delete the sdn domain, or make
73 Domain defaultDomain = store.readDomain(domainID);
74 if (defaultDomain != null) {
75 LOG.info("Found default domain in IDM store, skipping insertion of default data");
79 // Create default domain
80 Domain domain = new Domain();
81 domain.setEnabled(true);
82 domain.setName(IIDMStore.DEFAULT_DOMAIN);
83 domain.setDescription("default odl sdn domain");
84 domain = store.writeDomain(domain);
85 LOG.info("Created default domain");
86 String newDomainID = domain.getDomainid();
88 // Create default Roles ("admin" and "user")
89 Role adminRole = new Role();
90 adminRole.setName("admin");
91 adminRole.setDomainid(newDomainID);
92 adminRole.setDescription("a role for admins");
93 store.writeRole(adminRole);
94 LOG.info("Created 'admin' role");
96 Role userRole = new Role();
97 userRole.setName("user");
98 userRole.setDomainid(newDomainID);
99 userRole.setDescription("a role for users");
100 store.writeRole(userRole);
101 LOG.info("Created 'user' role");
107 * Initialize IIDMStore with the default domain and the 'user' and 'admin'
108 * roles AND a default admin account (with default password, which is bad practice).
110 * @param domainID ID (same as name) of the "authentication domain"
111 * @throws IDMStoreException for issues coming from the IIDMStore
113 public void initWithDefaultUsers(String domainID) throws IDMStoreException {
114 String newDomainID = initDomainAndRolesWithoutUsers(domainID);
115 if (newDomainID != null) {
116 createUser(newDomainID, "admin", "admin", true);
120 public List<String> getRoleIDs(String domainID, List<String> roleNames) throws IDMStoreException {
121 Map<String, String> roleNameToID = new HashMap<>();
122 List<Role> roles = store.getRoles().getRoles();
123 for (Role role : roles) {
124 if (domainID.equals(role.getDomainid())) {
125 roleNameToID.put(role.getName(), role.getRoleid());
129 List<String> roleIDs = new ArrayList<>(roleNames.size());
130 for (String roleName : roleNames) {
131 String roleID = roleNameToID.get(roleName);
132 if (roleID == null) {
133 throw new IllegalStateException("'" + roleName + "' role not found (in domain '" + domainID + "')");
144 * @param domainID ID (same as name) of the "authentication domain"
145 * @param userName new user name (without the domain prefix which gets automatically added)
146 * @param password the new user's initial password
147 * @param roleIDs list of IDs of roles to grant the new user (e.g. ["user", "admin"])
149 * @return ID of the just newly created user, useful to reference it e.g. in grants
150 * @throws IDMStoreException for issues coming from the IIDMStore
152 public String createUser(String domainID, String userName, String password, List<String> roleIDs)
153 throws IDMStoreException {
154 User newUser = new User();
155 newUser.setEnabled(true);
156 newUser.setDomainid(domainID);
157 newUser.setName(userName);
158 newUser.setDescription(userName + " user");
159 newUser.setEmail("");
160 newUser.setPassword(password);
161 newUser = store.writeUser(newUser);
162 LOG.debug("Created '" + userName + "' user in domain '" + domainID + "'");
164 String newUserID = newUser.getUserid();
165 for (String roleID : roleIDs) {
166 createGrant(domainID, newUserID, roleID);
171 public String createUser(String domainID, String userName, String password, boolean isAdmin)
172 throws IDMStoreException {
173 List<String> roleIDs;
175 roleIDs = getRoleIDs(domainID, Arrays.asList("user", "admin"));
177 roleIDs = getRoleIDs(domainID, Arrays.asList("user"));
179 return createUser(domainID, userName, password, roleIDs);
182 public boolean deleteUser(String domainID, String userName) throws IDMStoreException {
183 String userID = IDMStoreUtil.createUserid(userName, domainID);
184 Grants grants = store.getGrants(userID); // NOT store.getGrants(domainID, userName)
185 for (Grant grant : grants.getGrants()) {
186 store.deleteGrant(grant.getGrantid());
188 return store.deleteUser(userID) != null;
191 private void createGrant(String domainID, String userID, String roleID) throws IDMStoreException {
192 Grant grant = new Grant();
193 grant.setDomainid(domainID);
194 grant.setUserid(userID);
195 grant.setRoleid(roleID);
196 store.writeGrant(grant);
197 LOG.debug("Granted '" + userID + "' user the '" + roleID + "' role in domain '" + domainID + "'");