Skip to content

Commit

Permalink
Merge pull request #6007 from amanda-ariyaratne/issue-26702
Browse files Browse the repository at this point in the history
In-Memory Claim Management
  • Loading branch information
amanda-ariyaratne authored Nov 12, 2024
2 parents 85e4e76 + 3f9afca commit 03e96c3
Show file tree
Hide file tree
Showing 21 changed files with 3,397 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@

package org.wso2.carbon.identity.claim.metadata.mgt;

import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.claim.metadata.mgt.dao.CacheBackedLocalClaimDAO;
import org.wso2.carbon.identity.claim.metadata.mgt.dao.LocalClaimDAO;
import org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataClientException;
import org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException;
import org.wso2.carbon.identity.claim.metadata.mgt.model.Claim;
import org.wso2.carbon.identity.claim.metadata.mgt.model.ClaimDialect;
import org.wso2.carbon.identity.claim.metadata.mgt.model.ExternalClaim;
import org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;

import java.util.List;

import static org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants.ErrorMessage.ERROR_CODE_EMPTY_LOCAL_CLAIM_URI;
import static org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants.ErrorMessage.ERROR_CODE_EMPTY_MAPPED_ATTRIBUTES_IN_LOCAL_CLAIM;
import static org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants.ErrorMessage.ERROR_CODE_NON_EXISTING_LOCAL_CLAIM_URI;

/**
* This interface used to expose claim metadata management functionalities as an OSGi Service.
*/
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.claim.metadata.mgt;

import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.claim.metadata.mgt.dao.CacheBackedClaimDialectDAO;
import org.wso2.carbon.identity.claim.metadata.mgt.dao.CacheBackedExternalClaimDAO;
import org.wso2.carbon.identity.claim.metadata.mgt.dao.CacheBackedLocalClaimDAO;
import org.wso2.carbon.identity.claim.metadata.mgt.dao.ClaimDialectDAO;
import org.wso2.carbon.identity.claim.metadata.mgt.dao.ExternalClaimDAO;
import org.wso2.carbon.identity.claim.metadata.mgt.dao.LocalClaimDAO;
import org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException;
import org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataServerException;
import org.wso2.carbon.identity.claim.metadata.mgt.internal.ReadOnlyClaimMetadataManager;
import org.wso2.carbon.identity.claim.metadata.mgt.internal.ReadWriteClaimMetadataManager;
import org.wso2.carbon.identity.claim.metadata.mgt.model.Claim;
import org.wso2.carbon.identity.claim.metadata.mgt.model.ClaimDialect;
import org.wso2.carbon.identity.claim.metadata.mgt.model.ExternalClaim;
import org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim;
import org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants;
import org.wso2.carbon.user.api.UserStoreException;

import java.util.List;
import java.util.Optional;

