Search in sources :

Example 6 with ConfigException

use of org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException in project zookeeper by apache.

the class ConfigUtils method getClientConfigStr.

public static String getClientConfigStr(String configData) {
    Properties props = new Properties();
    try {
        props.load(new StringReader(configData));
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
    StringBuffer sb = new StringBuffer();
    boolean first = true;
    String version = "";
    for (Entry<Object, Object> entry : props.entrySet()) {
        String key = entry.getKey().toString().trim();
        String value = entry.getValue().toString().trim();
        if (key.equals("version")) {
            version = value;
        }
        if (!key.startsWith("server.")) {
            continue;
        }
        QuorumPeer.QuorumServer qs;
        try {
            qs = new QuorumPeer.QuorumServer(-1, value);
        } catch (ConfigException e) {
            e.printStackTrace();
            continue;
        }
        if (!first) {
            sb.append(",");
        } else {
            first = false;
        }
        if (null != qs.clientAddr) {
            sb.append(qs.clientAddr.getHostString() + ":" + qs.clientAddr.getPort());
        }
    }
    return version + " " + sb.toString();
}
Also used : StringReader(java.io.StringReader) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) Properties(java.util.Properties)

Example 7 with ConfigException

use of org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException in project zookeeper by apache.

the class QuorumHierarchical method readConfigFile.

/**
 * Reads a configuration file. Called from the constructor
 * that takes a file as an input.
 */
private void readConfigFile(String filename) throws ConfigException {
    File configFile = new File(filename);
    LOG.info("Reading configuration from: {}", configFile);
    try {
        if (!configFile.exists()) {
            throw new IllegalArgumentException(configFile.toString() + " file is missing");
        }
        Properties cfg = new Properties();
        FileInputStream in = new FileInputStream(configFile);
        try {
            cfg.load(in);
        } finally {
            in.close();
        }
        parse(cfg);
    } catch (IOException e) {
        throw new ConfigException("Error processing " + filename, e);
    } catch (IllegalArgumentException e) {
        throw new ConfigException("Error processing " + filename, e);
    }
}
Also used : ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 8 with ConfigException

use of org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException in project zookeeper by apache.

the class QuorumDigestAuthTest method testEnableQuorumAuthenticationConfigurations.

/**
 * If quorumpeer learner is not auth enabled then self won't be able to join
 * quorum. So this test is ensuring that the quorumpeer learner is also auth
 * enabled while enabling quorum server require sasl.
 */
@Test
@Timeout(value = 10)
public void testEnableQuorumAuthenticationConfigurations() throws Exception {
    Map<String, String> authConfigs = new HashMap<String, String>();
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearner");
    authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "false");
    // case-1) 'quorum.auth.enableSasl' is off. Tries to enable server sasl.
    authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "false");
    MainThread mthread = new MainThread(1, PortAssignment.unique(), "", authConfigs);
    String[] args = new String[1];
    args[0] = mthread.getConfFile().toString();
    try {
        new QuorumPeerMain() {

            @Override
            protected void initializeAndRun(String[] args) throws ConfigException, IOException, AdminServer.AdminServerException {
                super.initializeAndRun(args);
            }
        }.initializeAndRun(args);
        fail("Must throw exception as quorum sasl is not enabled!");
    } catch (ConfigException e) {
    // expected
    }
    // case-1) 'quorum.auth.enableSasl' is off. Tries to enable learner sasl.
    authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "false");
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
    try {
        new QuorumPeerMain() {

            @Override
            protected void initializeAndRun(String[] args) throws ConfigException, IOException, AdminServer.AdminServerException {
                super.initializeAndRun(args);
            }
        }.initializeAndRun(args);
        fail("Must throw exception as quorum sasl is not enabled!");
    } catch (ConfigException e) {
    // expected
    }
}
Also used : MainThread(org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread) QuorumPeerMain(org.apache.zookeeper.server.quorum.QuorumPeerMain) HashMap(java.util.HashMap) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 9 with ConfigException

use of org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException in project zookeeper by apache.

the class ZKConfig method addConfiguration.

/**
 * Add a configuration resource. The properties form this configuration will
 * overwrite corresponding already loaded property and system property
 *
 * @param configFile
 *            Configuration file.
 */
public void addConfiguration(File configFile) throws ConfigException {
    LOG.info("Reading configuration from: {}", configFile.getAbsolutePath());
    try {
        configFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath().failForNonExistingPath().build()).validate(configFile);
        Properties cfg = new Properties();
        FileInputStream in = new FileInputStream(configFile);
        try {
            cfg.load(in);
        } finally {
            in.close();
        }
        parseProperties(cfg);
    } catch (IOException | IllegalArgumentException e) {
        LOG.error("Error while configuration from: {}", configFile.getAbsolutePath(), e);
        throw new ConfigException("Error while processing " + configFile.getAbsolutePath(), e);
    }
}
Also used : ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Aggregations

ConfigException (org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException)9 IOException (java.io.IOException)6 Properties (java.util.Properties)6 Test (org.junit.jupiter.api.Test)4 HashMap (java.util.HashMap)3 FileInputStream (java.io.FileInputStream)2 StringReader (java.io.StringReader)2 QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)2 QuorumPeerMain (org.apache.zookeeper.server.quorum.QuorumPeerMain)2 MainThread (org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread)2 Timeout (org.junit.jupiter.api.Timeout)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 KeeperException (org.apache.zookeeper.KeeperException)1 BadArgumentsException (org.apache.zookeeper.KeeperException.BadArgumentsException)1 ClientX509Util (org.apache.zookeeper.common.ClientX509Util)1 ACL (org.apache.zookeeper.data.ACL)1 CheckVersionRequest (org.apache.zookeeper.proto.CheckVersionRequest)1 DeleteRequest (org.apache.zookeeper.proto.DeleteRequest)1 ReconfigRequest (org.apache.zookeeper.proto.ReconfigRequest)1