Skip to content

Commit

Permalink
KEYCLOAK-1187 DB migration support for oauth/application to client
Browse files Browse the repository at this point in the history
  • Loading branch information
stianst committed Apr 10, 2015
1 parent 1567982 commit 4ae9310
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 153 deletions.

This file was deleted.

Expand Up @@ -42,6 +42,11 @@
<constraints nullable="false"/> <constraints nullable="false"/>
</column> </column>
</addColumn> </addColumn>
<update tableName="CLIENT">
<column name="CONSENT_REQUIRED" valueBoolean="true"/>
<where>DTYPE = 'OAuthClientEntity'</where>
</update>
<dropColumn tableName="CLIENT" columnName="DTYPE"/> <dropColumn tableName="CLIENT" columnName="DTYPE"/>
<renameColumn tableName="CLIENT" newColumnName="CLIENT_ID" oldColumnName="NAME"/>
</changeSet> </changeSet>
</databaseChangeLog> </databaseChangeLog>
Expand Up @@ -10,6 +10,7 @@
import org.keycloak.connections.mongo.updater.impl.updates.Update1_0_0_Final; import org.keycloak.connections.mongo.updater.impl.updates.Update1_0_0_Final;
import org.keycloak.connections.mongo.updater.impl.updates.Update1_1_0_Beta1; import org.keycloak.connections.mongo.updater.impl.updates.Update1_1_0_Beta1;
import org.keycloak.connections.mongo.updater.impl.updates.Update1_2_0_Beta1; import org.keycloak.connections.mongo.updater.impl.updates.Update1_2_0_Beta1;
import org.keycloak.connections.mongo.updater.impl.updates.Update1_2_0_RC1;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;


import java.util.Date; import java.util.Date;
Expand All @@ -28,7 +29,8 @@ public class DefaultMongoUpdaterProvider implements MongoUpdaterProvider {
private Class<? extends Update>[] updates = new Class[]{ private Class<? extends Update>[] updates = new Class[]{
Update1_0_0_Final.class, Update1_0_0_Final.class,
Update1_1_0_Beta1.class, Update1_1_0_Beta1.class,
Update1_2_0_Beta1.class Update1_2_0_Beta1.class,
Update1_2_0_RC1.class
}; };


@Override @Override
Expand Down
Expand Up @@ -53,15 +53,8 @@ protected void deleteEntries(String collection) {
log.debugv("Deleted entries from {0}", collection); log.debugv("Deleted entries from {0}", collection);
} }


protected String insertApplicationRole(DBCollection roles, String roleName, String applicationId) { protected void renameCollection(String collection, String newName) {
BasicDBObject role = new BasicDBObject(); db.getCollection(collection).rename(newName);
String roleId = KeycloakModelUtils.generateId();
role.append("_id", roleId);
role.append("name", roleName);
role.append("applicationId", applicationId);
role.append("nameIndex", applicationId + "//" + roleName);
roles.insert(role);
return roleId;
} }


public void setLog(Logger log) { public void setLog(Logger log) {
Expand Down
Expand Up @@ -266,4 +266,15 @@ private void addDefaultMappers(KeycloakSession session, DBCollection clients) {
} }
} }


private String insertApplicationRole(DBCollection roles, String roleName, String applicationId) {
BasicDBObject role = new BasicDBObject();
String roleId = KeycloakModelUtils.generateId();
role.append("_id", roleId);
role.append("name", roleName);
role.append("applicationId", applicationId);
role.append("nameIndex", applicationId + "//" + roleName);
roles.insert(role);
return roleId;
}

} }
@@ -0,0 +1,51 @@
package org.keycloak.connections.mongo.updater.impl.updates;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import org.keycloak.models.KeycloakSession;

