|
| 1 | +package org.infinispan.tutorial.simple.security; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.security.Principal; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.UUID; |
| 8 | + |
| 9 | +import javax.security.auth.Subject; |
| 10 | + |
| 11 | +import org.infinispan.Cache; |
| 12 | +import org.infinispan.commons.api.CacheContainerAdmin; |
| 13 | +import org.infinispan.configuration.cache.CacheMode; |
| 14 | +import org.infinispan.configuration.cache.ConfigurationBuilder; |
| 15 | +import org.infinispan.configuration.global.GlobalConfigurationBuilder; |
| 16 | +import org.infinispan.manager.DefaultCacheManager; |
| 17 | +import org.infinispan.security.AuthorizationPermission; |
| 18 | +import org.infinispan.security.Security; |
| 19 | +import org.infinispan.security.mappers.IdentityRoleMapper; |
| 20 | + |
| 21 | +public class InfinispanCacheSecurity { |
| 22 | + |
| 23 | + public static final String CACHE_NAME = "my-cache"; |
| 24 | + public static final String SECRET_CACHE_NAME = "my-secret-cache"; |
| 25 | + public static final String READ_ONLY_ROLE = "read-only"; |
| 26 | + public static final String WRITE_ONLY_ROLE = "write-only"; |
| 27 | + public static final String ADMIN_ROLE = "admin"; |
| 28 | + public static final String SECRET_ROLE = "secret"; |
| 29 | + |
| 30 | + // These are the users authenticated by the application. |
| 31 | + // The subjects are provided by the application utilizing the caches. |
| 32 | + static final Subject ADMIN_USER = createSubject(ADMIN_ROLE); |
| 33 | + static final Subject SECRET_USER = createSubject(SECRET_ROLE); |
| 34 | + static final Subject READ_ONLY_USER = createSubject(READ_ONLY_ROLE); |
| 35 | + static final Subject WRITE_ONLY_USER = createSubject(WRITE_ONLY_ROLE); |
| 36 | + |
| 37 | + public DefaultCacheManager dcm; |
| 38 | + Cache<String, String> cache; |
| 39 | + Cache<String, String> secretCache; |
| 40 | + |
| 41 | + public static void main(String[] args) { |
| 42 | + InfinispanCacheSecurity security = new InfinispanCacheSecurity(); |
| 43 | + |
| 44 | + // First, create the caches and insert the entries. |
| 45 | + // Each operation is wrapped with the user with the correct permissions. |
| 46 | + security.createDefaultCacheManager(); |
| 47 | + security.createAllCachesAndPopulate(3); |
| 48 | + |
| 49 | + System.out.println("Entries for normal cache:"); |
| 50 | + security.showAllEntriesWithUser(READ_ONLY_USER, security.cache); |
| 51 | + |
| 52 | + System.out.println("\nEntries for secret cache:"); |
| 53 | + security.showAllEntriesWithUser(SECRET_USER, security.secretCache); |
| 54 | + |
| 55 | + // Now, we try to access the secret cache with a user that doesn't have the permission. |
| 56 | + // This operation should throw the SecurityException. |
| 57 | + System.out.println("\nFail to read with incorrect user:"); |
| 58 | + try { |
| 59 | + security.showAllEntriesWithUser(WRITE_ONLY_USER, security.secretCache); |
| 60 | + throw new AssertionError("Should have failed to read with incorrect user"); |
| 61 | + } catch (SecurityException ignore) { |
| 62 | + System.out.println("The user doesn't have permission to operate the secret cache"); |
| 63 | + } |
| 64 | + |
| 65 | + security.stop(); |
| 66 | + } |
| 67 | + |
| 68 | + public void createDefaultCacheManager() { |
| 69 | + // Setup up a clustered cache manager |
| 70 | + GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder(); |
| 71 | + |
| 72 | + // Enable authorization and define the roles and their permissions. |
| 73 | + global.security().authorization().enable() |
| 74 | + // Defines how to map a principal to a role. |
| 75 | + // The identity will simply map the principal name to the role name. |
| 76 | + // For example, user "admin" will map to role "admin", and so on. |
| 77 | + .principalRoleMapper(new IdentityRoleMapper()) |
| 78 | + .groupOnlyMapping(false) |
| 79 | + // Define different roles and associate the permissions. |
| 80 | + .role(ADMIN_ROLE).permission(AuthorizationPermission.ALL) |
| 81 | + .role(SECRET_ROLE).permission(AuthorizationPermission.ALL) |
| 82 | + .role(READ_ONLY_ROLE).permission(AuthorizationPermission.ALL_READ) |
| 83 | + .role(WRITE_ONLY_ROLE).permission(AuthorizationPermission.ALL_WRITE); |
| 84 | + |
| 85 | + // Initialize the cache manager |
| 86 | + // We pass false to not start automatically. |
| 87 | + // Then we call start using the admin user. The user needs LIFECYCLE permission. |
| 88 | + dcm = new DefaultCacheManager(global.build(), false); |
| 89 | + Security.doAs(ADMIN_USER, dcm::start); |
| 90 | + } |
| 91 | + |
| 92 | + public void createAllCachesAndPopulate(int size) { |
| 93 | + // First, create the first cache. |
| 94 | + // This cache is accessible to any role. |
| 95 | + ConfigurationBuilder builder1 = new ConfigurationBuilder(); |
| 96 | + builder1.clustering().cacheMode(CacheMode.DIST_SYNC); |
| 97 | + builder1.security().authorization().enable(); |
| 98 | + |
| 99 | + // We utilize an admin to create the cache. |
| 100 | + cache = Security.doAs(ADMIN_USER, () -> dcm.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE) |
| 101 | + .getOrCreateCache(CACHE_NAME, builder1.build())); |
| 102 | + |
| 103 | + |
| 104 | + // Now we create a cache only accessible to users with the secret role. |
| 105 | + ConfigurationBuilder builder2 = new ConfigurationBuilder(); |
| 106 | + builder2.clustering().cacheMode(CacheMode.DIST_SYNC); |
| 107 | + // The cache must include both roles so it can be stopped by the admin user as well. |
| 108 | + builder2.security().authorization().enable().roles(ADMIN_ROLE, SECRET_ROLE); |
| 109 | + |
| 110 | + // We utilize an admin to create the cache. |
| 111 | + secretCache = Security.doAs(ADMIN_USER, () -> dcm.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE) |
| 112 | + .getOrCreateCache(SECRET_CACHE_NAME, builder2.build())); |
| 113 | + |
| 114 | + // Now populate the caches with the correct user. |
| 115 | + Security.doAs(WRITE_ONLY_USER, () -> { |
| 116 | + for (int i = 0; i < size; i++) { |
| 117 | + cache.put(UUID.randomUUID().toString(), dcm.getNodeAddress()); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + Security.doAs(SECRET_USER, () -> { |
| 122 | + for (int i = 0; i < size; i++) { |
| 123 | + secretCache.put(UUID.randomUUID().toString(), dcm.getNodeAddress()); |
| 124 | + } |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + public void showAllEntriesWithUser(Subject user, Cache<String, String> c) { |
| 129 | + Security.doAs(user, () -> c.entrySet().forEach(e -> System.out.println(e.getKey() + " -> " + e.getValue()))); |
| 130 | + } |
| 131 | + |
| 132 | + public int getCacheSize(Subject user, Cache<String, String> c) { |
| 133 | + return Security.doAs(user, c::size); |
| 134 | + } |
| 135 | + |
| 136 | + public void stop() { |
| 137 | + if (dcm != null) { |
| 138 | + Security.doAs(ADMIN_USER, dcm::stop); |
| 139 | + dcm = null; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public static Subject createSubject(String principal) { |
| 144 | + return new Subject(true, Set.of(new ExamplePrincipal(principal)), Collections.emptySet(), Collections.emptySet()); |
| 145 | + } |
| 146 | + |
| 147 | + private record ExamplePrincipal(String name) implements Principal, Serializable { |
| 148 | + |
| 149 | + @Override |
| 150 | + public String getName() { |
| 151 | + return name; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments