SchemaUpgrade { @VisibleForTesting static @Nullable Set<String> upgradePermissions(Set<String> permissions) { Set<String> updatedPermissions = new HashSet<>(); ListMultimap<String, String> agentPermissions = ArrayListMultimap.create(); boolean needsUpgrade = false; for (String permission : permissions) { if (permission.startsWith("agent:")) { PermissionParser parser = new PermissionParser(permission); parser.parse(); String perm = parser.getPermission(); agentPermissions.put( PermissionParser.quoteIfNeededAndJoin(parser.getAgentRollupIds()), perm); if (perm.equals("agent:view")) { needsUpgrade = true; } } else if (permission.equals("admin") || permission.startsWith("admin:")) { updatedPermissions.add(permission); } else { logger.error("unexpected permission: {}", permission); } } if (!needsUpgrade) { return null; } for (Map.Entry<String, List<String>> entry : Multimaps.asMap(agentPermissions).entrySet()) { List<String> perms = entry.getValue(); PermissionParser.upgradeAgentPermissionsFrom_0_9_1_to_0_9_2(perms); for (String perm : perms) { updatedPermissions .add("agent:" + entry.getKey() + ":" + perm.substring("agent:".length())); } } if (updatedPermissions.contains("admin:view") && updatedPermissions.contains("admin:edit")) { updatedPermissions.remove("admin:view"); updatedPermissions.remove("admin:edit"); updatedPermissions.add("admin"); } return updatedPermissions; } SchemaUpgrade(Session session, Clock clock, boolean servlet); @Nullable Integer getInitialSchemaVersion(); void upgrade(); boolean reloadCentralConfiguration(); void updateSchemaVersionToCurent(); int getCurrentSchemaVersion(); void updateToMoreRecentCassandraOptions(CentralStorageConfig storageConfig); static AgentConfig upgradeOldAgentConfig(AgentConfig oldAgentConfig);  }
@Test public void shouldUpgradeWildcardPermissions() throws Exception { Set<String> permissions = ImmutableSet.of("agent:*:view"); permissions = SchemaUpgrade.upgradePermissions(permissions); assertThat(Ordering.natural().sortedCopy(permissions)).containsExactly( "agent:*:error", "agent:*:jvm:environment", "agent:*:jvm:gauges", "agent:*:transaction"); }
@Test public void shouldUpgradeSingleAgentPermissions() throws Exception { Set<String> permissions = ImmutableSet.of("agent:abc:view"); permissions = SchemaUpgrade.upgradePermissions(permissions); assertThat(Ordering.natural().sortedCopy(permissions)).containsExactly("agent:abc:error", "agent:abc:jvm:environment", "agent:abc:jvm:gauges", "agent:abc:transaction"); }
@Test public void shouldUpgradeMultiAgentPermissions() throws Exception { Set<String> permissions = ImmutableSet.of("agent:abc,mno,xyz:view"); permissions = SchemaUpgrade.upgradePermissions(permissions); assertThat(Ordering.natural().sortedCopy(permissions)).containsExactly( "agent:abc,mno,xyz:error", "agent:abc,mno,xyz:jvm:environment", "agent:abc,mno,xyz:jvm:gauges", "agent:abc,mno,xyz:transaction"); }