001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.keymanager.cli;
019    
020    import org.cumulus4j.keymanager.api.DateDependentKeyStrategyInitParam;
021    import org.cumulus4j.keymanager.api.DateDependentKeyStrategyInitResult;
022    import org.cumulus4j.keymanager.api.KeyManagerAPIConfiguration;
023    import org.cumulus4j.keystore.DateDependentKeyStrategy;
024    import org.cumulus4j.keystore.KeyStore;
025    import org.kohsuke.args4j.Option;
026    
027    /**
028     * <p>
029     * {@link SubCommand} implementation for creating & initialising a new key-store with the {@link DateDependentKeyStrategy}.
030     * </p>
031     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
032     */
033    public class InitDateDependentKeyStrategySubCommand
034    extends SubCommandWithKeyManagerAPI
035    {
036            @Option(
037                            name="-userName", required=true,
038                            usage="The first user, which is automatically created when initialising the key store."
039            )
040            private String userName;
041    
042            @Option(
043                            name="-password", required=false,
044                            usage="The password of the first user. If omitted, the user will be asked for it interactively."
045            )
046            private String password;
047    
048            @Option(
049                            name="-keyActivityPeriod", required=false, handler=TimePeriodOptionHandler.class,
050                            usage="How long should each key be valid. This must be a positive number followed by a unit symbol " +
051                                            "(ms = millisecond, s = second, min = minute, h = hour, d = day, a = y = year). " +
052                                            "If omitted, the default value '24h' will be used."
053            )
054            private long keyActivityPeriodMSec;
055    
056            @Option(
057                            name="-keyStorePeriod", required=false, handler=TimePeriodOptionHandler.class,
058                            usage="How long should the key store have fresh, unused keys. This number divided by the 'keyActivityPeriodMSec' " +
059                                            "determines, how many keys must be generated. This must be a positive number followed by a unit symbol " +
060                                            "(ms = millisecond, s = second, min = minute, h = hour, d = day, a = y = year). If omitted, the default value '50a' will be used.")
061            private long keyStorePeriodMSec;
062    
063            @Option(
064                            name="-keySize", required=false,
065                            usage="Set the key size of all generated keys (including the master-key). This is synonymous to the system property '" +
066                                            KeyStore.SYSTEM_PROPERTY_KEY_SIZE + "'. If both are present, this overwrites the system property."
067            )
068            private int keySize = -1;
069    
070            @Option(
071                            name="-encryptionAlgorithm", required=false,
072                            usage="Set the encryption algorithm to be used. This is synonymous to the system property '" +
073                                            KeyStore.SYSTEM_PROPERTY_ENCRYPTION_ALGORITHM + "'. If both are present, this overwrites the system property."
074            )
075            private String encryptionAlgorithm;
076    
077            @Override
078            public String getSubCommandName() {
079                    return "initDateDependentKeyStrategy";
080            }
081    
082            @Override
083            public String getSubCommandDescription() {
084                    return "Create and initialise a key store for the usage with Cumulus4j and the date-dependent key-assignment strategy.";
085            }
086    
087            @Override
088            public void prepare() throws Exception {
089                    super.prepare();
090    
091                    if (password == null)
092                            password = promptPassword("password: ");
093    
094                    if (keySize > 0)
095                            System.setProperty(KeyStore.SYSTEM_PROPERTY_KEY_SIZE, String.valueOf(keySize));
096    
097                    if (encryptionAlgorithm != null)
098                            System.setProperty(KeyStore.SYSTEM_PROPERTY_ENCRYPTION_ALGORITHM, encryptionAlgorithm);
099    
100                    KeyManagerAPIConfiguration configuration = new KeyManagerAPIConfiguration(getKeyManagerAPI().getConfiguration());
101                    configuration.setAuthUserName(userName);
102                    configuration.setAuthPassword(password == null ? null : password.toCharArray());
103                    getKeyManagerAPI().setConfiguration(configuration);
104            }
105    
106            @Override
107            public void run() throws Exception {
108                    DateDependentKeyStrategyInitParam param = new DateDependentKeyStrategyInitParam();
109                    param.setKeyActivityPeriodMSec(keyActivityPeriodMSec);
110                    param.setKeyStorePeriodMSec(keyStorePeriodMSec);
111                    DateDependentKeyStrategyInitResult result = getKeyManagerAPI().initDateDependentKeyStrategy(param);
112                    System.out.println("Generated " + result.getGeneratedKeyCount() + " keys.");
113            }
114    
115    }