Search in sources :

Example 41 with DevfileException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.

the class ProjectConverter method sanitizeClonePath.

private String sanitizeClonePath(String clonePath) throws DevfileException {
    URI uri = clonePathToURI(clonePath);
    if (uri.isAbsolute()) {
        throw new DevfileException("The clonePath must be a relative path. This seems to be an URI with a scheme: " + clonePath);
    }
    uri = uri.normalize();
    String path = uri.getPath();
    if (path.isEmpty()) {
        // the user tried to trick us with something like "blah/.."
        throw new DevfileException("Cloning directly into projects root is not allowed." + " The clonePath that resolves to empty path: " + clonePath);
    }
    if (path.startsWith("/")) {
        // user-authored. Just don't give the user impression we can support absolute paths.
        throw new DevfileException("The clonePath must be a relative. This seems to be an absolute path: " + clonePath);
    }
    if (path.startsWith("..")) {
        // an attempt to escape the projects root, e.g. "ich/../muss/../../raus". That's a no-no.
        throw new DevfileException("The clonePath cannot escape the projects root. Don't use .. to try and do that." + " The invalid path was: " + clonePath);
    }
    return path;
}
Also used : URI(java.net.URI) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 42 with DevfileException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.

the class EditorComponentToWorkspaceApplier method apply.

/**
 * Applies changes on workspace config according to the specified editor component.
 *
 * @param workspaceConfig workspace config on which changes should be applied
 * @param editorComponent plugin component that should be applied
 * @param contentProvider optional content provider that may be used for external component
 *     resource fetching
 * @throws IllegalArgumentException if specified workspace config or plugin component is null
 * @throws IllegalArgumentException if specified component has type different from cheEditor
 */
@Override
public void apply(WorkspaceConfigImpl workspaceConfig, ComponentImpl editorComponent, FileContentProvider contentProvider) throws DevfileException {
    checkArgument(workspaceConfig != null, "Workspace config must not be null");
    checkArgument(editorComponent != null, "Component must not be null");
    checkArgument(EDITOR_COMPONENT_TYPE.equals(editorComponent.getType()), format("Plugin must have `%s` type", EDITOR_COMPONENT_TYPE));
    final String editorComponentAlias = editorComponent.getAlias();
    final String editorId = editorComponent.getId();
    final String registryUrl = editorComponent.getRegistryUrl();
    final ExtendedPluginFQN fqn = componentFQNParser.evaluateFQN(editorComponent, contentProvider);
    if (!isNullOrEmpty(fqn.getReference())) {
        workspaceConfig.getAttributes().put(WORKSPACE_TOOLING_EDITOR_ATTRIBUTE, fqn.getReference());
    } else {
        workspaceConfig.getAttributes().put(WORKSPACE_TOOLING_EDITOR_ATTRIBUTE, componentFQNParser.getCompositeId(registryUrl, editorId));
    }
    workspaceConfig.getCommands().stream().filter(c -> editorComponentAlias != null && editorComponentAlias.equals(c.getAttributes().get(COMPONENT_ALIAS_COMMAND_ATTRIBUTE))).forEach(c -> c.getAttributes().put(PLUGIN_ATTRIBUTE, fqn.getId()));
    // make sure id is set to be able to match component with plugin broker result
    // when referenceContent is used
    editorComponent.setId(fqn.getId());
}
Also used : PLUGIN_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.PLUGIN_ATTRIBUTE) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) WORKSPACE_TOOLING_EDITOR_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE) String.format(java.lang.String.format) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) ComponentToWorkspaceApplier(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) COMPONENT_ALIAS_COMMAND_ATTRIBUTE(org.eclipse.che.api.workspace.server.devfile.Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE) EDITOR_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_TYPE) ComponentFQNParser(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentFQNParser) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN)

Example 43 with DevfileException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.

the class DevfileVersionDetector method devfileMajorVersion.

