Skip to content

Commit

Permalink
make sure the local admin user always has the builtin Admin role
Browse files Browse the repository at this point in the history
  • Loading branch information
kroepke committed Aug 19, 2015
1 parent baeb147 commit 3d5397f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
Expand Up @@ -47,4 +47,6 @@ public interface RoleService {
* @return the number of deleted roles * @return the number of deleted roles
*/ */
int delete(String roleName); int delete(String roleName);

String getAdminRoleObjectId();
} }
Expand Up @@ -59,6 +59,7 @@ public class RoleServiceImpl implements RoleService {


private final JacksonDBCollection<RoleImpl, ObjectId> dbCollection; private final JacksonDBCollection<RoleImpl, ObjectId> dbCollection;
private final Validator validator; private final Validator validator;
private final String adminRoleObjectId;


@Inject @Inject
protected RoleServiceImpl(MongoConnection mongoConnection, protected RoleServiceImpl(MongoConnection mongoConnection,
Expand All @@ -75,16 +76,16 @@ protected RoleServiceImpl(MongoConnection mongoConnection,
dbCollection.createIndex(new BasicDBObject(NAME_LOWER, 1), new BasicDBObject("unique", true)); dbCollection.createIndex(new BasicDBObject(NAME_LOWER, 1), new BasicDBObject("unique", true));


// make sure the two built-in roles actually exist // make sure the two built-in roles actually exist
ensureBuiltinRole(ADMIN_ROLENAME, Sets.newHashSet("*"), "Admin", adminRoleObjectId = ensureBuiltinRole(ADMIN_ROLENAME, Sets.newHashSet("*"), "Admin",
"Grants all permissions for Graylog administrators (built-in)"); "Grants all permissions for Graylog administrators (built-in)");
ensureBuiltinRole(READER_ROLENAME, RestPermissions.READER_BASE_PERMISSIONS, "Reader", ensureBuiltinRole(READER_ROLENAME, RestPermissions.READER_BASE_PERMISSIONS, "Reader",
"Grants basic permissions for every Graylog user (built-in)"); "Grants basic permissions for every Graylog user (built-in)");


} }


private void ensureBuiltinRole(String roleName, private String ensureBuiltinRole(String roleName,
Set<String> expectedPermissions, Set<String> expectedPermissions,
String name, String description) { String name, String description) {
RoleImpl previousRole = null; RoleImpl previousRole = null;
try { try {
previousRole = load(roleName); previousRole = load(roleName);
Expand All @@ -104,11 +105,17 @@ private void ensureBuiltinRole(String roleName,
fixedAdmin.setDescription(description); fixedAdmin.setDescription(description);
fixedAdmin.setPermissions(expectedPermissions); fixedAdmin.setPermissions(expectedPermissions);
try { try {
save(fixedAdmin); final RoleImpl savedRole = save(fixedAdmin);
return savedRole.getId();
} catch (DuplicateKeyException | ValidationException e) { } catch (DuplicateKeyException | ValidationException e) {
log.error("Unable to save fixed " + roleName + " role, please restart Graylog to fix this.", e); log.error("Unable to save fixed " + roleName + " role, please restart Graylog to fix this.", e);
} }
} }
if (previousRole == null) {
log.error("Unable to access fixed " + roleName + " role, please restart Graylog to fix this.");
return null;
}
return previousRole.getId();
} }


@Override @Override
Expand Down Expand Up @@ -185,5 +192,8 @@ public int delete(String roleName) {
return dbCollection.remove(nameMatchesAndNotReadonly).getN(); return dbCollection.remove(nameMatchesAndNotReadonly).getN();
} }



@Override
public String getAdminRoleObjectId() {
return adminRoleObjectId;
}
} }
Expand Up @@ -18,6 +18,7 @@


import com.google.common.collect.Collections2; import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
Expand Down Expand Up @@ -286,10 +287,12 @@ public void setStartpage(final String type, final String id) {


public static class LocalAdminUser extends UserImpl { public static class LocalAdminUser extends UserImpl {
private final Configuration configuration; private final Configuration configuration;
private final Set<String> roles;


public LocalAdminUser(Configuration configuration) { public LocalAdminUser(Configuration configuration, String adminRoleObjectId) {
super(null, Collections.<String, Object>emptyMap()); super(null, Collections.<String, Object>emptyMap());
this.configuration = configuration; this.configuration = configuration;
this.roles = ImmutableSet.of(adminRoleObjectId);
} }


@Override @Override
Expand Down Expand Up @@ -349,7 +352,7 @@ public boolean isLocalAdmin() {
@Nonnull @Nonnull
@Override @Override
public Set<String> getRoleIds() { public Set<String> getRoleIds() {
return Collections.emptySet(); return roles;
} }


@Override @Override
Expand Down
Expand Up @@ -72,7 +72,7 @@ public User load(final String username) {
// special case for the locally defined user, we don't store that in MongoDB. // special case for the locally defined user, we don't store that in MongoDB.
if (configuration.getRootUsername().equals(username)) { if (configuration.getRootUsername().equals(username)) {
LOG.debug("User {} is the built-in admin user", username); LOG.debug("User {} is the built-in admin user", username);
return new UserImpl.LocalAdminUser(configuration); return new UserImpl.LocalAdminUser(configuration, roleService.getAdminRoleObjectId());
} }


final DBObject query = new BasicDBObject(); final DBObject query = new BasicDBObject();
Expand Down Expand Up @@ -219,7 +219,7 @@ public <T extends Persisted> String save(T model) throws ValidationException {


@Override @Override
public User getAdminUser() { public User getAdminUser() {
return new UserImpl.LocalAdminUser(configuration); return new UserImpl.LocalAdminUser(configuration, roleService.getAdminRoleObjectId());
} }


@Override @Override
Expand Down

0 comments on commit 3d5397f

Please sign in to comment.