forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEYCLOAK-9129 Don't expose Keycloak version in resource paths
- Loading branch information
Showing
9 changed files
with
193 additions
and
27 deletions.
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
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 |
---|---|---|
|
@@ -17,40 +17,77 @@ | |
|
||
package org.keycloak.models.jpa; | ||
|
||
import org.keycloak.common.util.Time; | ||
import org.keycloak.migration.MigrationModel; | ||
import org.keycloak.models.jpa.entities.MigrationModelEntity; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.TypedQuery; | ||
import java.security.SecureRandom; | ||
import java.util.List; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Bill Burke</a> | ||
* @version $Revision: 1 $ | ||
*/ | ||
public class MigrationModelAdapter implements MigrationModel { | ||
protected EntityManager em; | ||
protected MigrationModelEntity latest; | ||
|
||
private static final int RESOURCE_TAG_LENGTH = 5; | ||
private static final char[] RESOURCE_TAG_CHARSET = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray(); | ||
|
||
public MigrationModelAdapter(EntityManager em) { | ||
this.em = em; | ||
init(); | ||
} | ||
|
||
@Override | ||
public String getStoredVersion() { | ||
MigrationModelEntity entity = em.find(MigrationModelEntity.class, MigrationModelEntity.SINGLETON_ID); | ||
if (entity == null) return null; | ||
return entity.getVersion(); | ||
return latest != null ? latest.getVersion() : null; | ||
} | ||
|
||
@Override | ||
public void setStoredVersion(String version) { | ||
MigrationModelEntity entity = em.find(MigrationModelEntity.class, MigrationModelEntity.SINGLETON_ID); | ||
if (entity == null) { | ||
entity = new MigrationModelEntity(); | ||
entity.setId(MigrationModelEntity.SINGLETON_ID); | ||
entity.setVersion(version); | ||
em.persist(entity); | ||
public String getResourcesTag() { | ||
return latest != null ? latest.getId() : null; | ||
} | ||
|
||
private void init() { | ||
TypedQuery<MigrationModelEntity> q = em.createNamedQuery("getLatest", MigrationModelEntity.class); | ||
q.setMaxResults(1); | ||
List<MigrationModelEntity> l = q.getResultList(); | ||
if (l.isEmpty()) { | ||
latest = null; | ||
} else { | ||
entity.setVersion(version); | ||
em.flush(); | ||
latest = l.get(0); | ||
} | ||
} | ||
|
||
@Override | ||
public void setStoredVersion(String version) { | ||
String resourceTag = createResourceTag(); | ||
|
||
// Make sure resource-tag is unique within current installation | ||
while (em.find(MigrationModelEntity.class, resourceTag) != null) { | ||
resourceTag = createResourceTag(); | ||
} | ||
|
||
MigrationModelEntity entity = new MigrationModelEntity(); | ||
entity.setId(resourceTag); | ||
entity.setVersion(version); | ||
entity.setUpdatedTime(Time.currentTime()); | ||
|
||
em.persist(entity); | ||
|
||
latest = entity; | ||
} | ||
|
||
private String createResourceTag() { | ||
StringBuilder sb = new StringBuilder(RESOURCE_TAG_LENGTH); | ||
for (int i = 0; i < RESOURCE_TAG_LENGTH; i++) { | ||
sb.append(RESOURCE_TAG_CHARSET[new SecureRandom().nextInt(RESOURCE_TAG_CHARSET.length)]); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -22,14 +22,20 @@ | |
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.NamedQueries; | ||
import javax.persistence.NamedQuery; | ||
import javax.persistence.Table; | ||
import java.util.Date; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Bill Burke</a> | ||
* @version $Revision: 1 $ | ||
*/ | ||
@Table(name="MIGRATION_MODEL") | ||
@Entity | ||
@NamedQueries({ | ||
@NamedQuery(name = "getLatest", query = "select m from MigrationModelEntity m ORDER BY m.updatedTime DESC") | ||
}) | ||
public class MigrationModelEntity { | ||
public static final String SINGLETON_ID = "SINGLETON"; | ||
@Id | ||
|
@@ -40,6 +46,9 @@ public class MigrationModelEntity { | |
@Column(name="VERSION", length = 36) | ||
protected String version; | ||
|
||
@Column(name="UPDATE_TIME") | ||
protected long updatedTime; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
@@ -56,6 +65,14 @@ public void setVersion(String version) { | |
this.version = version; | ||
} | ||
|
||
public long getUpdateTime() { | ||
return updatedTime; | ||
} | ||
|
||
public void setUpdatedTime(long updatedTime) { | ||
this.updatedTime = updatedTime; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
|
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
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
70 changes: 70 additions & 0 deletions
70
...-arquillian/tests/base/src/test/java/org/keycloak/testsuite/model/MigrationModelTest.java
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,70 @@ | ||
package org.keycloak.testsuite.model; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.keycloak.common.Version; | ||
import org.keycloak.common.util.Time; | ||
import org.keycloak.connections.jpa.JpaConnectionProvider; | ||
import org.keycloak.migration.MigrationModel; | ||
import org.keycloak.models.jpa.entities.MigrationModelEntity; | ||
import org.keycloak.representations.idm.RealmRepresentation; | ||
import org.keycloak.testsuite.AbstractKeycloakTest; | ||
import org.keycloak.testsuite.runonserver.RunOnServerDeployment; | ||
import org.keycloak.testsuite.runonserver.RunOnServerTest; | ||
|
||
import javax.persistence.EntityManager; | ||
import java.util.List; | ||
|
||
public class MigrationModelTest extends AbstractKeycloakTest { | ||
|
||
@Deployment | ||
public static WebArchive deploy() { | ||
return RunOnServerDeployment.create(MigrationModelTest.class); | ||
} | ||
|
||
@Override | ||
public void addTestRealms(List<RealmRepresentation> testRealms) { | ||
} | ||
|
||
@Test | ||
public void test() { | ||
testingClient.server().run(session -> { | ||
String currentVersion = Version.VERSION_KEYCLOAK.split("-")[0]; | ||
|
||
JpaConnectionProvider p = session.getProvider(JpaConnectionProvider.class); | ||
EntityManager em = p.getEntityManager(); | ||
|
||
List<MigrationModelEntity> l = em.createQuery("select m from MigrationModelEntity m ORDER BY m.updatedTime DESC", MigrationModelEntity.class).getResultList(); | ||
Assert.assertEquals(1, l.size()); | ||
Assert.assertTrue(l.get(0).getId().matches("[\\da-z]{5}")); | ||
Assert.assertEquals(currentVersion, l.get(0).getVersion()); | ||
|
||
MigrationModel m = session.realms().getMigrationModel(); | ||
Assert.assertEquals(currentVersion, m.getStoredVersion()); | ||
Assert.assertEquals(m.getResourcesTag(), l.get(0).getId()); | ||
|
||
Time.setOffset(-5000); | ||
|
||
session.realms().getMigrationModel().setStoredVersion("6.0.0"); | ||
em.flush(); | ||
|
||
Time.setOffset(0); | ||
|
||
l = em.createQuery("select m from MigrationModelEntity m ORDER BY m.updatedTime DESC", MigrationModelEntity.class).getResultList(); | ||
Assert.assertEquals(2, l.size()); | ||
Assert.assertTrue(l.get(0).getId().matches("[\\da-z]{5}")); | ||
Assert.assertEquals(currentVersion, l.get(0).getVersion()); | ||
Assert.assertTrue(l.get(1).getId().matches("[\\da-z]{5}")); | ||
Assert.assertEquals("6.0.0", l.get(1).getVersion()); | ||
|
||
m = session.realms().getMigrationModel(); | ||
Assert.assertEquals(l.get(0).getId(), m.getResourcesTag()); | ||
Assert.assertEquals(currentVersion, m.getStoredVersion()); | ||
|
||
em.remove(l.get(1)); | ||
}); | ||
} | ||
|
||
} |