Skip to content

Commit

Permalink
fix: update config
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed May 2, 2024
1 parent 94dc87b commit f371665
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.storage.postgresql.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface EditableInDashboard {
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
import io.supertokens.pluginInterface.ConfigFieldInfo;
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;
import io.supertokens.storage.postgresql.Start;
import io.supertokens.storage.postgresql.annotations.ConnectionPoolProperty;
import io.supertokens.storage.postgresql.annotations.IgnoreForAnnotationCheck;
import io.supertokens.storage.postgresql.annotations.NotConflictingWithinUserPool;
import io.supertokens.storage.postgresql.annotations.ConfigDescription;
import io.supertokens.storage.postgresql.annotations.UserPoolProperty;
import io.supertokens.storage.postgresql.annotations.*;

import java.lang.reflect.Field;
import java.net.URI;
Expand All @@ -52,6 +48,7 @@ public class PostgreSQLConfig {
@JsonProperty
@ConnectionPoolProperty
@ConfigDescription("Defines the connection pool size to PostgreSQL. (Default: 10)")
@EditableInDashboard
private int postgresql_connection_pool_size = 10;

@JsonProperty
Expand All @@ -62,6 +59,7 @@ public class PostgreSQLConfig {
@JsonProperty
@UserPoolProperty
@ConfigDescription("Specify the port to use when connecting to PostgreSQL instance. (Default: 5432)")
@EditableInDashboard
private int postgresql_port = -1;

@JsonProperty
Expand Down Expand Up @@ -140,11 +138,13 @@ public class PostgreSQLConfig {
@JsonProperty
@ConnectionPoolProperty
@ConfigDescription("Timeout in milliseconds for the idle connections to be closed. (Default: 60000)")
@EditableInDashboard
private long postgresql_idle_connection_timeout = 60000;

@JsonProperty
@ConnectionPoolProperty
@ConfigDescription("Minimum number of idle connections to be kept active. If not set, minimum idle connections will be same as the connection pool size. (Default: null)")
@EditableInDashboard
private Integer postgresql_minimum_idle_connections = null;

@IgnoreForAnnotationCheck
Expand Down Expand Up @@ -204,11 +204,11 @@ public static ArrayList<ConfigFieldInfo> getConfigFieldsInfo(Start start) {
JsonElement defaultValue = defaultConfig.get(field.getName());
boolean isNullable = defaultValue == null;

boolean isEditable = field.isAnnotationPresent(ConnectionPoolProperty.class);
boolean isEditable = field.isAnnotationPresent(EditableInDashboard.class);

result.add(new ConfigFieldInfo(
key, valueType, value, description, isSaasProtected, isDifferentAcrossTenants,
isConfigYamlOnly, possibleValues, isNullable, defaultValue, true, isEditable));
key, valueType, value, description, isDifferentAcrossTenants,
possibleValues, isNullable, defaultValue, true, isEditable));

} catch (NoSuchFieldException e) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ static String getQueryToCreateTenantConfigsTable(Start start) {
+ "email_password_enabled BOOLEAN,"
+ "passwordless_enabled BOOLEAN,"
+ "third_party_enabled BOOLEAN,"
+ "use_first_factors_from_static_if_empty BOOLEAN,"
+ "use_third_party_providers_from_static_if_empty BOOLEAN,"
+ "CONSTRAINT " + Utils.getConstraintName(schema, tenantConfigsTable, null, "pkey") + " PRIMARY KEY (connection_uri_domain, app_id, tenant_id)"
+ ");";
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

package io.supertokens.storage.postgresql.queries.multitenancy;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import io.supertokens.pluginInterface.RowMapper;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.*;
import io.supertokens.storage.postgresql.Start;
import io.supertokens.storage.postgresql.queries.utils.JsonUtils;
import io.supertokens.storage.postgresql.utils.Utils;

import java.sql.Connection;
import java.sql.ResultSet;
Expand Down Expand Up @@ -59,9 +55,13 @@ public TenantConfig map(ResultSet result) throws StorageQueryException {
return new TenantConfig(
new TenantIdentifier(result.getString("connection_uri_domain"), result.getString("app_id"), result.getString("tenant_id")),
new EmailPasswordConfig(result.getBoolean("email_password_enabled")),
new ThirdPartyConfig(result.getBoolean("third_party_enabled"), this.providers),
new ThirdPartyConfig(
result.getBoolean("third_party_enabled"),
result.getBoolean("use_third_party_providers_from_static_if_empty"),
this.providers),
new PasswordlessConfig(result.getBoolean("passwordless_enabled")),
firstFactors.length == 0 ? null : firstFactors,
result.getBoolean("use_first_factors_from_static_if_empty"),
requiredSecondaryFactors.length == 0 ? null : requiredSecondaryFactors,
JsonUtils.stringToJsonObject(result.getString("core_config"))
);
Expand All @@ -74,7 +74,8 @@ public TenantConfig map(ResultSet result) throws StorageQueryException {
public static TenantConfig[] selectAll(Start start, HashMap<TenantIdentifier, HashMap<String, ThirdPartyConfig.Provider>> providerMap, HashMap<TenantIdentifier, String[]> firstFactorsMap, HashMap<TenantIdentifier, String[]> requiredSecondaryFactorsMap)
throws SQLException, StorageQueryException {
String QUERY = "SELECT connection_uri_domain, app_id, tenant_id, core_config,"
+ " email_password_enabled, passwordless_enabled, third_party_enabled FROM "
+ " email_password_enabled, passwordless_enabled, third_party_enabled, "
+ " use_first_factors_from_static_if_empty, use_third_party_providers_from_static_if_empty FROM "
+ getConfig(start).getTenantConfigsTable() + ";";

TenantConfig[] tenantConfigs = execute(start, QUERY, pst -> {}, result -> {
Expand Down Expand Up @@ -104,8 +105,9 @@ public static void create(Start start, Connection sqlCon, TenantConfig tenantCon
throws SQLException, StorageQueryException {
String QUERY = "INSERT INTO " + getConfig(start).getTenantConfigsTable()
+ "(connection_uri_domain, app_id, tenant_id, core_config,"
+ " email_password_enabled, passwordless_enabled, third_party_enabled)"
+ " VALUES(?, ?, ?, ?, ?, ?, ?)";
+ " email_password_enabled, passwordless_enabled, third_party_enabled,"
+ " use_first_factors_from_static_if_empty, use_third_party_providers_from_static_if_empty)"
+ " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";

update(sqlCon, QUERY, pst -> {
pst.setString(1, tenantConfig.tenantIdentifier.getConnectionUriDomain());
Expand All @@ -115,6 +117,8 @@ public static void create(Start start, Connection sqlCon, TenantConfig tenantCon
pst.setBoolean(5, tenantConfig.emailPasswordConfig.enabled);
pst.setBoolean(6, tenantConfig.passwordlessConfig.enabled);
pst.setBoolean(7, tenantConfig.thirdPartyConfig.enabled);
pst.setBoolean(8, tenantConfig.useFirstFactorsFromStaticConfigIfEmpty);
pst.setBoolean(9, tenantConfig.thirdPartyConfig.useThirdPartyProvidersFromStaticConfigIfEmpty);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public void canLinkFailsIfTryingToLinkUsersAcrossDifferentStorageLayers() throws
new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null,
null, true, null,
coreConfig
)
);
Expand Down Expand Up @@ -129,9 +129,9 @@ public void canLinkFailsIfTryingToLinkUsersAcrossDifferentStorageLayers() throws
new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null,
null, true, null,
coreConfig
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public void testActiveConnectionsWithTenants() throws Exception {
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
new TenantIdentifier(null, null, "t1"),
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null, config
null, true, null, config
), false);

Thread.sleep(1000); // let the new tenant be ready
Expand All @@ -94,9 +94,9 @@ public void testActiveConnectionsWithTenants() throws Exception {
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
new TenantIdentifier(null, null, "t1"),
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null, config
null, true, null, config
), false);

Thread.sleep(2000); // let the new tenant be ready
Expand Down Expand Up @@ -137,9 +137,9 @@ public void testDownTimeWhenChangingConnectionPoolSize() throws Exception {
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
new TenantIdentifier(null, null, "t1"),
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null, config
null, true, null, config
), false);

Thread.sleep(5000); // let the new tenant be ready
Expand Down Expand Up @@ -197,9 +197,9 @@ public void testDownTimeWhenChangingConnectionPoolSize() throws Exception {
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
new TenantIdentifier(null, null, "t1"),
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null, config
null, true, null, config
), false);

Thread.sleep(3000); // let the new tenant be ready
Expand Down Expand Up @@ -279,9 +279,9 @@ public void testMinimumIdleConnectionForTenants() throws Exception {
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
new TenantIdentifier(null, null, "t1"),
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null, config
null, true, null, config
), false);

Thread.sleep(1000); // let the new tenant be ready
Expand All @@ -295,9 +295,9 @@ public void testMinimumIdleConnectionForTenants() throws Exception {
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
new TenantIdentifier(null, null, "t1"),
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null, config
null, true, null, config
), false);

Thread.sleep(2000); // let the new tenant be ready
Expand Down Expand Up @@ -338,9 +338,9 @@ public void testIdleConnectionTimeout() throws Exception {
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
new TenantIdentifier(null, null, "t1"),
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null, config
null, true, null, config
), false);

Thread.sleep(3000); // let the new tenant be ready
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -285,9 +284,9 @@ public void confirmHikariLoggerClosedOnlyWhenProcessEnds() throws Exception {
Multitenancy.addNewOrUpdateAppOrTenant(process.getProcess(), new TenantConfig(
tenant,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null,
null, true, null,
config
), false);

Expand Down Expand Up @@ -517,9 +516,9 @@ public void testDBPasswordIsNotLoggedWhenTenantIsCreated() throws Exception {
new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null, config
null, true, null, config
));

process.kill();
Expand Down Expand Up @@ -562,9 +561,9 @@ public void testDBPasswordIsNotLoggedWhenTenantIsCreated() throws Exception {
new TenantConfig(
tenantIdentifier,
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new ThirdPartyConfig(true, true, null),
new PasswordlessConfig(true),
null, null,
null, true, null,
new JsonObject()));

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public void testThatTenantCannotSetDatabaseRelatedConfigIfSuperTokensSaaSSecretI
JsonObject j = new JsonObject();
j.addProperty(PROTECTED_DB_CONFIG[i], "");
Multitenancy.addNewOrUpdateAppOrTenant(process.main, new TenantConfig(new TenantIdentifier(null, null, "t1"), new EmailPasswordConfig(false),
new ThirdPartyConfig(false, new ThirdPartyConfig.Provider[0]),
new ThirdPartyConfig(false, true, new ThirdPartyConfig.Provider[0]),
new PasswordlessConfig(false),
null, null,
null, true, null,
j), true);
fail();
} catch (BadPermissionException e) {
Expand Down Expand Up @@ -164,9 +164,9 @@ public void testThatTenantCanSetDatabaseRelatedConfigIfSuperTokensSaaSSecretIsNo
}
Multitenancy.addNewOrUpdateAppOrTenant(process.main, new TenantConfig(new TenantIdentifier(null, null, "t1"),
new EmailPasswordConfig(false),
new ThirdPartyConfig(false, new ThirdPartyConfig.Provider[0]),
new ThirdPartyConfig(false, true, new ThirdPartyConfig.Provider[0]),
new PasswordlessConfig(false),
null, null,
null, true, null,
j), false);
}

Expand Down Expand Up @@ -217,9 +217,9 @@ public void testThatTenantCannotGetDatabaseRelatedConfigIfSuperTokensSaaSSecretI
}
Multitenancy.addNewOrUpdateAppOrTenant(process.main, new TenantIdentifier(null, null, null),
new TenantConfig(new TenantIdentifier(null, null, "t" + i), new EmailPasswordConfig(false),
new ThirdPartyConfig(false, new ThirdPartyConfig.Provider[0]),
new ThirdPartyConfig(false, true, new ThirdPartyConfig.Provider[0]),
new PasswordlessConfig(false),
null, null,
null, true, null,
j));

{
Expand Down
Loading

0 comments on commit f371665

Please sign in to comment.