Skip to content
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

fix: core config validation #894

Merged
merged 10 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [7.0.16] - 2023-12-04

- Returns 400, instead of 500, for badly typed core config while creating CUD, App or Tenant

## [7.0.15] - 2023-11-28

- Adds test for user pagination from old version
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ compileTestJava { options.encoding = "UTF-8" }
// }
//}

version = "7.0.15"
version = "7.0.16"


repositories {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/io/supertokens/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.pluginInterface.utils.ConfigMapper;
import org.jetbrains.annotations.TestOnly;

import java.io.File;
Expand All @@ -49,15 +50,17 @@ public class Config extends ResourceDistributor.SingletonResource {
private Config(Main main, String configFilePath) throws InvalidConfigException, IOException {
this.main = main;
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
CoreConfig config = mapper.readValue(new File(configFilePath), CoreConfig.class);
Object configObj = mapper.readValue(new File(configFilePath), Object.class);
JsonObject jsonConfig = new Gson().toJsonTree(configObj).getAsJsonObject();
CoreConfig config = ConfigMapper.mapConfig(jsonConfig, CoreConfig.class);
config.normalizeAndValidate(main);
this.core = config;
}

private Config(Main main, JsonObject jsonConfig) throws IOException, InvalidConfigException {
this.main = main;
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
CoreConfig config = mapper.readValue(jsonConfig.toString(), CoreConfig.class);
CoreConfig config = ConfigMapper.mapConfig(jsonConfig, CoreConfig.class);
config.normalizeAndValidate(main);
this.core = config;
}
Expand Down
263 changes: 263 additions & 0 deletions src/test/java/io/supertokens/test/ConfigMapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
/*
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.supertokens.test;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.JsonObject;
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;
import io.supertokens.pluginInterface.utils.ConfigMapper;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class ConfigMapperTest {

public static class DummyConfig {
@JsonProperty
int int_property;

@JsonProperty
long long_property;

@JsonProperty
float float_property;

@JsonProperty
double double_property;

@JsonProperty
String string_property;

@JsonProperty
boolean bool_property;
}

@Test
public void testAllValidConversions() throws Exception {
// valid for int
{
JsonObject config = new JsonObject();
config.addProperty("int_property", "100");
assertEquals(100, ConfigMapper.mapConfig(config, DummyConfig.class).int_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("int_property", 100);
assertEquals(100, ConfigMapper.mapConfig(config, DummyConfig.class).int_property);
}

// valid for long
{
JsonObject config = new JsonObject();
config.addProperty("long_property", "100");
assertEquals(100, ConfigMapper.mapConfig(config, DummyConfig.class).long_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("long_property", 100);
assertEquals(100, ConfigMapper.mapConfig(config, DummyConfig.class).long_property);
}

// valid for float
{
JsonObject config = new JsonObject();
config.addProperty("float_property", 100);
System.out.println(ConfigMapper.mapConfig(config, DummyConfig.class).float_property);
assertEquals((float) 100, ConfigMapper.mapConfig(config, DummyConfig.class).float_property, 0.001);
}
{
JsonObject config = new JsonObject();
config.addProperty("float_property", 3.14);
assertEquals((float) 3.14, ConfigMapper.mapConfig(config, DummyConfig.class).float_property, 0.001);
}
{
JsonObject config = new JsonObject();
config.addProperty("float_property", "100");
assertEquals((float) 100, ConfigMapper.mapConfig(config, DummyConfig.class).float_property, 0.001);
}
{
JsonObject config = new JsonObject();
config.addProperty("float_property", "3.14");
assertEquals((float) 3.14, ConfigMapper.mapConfig(config, DummyConfig.class).float_property, 0.001);
}

// valid double
{
JsonObject config = new JsonObject();
config.addProperty("double_property", 100);
assertEquals((double) 100, ConfigMapper.mapConfig(config, DummyConfig.class).double_property, 0.001);
}
{
JsonObject config = new JsonObject();
config.addProperty("double_property", 3.14);
assertEquals((double) 3.14, ConfigMapper.mapConfig(config, DummyConfig.class).double_property, 0.001);
}
{
JsonObject config = new JsonObject();
config.addProperty("double_property", "100");
assertEquals((double) 100, ConfigMapper.mapConfig(config, DummyConfig.class).double_property, 0.001);
}
{
JsonObject config = new JsonObject();
config.addProperty("double_property", "3.14");
assertEquals((double) 3.14, ConfigMapper.mapConfig(config, DummyConfig.class).double_property, 0.001);
}

// valid for bool
{
JsonObject config = new JsonObject();
config.addProperty("bool_property", "true");
assertEquals(true, ConfigMapper.mapConfig(config, DummyConfig.class).bool_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("bool_property", "TRUE");
assertEquals(true, ConfigMapper.mapConfig(config, DummyConfig.class).bool_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("bool_property", "false");
assertEquals(false, ConfigMapper.mapConfig(config, DummyConfig.class).bool_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("bool_property", true);
assertEquals(true, ConfigMapper.mapConfig(config, DummyConfig.class).bool_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("bool_property", false);
assertEquals(false, ConfigMapper.mapConfig(config, DummyConfig.class).bool_property);
}

// valid for string
{
JsonObject config = new JsonObject();
config.addProperty("string_property", "true");
assertEquals("true", ConfigMapper.mapConfig(config, DummyConfig.class).string_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("string_property", true);
assertEquals("true", ConfigMapper.mapConfig(config, DummyConfig.class).string_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("string_property", 100);
assertEquals("100", ConfigMapper.mapConfig(config, DummyConfig.class).string_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("string_property", 3.14);
assertEquals("3.14", ConfigMapper.mapConfig(config, DummyConfig.class).string_property);
}
{
JsonObject config = new JsonObject();
config.addProperty("string_property", "hello");
assertEquals("hello", ConfigMapper.mapConfig(config, DummyConfig.class).string_property);
}
}

@Test
public void testInvalidConversions() throws Exception {
String[] properties = new String[]{
"int_property",
"int_property",
"int_property",
"int_property",
"int_property",

"long_property",
"long_property",
"long_property",
"long_property",

"float_property",
"float_property",
"float_property",

"double_property",
"double_property",
"double_property",
};
Object[] values = new Object[]{
"abcd", // int
"", // int
true, // int
new Double(4.5), // int
new Long(1234567892342l), // int

"abcd", // long
"", // long
true, // long
new Double(4.5), // long

"abcd", // float
"", // float
true, // float

"abcd", // double
"", // double
true, // double
};

String[] expectedErrorMessages = new String[]{
"'int_property' must be of type int", // int
"'int_property' must be of type int", // int
"'int_property' must be of type int", // int
"'int_property' must be of type int", // int
"'int_property' must be of type int", // int

"'long_property' must be of type long", // long
"'long_property' must be of type long", // long
"'long_property' must be of type long", // long
"'long_property' must be of type long", // long

"'float_property' must be of type float", // float
"'float_property' must be of type float", // float
"'float_property' must be of type float", // float

"'double_property' must be of type double", // double
"'double_property' must be of type double", // double
"'double_property' must be of type double", // double
};

for (int i = 0; i < properties.length; i++) {
try {
System.out.println("Test case " + i);
JsonObject config = new JsonObject();
if (values[i] == null) {
config.add(properties[i], null);
}
else if (values[i] instanceof String) {
config.addProperty(properties[i], (String) values[i]);
} else if (values[i] instanceof Boolean) {
config.addProperty(properties[i], (Boolean) values[i]);
} else if (values[i] instanceof Number) {
config.addProperty(properties[i], (Number) values[i]);
} else {
throw new RuntimeException("Invalid type");
}
DummyConfig dc = ConfigMapper.mapConfig(config, DummyConfig.class);
fail();
} catch (InvalidConfigException e) {
assertEquals(expectedErrorMessages[i], e.getMessage());
}
}
}
}
19 changes: 19 additions & 0 deletions src/test/java/io/supertokens/test/ConfigTest2_21.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.*;
import org.junit.rules.TestRule;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class ConfigTest2_21 {
Expand Down Expand Up @@ -84,4 +85,22 @@ public void testThatNewConfigWorks() throws Exception {
EventAndException stopEvent = process.checkOrWaitForEvent(PROCESS_STATE.STOPPED);
assertNotNull(stopEvent);
}

@Test
public void testCoreConfigTypeValidationInConfigYaml() throws Exception {
Utils.setValueInConfig("access_token_validity", "abcd");

String[] args = { "../" };

TestingProcess process = TestingProcessManager.start(args);

EventAndException startEvent = process.checkOrWaitForEvent(PROCESS_STATE.INIT_FAILURE);
assertNotNull(startEvent);

assertEquals("io.supertokens.pluginInterface.exceptions.InvalidConfigException: 'access_token_validity' must be of type long", startEvent.exception.getMessage());

process.kill();
EventAndException stopEvent = process.checkOrWaitForEvent(PROCESS_STATE.STOPPED);
assertNotNull(stopEvent);
}
}
Loading
Loading