Search in sources :

Example 1 with Plugin

use of hudson.Plugin in project blueocean-plugin by jenkinsci.

the class JwtImpl method getToken.

@Override
public JwtToken getToken(@Nullable @QueryParameter("expiryTimeInMins") Integer expiryTimeInMins, @Nullable @QueryParameter("maxExpiryTimeInMins") Integer maxExpiryTimeInMins) {
    String t = System.getProperty("EXPIRY_TIME_IN_MINS");
    long expiryTime = DEFAULT_EXPIRY_IN_SEC;
    if (t != null) {
        expiryTime = Integer.parseInt(t);
    }
    int maxExpiryTime = DEFAULT_MAX_EXPIRY_TIME_IN_MIN;
    t = System.getProperty("MAX_EXPIRY_TIME_IN_MINS");
    if (t != null) {
        maxExpiryTime = Integer.parseInt(t);
    }
    if (maxExpiryTimeInMins != null) {
        maxExpiryTime = maxExpiryTimeInMins;
    }
    if (expiryTimeInMins != null) {
        if (expiryTimeInMins > maxExpiryTime) {
            throw new ServiceException.BadRequestExpception(String.format("expiryTimeInMins %s can't be greated than %s", expiryTimeInMins, maxExpiryTime));
        }
        expiryTime = expiryTimeInMins * 60;
    }
    Authentication authentication = Jenkins.getInstance().getAuthentication();
    if (authentication == null) {
        throw new ServiceException.UnauthorizedException("Unauthorized: No login session found");
    }
    String userId = authentication.getName();
    User user = User.get(userId, false, Collections.emptyMap());
    String email = null;
    String fullName = null;
    if (user != null) {
        fullName = user.getFullName();
        userId = user.getId();
        Mailer.UserProperty p = user.getProperty(Mailer.UserProperty.class);
        if (p != null)
            email = p.getAddress();
    }
    Plugin plugin = Jenkins.getInstance().getPlugin("blueocean-jwt");
    String issuer = "blueocean-jwt:" + ((plugin != null) ? plugin.getWrapper().getVersion() : "");
    JwtToken jwtToken = new JwtToken();
    jwtToken.claim.put("jti", UUID.randomUUID().toString().replace("-", ""));
    jwtToken.claim.put("iss", issuer);
    jwtToken.claim.put("sub", userId);
    jwtToken.claim.put("name", fullName);
    long currentTime = System.currentTimeMillis() / 1000;
    jwtToken.claim.put("iat", currentTime);
    jwtToken.claim.put("exp", currentTime + expiryTime);
    jwtToken.claim.put("nbf", currentTime - DEFAULT_NOT_BEFORE_IN_SEC);
    //set claim
    JSONObject context = new JSONObject();
    JSONObject userObject = new JSONObject();
    userObject.put("id", userId);
    userObject.put("fullName", fullName);
    userObject.put("email", email);
    context.put("user", userObject);
    jwtToken.claim.put("context", context);
    return jwtToken;
}
Also used : JwtToken(io.jenkins.blueocean.auth.jwt.JwtToken) User(hudson.model.User) JSONObject(net.sf.json.JSONObject) Authentication(org.acegisecurity.Authentication) Mailer(hudson.tasks.Mailer) Plugin(hudson.Plugin)

Example 2 with Plugin

use of hudson.Plugin in project blueocean-plugin by jenkinsci.

the class PipelineSearch method search.

