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 java.util.LinkedList;
021    import java.util.List;
022    import java.util.Set;
023    import java.util.SortedSet;
024    
025    import org.cumulus4j.keystore.prop.Property;
026    
027    /**
028     * <p>
029     * {@link SubCommand} implementation for showing various infos about a key-store.
030     * </p>
031     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
032     */
033    public class InfoSubCommand extends SubCommandWithKeyStoreWithAuth
034    {
035            @Override
036            public String getSubCommandName() {
037                    return "info";
038            }
039    
040            @Override
041            public String getSubCommandDescription() {
042                    return "Display infos about the key-store.";
043            }
044    
045            @Override
046            public void run() throws Exception
047            {
048                    System.out.println("Key store: " + getKeyStoreFile().getCanonicalPath());
049                    System.out.println();
050    
051                    if (getKeyStore().isEmpty()) {
052                            System.out.println("This key store is empty!");
053                            System.out.println();
054                            return;
055                    }
056    
057                    System.out.println("Size of master key: " + getKeyStore().getMasterKeySize(getAuthUserName(), getAuthPasswordAsCharArray()) + " bit");
058                    System.out.println();
059    
060                    Set<String> users = getKeyStore().getUsers(getAuthUserName(), getAuthPasswordAsCharArray());
061                    System.out.println("Users: ");
062                    if (users.isEmpty())
063                            System.out.println("  --- EMPTY ---");
064    
065                    for (String user : users) {
066                            System.out.println("  " + user);
067                    }
068                    System.out.println();
069    
070                    SortedSet<Long> keyIDs = getKeyStore().getKeyIDs(getAuthUserName(), getAuthPasswordAsCharArray());
071                    List<Long[]> keyIDIntervals = new LinkedList<Long[]>();
072                    {
073                            Long[] keyIDInterval = null;
074                            for (Long keyID : keyIDs) {
075                                    if (keyIDInterval != null && keyID == keyIDInterval[1] + 1)
076                                            keyIDInterval[1] = keyID;
077                                    else {
078                                            keyIDInterval = new Long[2];
079                                            keyIDIntervals.add(keyIDInterval);
080                                            keyIDInterval[0] = keyID;
081                                            keyIDInterval[1] = keyID;
082                                    }
083                            }
084                    }
085    
086                    System.out.println("Key IDs:");
087                    if (keyIDIntervals.isEmpty())
088                            System.out.println("  --- EMPTY ---");
089    
090                    for (Long[] keyIDInterval : keyIDIntervals) {
091                            System.out.println("  " + keyIDInterval[0] + "..." + keyIDInterval[1]);
092                    }
093                    System.out.println();
094    
095                    System.out.println("Properties:");
096                    SortedSet<Property<?>> properties = getKeyStore().getProperties(getAuthUserName(), getAuthPasswordAsCharArray());
097                    if (properties.isEmpty())
098                            System.out.println("  --- EMPTY ---");
099    
100                    // We do not output the details here! 'info' should give an overview only. Details can be obtained via other sub-commands.
101    //              for (Property<?> property : properties) {
102    //                      System.out.println("  " + property.getName() + " (" + property.getClass().getName() + ") = " + property.getValue());
103    //              }
104                    for (Property<?> property : properties) {
105                            System.out.println("  " + property.getName() + " (" + property.getClass().getName() + ", " + property.getValueEncoded().length + " Byte)");
106                    }
107    
108                    System.out.println();
109            }
110    
111    }