Move trailing comments
[aaa.git] / aaa-authn-api / src / main / java / org / opendaylight / aaa / api / StoreBuilder.java
1 /*
2  * Copyright (c) 2014, 2016 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.aaa.api;
9
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
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;
22
23 /**
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:
28  * <ol>
29  * <li>admin</li>
30  * <li>user</li>
31  * </ol>
32  *
33  * <p>By default, the following domain is created:
34  * <ol>
35  * <li>sdn</li>
36  * </ol>
37  *
38  * <p>By default, the following grants are created:
39  * <ol>
40  * <li>admin with admin role on sdn</li>
41  * <li>admin with user role on sdn</li>
42  * </ol>
43  *
44  * @author peter.mellquist@hp.com
45  * @author saichler@cisco.com
46  * @author Michael Vorburger.ch - some refactoring, for new CLI tool
47  */
48 public class StoreBuilder {
49
50     private static final Logger LOG = LoggerFactory.getLogger(StoreBuilder.class);
51
52     private final IIDMStore store;
53
54     public StoreBuilder(final IIDMStore store) {
55         this.store = store;
56     }
57
58     /**
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).
61      *
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
65      */
66     public String initDomainAndRolesWithoutUsers(final String domainID) throws IDMStoreException {
67         LOG.info("Checking if default entries must be created in IDM store");
68
69         // Check whether the default domain exists. If it exists, then do not
70         // create default data in the store.
71         // FIXME: Address the fact that someone may delete the sdn domain, or make sdn mandatory.
72         Domain defaultDomain = store.readDomain(domainID);
73         if (defaultDomain != null) {
74             LOG.info("Found default domain in IDM store, skipping insertion of default data");
75             return null;
76         }
77
78         // Create default domain
79         Domain domain = new Domain();
80         domain.setEnabled(true);
81         domain.setName(IIDMStore.DEFAULT_DOMAIN);
82         domain.setDescription("default odl sdn domain");
83         domain = store.writeDomain(domain);
84         LOG.info("Created default domain");
85         String newDomainID = domain.getDomainid();
86
87         // Create default Roles ("admin" and "user")
88         Role adminRole = new Role();
89         adminRole.setName("admin");
90         adminRole.setDomainid(newDomainID);
91         adminRole.setDescription("a role for admins");
92         store.writeRole(adminRole);
93         LOG.info("Created 'admin' role");
94
95         Role userRole = new Role();
96         userRole.setName("user");
97         userRole.setDomainid(newDomainID);
98         userRole.setDescription("a role for users");
99         store.writeRole(userRole);
100         LOG.info("Created 'user' role");
101
102         return newDomainID;
103     }
104
105     /**
106      * Initialize IIDMStore with the default domain and the 'user' and 'admin'
107      * roles AND a default admin account (with default password, which is bad practice).
108      *
109      * @param domainID ID (same as name) of the "authentication domain"
110      * @throws IDMStoreException for issues coming from the IIDMStore
111      */
112     public void initWithDefaultUsers(final String domainID) throws IDMStoreException {
113         String newDomainID = initDomainAndRolesWithoutUsers(domainID);
114         if (newDomainID != null) {
115             createUser(newDomainID, "admin", "admin", true);
116         }
117     }
118
119     public List<String> getRoleIDs(final String domainID, final List<String> roleNames) throws IDMStoreException {
120         Map<String, String> roleNameToID = new HashMap<>();
121         List<Role> roles = store.getRoles().getRoles();
122         for (Role role : roles) {
123             if (domainID.equals(role.getDomainid())) {
124                 roleNameToID.put(role.getName(), role.getRoleid());
125             }
126         }
127
128         List<String> roleIDs = new ArrayList<>(roleNames.size());
129         for (String roleName : roleNames) {
130             String roleID = roleNameToID.get(roleName);
131             if (roleID == null) {
132                 throw new IllegalStateException("'" + roleName + "' role not found (in domain '" + domainID + "')");
133             }
134             roleIDs.add(roleID);
135         }
136
137         return roleIDs;
138     }
139
140     /**
141      * Create new user.
142      *
143      * @param domainID ID (same as name) of the "authentication domain"
144      * @param userName new user name (without the domain prefix which gets automatically added)
145      * @param password the new user's initial password
146      * @param roleIDs list of IDs of roles to grant the new user (e.g. ["user", "admin"])
147      *
148      * @return ID of the just newly created user, useful to reference it e.g. in grants
149      * @throws IDMStoreException for issues coming from the IIDMStore
150      */
151     public String createUser(final String domainID, final String userName, final String password,
152             final List<String> roleIDs) throws IDMStoreException {
153         User newUser = new User();
154         newUser.setEnabled(true);
155         newUser.setDomainid(domainID);
156         newUser.setName(userName);
157         newUser.setDescription(userName + " user");
158         newUser.setEmail("");
159         newUser.setPassword(password);
160         newUser = store.writeUser(newUser);
161         LOG.debug("Created '{}' user in domain '{}'", userName, domainID);
162
163         String newUserID = newUser.getUserid();
164         for (String roleID : roleIDs) {
165             createGrant(domainID, newUserID, roleID);
166         }
167         return newUserID;
168     }
169
170     public String createUser(final String domainID, final String userName, final String password, final boolean isAdmin)
171             throws IDMStoreException {
172         List<String> roleIDs;
173         if (isAdmin) {
174             roleIDs = getRoleIDs(domainID, Arrays.asList("user", "admin"));
175         } else {
176             roleIDs = getRoleIDs(domainID, Arrays.asList("user"));
177         }
178         return createUser(domainID, userName, password, roleIDs);
179     }
180
181     public boolean deleteUser(final String domainID, final String userName) throws IDMStoreException {
182         String userID = IDMStoreUtil.createUserid(userName, domainID);
183         // NOT store.getGrants(domainID, userName) !
184         Grants grants = store.getGrants(userID);
185         for (Grant grant : grants.getGrants()) {
186             store.deleteGrant(grant.getGrantid());
187         }
188         return store.deleteUser(userID) != null;
189     }
190
191     private void createGrant(final String domainID, final String userID, final 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 '{}' user the '{}' role in domain '{}'", userID, roleID, domainID);
198     }
199 }