@Override
public Pageable<BluePipeline> search(Query q) {
    String s = q.param(EXCLUDED_FROM_FLATTENING_PARAM);
    String org = q.param(ORGANIZATION_PARAM);
    if (org != null && !OrganizationImpl.INSTANCE.getName().equals(org)) {
        throw new ServiceException.BadRequestExpception(String.format("Organization %s not found. Query parameter %s value: %s is invalid. ", org, ORGANIZATION_PARAM, org));
    }
    List<Class> excludeList = new ArrayList<>();
    if (s != null) {
        for (String s1 : s.split(",")) {
            Class c = null;
            try {
                c = Class.forName(s1);
            } catch (ClassNotFoundException e) {
                try {
                    //TODO: There should be better ways to find a class from a plugin.
                    Plugin p = Jenkins.getInstance().getPlugin("blueocean-pipeline-api-impl");
                    if (p != null) {
                        c = p.getWrapper().classLoader.loadClass(s1);
                    } else {
                        logger.error("blueocean-pipeline-api-impl plugin not found!");
                    }
                } catch (ClassNotFoundException e1) {
                    logger.error(e.getMessage(), e1);
                }
            //ignored, give other OmniSearch implementations chance, they might handle it
            //throw new ServiceException.BadRequestExpception(String.format("%s parameter has invalid value: %s", EXCLUDED_FROM_FLATTENING_PARAM, s1), e);
            }
            if (c != null) {
                excludeList.add(c);
            }
        }
    }
    Collection<Item> items = new ArrayList<>();
    if (!excludeList.isEmpty()) {
        for (Item item : Jenkins.getActiveInstance().getAllItems(Item.class)) {
            if (!exclude(item.getParent(), excludeList)) {
                items.add(item);
            }
        }
    } else {
        items = Jenkins.getActiveInstance().getAllItems(Item.class);
    }
    items = ContainerFilter.filter(items);
    final Iterator<BluePipeline> pipelineIterator = new PipelineContainerImpl().getPipelines(items);
    final List<BluePipeline> pipelines = new ArrayList<>();
    String pipeline = q.param(getType());
    if (pipeline == null) {
        return Pageables.wrap(new Iterable<BluePipeline>() {

            @Override
            public Iterator<BluePipeline> iterator() {
                return pipelineIterator;
            }
        });
    } else {
        while (pipelineIterator.hasNext()) {
            BluePipeline p = pipelineIterator.next();
            if (!p.getName().equals(pipeline)) {
                continue;
            }
            pipelines.add(p);
        }
        return Pageables.wrap(pipelines);
    }
}
Also used : ArrayList(java.util.ArrayList) Item(hudson.model.Item) Iterator(java.util.Iterator) BluePipeline(io.jenkins.blueocean.rest.model.BluePipeline) Plugin(hudson.Plugin)

Example 3 with Plugin

use of hudson.Plugin in project hudson-2.x by hudson.

the class SmoothiePluginStrategy method load.

/**
     * Loads the optional {@link hudson.Plugin} instance, configures and starts it.
     */
public void load(final PluginWrapper plugin) throws IOException {
    checkNotNull(plugin);
    if (log.isDebugEnabled()) {
        log.debug("Configuring plugin: {}", plugin.getShortName());
    }
    container.register(plugin);
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(plugin.classLoader);
    try {
        Plugin instance;
        // Load the plugin instance, if one has been configured.
        if (plugin.getPluginClass() == null) {
            instance = new Plugin.DummyImpl();
        } else {
            try {
                // Ask the container to construct the instance
                Class<? extends Plugin> type = loadPluginClass(plugin);
                instance = container.injector(plugin).getInstance(type);
                log.trace("Plugin instance: {}", instance);
            } catch (Throwable e) {
                throw new IOException2("Failed to load plugin instance for: " + plugin.getShortName(), e);
            }
        }
        plugin.setPlugin(instance);
        try {
            start(plugin);
        } catch (Exception e) {
            throw new IOException2("Failed to start plugin: " + plugin.getShortName(), e);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : IOException2(hudson.util.IOException2) IOException(java.io.IOException) Plugin(hudson.Plugin)

Example 4 with Plugin

use of hudson.Plugin in project hudson-2.x by hudson.

the class SmoothiePluginStrategy method start.

/**
     * Configures and starts the {@link hudson.Plugin} instance.
     */
private void start(final PluginWrapper plugin) throws Exception {
    assert plugin != null;
    if (log.isDebugEnabled()) {
        log.debug("Starting plugin: {}", plugin.getShortName());
    }
    Plugin instance = plugin.getPlugin();
    instance.setServletContext(Hudson.getInstance().servletContext);
    instance.start();
}
Also used : Plugin(hudson.Plugin)

Aggregations

Plugin (hudson.Plugin)4 Item (hudson.model.Item)1 User (hudson.model.User)1 Mailer (hudson.tasks.Mailer)1 IOException2 (hudson.util.IOException2)1 JwtToken (io.jenkins.blueocean.auth.jwt.JwtToken)1 BluePipeline (io.jenkins.blueocean.rest.model.BluePipeline)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 JSONObject (net.sf.json.JSONObject)1 Authentication (org.acegisecurity.Authentication)1