/**
 * Gives major version of the devfile.
 *
 * <pre>
 *   1 -> 1
 *   1.0.0 -> 1
 *   1.99 -> 1
 *   2.0.0 -> 2
 *   2.1 -> 2
 *   a.a -> DevfileException
 *   a -> DevfileException
 * </pre>
 *
 * @param devfile to inspect
 * @return major version of the devfile
 * @throws DevfileException when can't find the field with version
 */
public int devfileMajorVersion(JsonNode devfile) throws DevfileException {
    String version = devfileVersion(devfile);
    int dot = version.indexOf(".");
    final String majorVersion = dot > 0 ? version.substring(0, dot) : version;
    try {
        return Integer.parseInt(majorVersion);
    } catch (NumberFormatException nfe) {
        throw new DevfileException("Unable to parse devfile version. This is not a valid devfile.", nfe);
    }
}
Also used : DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 44 with DevfileException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.

the class PluginFQNParser method evaluateFqn.

/**
 * Evaluates plugin FQN from provided reference by trying to fetch and parse its meta information.
 *
 * @param reference plugin reference to evaluate FQN from
 * @param fileContentProvider content provider instance to perform plugin meta requests
 * @return plugin FQN evaluated from given reference
 * @throws InfrastructureException if plugin reference is invalid or inaccessible
 */
public ExtendedPluginFQN evaluateFqn(String reference, FileContentProvider fileContentProvider) throws InfrastructureException {
    JsonNode contentNode;
    try {
        String pluginMetaContent = fileContentProvider.fetchContent(reference);
        contentNode = yamlReader.readTree(pluginMetaContent);
    } catch (DevfileException | IOException e) {
        throw new InfrastructureException(format("Plugin reference URL '%s' is invalid.", reference), e);
    }
    JsonNode publisher = contentNode.path("publisher");
    if (publisher.isMissingNode()) {
        throw new InfrastructureException(formatMessage(reference, "publisher"));
    }
    JsonNode name = contentNode.get("name");
    if (name.isMissingNode()) {
        throw new InfrastructureException(formatMessage(reference, "name"));
    }
    JsonNode version = contentNode.get("version");
    if (version.isMissingNode()) {
        throw new InfrastructureException(formatMessage(reference, "version"));
    }
    if (!version.isValueNode()) {
        throw new InfrastructureException(format("Plugin specified by reference URL '%s' has version field that cannot be parsed to string", reference));
    }
    return new ExtendedPluginFQN(reference, publisher.textValue(), name.textValue(), version.asText());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 45 with DevfileException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.

the class KubernetesComponentToWorkspaceApplierTest method shouldThrowExceptionWhenRecipeComponentIsPresentAndContentProviderDoesNotSupportFetching.

@Test(expectedExceptions = DevfileException.class, expectedExceptionsMessageRegExp = "Fetching content of file `reference.yaml` specified in `reference` field of component `foo` is not " + "supported. Please provide its content in `referenceContent` field. Cause: fetch is not supported")
public void shouldThrowExceptionWhenRecipeComponentIsPresentAndContentProviderDoesNotSupportFetching() throws Exception {
    // given
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    // when
    applier.apply(workspaceConfig, component, e -> {
        throw new DevfileException("fetch is not supported");
    });
}
Also used : ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Test(org.testng.annotations.Test)

Aggregations

DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)52 Test (org.testng.annotations.Test)22 IOException (java.io.IOException)20 ApiException (org.eclipse.che.api.core.ApiException)18 FileContentProvider (org.eclipse.che.api.workspace.server.devfile.FileContentProvider)18 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)16 HashMap (java.util.HashMap)14 List (java.util.List)14 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)14 Map (java.util.Map)12 Optional (java.util.Optional)12 ServerException (org.eclipse.che.api.core.ServerException)12 ExtendedError (org.eclipse.che.api.core.rest.shared.dto.ExtendedError)12 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)12 ScmUnauthorizedException (org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)12 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 Collections (java.util.Collections)10 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)10 UnknownScmProviderException (org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException)10