[Email] Added explicit senderMail in configuration (#8126)

* [Email] Added explicit senderMail in configuration

* Fix failing tests

Co-authored-by: Ayush Shah <ayush@getcollate.io>
This commit is contained in:
Mohit Yadav 2022-10-13 18:24:32 +05:30 committed by GitHub
parent 7975f0e6fe
commit cba9240fe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 11 deletions

View File

@ -243,6 +243,7 @@ email:
supportUrl: ${OM_SUPPORT_URL:-"https://slack.open-metadata.org"}
enableSmtpServer : ${AUTHORIZER_ENABLE_SMTP:-false}
openMetadataUrl: ${OPENMETADATA_SERVER_URL:-""}
senderMail: ${OPENMETADATA_SMTP_SENDER_MAIL:-""}
serverEndpoint: ${SMTP_SERVER_ENDPOINT:-""}
serverPort: ${SMTP_SERVER_PORT:-""}
username: ${SMTP_SERVER_USERNAME:-""}

View File

@ -96,7 +96,9 @@ public class OpenMetadataApplication extends Application<OpenMetadataApplication
throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException,
InvocationTargetException, IOException {
// init email Util for handling
EmailUtil.EmailUtilBuilder.build(catalogConfig.getSmtpSettings());
if (catalogConfig.getSmtpSettings() != null && catalogConfig.getSmtpSettings().getEnableSmtpServer()) {
EmailUtil.EmailUtilBuilder.build(catalogConfig.getSmtpSettings());
}
final Jdbi jdbi = createAndSetupJDBI(environment, catalogConfig.getDataSourceFactory());
final SecretsManager secretsManager =
SecretsManagerFactory.createSecretsManager(

View File

@ -65,12 +65,12 @@ public class UserRepository extends EntityRepository<User> {
}
public final Fields getFieldsWithUserAuth(String fields) {
List<String> tempFields = getAllowedFieldsCopy();
if (fields != null && fields.equals("*")) {
List<String> tempFields = getAllowedFieldsCopy();
tempFields.add("authenticationMechanism");
return new Fields(allowedFields, String.join(",", tempFields));
return new Fields(tempFields, String.join(",", tempFields));
}
return new Fields(allowedFields, fields);
return new Fields(tempFields, fields);
}
@Override

View File

@ -41,6 +41,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jdbi.v3.core.Jdbi;
import org.openmetadata.schema.api.configuration.airflow.AirflowConfiguration;
import org.openmetadata.schema.api.security.AuthenticationConfiguration;
import org.openmetadata.schema.email.SmtpSettings;
import org.openmetadata.schema.entity.Bot;
import org.openmetadata.schema.entity.BotType;
import org.openmetadata.schema.entity.teams.AuthenticationMechanism;
@ -71,6 +72,7 @@ import org.openmetadata.service.security.policyevaluator.SubjectCache;
import org.openmetadata.service.security.policyevaluator.SubjectContext;
import org.openmetadata.service.util.EmailUtil;
import org.openmetadata.service.util.EntityUtil;
import org.openmetadata.service.util.PasswordUtil;
import org.openmetadata.service.util.RestUtil;
@Slf4j
@ -81,8 +83,8 @@ public class DefaultAuthorizer implements Authorizer {
private Set<String> botPrincipalUsers;
private Set<String> testUsers;
private String principalDomain;
private String providerType;
private boolean isSmtpEnabled;
@Override
public void init(OpenMetadataApplicationConfig openMetadataApplicationConfig, Jdbi dbi) {
@ -94,6 +96,8 @@ public class DefaultAuthorizer implements Authorizer {
this.testUsers = new HashSet<>(openMetadataApplicationConfig.getAuthorizerConfiguration().getTestPrincipals());
this.principalDomain = openMetadataApplicationConfig.getAuthorizerConfiguration().getPrincipalDomain();
this.providerType = openMetadataApplicationConfig.getAuthenticationConfiguration().getProvider();
SmtpSettings smtpSettings = openMetadataApplicationConfig.getSmtpSettings();
this.isSmtpEnabled = smtpSettings != null && smtpSettings.getEnableSmtpServer();
SubjectCache.initialize();
PolicyCache.initialize();
RoleCache.initialize();
@ -149,7 +153,12 @@ public class DefaultAuthorizer implements Authorizer {
String[] tokens = adminUser.split(COLON_DELIMITER);
addUserForBasicAuth(tokens[0], tokens[1], domain);
} else {
addUserForBasicAuth(adminUser, DEFAULT_ADMIN, domain);
boolean isDefaultAdmin = adminUser.equals(DEFAULT_ADMIN);
String token = PasswordUtil.generateRandomPassword();
if (isDefaultAdmin || !isSmtpEnabled) {
token = DEFAULT_ADMIN;
}
addUserForBasicAuth(adminUser, token, domain);
}
}
}
@ -170,7 +179,9 @@ public class DefaultAuthorizer implements Authorizer {
User user = user(username, domain, username).withIsAdmin(true).withIsEmailVerified(true);
updateUserWithHashedPwd(user, pwd);
addOrUpdateUser(user);
sendInviteMailToAdmin(user, pwd);
if (isSmtpEnabled) {
sendInviteMailToAdmin(user, pwd);
}
}
}

View File

@ -173,7 +173,7 @@ public class EmailUtil {
&& request.getSubject() != null
&& !request.getSubject().equals("")) {
// Sender Details
emailBuilder.from(defaultSmtpSettings.getUsername());
emailBuilder.from(defaultSmtpSettings.getSenderMail());
// Recipient
request
@ -239,7 +239,7 @@ public class EmailUtil {
EmailPopulatingBuilder emailBuilder = EmailBuilder.startingBlank();
emailBuilder.withSubject(subject);
emailBuilder.to(to);
emailBuilder.from(defaultSmtpSettings.getUsername());
emailBuilder.from(defaultSmtpSettings.getSenderMail());
templateConfiguration.setClassForTemplateLoading(getClass(), baseTemplatePackage);
Template template = templateConfiguration.getTemplate(templatePath);
@ -253,7 +253,7 @@ public class EmailUtil {
}
public void sendMail(Email email) {
if (mailer != null) {
if (mailer != null && defaultSmtpSettings.getEnableSmtpServer()) {
mailer.sendMail(email, true);
}
}

View File

@ -177,7 +177,10 @@ airflowConfiguration:
email:
enableSmtpServer : false
emailingEntity: ""
supportUrl: ""
openMetadataUrl: ""
senderMail: ""
serverEndpoint: ""
serverPort: ""
username: ""

View File

@ -25,6 +25,10 @@
"description": "Openmetadata Server Endpoint",
"type": "string"
},
"senderMail": {
"description": "Mail of the sender",
"type": "string"
},
"serverEndpoint": {
"description": "Smtp Server Endpoint",
"type": "string"
@ -48,5 +52,5 @@
}
},
"additionalProperties": false,
"required": ["serverEndpoint", "serverPort", "username", "password"]
"required": ["serverEndpoint", "serverPort", "username", "password", "senderMail"]
}