-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GEN-927 - Add bot default roles #18256
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,12 +18,15 @@ | |
import static javax.ws.rs.core.Response.Status.FORBIDDEN; | ||
import static javax.ws.rs.core.Response.Status.OK; | ||
import static org.openmetadata.common.utils.CommonUtil.listOf; | ||
import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty; | ||
import static org.openmetadata.schema.api.teams.CreateUser.CreatePasswordType.ADMIN_CREATE; | ||
import static org.openmetadata.schema.auth.ChangePasswordRequest.RequestType.SELF; | ||
import static org.openmetadata.schema.entity.teams.AuthenticationMechanism.AuthType.BASIC; | ||
import static org.openmetadata.schema.entity.teams.AuthenticationMechanism.AuthType.JWT; | ||
import static org.openmetadata.schema.type.Include.ALL; | ||
import static org.openmetadata.service.exception.CatalogExceptionMessage.EMAIL_SENDING_ISSUE; | ||
import static org.openmetadata.service.jdbi3.RoleRepository.DEFAULT_BOT_ROLE; | ||
import static org.openmetadata.service.jdbi3.RoleRepository.DOMAIN_ONLY_ACCESS_ROLE; | ||
import static org.openmetadata.service.jdbi3.UserRepository.AUTH_MECHANISM_FIELD; | ||
import static org.openmetadata.service.secrets.ExternalSecretsManager.NULL_SECRET_STRING; | ||
import static org.openmetadata.service.security.jwt.JWTTokenGenerator.getExpiryDate; | ||
|
@@ -51,6 +54,7 @@ | |
import java.io.IOException; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
@@ -104,7 +108,6 @@ | |
import org.openmetadata.schema.auth.RegistrationRequest; | ||
import org.openmetadata.schema.auth.RevokePersonalTokenRequest; | ||
import org.openmetadata.schema.auth.RevokeTokenRequest; | ||
import org.openmetadata.schema.auth.SSOAuthMechanism; | ||
import org.openmetadata.schema.auth.ServiceTokenType; | ||
import org.openmetadata.schema.auth.TokenRefreshRequest; | ||
import org.openmetadata.schema.auth.TokenType; | ||
|
@@ -125,6 +128,7 @@ | |
import org.openmetadata.service.exception.EntityNotFoundException; | ||
import org.openmetadata.service.jdbi3.CollectionDAO; | ||
import org.openmetadata.service.jdbi3.ListFilter; | ||
import org.openmetadata.service.jdbi3.RoleRepository; | ||
import org.openmetadata.service.jdbi3.TokenRepository; | ||
import org.openmetadata.service.jdbi3.UserRepository; | ||
import org.openmetadata.service.jdbi3.UserRepository.UserCsv; | ||
|
@@ -173,6 +177,7 @@ public class UserResource extends EntityResource<User, UserRepository> { | |
public static final String USER_PROTECTED_FIELDS = "authenticationMechanism"; | ||
private final JWTTokenGenerator jwtTokenGenerator; | ||
private final TokenRepository tokenRepository; | ||
private final RoleRepository roleRepository; | ||
private AuthenticationConfiguration authenticationConfiguration; | ||
private AuthorizerConfiguration authorizerConfiguration; | ||
private final AuthenticatorHandler authHandler; | ||
|
@@ -197,6 +202,7 @@ public UserResource( | |
jwtTokenGenerator = JWTTokenGenerator.getInstance(); | ||
allowedFields.remove(USER_PROTECTED_FIELDS); | ||
tokenRepository = Entity.getTokenRepository(); | ||
roleRepository = Entity.getRoleRepository(); | ||
UserTokenCache.initialize(); | ||
authHandler = authenticatorHandler; | ||
} | ||
|
@@ -567,6 +573,7 @@ public Response createUser( | |
User user = getUser(securityContext.getUserPrincipal().getName(), create); | ||
if (Boolean.TRUE.equals(user.getIsBot())) { | ||
addAuthMechanismToBot(user, create, uriInfo); | ||
addRolesToBot(user, uriInfo); | ||
} | ||
|
||
// | ||
|
@@ -696,8 +703,8 @@ public Response createOrUpdateUser( | |
new OperationContext(entityType, EntityUtil.createOrUpdateOperation(resourceContext)); | ||
authorizer.authorize(securityContext, createOperationContext, resourceContext); | ||
} | ||
if (Boolean.TRUE.equals(create.getIsBot())) { // TODO expect bot to be created separately | ||
return createOrUpdateBot(user, create, uriInfo, securityContext); | ||
if (Boolean.TRUE.equals(create.getIsBot())) { | ||
return createOrUpdateBotUser(user, create, uriInfo, securityContext); | ||
} | ||
PutResponse<User> response = repository.createOrUpdate(uriInfo, user); | ||
addHref(uriInfo, response.getEntity()); | ||
|
@@ -1454,7 +1461,7 @@ public void validateEmailAlreadyExists(String email) { | |
} | ||
} | ||
|
||
private Response createOrUpdateBot( | ||
private Response createOrUpdateBotUser( | ||
User user, CreateUser create, UriInfo uriInfo, SecurityContext securityContext) { | ||
User original = retrieveBotUser(user, uriInfo); | ||
String botName = create.getBotName(); | ||
|
@@ -1488,8 +1495,9 @@ && userHasRelationshipWithAnyBot(original, bot)) { | |
original.getAuthenticationMechanism()); | ||
user.setRoles(original.getRoles()); | ||
} | ||
// TODO remove this | ||
// TODO remove this -> Still valid TODO? | ||
addAuthMechanismToBot(user, create, uriInfo); | ||
addRolesToBot(user, uriInfo); | ||
PutResponse<User> response = repository.createOrUpdate(uriInfo, user); | ||
decryptOrNullify(securityContext, response.getEntity()); | ||
return response.toResponse(); | ||
|
@@ -1531,41 +1539,54 @@ private List<CollectionDAO.EntityRelationshipRecord> retrieveBotRelationshipsFor | |
return repository.findToRecords(bot.getId(), Entity.BOT, Relationship.CONTAINS, Entity.USER); | ||
} | ||
|
||
// TODO remove this | ||
// TODO remove this -> still valid TODO? | ||
private void addAuthMechanismToBot(User user, @Valid CreateUser create, UriInfo uriInfo) { | ||
if (!Boolean.TRUE.equals(user.getIsBot())) { | ||
throw new IllegalArgumentException( | ||
"Authentication mechanism change is only supported for bot users"); | ||
} | ||
if (isValidAuthenticationMechanism(create)) { | ||
AuthenticationMechanism authMechanism = create.getAuthenticationMechanism(); | ||
AuthenticationMechanism.AuthType authType = authMechanism.getAuthType(); | ||
switch (authType) { | ||
case JWT -> { | ||
User original = retrieveBotUser(user, uriInfo); | ||
if (original == null | ||
|| !hasAJWTAuthMechanism(user, original.getAuthenticationMechanism())) { | ||
JWTAuthMechanism jwtAuthMechanism = | ||
JsonUtils.convertValue(authMechanism.getConfig(), JWTAuthMechanism.class); | ||
authMechanism.setConfig( | ||
jwtTokenGenerator.generateJWTToken(user, jwtAuthMechanism.getJWTTokenExpiry())); | ||
} else { | ||
authMechanism = original.getAuthenticationMechanism(); | ||
} | ||
} | ||
case SSO -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we now only support the UI to create JWT bots, took the chance to remove this |
||
SSOAuthMechanism ssoAuthMechanism = | ||
JsonUtils.convertValue(authMechanism.getConfig(), SSOAuthMechanism.class); | ||
authMechanism.setConfig(ssoAuthMechanism); | ||
} | ||
default -> throw new IllegalArgumentException( | ||
String.format("Not supported authentication mechanism type: [%s]", authType.value())); | ||
} | ||
user.setAuthenticationMechanism(authMechanism); | ||
AuthenticationMechanism authMechanism = create.getAuthenticationMechanism(); | ||
User original = retrieveBotUser(user, uriInfo); | ||
if (original == null || !hasAJWTAuthMechanism(user, original.getAuthenticationMechanism())) { | ||
JWTAuthMechanism jwtAuthMechanism = | ||
JsonUtils.convertValue(authMechanism.getConfig(), JWTAuthMechanism.class); | ||
authMechanism.setConfig( | ||
jwtTokenGenerator.generateJWTToken(user, jwtAuthMechanism.getJWTTokenExpiry())); | ||
} else { | ||
throw new IllegalArgumentException( | ||
String.format("Authentication mechanism is empty bot user: [%s]", user.getName())); | ||
authMechanism = original.getAuthenticationMechanism(); | ||
} | ||
user.setAuthenticationMechanism(authMechanism); | ||
} | ||
|
||
private void addRolesToBot(User user, UriInfo uriInfo) { | ||
if (!Boolean.TRUE.equals(user.getIsBot())) { | ||
throw new IllegalArgumentException("Bot roles are only supported for bot users"); | ||
} | ||
User original = retrieveBotUser(user, uriInfo); | ||
ArrayList<EntityReference> defaultBotRoles = getDefaultBotRoles(user); | ||
// Keep the incoming roles of the created user | ||
if (!nullOrEmpty(user.getRoles())) { | ||
defaultBotRoles.addAll(user.getRoles()); | ||
} | ||
// If user existed, merge roles | ||
if (original != null && !nullOrEmpty(original.getRoles())) { | ||
defaultBotRoles.addAll(original.getRoles()); | ||
} | ||
user.setRoles(defaultBotRoles); | ||
} | ||
|
||
private ArrayList<EntityReference> getDefaultBotRoles(User user) { | ||
ArrayList<EntityReference> defaultBotRoles = new ArrayList<>(); | ||
EntityReference defaultBotRole = | ||
roleRepository.getReferenceByName(DEFAULT_BOT_ROLE, Include.NON_DELETED); | ||
defaultBotRoles.add(defaultBotRole); | ||
|
||
if (!nullOrEmpty(user.getDomains())) { | ||
EntityReference domainOnlyAccessRole = | ||
roleRepository.getReferenceByName(DOMAIN_ONLY_ACCESS_ROLE, Include.NON_DELETED); | ||
defaultBotRoles.add(domainOnlyAccessRole); | ||
} | ||
return defaultBotRoles; | ||
} | ||
|
||
@Nullable | ||
|
17 changes: 17 additions & 0 deletions
17
openmetadata-service/src/main/resources/json/data/role/DefaultBotRole.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "DefaultBotRole", | ||
"displayName": "Default Bot Role", | ||
"description": "Role Corresponding to a Bot by default.", | ||
"allowDelete": false, | ||
"provider": "system", | ||
"policies" : [ | ||
{ | ||
"type" : "policy", | ||
"name" : "DefaultBotPolicy" | ||
}, | ||
{ | ||
"type" : "policy", | ||
"name" : "DataConsumerPolicy" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@harshach not sure what was the context on this TODO. Is it still valid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets remove it