Skip to content

Commit

Permalink
Fix Sonar finding
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhoelzl-sap committed Feb 8, 2024
1 parent 291144c commit e5f5791
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -541,27 +541,43 @@ private <T extends AbstractIdentityProviderDefinition> IdentityProvider<?> ensur
return originalIdp;
}

final IdentityProvider<?> persistedAliasIdp = createNewAliasIdp(aliasIdp, originalIdp.getAliasZid());

// update alias ID in original IdP
originalIdp.setAliasId(persistedAliasIdp.getId());
return identityProviderProvisioning.update(originalIdp, originalIdp.getIdentityZoneId());
}

/**
* Persist the given alias IdP in the given zone.
*
* @param aliasIdp the alias IdP to persist
* @param aliasZid the ID of the identity zone in which the alias should be persisted
* @return the persisted alias IdP
*/
private <T extends AbstractIdentityProviderDefinition> IdentityProvider<?> createNewAliasIdp(
final IdentityProvider<T> aliasIdp,
final String aliasZid
) throws IdpAliasFailedException {
// check if IdZ referenced in 'aliasZid' exists
try {
identityZoneProvisioning.retrieve(originalIdp.getAliasZid());
identityZoneProvisioning.retrieve(aliasZid);
} catch (final ZoneDoesNotExistsException e) {
throw new IdpAliasFailedException(originalIdp, ALIAS_ZONE_DOES_NOT_EXIST, e);
throw new IdpAliasFailedException(aliasIdp.getAliasId(), aliasIdp.getAliasZid(), null, aliasZid, ALIAS_ZONE_DOES_NOT_EXIST, e);
}

// create new alias IdP in alias zid
final IdentityProvider<?> persistedAliasIdp;
try {
persistedAliasIdp = identityProviderProvisioning.create(
aliasIdp,
originalIdp.getAliasZid()
aliasZid
);
} catch (final IdpAlreadyExistsException e) {
throw new IdpAliasFailedException(originalIdp, ORIGIN_KEY_ALREADY_USED_IN_ALIAS_ZONE, e);
throw new IdpAliasFailedException(aliasIdp.getAliasId(), aliasIdp.getAliasZid(), null, aliasZid, ORIGIN_KEY_ALREADY_USED_IN_ALIAS_ZONE, e);
}

// update alias ID in original IdP
originalIdp.setAliasId(persistedAliasIdp.getId());
return identityProviderProvisioning.update(originalIdp, originalIdp.getIdentityZoneId());
return persistedAliasIdp;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,51 @@ public IdpAliasFailedException(
@NonNull final Reason reason,
@Nullable final Throwable cause
) {
super(cause, ERROR, buildMessage(identityProvider, reason), reason.responseCode.value());
this(
identityProvider.getId(),
identityProvider.getIdentityZoneId(),
identityProvider.getAliasId(),
identityProvider.getAliasZid(),
reason,
cause
);
}

public IdpAliasFailedException(
@Nullable final String idpId,
@Nullable final String idzId,
@Nullable final String aliasId,
@Nullable final String aliasZid,
@NonNull final Reason reason,
@Nullable final Throwable cause
) {
super(cause, ERROR, buildMessage(idpId, idzId, aliasId, aliasZid, reason), reason.responseCode.value());
}

private static String buildMessagePrefix(@NonNull final IdentityProvider<?> idp) {
private static String buildMessagePrefix(
@Nullable final String idpId,
@Nullable final String idzId,
@Nullable final String aliasId,
@Nullable final String aliasZid
) {
return String.format(
"IdentityProvider[id=%s,zid=%s,aliasId=%s,aliasZid=%s]",
surroundWithSingleQuotesIfPresent(idp.getId()),
surroundWithSingleQuotesIfPresent(idp.getIdentityZoneId()),
surroundWithSingleQuotesIfPresent(idp.getAliasId()),
surroundWithSingleQuotesIfPresent(idp.getAliasZid())
surroundWithSingleQuotesIfPresent(idpId),
surroundWithSingleQuotesIfPresent(idzId),
surroundWithSingleQuotesIfPresent(aliasId),
surroundWithSingleQuotesIfPresent(aliasZid)
);
}

private static String buildMessage(
@NonNull final IdentityProvider<?> idp,
@Nullable final String idpId,
@Nullable final String idzId,
@Nullable final String aliasId,
@Nullable final String aliasZid,
@NonNull final Reason reason
) {
return String.format("%s - %s", buildMessagePrefix(idp), reason.message);
final String messagePrefix = buildMessagePrefix(idpId, idzId, aliasId, aliasZid);
return String.format("%s - %s", messagePrefix, reason.message);
}

private static String surroundWithSingleQuotesIfPresent(@Nullable final String input) {
Expand Down

0 comments on commit e5f5791

Please sign in to comment.