<commit_before>/**
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); 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 org.apache.kerby.kerberos.kerb.server;

import org.apache.kerby.kerberos.kerb.KrbException;
import org.apache.kerby.kerberos.kerb.admin.Kadmin;
import org.apache.kerby.kerberos.kerb.client.Krb5Conf;
import org.apache.kerby.kerberos.kerb.client.KrbClient;
import org.apache.kerby.util.NetworkUtil;

import java.io.File;
import java.io.IOException;

/**
 * A simple KDC server mainly for test usage. It also integrates krb client and
 * kadmin sides for convenience.
 */
public class SimpleKdcServer extends KdcServer {
    private final KrbClient krbClnt;
    private Kadmin kadmin;
    private Krb5Conf krb5Conf;

    private File workDir;

    /**
     * Default constructor.
     */
    public SimpleKdcServer() throws KrbException {
        super();
        this.krbClnt = new KrbClient();

        setKdcRealm("EXAMPLE.COM");
        setKdcHost("localhost");
        setKdcPort(NetworkUtil.getServerPort());
    }

    public void setWorkDir(File workDir) {
        this.workDir = workDir;
    }

    public File getWorkDir() {
        return workDir;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setKdcRealm(String realm) {
        super.setKdcRealm(realm);
        krbClnt.setKdcRealm(realm);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setKdcHost(String kdcHost) {
        super.setKdcHost(kdcHost);
        krbClnt.setKdcHost(kdcHost);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setKdcTcpPort(int kdcTcpPort) {
        super.setKdcTcpPort(kdcTcpPort);
        krbClnt.setKdcTcpPort(kdcTcpPort);
        setAllowTcp(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setAllowUdp(boolean allowUdp) {
        super.setAllowUdp(allowUdp);
        krbClnt.setAllowUdp(allowUdp);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setAllowTcp(boolean allowTcp) {
        super.setAllowTcp(allowTcp);
        krbClnt.setAllowTcp(allowTcp);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setKdcUdpPort(int kdcUdpPort) {
        super.setKdcUdpPort(kdcUdpPort);
        krbClnt.setKdcUdpPort(kdcUdpPort);
        setAllowUdp(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void init() throws KrbException {
        super.init();

        kadmin = new Kadmin(getKdcSetting(), getIdentityService());

        kadmin.createBuiltinPrincipals();

        try {
            krb5Conf = new Krb5Conf(this);
            krb5Conf.initKrb5conf();
        } catch (IOException e) {
            throw new KrbException("Failed to make krb5.conf", e);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void start() throws KrbException {
        super.start();

        krbClnt.init();
    }

    /**
     * Get krb client.
     * @return KrbClient
     */
    public KrbClient getKrbClient() {
        return krbClnt;
    }

    /**
     * Get Kadmin operation interface.
     * @return Kadmin
     */
    public Kadmin getKadmin() {
        return kadmin;
    }


    /**
     * Create principal with principal name.
     *
     * @param principal The principal name
     */
    public void createPrincipal(String principal) throws KrbException {
        kadmin.addPrincipal(principal);
    }

    /**
     * Create principal with principal name and password.
     *
     * @param principal The principal name
     * @param password The password to create keys
     */
    public void createPrincipal(String principal,
                                String password) throws KrbException {
        kadmin.addPrincipal(principal, password);
    }

    /**
     * Create principals.
     *
     * @param principals The principal list
     */
    public void createPrincipals(String ... principals) throws KrbException {
        for (String principal : principals) {
            kadmin.addPrincipal(principal);
        }
    }

    /**
     * Creates principals and export their keys to the specified keytab file.
     *
     * @param keytabFile The keytab file to store principal keys
     * @param principals The principals to be create
     */
    public void createAndExportPrincipals(File keytabFile,
                                String ... principals) throws KrbException {
        createPrincipals(principals);
        exportPrincipals(keytabFile);
    }

    /**
     * Delete principals.
     *
     * @param principals The principals to be delete
     */
    public void deletePrincipals(String ... principals) throws KrbException {
        for (String principal : principals) {
            deletePrincipal(principal);
        }
    }

    /**
     * Delete principal.
     *
     * @param principal The principal to be delete
     */
    public void deletePrincipal(String principal) throws KrbException {
        kadmin.deletePrincipal(principal);
    }

    /**
     * Export principals to keytab file.
     *
     * @param keytabFile The keytab file
     */
    public void exportPrincipals(File keytabFile) throws KrbException {
        kadmin.exportKeytab(keytabFile);
    }

    /**
     * Export the keys of the specified principal into keytab file.
     * @param principal
     * @param keytabFile
     * @throws KrbException
     */
    public void exportPrincipal(String principal, File keytabFile) throws KrbException {
        kadmin.exportKeytab(keytabFile, principal);
    }

    @Override
    public void stop() throws KrbException {
        super.stop();
        try {
            krb5Conf.deleteKrb5conf();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
<commit_msg>add JavaDoc for kerb-simplekdc module<commit_after>/**
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); 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 org.apache.kerby.kerberos.kerb.server;

import org.apache.kerby.kerberos.kerb.KrbException;
import org.apache.kerby.kerberos.kerb.admin.Kadmin;
import org.apache.kerby.kerberos.kerb.client.Krb5Conf;
import org.apache.kerby.kerberos.kerb.client.KrbClient;
import org.apache.kerby.util.NetworkUtil;

import java.io.File;
import java.io.IOException;

/**
 * A simple KDC server mainly for test usage. It also integrates krb client and
 * kadmin sides for convenience.
 */
public class SimpleKdcServer extends KdcServer {
    private final KrbClient krbClnt;
    private Kadmin kadmin;
    private Krb5Conf krb5Conf;

    private File workDir;

    /**
     * Default constructor.
     *
     * @throws org.apache.kerby.kerberos.kerb.KrbException
     */
    public SimpleKdcServer() throws KrbException {
        super();
        this.krbClnt = new KrbClient();

        setKdcRealm("EXAMPLE.COM");
        setKdcHost("localhost");
        setKdcPort(NetworkUtil.getServerPort());
    }

    public void setWorkDir(File workDir) {
        this.workDir = workDir;
    }

    public File getWorkDir() {
        return workDir;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setKdcRealm(String realm) {
        super.setKdcRealm(realm);
        krbClnt.setKdcRealm(realm);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setKdcHost(String kdcHost) {
        super.setKdcHost(kdcHost);
        krbClnt.setKdcHost(kdcHost);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setKdcTcpPort(int kdcTcpPort) {
        super.setKdcTcpPort(kdcTcpPort);
        krbClnt.setKdcTcpPort(kdcTcpPort);
        setAllowTcp(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setAllowUdp(boolean allowUdp) {
        super.setAllowUdp(allowUdp);
        krbClnt.setAllowUdp(allowUdp);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setAllowTcp(boolean allowTcp) {
        super.setAllowTcp(allowTcp);
        krbClnt.setAllowTcp(allowTcp);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setKdcUdpPort(int kdcUdpPort) {
        super.setKdcUdpPort(kdcUdpPort);
        krbClnt.setKdcUdpPort(kdcUdpPort);
        setAllowUdp(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void init() throws KrbException {
        super.init();

        kadmin = new Kadmin(getKdcSetting(), getIdentityService());

        kadmin.createBuiltinPrincipals();

        try {
            krb5Conf = new Krb5Conf(this);
            krb5Conf.initKrb5conf();
        } catch (IOException e) {
            throw new KrbException("Failed to make krb5.conf", e);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void start() throws KrbException {
        super.start();

        krbClnt.init();
    }

    /**
     * Get krb client.
     * @return KrbClient
     */
    public KrbClient getKrbClient() {
        return krbClnt;
    }

    /**
     * Get Kadmin operation interface.
     * @return Kadmin
     */
    public Kadmin getKadmin() {
        return kadmin;
    }


    /**
     * Create principal with principal name.
     *
     * @throws org.apache.kerby.kerberos.kerb.KrbException
     * @param principal The principal name
     */
    public void createPrincipal(String principal) throws KrbException {
        kadmin.addPrincipal(principal);
    }

    /**
     * Create principal with principal name and password.
     *
     * @throws org.apache.kerby.kerberos.kerb.KrbException
     * @param principal The principal name
     * @param password The password to create keys
     */
    public void createPrincipal(String principal,
                                String password) throws KrbException {
        kadmin.addPrincipal(principal, password);
    }

    /**
     * Create principals.
     *
     * @throws org.apache.kerby.kerberos.kerb.KrbException
     * @param principals The principal list
     */
    public void createPrincipals(String ... principals) throws KrbException {
        for (String principal : principals) {
            kadmin.addPrincipal(principal);
        }
    }

    /**
     * Creates principals and export their keys to the specified keytab file.
     *
     * @throws org.apache.kerby.kerberos.kerb.KrbException
     * @param keytabFile The keytab file to store principal keys
     * @param principals The principals to be create
     */
    public void createAndExportPrincipals(File keytabFile,
                                String ... principals) throws KrbException {
        createPrincipals(principals);
        exportPrincipals(keytabFile);
    }

    /**
     * Delete principals.
     *
     * @throws org.apache.kerby.kerberos.kerb.KrbException
     * @param principals The principals to be delete
     */
    public void deletePrincipals(String ... principals) throws KrbException {
        for (String principal : principals) {
            deletePrincipal(principal);
        }
    }

    /**
     * Delete principal.
     *
     * @throws org.apache.kerby.kerberos.kerb.KrbException
     * @param principal The principal to be delete
     */
    public void deletePrincipal(String principal) throws KrbException {
        kadmin.deletePrincipal(principal);
    }

    /**
     * Export principals to keytab file.
     *
     * @param keytabFile The keytab file
     */
    public void exportPrincipals(File keytabFile) throws KrbException {
        kadmin.exportKeytab(keytabFile);
    }

    /**
     * Export the keys of the specified principal into keytab file.
     * @param principal principal
     * @param keytabFile keytab file
     * @throws org.apache.kerby.kerberos.kerb.KrbException
     */
    public void exportPrincipal(String principal, File keytabFile) throws KrbException {
        kadmin.exportKeytab(keytabFile, principal);
    }

    /**
     * @throws KrbException
     */
    @Override
    public void stop() throws KrbException {
        super.stop();
        try {
            krb5Conf.deleteKrb5conf();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

{'fraction_non_alphanumeric': 0.0932209654764545, 'fraction_numerical': 0.0020699342056627484, 'mean_word_length': 2.28988326848249, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6445374', 'n_tokens_mistral': 4078, 'n_tokens_neox': 4026, 'n_words': 1003}