/**
* Database based claim metadata manager.
*/
public class DBBasedClaimMetadataManager implements ReadWriteClaimMetadataManager {

private final ClaimDialectDAO claimDialectDAO = new CacheBackedClaimDialectDAO();
private final CacheBackedLocalClaimDAO localClaimDAO = new CacheBackedLocalClaimDAO(new LocalClaimDAO());
private final CacheBackedExternalClaimDAO externalClaimDAO = new CacheBackedExternalClaimDAO(new ExternalClaimDAO());

@Override
public List<ClaimDialect> getClaimDialects(int tenantId) throws ClaimMetadataException {

return this.claimDialectDAO.getClaimDialects(tenantId);
}

@Override
public Optional<ClaimDialect> getClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException {

if (StringUtils.isBlank(claimDialectURI)) {
throw new ClaimMetadataException("Invalid claim dialect URI: " + claimDialectURI);
}

return this.claimDialectDAO.getClaimDialects(tenantId).stream()
.filter(claimDialect -> claimDialectURI.equals(claimDialect.getClaimDialectURI()))
.findFirst();
}

@Override
public void addClaimDialect(ClaimDialect claimDialect, int tenantId) throws ClaimMetadataException {

this.claimDialectDAO.addClaimDialect(claimDialect, tenantId);
}

@Override
public void removeClaimDialect(ClaimDialect claimDialect, int tenantId) throws ClaimMetadataException {

this.claimDialectDAO.removeClaimDialect(claimDialect, tenantId);
// When deleting a claim dialect the relevant external claim deletion is handled by the DB through
// ON DELETE CASCADE. Here we are removing the relevant cache entry.
externalClaimDAO.removeExternalClaimCache(claimDialect.getClaimDialectURI(), tenantId);
}

@Override
public List<LocalClaim> getLocalClaims(int tenantId) throws ClaimMetadataException {

return this.localClaimDAO.getLocalClaims(tenantId);
}

@Override
public Optional<LocalClaim> getLocalClaim(String localClaimURI , int tenantId) throws ClaimMetadataException {

if (StringUtils.isBlank(localClaimURI)) {
throw new ClaimMetadataException("Invalid local claim URI: " + localClaimURI);
}

List<LocalClaim> localClaims = this.localClaimDAO.getLocalClaims(tenantId);
return localClaims.stream()
.filter(localClaim -> localClaimURI.equals(localClaim.getClaimURI()))
.findFirst();
}

@Override
public List<ExternalClaim> getExternalClaims(String externalClaimDialectURI, int tenantId)
throws ClaimMetadataException {

return this.externalClaimDAO.getExternalClaims(externalClaimDialectURI, tenantId);
}

@Override
public Optional<ExternalClaim> getExternalClaim(String externalClaimDialectURI, String claimURI, int tenantId)
throws ClaimMetadataException {

if (StringUtils.isBlank(externalClaimDialectURI) || StringUtils.isBlank(claimURI)) {
throw new ClaimMetadataException("Invalid external claim dialect URI or claim URI");
}

return this.externalClaimDAO.getExternalClaims(externalClaimDialectURI, tenantId).stream()
.filter(externalClaim -> claimURI.equals(externalClaim.getClaimURI()))
.findFirst();
}

@Override
public void addLocalClaim(LocalClaim localClaim, int tenantId) throws ClaimMetadataException {

this.localClaimDAO.addLocalClaim(localClaim, tenantId);
}

@Override
public void updateLocalClaim(LocalClaim localClaim, int tenantId) throws ClaimMetadataException {

this.localClaimDAO.updateLocalClaim(localClaim, tenantId);
}

@Override
public void updateLocalClaimMappings(List<LocalClaim> localClaims, int tenantId, String userStoreDomain)
throws ClaimMetadataException {

this.localClaimDAO.updateLocalClaimMappings(localClaims, tenantId, userStoreDomain);
}

@Override
public void removeLocalClaim(String localClaimURI, int tenantId) throws ClaimMetadataException {

this.localClaimDAO.removeLocalClaim(localClaimURI, tenantId);
}

@Override
public void removeClaimMappingAttributes(int tenantId, String userstoreDomain) throws ClaimMetadataException {

try {
this.localClaimDAO.removeClaimMappingAttributes(tenantId, userstoreDomain);
} catch (UserStoreException e) {
String errorMessage = String.format(
ClaimConstants.ErrorMessage.ERROR_CODE_SERVER_ERROR_DELETING_CLAIM_MAPPINGS.getMessage(),
tenantId, userstoreDomain);
throw new ClaimMetadataServerException(
ClaimConstants.ErrorMessage.ERROR_CODE_SERVER_ERROR_DELETING_CLAIM_MAPPINGS.getCode(),
errorMessage, e);
}
}

@Override
public void addExternalClaim(ExternalClaim externalClaim, int tenantId) throws ClaimMetadataException {

this.externalClaimDAO.addExternalClaim(externalClaim, tenantId);
}

@Override
public void updateExternalClaim(ExternalClaim externalClaim, int tenantId) throws ClaimMetadataException {

this.externalClaimDAO.updateExternalClaim(externalClaim, tenantId);
}

@Override
public void removeExternalClaim(String externalClaimDialectURI, String externalClaimURI, int tenantId)
throws ClaimMetadataException {

this.externalClaimDAO.removeExternalClaim(externalClaimDialectURI, externalClaimURI, tenantId);
}

@Override
public List<Claim> getMappedExternalClaims(String localClaimURI, int tenantId) throws ClaimMetadataException {

return this.localClaimDAO.fetchMappedExternalClaims(localClaimURI, tenantId);
}

@Override
public void renameClaimDialect(ClaimDialect oldClaimDialect, ClaimDialect newClaimDialect, int tenantId)
throws ClaimMetadataException {

this.claimDialectDAO.renameClaimDialect(oldClaimDialect, newClaimDialect, tenantId);
externalClaimDAO.removeExternalClaimCache(oldClaimDialect.getClaimDialectURI(), tenantId);
}

@Override
public void removeAllClaimDialects(int tenantId) throws ClaimMetadataException {

// The relevant external claim deletions are handled by the DB through ON DELETE CASCADE.
this.claimDialectDAO.removeAllClaimDialects(tenantId);
}

@Override
public boolean isMappedLocalClaim(String localClaimURI, int tenantId) throws ClaimMetadataException {

return this.externalClaimDAO.isMappedLocalClaim(localClaimURI, tenantId);
}

@Override
public boolean isLocalClaimMappedWithinDialect(String mappedLocalClaim, String externalClaimDialectURI,
int tenantId) throws ClaimMetadataException {

return this.externalClaimDAO.isLocalClaimMappedWithinDialect(mappedLocalClaim, externalClaimDialectURI,
tenantId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public class DefaultClaimMetadataStore implements ClaimMetadataStore {

private static final Log log = LogFactory.getLog(DefaultClaimMetadataStore.class);

private ClaimDialectDAO claimDialectDAO = new CacheBackedClaimDialectDAO();
private CacheBackedLocalClaimDAO localClaimDAO = new CacheBackedLocalClaimDAO(new LocalClaimDAO());
private CacheBackedExternalClaimDAO externalClaimDAO = new CacheBackedExternalClaimDAO(new ExternalClaimDAO());
private final UnifiedClaimMetadataManager unifiedClaimMetadataManager = new UnifiedClaimMetadataManager();

private int tenantId;

Expand All @@ -71,7 +69,7 @@ public static DefaultClaimMetadataStore getInstance(int tenantId) {
public DefaultClaimMetadataStore(ClaimConfig claimConfig, int tenantId) {

try {
if (claimDialectDAO.getClaimDialects(tenantId).size() == 0) {
if (unifiedClaimMetadataManager.getClaimDialects(tenantId).size() == 0) {
IdentityClaimManagementServiceDataHolder.getInstance().getClaimConfigInitDAO()
.initClaimConfig(claimConfig, tenantId);
}
Expand All @@ -96,7 +94,7 @@ public String[] getAllClaimUris() throws UserStoreException {

try {

List<LocalClaim> localClaimList = this.localClaimDAO.getLocalClaims(tenantId);
List<LocalClaim> localClaimList = this.unifiedClaimMetadataManager.getLocalClaims(tenantId);

localClaims = new String[localClaimList.size()];

Expand Down Expand Up @@ -136,7 +134,7 @@ public String getAttributeName(String domainName, String claimURI) throws UserSt
try {
// Add listener

List<LocalClaim> localClaimList = this.localClaimDAO.getLocalClaims(tenantId);
List<LocalClaim> localClaimList = this.unifiedClaimMetadataManager.getLocalClaims(tenantId);

// Add listener

Expand All @@ -148,14 +146,14 @@ public String getAttributeName(String domainName, String claimURI) throws UserSt


// For backward compatibility
List<ClaimDialect> claimDialects = claimDialectDAO.getClaimDialects(tenantId);
List<ClaimDialect> claimDialects = unifiedClaimMetadataManager.getClaimDialects(tenantId);

for (ClaimDialect claimDialect : claimDialects) {
if (ClaimConstants.LOCAL_CLAIM_DIALECT_URI.equalsIgnoreCase(claimDialect.getClaimDialectURI())) {
continue;
}

List<ExternalClaim> externalClaims = externalClaimDAO.getExternalClaims(claimDialect
List<ExternalClaim> externalClaims = unifiedClaimMetadataManager.getExternalClaims(claimDialect
.getClaimDialectURI(), tenantId);

for (ExternalClaim externalClaim : externalClaims) {
Expand Down Expand Up @@ -247,7 +245,7 @@ public String getAttributeName(String claimURI) throws UserStoreException {
@Deprecated
public Claim getClaim(String claimURI) throws UserStoreException {
try {
List<LocalClaim> localClaims = localClaimDAO.getLocalClaims(this.tenantId);
List<LocalClaim> localClaims = unifiedClaimMetadataManager.getLocalClaims(this.tenantId);

for (LocalClaim localClaim : localClaims) {
if (localClaim.getClaimURI().equalsIgnoreCase(claimURI)) {
Expand All @@ -258,14 +256,14 @@ public Claim getClaim(String claimURI) throws UserStoreException {
}

// For backward compatibility
List<ClaimDialect> claimDialects = claimDialectDAO.getClaimDialects(tenantId);
List<ClaimDialect> claimDialects = unifiedClaimMetadataManager.getClaimDialects(tenantId);

for (ClaimDialect claimDialect : claimDialects) {
if (ClaimConstants.LOCAL_CLAIM_DIALECT_URI.equalsIgnoreCase(claimDialect.getClaimDialectURI())) {
continue;
}

List<ExternalClaim> externalClaims = externalClaimDAO.getExternalClaims(claimDialect
List<ExternalClaim> externalClaims = unifiedClaimMetadataManager.getExternalClaims(claimDialect
.getClaimDialectURI(), tenantId);

for (ExternalClaim externalClaim : externalClaims) {
Expand Down Expand Up @@ -294,7 +292,7 @@ public Claim getClaim(String claimURI) throws UserStoreException {
@Deprecated
public ClaimMapping getClaimMapping(String claimURI) throws UserStoreException {
try {
List<LocalClaim> localClaims = localClaimDAO.getLocalClaims(this.tenantId);
List<LocalClaim> localClaims = unifiedClaimMetadataManager.getLocalClaims(this.tenantId);

for (LocalClaim localClaim : localClaims) {
if (localClaim.getClaimURI().equalsIgnoreCase(claimURI)) {
Expand All @@ -305,14 +303,14 @@ public ClaimMapping getClaimMapping(String claimURI) throws UserStoreException {
}

// For backward compatibility
List<ClaimDialect> claimDialects = claimDialectDAO.getClaimDialects(tenantId);
List<ClaimDialect> claimDialects = unifiedClaimMetadataManager.getClaimDialects(tenantId);

for (ClaimDialect claimDialect : claimDialects) {
if (ClaimConstants.LOCAL_CLAIM_DIALECT_URI.equalsIgnoreCase(claimDialect.getClaimDialectURI())) {
continue;
}

List<ExternalClaim> externalClaims = externalClaimDAO.getExternalClaims(claimDialect
List<ExternalClaim> externalClaims = unifiedClaimMetadataManager.getExternalClaims(claimDialect
.getClaimDialectURI(), tenantId);

for (ExternalClaim externalClaim : externalClaims) {
Expand Down Expand Up @@ -345,7 +343,7 @@ public ClaimMapping[] getAllClaimMappings(String dialectUri) throws UserStoreExc

if (ClaimConstants.LOCAL_CLAIM_DIALECT_URI.equalsIgnoreCase(dialectUri)) {
try {
List<LocalClaim> localClaims = localClaimDAO.getLocalClaims(this.tenantId);
List<LocalClaim> localClaims = unifiedClaimMetadataManager.getLocalClaims(this.tenantId);

List<ClaimMapping> claimMappings = new ArrayList<>();

Expand All @@ -365,8 +363,9 @@ public ClaimMapping[] getAllClaimMappings(String dialectUri) throws UserStoreExc
}
} else {
try {
List<ExternalClaim> externalClaims = externalClaimDAO.getExternalClaims(dialectUri, this.tenantId);
List<LocalClaim> localClaims = localClaimDAO.getLocalClaims(this.tenantId);
List<ExternalClaim> externalClaims = unifiedClaimMetadataManager.getExternalClaims(dialectUri,
this.tenantId);
List<LocalClaim> localClaims = unifiedClaimMetadataManager.getLocalClaims(this.tenantId);

List<ClaimMapping> claimMappings = new ArrayList<>();

Expand Down Expand Up @@ -414,7 +413,7 @@ public void updateClaimMapping(ClaimMapping claimMapping) throws UserStoreExcept
public ClaimMapping[] getAllSupportClaimMappingsByDefault() throws UserStoreException {

try {
List<LocalClaim> localClaims = localClaimDAO.getLocalClaims(this.tenantId);
List<LocalClaim> localClaims = unifiedClaimMetadataManager.getLocalClaims(this.tenantId);

List<ClaimMapping> claimMappings = new ArrayList<>();

Expand Down Expand Up @@ -442,7 +441,7 @@ public ClaimMapping[] getAllSupportClaimMappingsByDefault() throws UserStoreExce
public ClaimMapping[] getAllRequiredClaimMappings() throws UserStoreException {

try {
List<LocalClaim> localClaims = localClaimDAO.getLocalClaims(this.tenantId);
List<LocalClaim> localClaims = unifiedClaimMetadataManager.getLocalClaims(this.tenantId);

List<ClaimMapping> claimMappings = new ArrayList<>();

Expand Down
Loading

0 comments on commit 03e96c3

Please sign in to comment.