Search in sources :

Example 1 with CliException

use of com.fathomdb.cli.CliException in project platformlayer by platformlayer.

the class JoinProject method runCommand.

@Override
public Object runCommand() throws RepositoryException, IOException {
    UserDatabase userRepository = getContext().getUserRepository();
    UserEntity me = getContext().loginDirect();
    ProjectEntity project = userRepository.findProjectByKey(projectKey.getKey());
    if (project == null) {
        throw new CliException("Project not found: " + projectKey.getKey());
    }
    SecretStore secretStore = new SecretStore(project.secretData);
    CryptoKey projectSecret = secretStore.getSecretFromUser(me);
    if (projectSecret == null) {
        String msg = "Cannot retrieve project secret.";
        msg += " Is " + me.key + " a member of " + project.getName() + "?";
        throw new CliException(msg);
    }
    if (Strings.isNullOrEmpty(roleKey)) {
        throw new CliException("Role is required");
    }
    RoleId role = new RoleId(roleKey);
    userRepository.addUserToProject(username.getKey(), project.getName(), projectSecret, Collections.singletonList(role));
    return project;
}
Also used : CliException(com.fathomdb.cli.CliException) ProjectEntity(org.platformlayer.auth.ProjectEntity) UserDatabase(org.platformlayer.auth.UserDatabase) CryptoKey(com.fathomdb.crypto.CryptoKey) SecretStore(org.platformlayer.auth.crypto.SecretStore) RoleId(org.platformlayer.model.RoleId) UserEntity(org.platformlayer.auth.UserEntity)

Example 2 with CliException

use of com.fathomdb.cli.CliException in project platformlayer by platformlayer.

the class ConfigurationOptions method buildPlatformLayerClient.

public PlatformLayerClient buildPlatformLayerClient() throws IOException, OpsException {
    HttpPlatformLayerClient client;
    if (configFile == null) {
        throw new IllegalArgumentException("Config file is required");
    }
    InputStream is = null;
    try {
        if (configFile.equals("-")) {
            // Read from stdin
            // Don't auto-close it: that terminates nailgun
            is = new NoCloseInputStream(System.in);
        } else {
            if (isServerMode()) {
                throw new IllegalArgumentException("Must pass config file over stdin in server mode");
            }
            File file = IoUtils.resolve(configFile);
            if (!file.exists()) {
                throw new FileNotFoundException("Configuration file not found: " + file);
            }
            is = new FileInputStream(file);
        }
        final Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            throw new IOException("Error reading configuration file", e);
        }
        if (properties.getProperty("platformlayer.username") == null) {
            throw new CliException("User property not set in configuration file");
        }
        if (debug) {
            client = buildPlatformLayerClient(properties, debug);
        } else {
            String propertiesKey = PropertyUtils.serialize(properties);
            try {
                client = platformLayerClientCache.get(propertiesKey, new Callable<HttpPlatformLayerClient>() {

                    @Override
                    public HttpPlatformLayerClient call() throws Exception {
                        return buildPlatformLayerClient(properties, false);
                    }
                });
            } catch (ExecutionException e) {
                throw new CliException("Error building platformlayer client", e);
            }
        }
    } finally {
        if (is != System.in) {
            Closeables.closeQuietly(is);
        }
    }
    return client;
}
Also used : HttpPlatformLayerClient(org.platformlayer.HttpPlatformLayerClient) FileInputStream(java.io.FileInputStream) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Callable(java.util.concurrent.Callable) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) CliException(com.fathomdb.cli.CliException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 3 with CliException

use of com.fathomdb.cli.CliException in project platformlayer by platformlayer.

the class KeystoneCliContext method getCertificateChain.

public Certificate[] getCertificateChain(String keystore, String keystoreSecret, String keyAlias) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    if (getOptions().isServerMode()) {
        throw new IllegalArgumentException("Files not supported in server mode");
    }
    if (keystoreSecret == null) {
        keystoreSecret = KeyStoreUtils.DEFAULT_KEYSTORE_SECRET;
    }
    KeyStore keyStore = KeyStoreUtils.load(new File(keystore), keystoreSecret);
    if (keyAlias == null) {
        List<String> keyAliases = KeyStoreUtils.getKeyAliases(keyStore);
        if (keyAliases.size() == 0) {
            throw new CliException("No keys found in keystore");
        }
        if (keyAliases.size() != 1) {
            System.out.println("Found keys:\n\t" + Joiner.on("\n\t").join(keyAliases));
            throw new CliException("Multiple keys found in keystore; specify --alias");
        }
        keyAlias = keyAliases.get(0);
    }
    Certificate[] certificateChain = keyStore.getCertificateChain(keyAlias);
    return certificateChain;
}
Also used : CliException(com.fathomdb.cli.CliException) KeyStore(java.security.KeyStore) File(java.io.File) Certificate(java.security.cert.Certificate)

Example 4 with CliException

use of com.fathomdb.cli.CliException in project platformlayer by platformlayer.

the class CreateProject method runCommand.

@Override
public Object runCommand() throws RepositoryException {
    UserDatabase userRepository = getContext().getUserRepository();
    // We need to login to unlock the user key so we can encrypt the project key!
    UserEntity me = getContext().loginDirect();
    if (projectKey.contains("@@")) {
        throw new CliException("Project names with @@ are reserved for system uses");
    }
    ProjectEntity project = userRepository.createProject(projectKey, me);
    return project;
}
Also used : CliException(com.fathomdb.cli.CliException) ProjectEntity(org.platformlayer.auth.ProjectEntity) UserDatabase(org.platformlayer.auth.UserDatabase) UserEntity(org.platformlayer.auth.UserEntity)

Example 5 with CliException

use of com.fathomdb.cli.CliException in project platformlayer by platformlayer.

the class SetProperty method changeItem.

@Override
protected void changeItem(UntypedItemXml item) {
    Element element = getElement(item, key);
    if (element == null) {
        throw new CliException("Cannot find element: " + key);
    }
    element.setTextContent(value);
}
Also used : CliException(com.fathomdb.cli.CliException) Element(org.w3c.dom.Element)

Aggregations

CliException (com.fathomdb.cli.CliException)12 IOException (java.io.IOException)4 NoCloseInputStream (com.fathomdb.io.NoCloseInputStream)3 File (java.io.File)3 InputStream (java.io.InputStream)3 PlatformLayerClient (org.platformlayer.PlatformLayerClient)3 UserDatabase (org.platformlayer.auth.UserDatabase)3 Ansi (com.fathomdb.cli.commands.Ansi)2 Certificate (java.security.cert.Certificate)2 ProjectEntity (org.platformlayer.auth.ProjectEntity)2 UserEntity (org.platformlayer.auth.UserEntity)2 UntypedItem (org.platformlayer.common.UntypedItem)2 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)2 Element (org.w3c.dom.Element)2 ClientAction (com.fathomdb.cli.output.ClientAction)1 CryptoKey (com.fathomdb.crypto.CryptoKey)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InetAddress (java.net.InetAddress)1 KeyStore (java.security.KeyStore)1