<commit_before>package org.yamcs.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yamcs.ConfigurationException;
import org.yamcs.YConfiguration;
import org.yamcs.utils.YObjectLoader;

/**
 * Manages the security layer. It allows logging in users with a pluggable variety of extensions. The Security Store
 * reads its configuration from the security.yaml file. This file may define any number of {@link AuthModule}s that may
 * all participate in the authentication and authorization process.
 */
public class SecurityStore {

    private static final Logger log = LoggerFactory.getLogger(SecurityStore.class);
    private static SecurityStore instance;

    private boolean enabled = false;

    private List<AuthModule> authModules = new ArrayList<>();

    private User systemUser;
    private User unauthenticatedUser;

    private SecurityStore() {
        YConfiguration yconf = YConfiguration.getConfiguration("security");

        enabled = yconf.getBoolean("enabled");
        if (!enabled) {
            log.warn("Security disabled");
            if (yconf.containsKey("unauthenticatedUser")) {
                Map<String, Object> userProps = yconf.getMap("unauthenticatedUser");
                String username = YConfiguration.getString(userProps, "username");
                if (username.isEmpty() || username.contains(":")) {
                    throw new ConfigurationException("Invalid username '" + username + "' for unauthenticatedUser");
                }

                unauthenticatedUser = new User(username);
                unauthenticatedUser.setSuperuser(YConfiguration.getBoolean(userProps, "superuser", false));

                // TODO allow configuring privileges? Probably shouldn't come from an AuthModule because enabled=false
            } else {
                unauthenticatedUser = new User("admin");
                unauthenticatedUser.setSuperuser(true);
            }
        }

        if (yconf.containsKey("authModules")) {
            for (Map<String, Object> moduleConf : yconf.<Map<String, Object>> getList("authModules")) {
                try {
                    AuthModule authModule = YObjectLoader.loadObject(moduleConf);
                    authModules.add(authModule);
                } catch (IOException e) {
                    throw new ConfigurationException("Failed to load AuthModule", e);
                }
            }
        }

        systemUser = new User("System");
        systemUser.setSuperuser(true);
    }
<commit_msg>Load AuthModules only if security is enabled<commit_after>package org.yamcs.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yamcs.ConfigurationException;
import org.yamcs.YConfiguration;
import org.yamcs.utils.YObjectLoader;

/**
 * Manages the security layer. It allows logging in users with a pluggable variety of extensions. The Security Store
 * reads its configuration from the security.yaml file. This file may define any number of {@link AuthModule}s that may
 * all participate in the authentication and authorization process.
 */
public class SecurityStore {

    private static final Logger log = LoggerFactory.getLogger(SecurityStore.class);
    private static SecurityStore instance;

    private boolean enabled = false;

    private List<AuthModule> authModules = new ArrayList<>();

    private User systemUser;
    private User unauthenticatedUser;

    private SecurityStore() {
        YConfiguration yconf = YConfiguration.getConfiguration("security");

        enabled = yconf.getBoolean("enabled");
        if (enabled) {
            if (yconf.containsKey("authModules")) {
                for (Map<String, Object> moduleConf : yconf.<Map<String, Object>> getList("authModules")) {
                    log.info("Loading AuthModule " + YConfiguration.getString(moduleConf, "class"));
                    try {
                        AuthModule authModule = YObjectLoader.loadObject(moduleConf);
                        authModules.add(authModule);
                    } catch (IOException e) {
                        throw new ConfigurationException("Failed to load AuthModule", e);
                    }
                }
            }
        } else {
            log.warn("Security disabled");
            if (yconf.containsKey("unauthenticatedUser")) {
                Map<String, Object> userProps = yconf.getMap("unauthenticatedUser");
                String username = YConfiguration.getString(userProps, "username");
                if (username.isEmpty() || username.contains(":")) {
                    throw new ConfigurationException("Invalid username '" + username + "' for unauthenticatedUser");
                }

                unauthenticatedUser = new User(username);
                unauthenticatedUser.setSuperuser(YConfiguration.getBoolean(userProps, "superuser", false));

                // TODO allow configuring privileges? Probably shouldn't come from an AuthModule because enabled=false
            } else {
                unauthenticatedUser = new User("admin");
                unauthenticatedUser.setSuperuser(true);
            }
        }

        systemUser = new User("System");
        systemUser.setSuperuser(true);
    }

{'fraction_non_alphanumeric': 0.0913852266570345, 'fraction_numerical': 0.0007224128589488893, 'mean_word_length': 2.6031229668184777, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3401610', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1307, 'n_words': 363}