private String[] getAllPermissionsAsStrings() { if (allPermissions[0] == null) { synchronized (allPermissions) { if (allPermissions[0] == null) { final Permission[] killbillPermissions = Permission.values(); for (int i = 0; i < killbillPermissions.length; i++) { allPermissions[i] = killbillPermissions[i].toString(); } } } } return allPermissions; }
public void assertAuthorized(final Annotation annotation) throws AuthorizationException { if (!(annotation instanceof RequiresPermissions)) { return; } final RequiresPermissions requiresPermissions = (RequiresPermissions) annotation; try { securityApi.checkCurrentUserPermissions(ImmutableList.<Permission>copyOf(requiresPermissions.value()), requiresPermissions.logical(), context); } catch (SecurityApiException e) { if (e.getCause() != null && e.getCause() instanceof AuthorizationException) { throw (AuthorizationException) e.getCause(); } else if (e.getCause() != null) { throw new AuthorizationException(e.getCause()); } else { throw new AuthorizationException(e); } } } }
@Override protected SecurityApiException generateAlreadyExistsException(final KombuchaModelDao entity, final InternalCallContext context) { return new SecurityApiException(ErrorCode.__UNKNOWN_ERROR_CODE); }
@Test(groups = "slow") public void testAuthorization() throws SecurityApiException { final String username = "i like"; final String password = "c0ff33"; securityApi.addRoleDefinition("restricted", ImmutableList.of("account:*", "invoice", "tag:create_tag_definition"), callContext); securityApi.addUserRoles(username, password, ImmutableList.of("restricted"), callContext); final AuthenticationToken goodToken = new UsernamePasswordToken(username, password); final Subject subject = securityManager.login(null, goodToken); subject.checkPermission(Permission.ACCOUNT_CAN_CHARGE.toString()); subject.checkPermission(Permission.INVOICE_CAN_CREDIT.toString()); subject.checkPermission(Permission.TAG_CAN_CREATE_TAG_DEFINITION.toString()); try { subject.checkPermission(Permission.TAG_CAN_DELETE_TAG_DEFINITION.toString()); Assert.fail("Subject should not have rights to delete tag definitions"); } catch (AuthorizationException e) { } subject.logout(); securityApi.addRoleDefinition("newRestricted", ImmutableList.of("account:*", "invoice", "tag:delete_tag_definition"), callContext); securityApi.updateUserRoles(username, ImmutableList.of("newRestricted"), callContext); final Subject newSubject = securityManager.login(null, goodToken); newSubject.checkPermission(Permission.ACCOUNT_CAN_CHARGE.toString()); newSubject.checkPermission(Permission.INVOICE_CAN_CREDIT.toString()); newSubject.checkPermission(Permission.TAG_CAN_DELETE_TAG_DEFINITION.toString()); try { newSubject.checkPermission(Permission.TAG_CAN_CREATE_TAG_DEFINITION.toString()); Assert.fail("Subject should not have rights to create tag definitions"); } catch (AuthorizationException e) { } }
@RequiresPermissions(Permission.PAYMENT_CAN_REFUND) public void createRefund(); }
throw new SecurityApiException(ErrorCode.SECURITY_INVALID_PERMISSIONS, curPerm); for (final Permission cur : Permission.values()) { if (!cur.getGroup().equals(permissionParts[0])) { continue; if (cur.getValue().equals(permissionParts[1])) { groupPermissions.add(permissionParts[1]); resolved = true; throw new SecurityApiException(ErrorCode.SECURITY_INVALID_PERMISSIONS, curPerm);
@Override public void checkCurrentUserPermissions(final List<Permission> permissions, final Logical logical, final TenantContext context) throws SecurityApiException { final String[] permissionsString = Lists.<Permission, String>transform(permissions, Functions.toStringFunction()).toArray(new String[permissions.size()]); try { final Subject subject = SecurityUtils.getSubject(); if (permissionsString.length == 1) { subject.checkPermission(permissionsString[0]); } else if (Logical.AND.equals(logical)) { subject.checkPermissions(permissionsString); } else if (Logical.OR.equals(logical)) { boolean hasAtLeastOnePermission = false; for (final String permission : permissionsString) { if (subject.isPermitted(permission)) { hasAtLeastOnePermission = true; break; } } // Cause the exception if none match if (!hasAtLeastOnePermission) { subject.checkPermission(permissionsString[0]); } } } catch (final AuthorizationException e) { throw new SecurityApiException(e, ErrorCode.SECURITY_NOT_ENOUGH_PERMISSIONS); } }
private void testInvalidPermissionScenario(final List<String> permissions) { try { securityApi.addRoleDefinition("failed", permissions, callContext); Assert.fail("Should fail permissions " + permissions + " were invalid"); } catch (SecurityApiException expected) { Assert.assertEquals(expected.getCode(), ErrorCode.SECURITY_INVALID_PERMISSIONS.getCode()); } }
protected void configureShiro() { final Ini config = new Ini(); config.addSection("users"); config.getSection("users").put("pierre", "password, creditor"); config.getSection("users").put("stephane", "password, refunder"); config.addSection("roles"); config.getSection("roles").put("creditor", Permission.INVOICE_CAN_CREDIT.toString() + "," + Permission.INVOICE_CAN_ITEM_ADJUST.toString()); config.getSection("roles").put("refunder", Permission.PAYMENT_CAN_REFUND.toString()); // Reset the security manager ThreadContext.unbindSecurityManager(); final Factory<SecurityManager> factory = new IniSecurityManagerFactory(config); final SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); } }
@Override public Set<Permission> getCurrentUserPermissions(final TenantContext context) { final Permission[] killbillPermissions = Permission.values(); final String[] killbillPermissionsString = getAllPermissionsAsStrings(); final Subject subject = SecurityUtils.getSubject(); // Bulk (optimized) call final boolean[] permissions = subject.isPermitted(killbillPermissionsString); final Set<Permission> userPermissions = new HashSet<Permission>(); for (int i = 0; i < permissions.length; i++) { if (permissions[i]) { userPermissions.add(killbillPermissions[i]); } } return userPermissions; }
Assert.fail("test should throw SecurityApiException"); } catch (final SecurityApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.__UNKNOWN_ERROR_CODE.getCode());
@RequiresPermissions(Permission.PAYMENT_CAN_REFUND) public void createRefund() {} }
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final DateTime updatedDate = clock.getUTCNow(); final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username); } usersSqlDao.invalidate(username, updatedDate.toDate(), updatedBy); return null; } });
/** * Update the externalKey for a given bundle * * @param bundleId ; bundle id * @param newExternalKey : the new value for the externalKey * @param context : the call context */ @RequiresPermissions(ENTITLEMENT_CAN_CREATE) public void updateExternalKey(UUID bundleId, String newExternalKey, CallContext context) throws EntitlementApiException;
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final RolesPermissionsSqlDao rolesPermissionsSqlDao = handle.attach(RolesPermissionsSqlDao.class); final List<RolesPermissionsModelDao> existingRole = rolesPermissionsSqlDao.getByRoleName(role); if (!existingRole.isEmpty()) { throw new SecurityApiException(ErrorCode.SECURITY_ROLE_ALREADY_EXISTS, role); } for (final String permission : permissions) { rolesPermissionsSqlDao.create(new RolesPermissionsModelDao(role, permission, createdDate, createdBy)); } return null; } });
/** @param accountId account id * @param invoiceDate maximum billing event day to consider (in the account timezone) * @param items items to be placed on the migration invoice * @param context call call context * @return The UUID of the created invoice */ @RequiresPermissions(INVOICE_CAN_TRIGGER_INVOICE) public UUID createMigrationInvoice(UUID accountId, LocalDate invoiceDate, Iterable<InvoiceItem> items, CallContext context);
@Override public List<UserRolesModelDao> inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username); } final UserRolesSqlDao userRolesSqlDao = handle.attach(UserRolesSqlDao.class); return userRolesSqlDao.getByUsername(username); } });
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final DateTime updatedDate = clock.getUTCNow(); final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username); } usersSqlDao.updatePassword(username, hashedPasswordBase64, salt.toBase64(), updatedDate.toDate(), updatedBy); return null; } });
final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username);
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final UserRolesSqlDao userRolesSqlDao = handle.attach(UserRolesSqlDao.class); for (final String role : roles) { userRolesSqlDao.create(new UserRolesModelDao(username, role, createdDate, createdBy)); } final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao != null) { throw new SecurityApiException(ErrorCode.SECURITY_USER_ALREADY_EXISTS, username); } usersSqlDao.create(new UserModelDao(username, hashedPasswordBase64, salt.toBase64(), createdDate, createdBy)); return null; } });