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: toggle oidc login via settings #4588

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import graphql.Scalars;
import graphql.schema.*;
import java.util.*;
import java.util.stream.Collectors;
import org.molgenis.emx2.*;
import org.molgenis.emx2.datamodels.DataModels;
import org.molgenis.emx2.tasks.Task;
Expand Down Expand Up @@ -142,13 +141,16 @@ public GraphQLFieldDefinition.Builder settingsQueryField(Database database) {
dataFetchingEnvironment -> {
final List<String> selectedKeys =
dataFetchingEnvironment.getArgumentOrDefault(KEYS, new ArrayList<>());
Map<String, String> selectedSettings =
database.getSettings().entrySet().stream()
.filter(
setting ->
selectedKeys.isEmpty() || selectedKeys.contains(setting.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return mapSettingsToGraphql(selectedSettings);

Map<String, String> filtered = new HashMap<>();
for (Map.Entry<String, String> setting : database.getSettings().entrySet()) {
if (selectedKeys.isEmpty() || selectedKeys.contains(setting.getKey())) {
filtered.put(setting.getKey(), setting.getValue());
}
}
filtered.put(Constants.IS_OIDC_ENABLED, String.valueOf(database.isOidcEnabled()));

return mapSettingsToGraphql(filtered);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class SqlDatabase extends HasSettings<Database> implements Database {
private String initialAdminPassword =
(String)
EnvironmentProperty.getParameter(Constants.MOLGENIS_ADMIN_PW, ADMIN_PW_DEFAULT, STRING);
private Boolean isOidcEnabled = false;

private static String postgresUser =
(String)
EnvironmentProperty.getParameter(
Expand Down Expand Up @@ -178,7 +178,11 @@ public void init() { // setup default stuff
// get the settings
clearCache();

initOidc();
if (!this.getSettings().containsKey(Constants.IS_OIDC_ENABLED)) {
mswertz marked this conversation as resolved.
Show resolved Hide resolved
this.setSetting(
Constants.IS_OIDC_ENABLED,
(String) EnvironmentProperty.getParameter(MOLGENIS_OIDC_CLIENT_ID, "false", STRING));
}

String instanceId = getSetting(Constants.MOLGENIS_INSTANCE_ID);
if (instanceId == null) {
Expand Down Expand Up @@ -226,38 +230,6 @@ public void init() { // setup default stuff
}
}

private void initOidc() {
// oidc settings takes priority over env variables
String isOidcEnabledSetting = getSetting(Constants.IS_OIDC_ENABLED);
if (isOidcEnabledSetting != null) {
this.isOidcEnabled = Boolean.parseBoolean(isOidcEnabledSetting);
} else {
Object envSetting =
EnvironmentProperty.getParameter(Constants.MOLGENIS_OIDC_CLIENT_ID, null, STRING);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should at some point have some way to find out about oidc errors but this is probably not the way.

if (envSetting != null) {
this.setSetting(Constants.IS_OIDC_ENABLED, "true");
this.isOidcEnabled = true;
}
}
if (!this.isOidcEnabled) return;

// check if OIDC settings are complete otherwise log error and set to false
if (!isValidOidcSettings()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we don't check if OIDC settings are complete, when enabled? This is fine by me, but I made it because it was requested

setSetting(
Constants.IS_OIDC_ENABLED,
"error: Environment OIDC settings are incomplete. Fix and then set again to true");
}
}

private boolean isValidOidcSettings() {
Object oidcClientId =
EnvironmentProperty.getParameter(Constants.MOLGENIS_OIDC_CLIENT_ID, null, STRING);
Object clientSecret =
EnvironmentProperty.getParameter(Constants.MOLGENIS_OIDC_CLIENT_SECRET, null, STRING);

return clientSecret != null && oidcClientId != null;
}

@Override
public void setListener(DatabaseListener listener) {
this.listener = listener;
Expand Down Expand Up @@ -433,18 +405,10 @@ public Database setSettings(Map<String, String> settings) {
if (!isAdmin()) {
throw new MolgenisException("Insufficient rights to create database level setting");
}
if (settings.containsKey(Constants.IS_OIDC_ENABLED)) {
if (Boolean.parseBoolean(settings.get(Constants.IS_OIDC_ENABLED)) && !isValidOidcSettings()) {
throw new MolgenisException("OIDC environment setting are incomplete");
}
this.isOidcEnabled = Boolean.parseBoolean(settings.get(Constants.IS_OIDC_ENABLED));
}

super.setSettings(settings);
MetadataUtils.saveDatabaseSettings(jooq, getSettings());
// force all sessions to reload
this.listener.afterCommit();

return this;
}

Expand Down Expand Up @@ -782,7 +746,8 @@ public void becomeAdmin() {

@Override
public boolean isOidcEnabled() {
return this.isOidcEnabled;
return this.getSettings().containsKey(Constants.IS_OIDC_ENABLED)
&& Boolean.parseBoolean(this.getSettings().get(Constants.IS_OIDC_ENABLED));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.molgenis.emx2.Constants;
import org.molgenis.emx2.MolgenisException;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
Expand Down Expand Up @@ -42,13 +41,4 @@ void enableOIDCFlagViaSettings() {
sqlDatabase.setSettings(settings);
assertFalse(sqlDatabase.isOidcEnabled());
}

@Test
void invalidOIDCSettings() {
environmentVariables.set(Constants.MOLGENIS_OIDC_CLIENT_ID, null);
environmentVariables.set(Constants.MOLGENIS_OIDC_CLIENT_SECRET, null);
Map<String, String> settings = Maps.newHashMap();
settings.put(Constants.IS_OIDC_ENABLED, "true");
assertThrows(MolgenisException.class, () -> sqlDatabase.setSettings(settings));
}
}
Loading