Skip to content

Commit

Permalink
Ensure that the table groups is properly escaped since groups is a …
Browse files Browse the repository at this point in the history
…keyword in SQL

Now we can use MySQL 8
Downgrade to PostgreSQL 15 (need to find out exactly what the test matrix should be)
  • Loading branch information
fhanik committed Dec 11, 2024
1 parent 42a2fd0 commit e057424
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions scripts/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: uaa

services:
postgres:
image: "postgres:17.2"
image: "postgres:15"
ports:
- 5432:5432
volumes:
- ./postgresql:/docker-entrypoint-initdb.d/
environment:
- POSTGRES_PASSWORD=changeme
mysql:
image: "mysql:5.7.44-oraclelinux7"
image: "mysql:8"
ports:
- 3306:3306
volumes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import org.cloudfoundry.identity.uaa.zone.JdbcIdentityZoneProvisioning;
import org.cloudfoundry.identity.uaa.zone.MultitenancyFixture;
import org.flywaydb.core.api.migration.Context;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.cloudfoundry.identity.uaa.oauth.common.util.RandomValueStringGenerator;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
Expand All @@ -36,15 +38,23 @@ class V2_7_3__StoreSubDomainAsLowerCase_Tests {
@Autowired
private JdbcTemplate jdbcTemplate;

private Connection connection;

@AfterEach
void closeConnection() {
try {
connection.close();
} catch (Exception ignore) {
}
}
@BeforeEach
void setUpDuplicateZones() throws SQLException {
provisioning = new JdbcIdentityZoneProvisioning(jdbcTemplate);
migration = new V2_7_3__StoreSubDomainAsLowerCase();
generator = new RandomValueStringGenerator(6);

connection = jdbcTemplate.getDataSource().getConnection();
context = mock(Context.class);
when(context.getConnection()).thenReturn(
jdbcTemplate.getDataSource().getConnection());
when(context.getConnection()).thenReturn(connection);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.cloudfoundry.identity.uaa.scim.test;

import org.cloudfoundry.identity.uaa.scim.ScimUser;
import org.cloudfoundry.identity.uaa.util.beans.DbUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;
import java.util.Collections;
import java.util.stream.Stream;

Expand All @@ -14,8 +16,15 @@ public class TestUtils {
public static void deleteFrom(
final JdbcTemplate jdbcTemplate,
final String... tables) {
DbUtils dbUtils = new DbUtils();
Stream.of(tables)
.map(table -> "delete from " + table)
.map(table -> {
try {
return "delete from " + dbUtils.getQuotedIdentifier(table, jdbcTemplate);
} catch (SQLException e) {
throw new RuntimeException(e);
}
})
.forEach(jdbcTemplate::update);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.cloudfoundry.identity.uaa.scim.ScimUser;
import org.cloudfoundry.identity.uaa.scim.endpoints.ScimGroupEndpoints;
import org.cloudfoundry.identity.uaa.scim.endpoints.ScimUserEndpoints;
import org.cloudfoundry.identity.uaa.util.beans.DbUtils;
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneEndpoints;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
Expand Down Expand Up @@ -91,9 +92,11 @@ protected Object getActualFieldValue(String field) {
}

@Test
void test_Ensure_That_New_Fields_NotNull() {
assertThat(webApplicationContext.getBean(JdbcTemplate.class).queryForObject("SELECT count(*) FROM external_group_mapping WHERE origin IS NULL", Integer.class), is(0));
assertThat(webApplicationContext.getBean(JdbcTemplate.class).queryForObject("SELECT count(*) FROM groups WHERE identity_zone_id IS NULL", Integer.class), is(0));
void test_Ensure_That_New_Fields_NotNull() throws Exception {
JdbcTemplate jdbcTemplate = webApplicationContext.getBean(JdbcTemplate.class);
DbUtils dbUtils = webApplicationContext.getBean(DbUtils.class);
assertThat(jdbcTemplate.queryForObject("SELECT count(*) FROM external_group_mapping WHERE origin IS NULL", Integer.class), is(0));
assertThat(jdbcTemplate.queryForObject("SELECT count(*) FROM "+ dbUtils.getQuotedIdentifier("groups", jdbcTemplate) +" WHERE identity_zone_id IS NULL", Integer.class), is(0));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.cloudfoundry.identity.uaa.util.JsonUtils;
import org.cloudfoundry.identity.uaa.util.KeyWithCertTest;
import org.cloudfoundry.identity.uaa.util.SetServerNameRequestPostProcessor;
import org.cloudfoundry.identity.uaa.util.beans.DbUtils;
import org.cloudfoundry.identity.uaa.zone.BrandingInformation;
import org.cloudfoundry.identity.uaa.zone.BrandingInformation.Banner;
import org.cloudfoundry.identity.uaa.zone.Consent;
Expand Down Expand Up @@ -172,6 +173,8 @@ class IdentityZoneEndpointsMockMvcTests {
private MockMvc mockMvc;
private TestClient testClient;

private DbUtils dbUtils;

@BeforeEach
void setUp(
@Autowired WebApplicationContext webApplicationContext,
Expand All @@ -186,6 +189,8 @@ void setUp(
this.mockMvc = mockMvc;
this.testClient = testClient;

dbUtils = webApplicationContext.getBean(DbUtils.class);

UaaClientDetails uaaAdminClient = new UaaClientDetails("uaa-admin-" + generator.generate().toLowerCase(),
null,
"uaa.admin",
Expand Down Expand Up @@ -1737,7 +1742,7 @@ void test_delete_zone_cleans_db() throws Exception {

assertThat(template.queryForObject("select count(*) from identity_zone where id=?", new Object[]{zone.getId()}, Integer.class)).isZero();
assertThat(template.queryForObject("select count(*) from oauth_client_details where identity_zone_id=?", new Object[]{zone.getId()}, Integer.class)).isZero();
assertThat(template.queryForObject("select count(*) from groups where identity_zone_id=?", new Object[]{zone.getId()}, Integer.class)).isZero();
assertThat(template.queryForObject("select count(*) from "+dbUtils.getQuotedIdentifier("groups", template)+" where identity_zone_id=?", new Object[]{zone.getId()}, Integer.class)).isZero();
assertThat(template.queryForObject("select count(*) from sec_audit where identity_zone_id=?", new Object[]{zone.getId()}, Integer.class)).isZero();
assertThat(template.queryForObject("select count(*) from users where identity_zone_id=?", new Object[]{zone.getId()}, Integer.class)).isZero();
assertThat(template.queryForObject("select count(*) from external_group_mapping where origin=?", new Object[]{LOGIN_SERVER}, Integer.class)).isZero();
Expand Down

0 comments on commit e057424

Please sign in to comment.