Fix some Email Issue (#11025)

* Fix some Email Issue

* Restore default email setting

* Removed Unused config
This commit is contained in:
Mohit Yadav 2023-04-12 19:11:10 +05:30 committed by GitHub
parent 34b0d01e98
commit a7760dc2be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 9 deletions

View File

@ -125,6 +125,9 @@ public class OpenMetadataApplication extends Application<OpenMetadataApplication
ChangeEventConfig.initialize(catalogConfig);
final Jdbi jdbi = createAndSetupJDBI(environment, catalogConfig.getDataSourceFactory());
// Configure the Fernet instance
Fernet.getInstance().setFernetKey(catalogConfig);
// Init Settings Cache
SettingsCache.initialize(jdbi.onDemand(CollectionDAO.class), catalogConfig);
@ -135,8 +138,6 @@ public class OpenMetadataApplication extends Application<OpenMetadataApplication
// init Entity Masker
EntityMaskerFactory.createEntityMasker(catalogConfig.getSecurityConfiguration());
// Configure the Fernet instance
Fernet.getInstance().setFernetKey(catalogConfig);
// Instantiate JWT Token Generator
JWTTokenGenerator.getInstance().init(catalogConfig.getJwtTokenConfiguration());

View File

@ -120,9 +120,7 @@ public class SystemRepository {
try {
if (setting.getConfigType() == SettingsType.EMAIL_CONFIGURATION) {
SmtpSettings emailConfig = JsonUtils.convertValue(setting.getConfigValue(), SmtpSettings.class);
if (!Fernet.isTokenized(emailConfig.getPassword())) {
setting.setConfigValue(encryptSetting(emailConfig));
}
setting.setConfigValue(encryptSetting(emailConfig));
// Invalidate Setting
SettingsCache.getInstance().invalidateSettings(SettingsType.EMAIL_CONFIGURATION.value());
}
@ -133,7 +131,7 @@ public class SystemRepository {
}
public static SmtpSettings encryptSetting(SmtpSettings decryptedSetting) {
if (Fernet.getInstance().isKeyDefined()) {
if (Fernet.getInstance().isKeyDefined() && !Fernet.isTokenized(decryptedSetting.getPassword())) {
String encryptedPwd = Fernet.getInstance().encrypt(decryptedSetting.getPassword());
return decryptedSetting.withPassword(encryptedPwd);
}
@ -141,7 +139,7 @@ public class SystemRepository {
}
public static SmtpSettings decryptSetting(SmtpSettings encryptedSetting) {
if (Fernet.getInstance().isKeyDefined()) {
if (Fernet.getInstance().isKeyDefined() && Fernet.isTokenized(encryptedSetting.getPassword())) {
String decryptedPassword = Fernet.getInstance().decrypt(encryptedSetting.getPassword());
return encryptedSetting.withPassword(decryptedPassword);
}

View File

@ -32,6 +32,7 @@ import javax.ws.rs.core.UriInfo;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.common.utils.CommonUtil;
import org.openmetadata.schema.settings.Settings;
import org.openmetadata.schema.settings.SettingsType;
import org.openmetadata.schema.type.Include;
import org.openmetadata.schema.util.EntitiesCount;
import org.openmetadata.schema.util.ServicesCount;
@ -56,6 +57,7 @@ public class SystemResource {
public static final String COLLECTION_PATH = "/v1/util";
private final SystemRepository systemRepository;
private final Authorizer authorizer;
private OpenMetadataApplicationConfig applicationConfig;
public SystemResource(CollectionDAO dao, Authorizer authorizer) {
Objects.requireNonNull(dao, "SystemRepository must not be null");
@ -65,10 +67,11 @@ public class SystemResource {
@SuppressWarnings("unused") // Method used for reflection
public void initialize(OpenMetadataApplicationConfig config) throws IOException {
initSettings(config);
this.applicationConfig = config;
initSettings();
}
private void initSettings(OpenMetadataApplicationConfig applicationConfig) throws IOException {
private void initSettings() throws IOException {
List<String> jsonDataFiles = EntityUtil.getJsonDataResources(".*json/data/settings/settingsData.json$");
if (jsonDataFiles.size() != 1) {
LOG.warn("Invalid number of jsonDataFiles {}. Only one expected.", jsonDataFiles.size());
@ -184,6 +187,30 @@ public class SystemResource {
return systemRepository.patchSetting(settingName, patch);
}
@PUT
@Path("/restore/default/email")
@Operation(
operationId = "restoreEmailSettingToDefault",
summary = "Restore Email to Default setting",
description = "Restore Email to Default settings",
responses = {
@ApiResponse(
responseCode = "200",
description = "Settings",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Settings.class)))
})
public Response restoreDefaultEmailSetting(
@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Name of the setting", schema = @Schema(type = "string")) @PathParam("settingName")
String name) {
authorizer.authorizeAdmin(securityContext);
return systemRepository.createOrUpdate(
new Settings()
.withConfigType(SettingsType.EMAIL_CONFIGURATION)
.withConfigValue(applicationConfig.getSmtpSettings()));
}
@GET
@Path("/entities/count")
@Operation(