-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-18114] Fixing CLI crumbs (#3019)
* [JENKINS-18114] The CLI client already asks for a crumb; we just to actually serve it, from CrumbIssuer.RestrictedApi. * serveCliActionToAnonymousUserWithoutPermissions() was checking some stuff better checked by authentication(), and was failing since this fake client was not passing a crumb. * Bring back CliCrumbExclusion, needed for anonymous use with no read access, and just let the client stop asking for a crumb. * Added FullDuplexHttpServiceTest; useful to have a simple, self-contained demonstration of the HTTP Duplex transport.
- Loading branch information
1 parent
3f84d30
commit 5e84c54
Showing
7 changed files
with
137 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
test/src/test/java/jenkins/util/FullDuplexHttpServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2017 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package jenkins.util; | ||
|
||
import hudson.cli.FullDuplexHttpStream; | ||
import hudson.model.RootAction; | ||
import hudson.security.csrf.CrumbExclusion; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
import org.junit.Rule; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.LoggerRule; | ||
import org.jvnet.hudson.test.TestExtension; | ||
import org.kohsuke.stapler.HttpResponse; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
|
||
public class FullDuplexHttpServiceTest { | ||
|
||
@Rule | ||
public JenkinsRule r = new JenkinsRule(); | ||
|
||
@Rule | ||
public LoggerRule logging = new LoggerRule().record(FullDuplexHttpService.class, Level.FINE).record(FullDuplexHttpStream.class, Level.FINE); | ||
|
||
@Test | ||
public void smokes() throws Exception { | ||
logging.record("org.eclipse.jetty", Level.ALL); | ||
FullDuplexHttpStream con = new FullDuplexHttpStream(r.getURL(), "test/", null); | ||
InputStream is = con.getInputStream(); | ||
OutputStream os = con.getOutputStream(); | ||
os.write(33); | ||
os.flush(); | ||
Logger.getLogger(FullDuplexHttpServiceTest.class.getName()).info("uploaded initial content"); | ||
assertEquals(0, is.read()); // see FullDuplexHttpStream.getInputStream | ||
assertEquals(66, is.read()); | ||
} | ||
@TestExtension("smokes") | ||
public static class Endpoint implements RootAction { | ||
private transient final Map<UUID, FullDuplexHttpService> duplexServices = new HashMap<>(); | ||
@Override | ||
public String getUrlName() { | ||
return "test"; | ||
} | ||
@Override | ||
public String getIconFileName() { | ||
return null; | ||
} | ||
@Override | ||
public String getDisplayName() { | ||
return null; | ||
} | ||
public HttpResponse doIndex() { | ||
return new FullDuplexHttpService.Response(duplexServices) { | ||
@Override | ||
protected FullDuplexHttpService createService(StaplerRequest req, UUID uuid) throws IOException, InterruptedException { | ||
return new FullDuplexHttpService(uuid) { | ||
@Override | ||
protected void run(InputStream upload, OutputStream download) throws IOException, InterruptedException { | ||
int x = upload.read(); | ||
download.write(x * 2); | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
} | ||
@TestExtension("smokes") | ||
public static class EndpointCrumbExclusion extends CrumbExclusion { | ||
@Override | ||
public boolean process(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
if ("/test/".equals(request.getPathInfo())) { | ||
chain.doFilter(request, response); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
} |