From 71bb48f00c9ba92874999ff99821f387573fc229 Mon Sep 17 00:00:00 2001 From: lashini Date: Tue, 1 Apr 2025 09:18:31 +0530 Subject: [PATCH 1/3] Add secret processor --- .../pom.xml | 10 + ...NotificationSenderManagementConstants.java | 27 ++- ...tificationSenderManagementServiceImpl.java | 180 ++++++++++++++---- .../tenant/config/dto/EmailSenderDTO.java | 11 ++ ...ificationSenderTenantConfigDataHolder.java | 45 +++++ ...tificationSenderTenantConfigServiceDS.java | 40 ++++ .../NotificationSenderSecretProcessor.java | 151 +++++++++++++++ .../config/utils/NotificationSenderUtils.java | 30 +++ pom.xml | 5 + 9 files changed, 463 insertions(+), 36 deletions(-) create mode 100644 components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderSecretProcessor.java diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml index dc21c7a17..333a869c3 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml @@ -66,6 +66,10 @@ org.wso2.carbon.identity.framework org.wso2.carbon.identity.core + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.secret.mgt.core + org.wso2.carbon.identity.notification.push org.wso2.carbon.identity.notification.push.provider @@ -146,6 +150,8 @@ org.wso2.carbon.base.api; version="${carbon.kernel.carbon.base.pkg.version}", org.wso2.carbon.context; version="${carbon.kernel.imp.pkg.version.range}", + org.wso2.carbon.core.util; version="${carbon.kernel.imp.pkg.version.range}", + org.wso2.carbon.email.mgt; version="${identity.event.handler.notification.imp.pkg.version.range}", org.wso2.carbon.email.mgt.model; version="${identity.event.handler.notification.imp.pkg.version.range}", @@ -187,6 +193,10 @@ org.wso2.carbon.identity.notification.push.provider.model; version="${identity.notification.push.version.range}", org.wso2.carbon.identity.application.authentication.framework.util; version="${carbon.identity.framework.imp.pkg.version.range}", + + org.wso2.carbon.identity.secret.mgt.core; version="${carbon.identity.framework.imp.pkg.version.range}", + org.wso2.carbon.identity.secret.mgt.core.exception; version="${carbon.identity.framework.imp.pkg.version.range}", + org.wso2.carbon.identity.secret.mgt.core.model; version="${carbon.identity.framework.imp.pkg.version.range}", diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementConstants.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementConstants.java index f4cce296b..e0fc837f2 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementConstants.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementConstants.java @@ -46,14 +46,33 @@ private NotificationSenderManagementConstants() { public static final String PLUS = "+"; public static final String URL_ENCODED_SPACE = "%20"; public static final String CONFIG_MGT_ERROR_CODE_DELIMITER = "_"; + public static final String SECRET_PROPERTIES = "_SECRET_PROPERTIES"; + + // Notification Type + public static final String EMAIL_PROVIDER = "EMAIL_PROVIDER"; + public static final String SMS_PROVIDER = "SMS_PROVIDER"; + public static final String PUSH_PROVIDER = "PUSH_PROVIDER"; // Email Sender's main properties. public static final String SMTP_SERVER_HOST = "smtpServerHost"; public static final String SMTP_PORT = "smtpPort"; public static final String FROM_ADDRESS = "fromAddress"; + public static final String EMAIL_PUBLISHER_TYPE = "email"; + public static final String AUTH_TYPE = "authType"; + + // Email Sender's additional properties. + public static final String REPLY_TO_ADDRESS = "mail.smtp.replyTo"; + public static final String DISPLAY_NAME = "mail.smtp.signature"; + public static final String CLIENT_ID = "clientId"; + public static final String CLIENT_SECRET = "clientSecret"; + public static final String TOKEN_ENDPOINT = "tokenEndpoint"; + public static final String SCOPES = "scopes"; public static final String USERNAME = "userName"; public static final String PASSWORD = "password"; - public static final String EMAIL_PUBLISHER_TYPE = "email"; + + // Email Sender Authentication types. + public static final String BASIC = "BASIC"; + public static final String CLIENT_CREDENTIAL = "CLIENT_CREDENTIAL"; // SMS Sender's main properties. public static final String PROVIDER = "provider"; @@ -194,7 +213,11 @@ public enum ErrorMessage { ERROR_CODE_ERROR_ADDING_NOTIFICATION_SENDER_SECRETS("65020", "Unable to add notification sender.", "Server encountered an error while adding the notification sender secrets to resource: %s"), ERROR_CODE_ERROR_DELETING_NOTIFICATION_SENDER_SECRETS("65021", "Unable to delete notification sender.", - "Server encountered an error while deleting the notification sender secrets from resource: %s"); + "Server encountered an error while deleting the notification sender secrets from resource: %s"), + ERROR_CODE_INVALID_INPUTS("65022", "Invalid input.", + "Invalid input received for notification sender."), + ERROR_CODE_ERROR_WHILE_ENCRYPTING_CREDENTIALS("65023", "Error while encrypting credentials.", + "Error while encrypting credentials for notification sender: %s."); private final String code; private final String message; diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java index a591c88e9..d4a88a4a0 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java @@ -52,6 +52,7 @@ import org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderUtils; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; +import org.wso2.carbon.identity.secret.mgt.core.exception.SecretManagementException; import org.wso2.carbon.identity.tenant.resource.manager.exception.TenantResourceManagementException; import org.wso2.carbon.identity.tenant.resource.manager.util.ResourceUtils; import org.wso2.carbon.idp.mgt.model.ConnectedAppsResult; @@ -71,11 +72,18 @@ import static org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_DOES_NOT_EXISTS; import static org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_TYPE_DOES_NOT_EXISTS; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.AUTH_TYPE; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.BASIC; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.CHANNEL_TYPE_PROPERTY; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.CLIENT_CREDENTIAL; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.CLIENT_ID; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.CLIENT_SECRET; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DEFAULT_EMAIL_PUBLISHER; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DEFAULT_HANDLER_NAME; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DEFAULT_PUSH_PUBLISHER; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DEFAULT_SMS_PUBLISHER; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DISPLAY_NAME; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.EMAIL_PROVIDER; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.EMAIL_PUBLISHER_TYPE; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.ErrorMessage.ERROR_CODE_CHANNEL_TYPE_UPDATE_NOT_ALLOWED; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.ErrorMessage.ERROR_CODE_CONFIGURATION_HANDLER_NOT_FOUND; @@ -102,13 +110,18 @@ import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.PUBLISHER_RESOURCE_TYPE; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.PUBLISHER_TYPE_PROPERTY; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.PUSH_PUBLISHER_TYPE; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.REPLY_TO_ADDRESS; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.RESOURCE_NOT_EXISTS_ERROR_CODE; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SCOPES; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SMS_PUBLISHER_TYPE; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SMTP_PORT; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SMTP_SERVER_HOST; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.STREAM_NAME; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.STREAM_VERSION; +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.TOKEN_ENDPOINT; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.USERNAME; +import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderSecretProcessor.decryptCredential; +import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderSecretProcessor.encryptCredential; import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderUtils.buildPushSenderFromResource; import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderUtils.buildResourceFromPushSender; import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderUtils.buildSmsSenderFromResource; @@ -126,16 +139,19 @@ public class NotificationSenderManagementServiceImpl implements NotificationSend public static final int MAX_RETRY_COUNT = 60; public static final String SMS_OTP_AUTHENTICATOR = "sms-otp-authenticator"; - static final Map SENDERS = new HashMap() { { - put("SMSPublisher", SMS_OTP_AUTHENTICATOR); - } }; + static final Map SENDERS = new HashMap() { + { + put("SMSPublisher", SMS_OTP_AUTHENTICATOR); + } + }; @Override public EmailSenderDTO addEmailSender(EmailSenderDTO emailSender) throws NotificationSenderManagementException { + validateInputs(emailSender); // Set the default publisher name if name is not defined. - if (StringUtils.isEmpty(emailSender.getName())) { + if (StringUtils.isBlank(emailSender.getName())) { emailSender.setName(DEFAULT_EMAIL_PUBLISHER); } @@ -172,6 +188,53 @@ public EmailSenderDTO addEmailSender(EmailSenderDTO emailSender) throws Notifica } } + private void validateInputs(EmailSenderDTO emailSender) throws NotificationSenderManagementClientException { + + if (StringUtils.isBlank(emailSender.getSmtpServerHost()) || + emailSender.getSmtpPort() == null || + StringUtils.isBlank(emailSender.getFromAddress()) || + StringUtils.isBlank(emailSender.getProperties().get(DISPLAY_NAME)) || + StringUtils.isBlank(emailSender.getProperties().get(REPLY_TO_ADDRESS))) { + throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); + } + + // if authType is not specified, username and password should be set in the first class attributes. + if (StringUtils.isBlank(emailSender.getAuthType())) { + if (StringUtils.isBlank(emailSender.getUsername()) || StringUtils.isBlank(emailSender.getPassword())) { + throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); + } + return; + } + + // If authType is specified, username & password should NOT be set as first class attributes. + if (StringUtils.isNotBlank(emailSender.getUsername()) || StringUtils.isNotBlank(emailSender.getPassword())) { + throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); + } + + // If authType is set to BASIC, username & password should be set in the properties. + if (BASIC.equalsIgnoreCase(emailSender.getAuthType())) { + if (StringUtils.isBlank(emailSender.getProperties().get(USERNAME)) || + StringUtils.isBlank(emailSender.getProperties().get(PASSWORD))) { + throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); + } + return; + } + + // If authType is set to CLIENT_CREDENTIAL, client_id, client_secret, token_endpoint & scopes + // should be set in the properties. + if (CLIENT_CREDENTIAL.equalsIgnoreCase(emailSender.getAuthType())) { + if (StringUtils.isBlank(emailSender.getProperties().get(CLIENT_ID)) || + StringUtils.isBlank(emailSender.getProperties().get(CLIENT_SECRET)) || + StringUtils.isBlank(emailSender.getProperties().get(TOKEN_ENDPOINT)) || + StringUtils.isBlank(emailSender.getProperties().get(SCOPES))) { + throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); + } + return; + } + + throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); + } + @Override public SMSSenderDTO addSMSSender(SMSSenderDTO smsSender) throws NotificationSenderManagementException { @@ -244,7 +307,7 @@ public void deleteNotificationSender(String senderName) throws NotificationSende @Override public EmailSenderDTO getEmailSender(String senderName) throws NotificationSenderManagementException { - Optional resourceOptional = getPublisherResource(senderName); + Optional resourceOptional = getPublisherResource(senderName); if (!resourceOptional.isPresent()) { throw new NotificationSenderManagementClientException(ERROR_CODE_PUBLISHER_NOT_EXISTS, senderName); } @@ -408,7 +471,7 @@ public List getPushSenders(boolean inheritTenantSettings) for (Resource resource : publisherResources.getResources()) { if (resource.getAttributes().stream().anyMatch( attribute -> PUBLISHER_TYPE_PROPERTY.equals(attribute.getKey()) - && PUSH_PUBLISHER_TYPE.equals(attribute.getValue()))) { + && PUSH_PUBLISHER_TYPE.equals(attribute.getValue()))) { pushSenders.add(buildPushSenderFromResource(resource, true)); } } @@ -624,7 +687,7 @@ private EventPublisherConfiguration getPublisherInSuperTenant(String eventPublis eventPublisherName); } } catch (EventPublisherConfigurationException e) { - throw new NotificationSenderManagementServerException(ERROR_CODE_SERVER_ERRORS_GETTING_EVENT_PUBLISHER, + throw new NotificationSenderManagementServerException(ERROR_CODE_SERVER_ERRORS_GETTING_EVENT_PUBLISHER, eventPublisherName, e); } return publisherInSuperTenant; @@ -633,9 +696,8 @@ private EventPublisherConfiguration getPublisherInSuperTenant(String eventPublis /** * Get default properties of super tenant Publisher. * - * @param publisherName Name of the publisher. - * - * @return Map of Properties. + * @param publisherName Name of the publisher. + * @return Map of Properties. */ private Map getDefaultPublisherProperties(String publisherName) throws NotificationSenderManagementException { @@ -669,23 +731,54 @@ private Resource buildResourceFromEmailSender(EmailSenderDTO emailSender) } Resource resource = new Resource(); - resource.setResourceName(emailSender.getName()); - Map emailSenderAttributes = emailSender.getProperties(); - emailSenderAttributes.put(FROM_ADDRESS, emailSender.getFromAddress()); - if (StringUtils.isNotEmpty(emailSender.getUsername())) { - emailSenderAttributes.put(USERNAME, emailSender.getUsername()); - } - if (StringUtils.isNotEmpty(emailSender.getPassword())) { - emailSenderAttributes.put(PASSWORD, emailSender.getPassword()); - } - emailSenderAttributes.put(SMTP_SERVER_HOST, emailSender.getSmtpServerHost()); - emailSenderAttributes.put(SMTP_PORT, String.valueOf(emailSender.getSmtpPort())); - - List resourceAttributes = - emailSenderAttributes.entrySet().stream() - .filter(attribute -> attribute.getValue() != null && !"null".equals(attribute.getValue())) - .map(attribute -> new Attribute(attribute.getKey(), attribute.getValue())) - .collect(Collectors.toList()); + List resourceAttributes = new ArrayList<>(); + try { + resource.setResourceName(emailSender.getName()); + Map emailSenderAttributes = emailSender.getProperties(); + emailSenderAttributes.put(FROM_ADDRESS, emailSender.getFromAddress()); + if (StringUtils.isNotEmpty(emailSender.getUsername())) { + resourceAttributes.add(new Attribute(USERNAME, encryptCredential(EMAIL_PROVIDER, BASIC, USERNAME, + emailSender.getUsername()))); + } + if (StringUtils.isNotEmpty(emailSender.getPassword())) { + resourceAttributes.add(new Attribute(PASSWORD, encryptCredential(EMAIL_PROVIDER, BASIC, PASSWORD, + emailSender.getPassword()))); + } + if (StringUtils.isNotEmpty(emailSender.getAuthType())) { + emailSenderAttributes.put(AUTH_TYPE, emailSender.getAuthType()); + } + emailSenderAttributes.put(SMTP_SERVER_HOST, emailSender.getSmtpServerHost()); + emailSenderAttributes.put(SMTP_PORT, String.valueOf(emailSender.getSmtpPort())); + + for (Map.Entry entry : emailSenderAttributes.entrySet()) { + if (entry.getValue() != null && !"null".equals(entry.getValue())) { + String key = entry.getKey(); + switch (key) { + case USERNAME: + resourceAttributes.add(new Attribute(key, encryptCredential(EMAIL_PROVIDER, BASIC, USERNAME, + entry.getValue()))); + break; + case PASSWORD: + resourceAttributes.add(new Attribute(key, encryptCredential(EMAIL_PROVIDER, BASIC, PASSWORD, + entry.getValue()))); + break; + case CLIENT_ID: + resourceAttributes.add(new Attribute(key, encryptCredential(EMAIL_PROVIDER, + CLIENT_CREDENTIAL, CLIENT_ID, entry.getValue()))); + break; + case CLIENT_SECRET: + resourceAttributes.add(new Attribute(key, encryptCredential(EMAIL_PROVIDER, + CLIENT_CREDENTIAL, CLIENT_SECRET, entry.getValue()))); + break; + default: + resourceAttributes.add(new Attribute(key, entry.getValue())); + break; + } + } + } + } catch (Exception e) { + throw new NotificationSenderManagementServerException(ERROR_CODE_ERROR_ADDING_NOTIFICATION_SENDER, e.getMessage(), e); + } resource.setAttributes(resourceAttributes); // Set file. ResourceFile file = new ResourceFile(); @@ -712,6 +805,14 @@ private EmailSenderDTO buildEmailSenderFromResource(Resource resource) { resource.getAttributes().stream() .filter(attribute -> !(INTERNAL_PROPERTIES.contains(attribute.getKey()))) .collect(Collectors.toMap(Attribute::getKey, Attribute::getValue)); + // If authType is set to BASIC, username & password should be set in the properties. If not, set as first class + // attributes. + if (BASIC.equals(attributesMap.get(AUTH_TYPE))) { + emailSender.getProperties().put(USERNAME, attributesMap.get(USERNAME)); + emailSender.getProperties().put(PASSWORD, attributesMap.get(PASSWORD)); + attributesMap.remove(attributesMap.get(USERNAME)); + attributesMap.remove(attributesMap.get(PASSWORD)); + } attributesMap.forEach((key, value) -> { switch (key) { case SMTP_SERVER_HOST: @@ -724,10 +825,21 @@ private EmailSenderDTO buildEmailSenderFromResource(Resource resource) { emailSender.setFromAddress(value); break; case USERNAME: - emailSender.setUsername(value); + try { + emailSender.setUsername(decryptCredential(EMAIL_PROVIDER, BASIC, USERNAME)); + } catch (SecretManagementException e) { + throw new RuntimeException(e); + } break; case PASSWORD: - emailSender.setPassword(value); + try { + emailSender.setPassword(decryptCredential(EMAIL_PROVIDER, BASIC, PASSWORD)); + } catch (SecretManagementException e) { + throw new RuntimeException(e); + } + break; + case AUTH_TYPE: + emailSender.setAuthType(value); break; default: emailSender.getProperties().put(key, value); @@ -799,8 +911,8 @@ private String getChannelTypeFromResource(Resource resource) { /** * Method to check whether the sender is allowed to delete. * - * @param senderName Name of the sender. - * @return whether the sender is allowed to delete. + * @param senderName Name of the sender. + * @return whether the sender is allowed to delete. * @throws NotificationSenderManagementException If an error occurred while checking the sender. */ private boolean canSenderDelete(String senderName) throws NotificationSenderManagementException { @@ -815,8 +927,8 @@ private boolean canSenderDelete(String senderName) throws NotificationSenderMana // and offset as 0. ConnectedAppsResult appsResult = NotificationSenderTenantConfigDataHolder.getInstance() - .getApplicationManagementService() - .getConnectedAppsForLocalAuthenticator(authenticatorId, tenantDomain, 1 , 0); + .getApplicationManagementService() + .getConnectedAppsForLocalAuthenticator(authenticatorId, tenantDomain, 1, 0); // If there are any connected apps, the sender cannot be deleted. if (appsResult.getApps() != null && appsResult.getApps().size() > 0) { return false; @@ -830,7 +942,7 @@ private boolean canSenderDelete(String senderName) throws NotificationSenderMana List smsAttributes = resource.getAttributes().stream().filter(attribute -> attribute .getKey().equals("sms_otp_enabled")).collect( Collectors.toList()); - String smsOtpEnabled = null; + String smsOtpEnabled = null; if (!smsAttributes.isEmpty()) { smsOtpEnabled = smsAttributes.get(0).getValue(); } diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/dto/EmailSenderDTO.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/dto/EmailSenderDTO.java index 71ee81022..4fd19b847 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/dto/EmailSenderDTO.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/dto/EmailSenderDTO.java @@ -32,6 +32,7 @@ public class EmailSenderDTO { private String fromAddress; private String username; private String password; + private String authType; private Map properties = new HashMap<>(); public String getName() { @@ -104,4 +105,14 @@ public void setProperties(Map properties) { this.properties = properties; } + + public String getAuthType() { + + return authType; + } + + public void setAuthType(String authType) { + + this.authType = authType; + } } diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigDataHolder.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigDataHolder.java index 71efe174d..035c0c832 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigDataHolder.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigDataHolder.java @@ -26,6 +26,8 @@ import org.wso2.carbon.identity.notification.push.provider.PushProvider; import org.wso2.carbon.identity.notification.sender.tenant.config.handlers.ChannelConfigurationHandler; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; +import org.wso2.carbon.identity.secret.mgt.core.SecretManager; +import org.wso2.carbon.identity.secret.mgt.core.SecretResolveManager; import org.wso2.carbon.identity.tenant.resource.manager.core.ResourceManager; import java.util.HashMap; @@ -47,6 +49,8 @@ public class NotificationSenderTenantConfigDataHolder { private ApplicationManagementService applicationManagementService = null; private OrganizationManager organizationManager = null; private final Map pushNotificationProviders = new HashMap<>(); + private SecretManager secretManager; + private SecretResolveManager secretResolveManager; private NotificationSenderTenantConfigDataHolder() { } @@ -141,6 +145,47 @@ public OrganizationManager getOrganizationManager() { return organizationManager; } + + /** + * Get the SecretManager. + * + * @return SecretManager instance. + */ + public SecretManager getSecretManager() { + + return secretManager; + } + + /** + * Set the SecretManager. + * + * @param secretManager SecretManager instance. + */ + public void setSecretManager(SecretManager secretManager) { + + this.secretManager = secretManager; + } + + /** + * Get the SecretResolveManager. + * + * @return SecretResolveManager instance. + */ + public SecretResolveManager getSecretResolveManager() { + + return secretResolveManager; + } + + /** + * Set the SecretResolveManager. + * + * @param secretResolveManager SecretResolveManager instance. + */ + public void setSecretResolveManager(SecretResolveManager secretResolveManager) { + + this.secretResolveManager = secretResolveManager; + } + /** * Add a push notification provider. * diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigServiceDS.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigServiceDS.java index d8a898081..e95569ce0 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigServiceDS.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigServiceDS.java @@ -37,6 +37,8 @@ import org.wso2.carbon.identity.notification.sender.tenant.config.handlers.ChannelConfigurationHandler; import org.wso2.carbon.identity.notification.sender.tenant.config.handlers.DefaultChannelConfigurationHandler; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; +import org.wso2.carbon.identity.secret.mgt.core.SecretManager; +import org.wso2.carbon.identity.secret.mgt.core.SecretResolveManager; import org.wso2.carbon.identity.tenant.resource.manager.core.ResourceManager; import org.wso2.carbon.utils.ConfigurationContextService; @@ -236,4 +238,42 @@ protected void unsetPushProvider(PushProvider provider) { NotificationSenderTenantConfigDataHolder.getInstance().removePushProvider(provider.getName()); } + + @Reference( + name = "org.wso2.carbon.identity.secret.mgt.core.SecretManager", + service = SecretManager.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetSecretManager" + ) + private void setSecretManager(SecretManager secretManager) { + + NotificationSenderTenantConfigDataHolder.getInstance().setSecretManager(secretManager); + log.debug("SecretManager set in NotificationSenderTenantConfigDataHolder bundle."); + } + + private void unsetSecretManager(SecretManager secretManager) { + + NotificationSenderTenantConfigDataHolder.getInstance().setSecretManager(null); + log.debug("SecretManager unset in NotificationSenderTenantConfigDataHolder bundle."); + } + + @Reference( + name = "org.wso2.carbon.identity.secret.mgt.core.SecretResolveManager", + service = SecretResolveManager.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetSecretResolveManager" + ) + private void setSecretResolveManager(SecretResolveManager secretResolveManager) { + + NotificationSenderTenantConfigDataHolder.getInstance().setSecretResolveManager(secretResolveManager); + log.debug("SecretResolveManager set in NotificationSenderTenantConfigDataHolder bundle."); + } + + private void unsetSecretResolveManager(SecretResolveManager secretResolveManager) { + + NotificationSenderTenantConfigDataHolder.getInstance().setSecretResolveManager(null); + log.debug("SecretResolveManager unset in NotificationSenderTenantConfigDataHolder bundle."); + } } diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderSecretProcessor.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderSecretProcessor.java new file mode 100644 index 000000000..6206d3da5 --- /dev/null +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderSecretProcessor.java @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2025, 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.notification.sender.tenant.config.utils; + +import org.wso2.carbon.identity.notification.sender.tenant.config.internal.NotificationSenderTenantConfigDataHolder; +import org.wso2.carbon.identity.secret.mgt.core.exception.SecretManagementException; +import org.wso2.carbon.identity.secret.mgt.core.model.ResolvedSecret; +import org.wso2.carbon.identity.secret.mgt.core.model.Secret; + +import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SECRET_PROPERTIES; + +public class NotificationSenderSecretProcessor { + + /** + * Encrypt secret property. + * + * @param notificationSender Notification Sender: EMAIL_PROVIDER. + * @param authType Authentication Type + * @param property Authentication Property. + * @param value Authentication Property Value. + * @return Reference of the secret. + * @throws SecretManagementException If an error occurs while encrypting the secret. + */ + public static String encryptCredential(String notificationSender, String authType, String property, String value) + throws SecretManagementException { + + String secretName = buildSecretName(notificationSender, authType, property); + String secretType = notificationSender + SECRET_PROPERTIES; + if (isSecretPropertyExists(secretType, secretName)) { + updateExistingSecretProperty(secretType, secretName, value); + } else { + addNewActionSecretProperty(secretType, secretName, value); + } + return buildSecretReference(secretType, secretName); + } + + /** + * Decrypt secret property. + * + * @param notificationSender Notification Sender: EMAIL_PROVIDER. + * @param authType Authentication Type + * @param property Authentication Property. + * @throws SecretManagementException If an error occurs while decrypting the secret. + */ + public static String decryptCredential(String notificationSender, String authType, String property) + throws SecretManagementException { + + String secretName = buildSecretName(notificationSender, authType, property); + String secretType = notificationSender + SECRET_PROPERTIES; + if (!isSecretPropertyExists(secretType, secretName)) { + throw new SecretManagementException(String.format("Unable to find the Secret Property: %s of " + + "Auth Type: %s and Action ID: %s from the system.", property, authType, notificationSender)); + } + ResolvedSecret resolvedSecret = NotificationSenderTenantConfigDataHolder.getInstance().getSecretResolveManager() + .getResolvedSecret(secretType, secretName); + + return resolvedSecret.getResolvedSecretValue(); + } + + /** + * Create secret name. + * + * @param notificationSender Notification Sender. + * @param authType Authentication Type. + * @param authProperty Authentication Property. + * @return Secret Name. + */ + private static String buildSecretName(String notificationSender, String authType, String authProperty) { + + return notificationSender + ":" + authType + ":" + authProperty; + } + + /** + * Create secret reference name. + * + * @param secretName Name of the secret. + * @return Secret reference name. + * @throws SecretManagementException If an error occurs while retrieving the secret type. + */ + private static String buildSecretReference(String secretType, String secretName) throws SecretManagementException { + + String secretTypeId = NotificationSenderTenantConfigDataHolder.getInstance().getSecretManager() + .getSecretType(secretType).getId(); + return secretTypeId + ":" + secretName; + } + + /** + * Check whether the secret property exists. + * + * @param secretName Secret Name. + * @return True if the secret property exists. + * @throws SecretManagementException If an error occurs while checking the existence of the secret. + */ + private static boolean isSecretPropertyExists(String secretType, String secretName) + throws SecretManagementException { + + return NotificationSenderTenantConfigDataHolder.getInstance().getSecretManager() + .isSecretExist(secretType, secretName); + } + + /** + * Add new Secret for Action secret type. + * + * @param secretType Secret type. + * @param secretName Name of the secret. + * @param value secret value. + * @throws SecretManagementException If an error occurs while adding the secret. + */ + private static void addNewActionSecretProperty(String secretType, String secretName, String value) throws SecretManagementException { + + Secret secret = new Secret(); + secret.setSecretName(secretName); + secret.setSecretValue(value); + NotificationSenderTenantConfigDataHolder.getInstance().getSecretManager().addSecret(secretType, secret); + } + + /** + * Update an existing secret of Action secret type. + * + * @param secretType Secret type. + * @param secretName Name of the secret. + * @param value secret value. + * @throws SecretManagementException If an error occurs while adding the secret. + */ + private static void updateExistingSecretProperty(String secretType, String secretName, String value) + throws SecretManagementException { + + ResolvedSecret resolvedSecret = NotificationSenderTenantConfigDataHolder.getInstance().getSecretResolveManager() + .getResolvedSecret(secretType, secretName); + if (!resolvedSecret.getResolvedSecretValue().equals(value)) { + NotificationSenderTenantConfigDataHolder.getInstance().getSecretManager() + .updateSecretValue(secretType, secretName, value); + } + } +} diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderUtils.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderUtils.java index ba0066892..139a832fd 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderUtils.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderUtils.java @@ -18,10 +18,13 @@ package org.wso2.carbon.identity.notification.sender.tenant.config.utils; +import org.apache.commons.io.Charsets; import org.apache.commons.lang.StringUtils; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.wso2.carbon.core.util.CryptoException; +import org.wso2.carbon.core.util.CryptoUtil; import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils; import org.wso2.carbon.identity.configuration.mgt.core.model.Attribute; import org.wso2.carbon.identity.configuration.mgt.core.model.Resource; @@ -41,6 +44,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -592,4 +596,30 @@ public static PushSenderData buildPushSenderData(PushSenderDTO pushSenderDTO) { pushSenderData.setProviderId(pushSenderDTO.getProviderId()); return pushSenderData; } + + /** + * Encrypt the given text. + * + * @param plainText text to be encrypted. + * @return encrypted claim value. + */ + public static String encrypt(String plainText) throws CryptoException { + + if (plainText.isEmpty()) { + return plainText; + } + return CryptoUtil.getDefaultCryptoUtil().encryptAndBase64Encode(plainText.getBytes(StandardCharsets.UTF_8)); + } + + /** + * Decrypt the given cipher text. + * + * @param cipherText The string which needs to be decrypted + * @return Base64 decoded string + * @throws CryptoException On an error during decryption + */ + public static String decrypt(String cipherText) throws CryptoException { + + return new String(CryptoUtil.getDefaultCryptoUtil().base64DecodeAndDecrypt(cipherText), Charsets.UTF_8); + } } diff --git a/pom.xml b/pom.xml index 65d2b32f5..8865ac2a9 100644 --- a/pom.xml +++ b/pom.xml @@ -169,6 +169,11 @@ org.wso2.carbon.identity.application.common ${carbon.identity.framework.version} + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.secret.mgt.core + ${carbon.identity.framework.version} + From b146314b086d0a374c0c3d22a7835cad0d082409 Mon Sep 17 00:00:00 2001 From: lashini Date: Tue, 1 Apr 2025 11:19:22 +0530 Subject: [PATCH 2/3] Revert "Add secret processor" This reverts commit 71bb48f00c9ba92874999ff99821f387573fc229. --- .../pom.xml | 10 - ...NotificationSenderManagementConstants.java | 27 +-- ...tificationSenderManagementServiceImpl.java | 180 ++++-------------- .../tenant/config/dto/EmailSenderDTO.java | 11 -- ...ificationSenderTenantConfigDataHolder.java | 45 ----- ...tificationSenderTenantConfigServiceDS.java | 40 ---- .../NotificationSenderSecretProcessor.java | 151 --------------- .../config/utils/NotificationSenderUtils.java | 30 --- pom.xml | 5 - 9 files changed, 36 insertions(+), 463 deletions(-) delete mode 100644 components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderSecretProcessor.java diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml index 333a869c3..dc21c7a17 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/pom.xml @@ -66,10 +66,6 @@ org.wso2.carbon.identity.framework org.wso2.carbon.identity.core - - org.wso2.carbon.identity.framework - org.wso2.carbon.identity.secret.mgt.core - org.wso2.carbon.identity.notification.push org.wso2.carbon.identity.notification.push.provider @@ -150,8 +146,6 @@ org.wso2.carbon.base.api; version="${carbon.kernel.carbon.base.pkg.version}", org.wso2.carbon.context; version="${carbon.kernel.imp.pkg.version.range}", - org.wso2.carbon.core.util; version="${carbon.kernel.imp.pkg.version.range}", - org.wso2.carbon.email.mgt; version="${identity.event.handler.notification.imp.pkg.version.range}", org.wso2.carbon.email.mgt.model; version="${identity.event.handler.notification.imp.pkg.version.range}", @@ -193,10 +187,6 @@ org.wso2.carbon.identity.notification.push.provider.model; version="${identity.notification.push.version.range}", org.wso2.carbon.identity.application.authentication.framework.util; version="${carbon.identity.framework.imp.pkg.version.range}", - - org.wso2.carbon.identity.secret.mgt.core; version="${carbon.identity.framework.imp.pkg.version.range}", - org.wso2.carbon.identity.secret.mgt.core.exception; version="${carbon.identity.framework.imp.pkg.version.range}", - org.wso2.carbon.identity.secret.mgt.core.model; version="${carbon.identity.framework.imp.pkg.version.range}", diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementConstants.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementConstants.java index e0fc837f2..f4cce296b 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementConstants.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementConstants.java @@ -46,33 +46,14 @@ private NotificationSenderManagementConstants() { public static final String PLUS = "+"; public static final String URL_ENCODED_SPACE = "%20"; public static final String CONFIG_MGT_ERROR_CODE_DELIMITER = "_"; - public static final String SECRET_PROPERTIES = "_SECRET_PROPERTIES"; - - // Notification Type - public static final String EMAIL_PROVIDER = "EMAIL_PROVIDER"; - public static final String SMS_PROVIDER = "SMS_PROVIDER"; - public static final String PUSH_PROVIDER = "PUSH_PROVIDER"; // Email Sender's main properties. public static final String SMTP_SERVER_HOST = "smtpServerHost"; public static final String SMTP_PORT = "smtpPort"; public static final String FROM_ADDRESS = "fromAddress"; - public static final String EMAIL_PUBLISHER_TYPE = "email"; - public static final String AUTH_TYPE = "authType"; - - // Email Sender's additional properties. - public static final String REPLY_TO_ADDRESS = "mail.smtp.replyTo"; - public static final String DISPLAY_NAME = "mail.smtp.signature"; - public static final String CLIENT_ID = "clientId"; - public static final String CLIENT_SECRET = "clientSecret"; - public static final String TOKEN_ENDPOINT = "tokenEndpoint"; - public static final String SCOPES = "scopes"; public static final String USERNAME = "userName"; public static final String PASSWORD = "password"; - - // Email Sender Authentication types. - public static final String BASIC = "BASIC"; - public static final String CLIENT_CREDENTIAL = "CLIENT_CREDENTIAL"; + public static final String EMAIL_PUBLISHER_TYPE = "email"; // SMS Sender's main properties. public static final String PROVIDER = "provider"; @@ -213,11 +194,7 @@ public enum ErrorMessage { ERROR_CODE_ERROR_ADDING_NOTIFICATION_SENDER_SECRETS("65020", "Unable to add notification sender.", "Server encountered an error while adding the notification sender secrets to resource: %s"), ERROR_CODE_ERROR_DELETING_NOTIFICATION_SENDER_SECRETS("65021", "Unable to delete notification sender.", - "Server encountered an error while deleting the notification sender secrets from resource: %s"), - ERROR_CODE_INVALID_INPUTS("65022", "Invalid input.", - "Invalid input received for notification sender."), - ERROR_CODE_ERROR_WHILE_ENCRYPTING_CREDENTIALS("65023", "Error while encrypting credentials.", - "Error while encrypting credentials for notification sender: %s."); + "Server encountered an error while deleting the notification sender secrets from resource: %s"); private final String code; private final String message; diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java index d4a88a4a0..a591c88e9 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java @@ -52,7 +52,6 @@ import org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderUtils; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; -import org.wso2.carbon.identity.secret.mgt.core.exception.SecretManagementException; import org.wso2.carbon.identity.tenant.resource.manager.exception.TenantResourceManagementException; import org.wso2.carbon.identity.tenant.resource.manager.util.ResourceUtils; import org.wso2.carbon.idp.mgt.model.ConnectedAppsResult; @@ -72,18 +71,11 @@ import static org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_DOES_NOT_EXISTS; import static org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_TYPE_DOES_NOT_EXISTS; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.AUTH_TYPE; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.BASIC; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.CHANNEL_TYPE_PROPERTY; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.CLIENT_CREDENTIAL; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.CLIENT_ID; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.CLIENT_SECRET; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DEFAULT_EMAIL_PUBLISHER; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DEFAULT_HANDLER_NAME; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DEFAULT_PUSH_PUBLISHER; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DEFAULT_SMS_PUBLISHER; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.DISPLAY_NAME; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.EMAIL_PROVIDER; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.EMAIL_PUBLISHER_TYPE; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.ErrorMessage.ERROR_CODE_CHANNEL_TYPE_UPDATE_NOT_ALLOWED; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.ErrorMessage.ERROR_CODE_CONFIGURATION_HANDLER_NOT_FOUND; @@ -110,18 +102,13 @@ import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.PUBLISHER_RESOURCE_TYPE; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.PUBLISHER_TYPE_PROPERTY; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.PUSH_PUBLISHER_TYPE; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.REPLY_TO_ADDRESS; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.RESOURCE_NOT_EXISTS_ERROR_CODE; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SCOPES; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SMS_PUBLISHER_TYPE; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SMTP_PORT; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SMTP_SERVER_HOST; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.STREAM_NAME; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.STREAM_VERSION; -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.TOKEN_ENDPOINT; import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.USERNAME; -import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderSecretProcessor.decryptCredential; -import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderSecretProcessor.encryptCredential; import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderUtils.buildPushSenderFromResource; import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderUtils.buildResourceFromPushSender; import static org.wso2.carbon.identity.notification.sender.tenant.config.utils.NotificationSenderUtils.buildSmsSenderFromResource; @@ -139,19 +126,16 @@ public class NotificationSenderManagementServiceImpl implements NotificationSend public static final int MAX_RETRY_COUNT = 60; public static final String SMS_OTP_AUTHENTICATOR = "sms-otp-authenticator"; - static final Map SENDERS = new HashMap() { - { - put("SMSPublisher", SMS_OTP_AUTHENTICATOR); - } - }; + static final Map SENDERS = new HashMap() { { + put("SMSPublisher", SMS_OTP_AUTHENTICATOR); + } }; @Override public EmailSenderDTO addEmailSender(EmailSenderDTO emailSender) throws NotificationSenderManagementException { - validateInputs(emailSender); // Set the default publisher name if name is not defined. - if (StringUtils.isBlank(emailSender.getName())) { + if (StringUtils.isEmpty(emailSender.getName())) { emailSender.setName(DEFAULT_EMAIL_PUBLISHER); } @@ -188,53 +172,6 @@ public EmailSenderDTO addEmailSender(EmailSenderDTO emailSender) throws Notifica } } - private void validateInputs(EmailSenderDTO emailSender) throws NotificationSenderManagementClientException { - - if (StringUtils.isBlank(emailSender.getSmtpServerHost()) || - emailSender.getSmtpPort() == null || - StringUtils.isBlank(emailSender.getFromAddress()) || - StringUtils.isBlank(emailSender.getProperties().get(DISPLAY_NAME)) || - StringUtils.isBlank(emailSender.getProperties().get(REPLY_TO_ADDRESS))) { - throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); - } - - // if authType is not specified, username and password should be set in the first class attributes. - if (StringUtils.isBlank(emailSender.getAuthType())) { - if (StringUtils.isBlank(emailSender.getUsername()) || StringUtils.isBlank(emailSender.getPassword())) { - throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); - } - return; - } - - // If authType is specified, username & password should NOT be set as first class attributes. - if (StringUtils.isNotBlank(emailSender.getUsername()) || StringUtils.isNotBlank(emailSender.getPassword())) { - throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); - } - - // If authType is set to BASIC, username & password should be set in the properties. - if (BASIC.equalsIgnoreCase(emailSender.getAuthType())) { - if (StringUtils.isBlank(emailSender.getProperties().get(USERNAME)) || - StringUtils.isBlank(emailSender.getProperties().get(PASSWORD))) { - throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); - } - return; - } - - // If authType is set to CLIENT_CREDENTIAL, client_id, client_secret, token_endpoint & scopes - // should be set in the properties. - if (CLIENT_CREDENTIAL.equalsIgnoreCase(emailSender.getAuthType())) { - if (StringUtils.isBlank(emailSender.getProperties().get(CLIENT_ID)) || - StringUtils.isBlank(emailSender.getProperties().get(CLIENT_SECRET)) || - StringUtils.isBlank(emailSender.getProperties().get(TOKEN_ENDPOINT)) || - StringUtils.isBlank(emailSender.getProperties().get(SCOPES))) { - throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); - } - return; - } - - throw new NotificationSenderManagementClientException(ErrorMessage.ERROR_CODE_INVALID_INPUTS); - } - @Override public SMSSenderDTO addSMSSender(SMSSenderDTO smsSender) throws NotificationSenderManagementException { @@ -307,7 +244,7 @@ public void deleteNotificationSender(String senderName) throws NotificationSende @Override public EmailSenderDTO getEmailSender(String senderName) throws NotificationSenderManagementException { - Optional resourceOptional = getPublisherResource(senderName); + Optional resourceOptional = getPublisherResource(senderName); if (!resourceOptional.isPresent()) { throw new NotificationSenderManagementClientException(ERROR_CODE_PUBLISHER_NOT_EXISTS, senderName); } @@ -471,7 +408,7 @@ public List getPushSenders(boolean inheritTenantSettings) for (Resource resource : publisherResources.getResources()) { if (resource.getAttributes().stream().anyMatch( attribute -> PUBLISHER_TYPE_PROPERTY.equals(attribute.getKey()) - && PUSH_PUBLISHER_TYPE.equals(attribute.getValue()))) { + && PUSH_PUBLISHER_TYPE.equals(attribute.getValue()))) { pushSenders.add(buildPushSenderFromResource(resource, true)); } } @@ -687,7 +624,7 @@ private EventPublisherConfiguration getPublisherInSuperTenant(String eventPublis eventPublisherName); } } catch (EventPublisherConfigurationException e) { - throw new NotificationSenderManagementServerException(ERROR_CODE_SERVER_ERRORS_GETTING_EVENT_PUBLISHER, + throw new NotificationSenderManagementServerException(ERROR_CODE_SERVER_ERRORS_GETTING_EVENT_PUBLISHER, eventPublisherName, e); } return publisherInSuperTenant; @@ -696,8 +633,9 @@ private EventPublisherConfiguration getPublisherInSuperTenant(String eventPublis /** * Get default properties of super tenant Publisher. * - * @param publisherName Name of the publisher. - * @return Map of Properties. + * @param publisherName Name of the publisher. + * + * @return Map of Properties. */ private Map getDefaultPublisherProperties(String publisherName) throws NotificationSenderManagementException { @@ -731,54 +669,23 @@ private Resource buildResourceFromEmailSender(EmailSenderDTO emailSender) } Resource resource = new Resource(); - List resourceAttributes = new ArrayList<>(); - try { - resource.setResourceName(emailSender.getName()); - Map emailSenderAttributes = emailSender.getProperties(); - emailSenderAttributes.put(FROM_ADDRESS, emailSender.getFromAddress()); - if (StringUtils.isNotEmpty(emailSender.getUsername())) { - resourceAttributes.add(new Attribute(USERNAME, encryptCredential(EMAIL_PROVIDER, BASIC, USERNAME, - emailSender.getUsername()))); - } - if (StringUtils.isNotEmpty(emailSender.getPassword())) { - resourceAttributes.add(new Attribute(PASSWORD, encryptCredential(EMAIL_PROVIDER, BASIC, PASSWORD, - emailSender.getPassword()))); - } - if (StringUtils.isNotEmpty(emailSender.getAuthType())) { - emailSenderAttributes.put(AUTH_TYPE, emailSender.getAuthType()); - } - emailSenderAttributes.put(SMTP_SERVER_HOST, emailSender.getSmtpServerHost()); - emailSenderAttributes.put(SMTP_PORT, String.valueOf(emailSender.getSmtpPort())); - - for (Map.Entry entry : emailSenderAttributes.entrySet()) { - if (entry.getValue() != null && !"null".equals(entry.getValue())) { - String key = entry.getKey(); - switch (key) { - case USERNAME: - resourceAttributes.add(new Attribute(key, encryptCredential(EMAIL_PROVIDER, BASIC, USERNAME, - entry.getValue()))); - break; - case PASSWORD: - resourceAttributes.add(new Attribute(key, encryptCredential(EMAIL_PROVIDER, BASIC, PASSWORD, - entry.getValue()))); - break; - case CLIENT_ID: - resourceAttributes.add(new Attribute(key, encryptCredential(EMAIL_PROVIDER, - CLIENT_CREDENTIAL, CLIENT_ID, entry.getValue()))); - break; - case CLIENT_SECRET: - resourceAttributes.add(new Attribute(key, encryptCredential(EMAIL_PROVIDER, - CLIENT_CREDENTIAL, CLIENT_SECRET, entry.getValue()))); - break; - default: - resourceAttributes.add(new Attribute(key, entry.getValue())); - break; - } - } - } - } catch (Exception e) { - throw new NotificationSenderManagementServerException(ERROR_CODE_ERROR_ADDING_NOTIFICATION_SENDER, e.getMessage(), e); - } + resource.setResourceName(emailSender.getName()); + Map emailSenderAttributes = emailSender.getProperties(); + emailSenderAttributes.put(FROM_ADDRESS, emailSender.getFromAddress()); + if (StringUtils.isNotEmpty(emailSender.getUsername())) { + emailSenderAttributes.put(USERNAME, emailSender.getUsername()); + } + if (StringUtils.isNotEmpty(emailSender.getPassword())) { + emailSenderAttributes.put(PASSWORD, emailSender.getPassword()); + } + emailSenderAttributes.put(SMTP_SERVER_HOST, emailSender.getSmtpServerHost()); + emailSenderAttributes.put(SMTP_PORT, String.valueOf(emailSender.getSmtpPort())); + + List resourceAttributes = + emailSenderAttributes.entrySet().stream() + .filter(attribute -> attribute.getValue() != null && !"null".equals(attribute.getValue())) + .map(attribute -> new Attribute(attribute.getKey(), attribute.getValue())) + .collect(Collectors.toList()); resource.setAttributes(resourceAttributes); // Set file. ResourceFile file = new ResourceFile(); @@ -805,14 +712,6 @@ private EmailSenderDTO buildEmailSenderFromResource(Resource resource) { resource.getAttributes().stream() .filter(attribute -> !(INTERNAL_PROPERTIES.contains(attribute.getKey()))) .collect(Collectors.toMap(Attribute::getKey, Attribute::getValue)); - // If authType is set to BASIC, username & password should be set in the properties. If not, set as first class - // attributes. - if (BASIC.equals(attributesMap.get(AUTH_TYPE))) { - emailSender.getProperties().put(USERNAME, attributesMap.get(USERNAME)); - emailSender.getProperties().put(PASSWORD, attributesMap.get(PASSWORD)); - attributesMap.remove(attributesMap.get(USERNAME)); - attributesMap.remove(attributesMap.get(PASSWORD)); - } attributesMap.forEach((key, value) -> { switch (key) { case SMTP_SERVER_HOST: @@ -825,21 +724,10 @@ private EmailSenderDTO buildEmailSenderFromResource(Resource resource) { emailSender.setFromAddress(value); break; case USERNAME: - try { - emailSender.setUsername(decryptCredential(EMAIL_PROVIDER, BASIC, USERNAME)); - } catch (SecretManagementException e) { - throw new RuntimeException(e); - } + emailSender.setUsername(value); break; case PASSWORD: - try { - emailSender.setPassword(decryptCredential(EMAIL_PROVIDER, BASIC, PASSWORD)); - } catch (SecretManagementException e) { - throw new RuntimeException(e); - } - break; - case AUTH_TYPE: - emailSender.setAuthType(value); + emailSender.setPassword(value); break; default: emailSender.getProperties().put(key, value); @@ -911,8 +799,8 @@ private String getChannelTypeFromResource(Resource resource) { /** * Method to check whether the sender is allowed to delete. * - * @param senderName Name of the sender. - * @return whether the sender is allowed to delete. + * @param senderName Name of the sender. + * @return whether the sender is allowed to delete. * @throws NotificationSenderManagementException If an error occurred while checking the sender. */ private boolean canSenderDelete(String senderName) throws NotificationSenderManagementException { @@ -927,8 +815,8 @@ private boolean canSenderDelete(String senderName) throws NotificationSenderMana // and offset as 0. ConnectedAppsResult appsResult = NotificationSenderTenantConfigDataHolder.getInstance() - .getApplicationManagementService() - .getConnectedAppsForLocalAuthenticator(authenticatorId, tenantDomain, 1, 0); + .getApplicationManagementService() + .getConnectedAppsForLocalAuthenticator(authenticatorId, tenantDomain, 1 , 0); // If there are any connected apps, the sender cannot be deleted. if (appsResult.getApps() != null && appsResult.getApps().size() > 0) { return false; @@ -942,7 +830,7 @@ private boolean canSenderDelete(String senderName) throws NotificationSenderMana List smsAttributes = resource.getAttributes().stream().filter(attribute -> attribute .getKey().equals("sms_otp_enabled")).collect( Collectors.toList()); - String smsOtpEnabled = null; + String smsOtpEnabled = null; if (!smsAttributes.isEmpty()) { smsOtpEnabled = smsAttributes.get(0).getValue(); } diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/dto/EmailSenderDTO.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/dto/EmailSenderDTO.java index 4fd19b847..71ee81022 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/dto/EmailSenderDTO.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/dto/EmailSenderDTO.java @@ -32,7 +32,6 @@ public class EmailSenderDTO { private String fromAddress; private String username; private String password; - private String authType; private Map properties = new HashMap<>(); public String getName() { @@ -105,14 +104,4 @@ public void setProperties(Map properties) { this.properties = properties; } - - public String getAuthType() { - - return authType; - } - - public void setAuthType(String authType) { - - this.authType = authType; - } } diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigDataHolder.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigDataHolder.java index 035c0c832..71efe174d 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigDataHolder.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigDataHolder.java @@ -26,8 +26,6 @@ import org.wso2.carbon.identity.notification.push.provider.PushProvider; import org.wso2.carbon.identity.notification.sender.tenant.config.handlers.ChannelConfigurationHandler; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; -import org.wso2.carbon.identity.secret.mgt.core.SecretManager; -import org.wso2.carbon.identity.secret.mgt.core.SecretResolveManager; import org.wso2.carbon.identity.tenant.resource.manager.core.ResourceManager; import java.util.HashMap; @@ -49,8 +47,6 @@ public class NotificationSenderTenantConfigDataHolder { private ApplicationManagementService applicationManagementService = null; private OrganizationManager organizationManager = null; private final Map pushNotificationProviders = new HashMap<>(); - private SecretManager secretManager; - private SecretResolveManager secretResolveManager; private NotificationSenderTenantConfigDataHolder() { } @@ -145,47 +141,6 @@ public OrganizationManager getOrganizationManager() { return organizationManager; } - - /** - * Get the SecretManager. - * - * @return SecretManager instance. - */ - public SecretManager getSecretManager() { - - return secretManager; - } - - /** - * Set the SecretManager. - * - * @param secretManager SecretManager instance. - */ - public void setSecretManager(SecretManager secretManager) { - - this.secretManager = secretManager; - } - - /** - * Get the SecretResolveManager. - * - * @return SecretResolveManager instance. - */ - public SecretResolveManager getSecretResolveManager() { - - return secretResolveManager; - } - - /** - * Set the SecretResolveManager. - * - * @param secretResolveManager SecretResolveManager instance. - */ - public void setSecretResolveManager(SecretResolveManager secretResolveManager) { - - this.secretResolveManager = secretResolveManager; - } - /** * Add a push notification provider. * diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigServiceDS.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigServiceDS.java index e95569ce0..d8a898081 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigServiceDS.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/internal/NotificationSenderTenantConfigServiceDS.java @@ -37,8 +37,6 @@ import org.wso2.carbon.identity.notification.sender.tenant.config.handlers.ChannelConfigurationHandler; import org.wso2.carbon.identity.notification.sender.tenant.config.handlers.DefaultChannelConfigurationHandler; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; -import org.wso2.carbon.identity.secret.mgt.core.SecretManager; -import org.wso2.carbon.identity.secret.mgt.core.SecretResolveManager; import org.wso2.carbon.identity.tenant.resource.manager.core.ResourceManager; import org.wso2.carbon.utils.ConfigurationContextService; @@ -238,42 +236,4 @@ protected void unsetPushProvider(PushProvider provider) { NotificationSenderTenantConfigDataHolder.getInstance().removePushProvider(provider.getName()); } - - @Reference( - name = "org.wso2.carbon.identity.secret.mgt.core.SecretManager", - service = SecretManager.class, - cardinality = ReferenceCardinality.MANDATORY, - policy = ReferencePolicy.DYNAMIC, - unbind = "unsetSecretManager" - ) - private void setSecretManager(SecretManager secretManager) { - - NotificationSenderTenantConfigDataHolder.getInstance().setSecretManager(secretManager); - log.debug("SecretManager set in NotificationSenderTenantConfigDataHolder bundle."); - } - - private void unsetSecretManager(SecretManager secretManager) { - - NotificationSenderTenantConfigDataHolder.getInstance().setSecretManager(null); - log.debug("SecretManager unset in NotificationSenderTenantConfigDataHolder bundle."); - } - - @Reference( - name = "org.wso2.carbon.identity.secret.mgt.core.SecretResolveManager", - service = SecretResolveManager.class, - cardinality = ReferenceCardinality.MANDATORY, - policy = ReferencePolicy.DYNAMIC, - unbind = "unsetSecretResolveManager" - ) - private void setSecretResolveManager(SecretResolveManager secretResolveManager) { - - NotificationSenderTenantConfigDataHolder.getInstance().setSecretResolveManager(secretResolveManager); - log.debug("SecretResolveManager set in NotificationSenderTenantConfigDataHolder bundle."); - } - - private void unsetSecretResolveManager(SecretResolveManager secretResolveManager) { - - NotificationSenderTenantConfigDataHolder.getInstance().setSecretResolveManager(null); - log.debug("SecretResolveManager unset in NotificationSenderTenantConfigDataHolder bundle."); - } } diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderSecretProcessor.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderSecretProcessor.java deleted file mode 100644 index 6206d3da5..000000000 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderSecretProcessor.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright (c) 2025, 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.notification.sender.tenant.config.utils; - -import org.wso2.carbon.identity.notification.sender.tenant.config.internal.NotificationSenderTenantConfigDataHolder; -import org.wso2.carbon.identity.secret.mgt.core.exception.SecretManagementException; -import org.wso2.carbon.identity.secret.mgt.core.model.ResolvedSecret; -import org.wso2.carbon.identity.secret.mgt.core.model.Secret; - -import static org.wso2.carbon.identity.notification.sender.tenant.config.NotificationSenderManagementConstants.SECRET_PROPERTIES; - -public class NotificationSenderSecretProcessor { - - /** - * Encrypt secret property. - * - * @param notificationSender Notification Sender: EMAIL_PROVIDER. - * @param authType Authentication Type - * @param property Authentication Property. - * @param value Authentication Property Value. - * @return Reference of the secret. - * @throws SecretManagementException If an error occurs while encrypting the secret. - */ - public static String encryptCredential(String notificationSender, String authType, String property, String value) - throws SecretManagementException { - - String secretName = buildSecretName(notificationSender, authType, property); - String secretType = notificationSender + SECRET_PROPERTIES; - if (isSecretPropertyExists(secretType, secretName)) { - updateExistingSecretProperty(secretType, secretName, value); - } else { - addNewActionSecretProperty(secretType, secretName, value); - } - return buildSecretReference(secretType, secretName); - } - - /** - * Decrypt secret property. - * - * @param notificationSender Notification Sender: EMAIL_PROVIDER. - * @param authType Authentication Type - * @param property Authentication Property. - * @throws SecretManagementException If an error occurs while decrypting the secret. - */ - public static String decryptCredential(String notificationSender, String authType, String property) - throws SecretManagementException { - - String secretName = buildSecretName(notificationSender, authType, property); - String secretType = notificationSender + SECRET_PROPERTIES; - if (!isSecretPropertyExists(secretType, secretName)) { - throw new SecretManagementException(String.format("Unable to find the Secret Property: %s of " + - "Auth Type: %s and Action ID: %s from the system.", property, authType, notificationSender)); - } - ResolvedSecret resolvedSecret = NotificationSenderTenantConfigDataHolder.getInstance().getSecretResolveManager() - .getResolvedSecret(secretType, secretName); - - return resolvedSecret.getResolvedSecretValue(); - } - - /** - * Create secret name. - * - * @param notificationSender Notification Sender. - * @param authType Authentication Type. - * @param authProperty Authentication Property. - * @return Secret Name. - */ - private static String buildSecretName(String notificationSender, String authType, String authProperty) { - - return notificationSender + ":" + authType + ":" + authProperty; - } - - /** - * Create secret reference name. - * - * @param secretName Name of the secret. - * @return Secret reference name. - * @throws SecretManagementException If an error occurs while retrieving the secret type. - */ - private static String buildSecretReference(String secretType, String secretName) throws SecretManagementException { - - String secretTypeId = NotificationSenderTenantConfigDataHolder.getInstance().getSecretManager() - .getSecretType(secretType).getId(); - return secretTypeId + ":" + secretName; - } - - /** - * Check whether the secret property exists. - * - * @param secretName Secret Name. - * @return True if the secret property exists. - * @throws SecretManagementException If an error occurs while checking the existence of the secret. - */ - private static boolean isSecretPropertyExists(String secretType, String secretName) - throws SecretManagementException { - - return NotificationSenderTenantConfigDataHolder.getInstance().getSecretManager() - .isSecretExist(secretType, secretName); - } - - /** - * Add new Secret for Action secret type. - * - * @param secretType Secret type. - * @param secretName Name of the secret. - * @param value secret value. - * @throws SecretManagementException If an error occurs while adding the secret. - */ - private static void addNewActionSecretProperty(String secretType, String secretName, String value) throws SecretManagementException { - - Secret secret = new Secret(); - secret.setSecretName(secretName); - secret.setSecretValue(value); - NotificationSenderTenantConfigDataHolder.getInstance().getSecretManager().addSecret(secretType, secret); - } - - /** - * Update an existing secret of Action secret type. - * - * @param secretType Secret type. - * @param secretName Name of the secret. - * @param value secret value. - * @throws SecretManagementException If an error occurs while adding the secret. - */ - private static void updateExistingSecretProperty(String secretType, String secretName, String value) - throws SecretManagementException { - - ResolvedSecret resolvedSecret = NotificationSenderTenantConfigDataHolder.getInstance().getSecretResolveManager() - .getResolvedSecret(secretType, secretName); - if (!resolvedSecret.getResolvedSecretValue().equals(value)) { - NotificationSenderTenantConfigDataHolder.getInstance().getSecretManager() - .updateSecretValue(secretType, secretName, value); - } - } -} diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderUtils.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderUtils.java index 139a832fd..ba0066892 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderUtils.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/utils/NotificationSenderUtils.java @@ -18,13 +18,10 @@ package org.wso2.carbon.identity.notification.sender.tenant.config.utils; -import org.apache.commons.io.Charsets; import org.apache.commons.lang.StringUtils; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.wso2.carbon.core.util.CryptoException; -import org.wso2.carbon.core.util.CryptoUtil; import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils; import org.wso2.carbon.identity.configuration.mgt.core.model.Attribute; import org.wso2.carbon.identity.configuration.mgt.core.model.Resource; @@ -44,7 +41,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -596,30 +592,4 @@ public static PushSenderData buildPushSenderData(PushSenderDTO pushSenderDTO) { pushSenderData.setProviderId(pushSenderDTO.getProviderId()); return pushSenderData; } - - /** - * Encrypt the given text. - * - * @param plainText text to be encrypted. - * @return encrypted claim value. - */ - public static String encrypt(String plainText) throws CryptoException { - - if (plainText.isEmpty()) { - return plainText; - } - return CryptoUtil.getDefaultCryptoUtil().encryptAndBase64Encode(plainText.getBytes(StandardCharsets.UTF_8)); - } - - /** - * Decrypt the given cipher text. - * - * @param cipherText The string which needs to be decrypted - * @return Base64 decoded string - * @throws CryptoException On an error during decryption - */ - public static String decrypt(String cipherText) throws CryptoException { - - return new String(CryptoUtil.getDefaultCryptoUtil().base64DecodeAndDecrypt(cipherText), Charsets.UTF_8); - } } diff --git a/pom.xml b/pom.xml index 8865ac2a9..65d2b32f5 100644 --- a/pom.xml +++ b/pom.xml @@ -169,11 +169,6 @@ org.wso2.carbon.identity.application.common ${carbon.identity.framework.version} - - org.wso2.carbon.identity.framework - org.wso2.carbon.identity.secret.mgt.core - ${carbon.identity.framework.version} - From 5cbf53fc6b41044d73c8f2a3383d6a459c3ed234 Mon Sep 17 00:00:00 2001 From: lashini Date: Wed, 29 Oct 2025 14:04:15 +0530 Subject: [PATCH 3/3] Return client secret of email provider from service level --- .../NotificationSenderManagementServiceImpl.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java index 8c4a97f34..d89b2fc0e 100644 --- a/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java +++ b/components/notification-sender-config/org.wso2.carbon.identity.notification.sender.tenant.config/src/main/java/org/wso2/carbon/identity/notification/sender/tenant/config/NotificationSenderManagementServiceImpl.java @@ -871,9 +871,16 @@ private EmailSenderDTO buildEmailSenderFromResource(Resource resource) throws } emailSender.getProperties().put(CLIENT_ID, value); break; - /* Client secret needs to be ignored as it is not supported with v1 and secrets are - not included in the v2 response. */ + /* Client secret will be dropped from the response from api level, However returning the secret is + required at this level to support the patch request. */ case CLIENT_SECRET: + try { + value = decryptCredential(EMAIL_PROVIDER, CLIENT_CREDENTIAL, CLIENT_SECRET); + } catch (SecretManagementException e) { + throw new NotificationSenderManagementServerException( + ERROR_CODE_ERROR_WHILE_DECRYPTING_CREDENTIALS, e.getMessage(), e); + } + emailSender.getProperties().put(CLIENT_SECRET, value); break; case AUTH_TYPE: emailSender.setAuthType(value);