/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class Update1_2_0_RC1 extends Update {

@Override
public String getId() {
return "1.2.0.RC1";
}

@Override
public void update(KeycloakSession session) {
convertApplicationsToClients();
convertOAuthClientsToClients();
}

private void convertApplicationsToClients() {
DBCollection applications = db.getCollection("applications");
applications.update(new BasicDBObject(), new BasicDBObject("$set", new BasicDBObject("consentRequired", false)), false, true);
applications.update(new BasicDBObject(), new BasicDBObject("$rename", new BasicDBObject("name", "clientId")), false, true);
renameCollection("applications", "clients");
log.debugv("Converted applications to clients");

DBCollection roles = db.getCollection("roles");
roles.update(new BasicDBObject(), new BasicDBObject("$rename", new BasicDBObject("applicationId", "clientId")), false, true);
log.debugv("Renamed roles.applicationId to roles.clientId");
}

private void convertOAuthClientsToClients() {
DBCollection clients = db.getCollection("clients");
DBCollection oauthClients = db.getCollection("oauthClients");
oauthClients.update(new BasicDBObject(), new BasicDBObject("$rename", new BasicDBObject("name", "clientId")), false, true);
oauthClients.update(new BasicDBObject(), new BasicDBObject("$set", new BasicDBObject("consentRequired", true)), false, true);

DBCursor curs = oauthClients.find();
while (curs.hasNext()) {
clients.insert(curs.next());
}

oauthClients.drop();
log.debugv("Converted oauthClients to clients");
}

}
Expand Up @@ -8,9 +8,9 @@
/** /**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a> * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/ */
public class ApplicationEntity extends AbstractIdentifiableEntity { public class ClientEntity extends AbstractIdentifiableEntity {


private String name; private String clientId;
private String realmId; private String realmId;
private boolean enabled; private boolean enabled;
private String secret; private String secret;
Expand Down Expand Up @@ -41,12 +41,12 @@ public class ApplicationEntity extends AbstractIdentifiableEntity {
private List<ClientIdentityProviderMappingEntity> identityProviders = new ArrayList<ClientIdentityProviderMappingEntity>(); private List<ClientIdentityProviderMappingEntity> identityProviders = new ArrayList<ClientIdentityProviderMappingEntity>();
private List<ProtocolMapperEntity> protocolMappers = new ArrayList<ProtocolMapperEntity>(); private List<ProtocolMapperEntity> protocolMappers = new ArrayList<ProtocolMapperEntity>();


public String getName() { public String getClientId() {
return name; return clientId;
} }


public void setName(String name) { public void setClientId(String clientId) {
this.name = name; this.clientId = clientId;
} }


public boolean isEnabled() { public boolean isEnabled() {
Expand Down
Expand Up @@ -33,7 +33,7 @@
import org.keycloak.connections.file.InMemoryModel; import org.keycloak.connections.file.InMemoryModel;
import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.entities.ApplicationEntity; import org.keycloak.models.entities.ClientEntity;
import org.keycloak.models.entities.ClientIdentityProviderMappingEntity; import org.keycloak.models.entities.ClientIdentityProviderMappingEntity;
import org.keycloak.models.entities.ProtocolMapperEntity; import org.keycloak.models.entities.ProtocolMapperEntity;
import org.keycloak.models.entities.RoleEntity; import org.keycloak.models.entities.RoleEntity;
Expand All @@ -48,13 +48,13 @@ public class ClientAdapter implements ClientModel {


private final RealmModel realm; private final RealmModel realm;
private KeycloakSession session; private KeycloakSession session;
private final ApplicationEntity entity; private final ClientEntity entity;
private final InMemoryModel inMemoryModel; private final InMemoryModel inMemoryModel;


private final Map<String, RoleAdapter> allRoles = new HashMap<String, RoleAdapter>(); private final Map<String, RoleAdapter> allRoles = new HashMap<String, RoleAdapter>();
private final Map<String, RoleModel> allScopeMappings = new HashMap<String, RoleModel>(); private final Map<String, RoleModel> allScopeMappings = new HashMap<String, RoleModel>();


public ClientAdapter(KeycloakSession session, RealmModel realm, ApplicationEntity entity, InMemoryModel inMemoryModel) { public ClientAdapter(KeycloakSession session, RealmModel realm, ClientEntity entity, InMemoryModel inMemoryModel) {
this.realm = realm; this.realm = realm;
this.session = session; this.session = session;
this.entity = entity; this.entity = entity;
Expand Down Expand Up @@ -406,13 +406,13 @@ public boolean isAllowedRetrieveTokenFromIdentityProvider(String providerId) {


@Override @Override
public String getClientId() { public String getClientId() {
return entity.getName(); return entity.getClientId();
} }


@Override @Override
public void setClientId(String clientId) { public void setClientId(String clientId) {
if (appNameExists(clientId)) throw new ModelDuplicateException("Application named " + clientId + " already exists."); if (appNameExists(clientId)) throw new ModelDuplicateException("Application named " + clientId + " already exists.");
entity.setName(clientId); entity.setClientId(clientId);
} }


private boolean appNameExists(String name) { private boolean appNameExists(String name) {
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.keycloak.models.RoleModel; import org.keycloak.models.RoleModel;
import org.keycloak.models.UserFederationProviderModel; import org.keycloak.models.UserFederationProviderModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.entities.ApplicationEntity; import org.keycloak.models.entities.ClientEntity;
import org.keycloak.models.entities.IdentityProviderMapperEntity; import org.keycloak.models.entities.IdentityProviderMapperEntity;
import org.keycloak.models.entities.RealmEntity; import org.keycloak.models.entities.RealmEntity;
import org.keycloak.models.entities.RequiredCredentialEntity; import org.keycloak.models.entities.RequiredCredentialEntity;
Expand Down Expand Up @@ -626,9 +626,9 @@ public ClientModel addClient(String id, String clientId) {
throw new ModelDuplicateException("Application named '" + clientId + "' already exists."); throw new ModelDuplicateException("Application named '" + clientId + "' already exists.");
} }


ApplicationEntity appEntity = new ApplicationEntity(); ClientEntity appEntity = new ClientEntity();
appEntity.setId(id); appEntity.setId(id);
appEntity.setName(clientId); appEntity.setClientId(clientId);
appEntity.setRealmId(getId()); appEntity.setRealmId(getId());
appEntity.setEnabled(true); appEntity.setEnabled(true);


Expand Down
Expand Up @@ -475,12 +475,12 @@ public void updateApplication() {


@Override @Override
public String getClientId() { public String getClientId() {
return entity.getName(); return entity.getClientId();
} }


@Override @Override
public void setClientId(String clientId) { public void setClientId(String clientId) {
entity.setName(clientId); entity.setClientId(clientId);
} }


@Override @Override
Expand Down
Expand Up @@ -635,7 +635,7 @@ public ClientModel addClient(String name) {
public ClientModel addClient(String id, String clientId) { public ClientModel addClient(String id, String clientId) {
ClientEntity applicationData = new ClientEntity(); ClientEntity applicationData = new ClientEntity();
applicationData.setId(id); applicationData.setId(id);
applicationData.setName(clientId); applicationData.setClientId(clientId);
applicationData.setEnabled(true); applicationData.setEnabled(true);
applicationData.setRealm(realm); applicationData.setRealm(realm);
realm.getApplications().add(applicationData); realm.getApplications().add(applicationData);
Expand Down
Expand Up @@ -26,14 +26,14 @@
* @version $Revision: 1 $ * @version $Revision: 1 $
*/ */
@Entity @Entity
@Table(name="CLIENT", uniqueConstraints = {@UniqueConstraint(columnNames = {"REALM_ID", "NAME"})}) @Table(name="CLIENT", uniqueConstraints = {@UniqueConstraint(columnNames = {"REALM_ID", "CLIENT_ID"})})
public class ClientEntity { public class ClientEntity {


@Id @Id
@Column(name="ID", length = 36) @Column(name="ID", length = 36)
private String id; private String id;
@Column(name = "NAME") @Column(name = "CLIENT_ID")
private String name; private String clientId;
@Column(name="ENABLED") @Column(name="ENABLED")
private boolean enabled; private boolean enabled;
@Column(name="SECRET") @Column(name="SECRET")
Expand Down Expand Up @@ -133,12 +133,12 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled; this.enabled = enabled;
} }


public String getName() { public String getClientId() {
return name; return clientId;
} }


public void setName(String name) { public void setClientId(String clientId) {
this.name = name; this.clientId = clientId;
} }


public Set<String> getWebOrigins() { public Set<String> getWebOrigins() {
Expand Down

0 comments on commit 4ae9310

Please sign in to comment.