forked from maxlaverse/kubernetes-credentials-plugin
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JENKINS-73139] Converting tests to com.sun.net.httpserver
#54
Merged
basil
merged 20 commits into
jenkinsci:master
from
nevingeorgesunny:using-in-memory-mock-httpserver
Aug 14, 2024
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f6a24cd
create a mockserver with com.sun.net.httpserver
nevingeorgesunny 9d0e47a
using the mockserver in OpenShiftBearerTokenCredentialTest
nevingeorgesunny d685500
Merge branch 'jenkinsci:master' into using-in-memory-mock-httpserver
nevingeorgesunny 9333af7
removed JenkinsRule by mistake putting it back
nevingeorgesunny a016ef7
Merge remote-tracking branch 'dev/using-in-memory-mock-httpserver' in…
nevingeorgesunny 6daded2
- AbstractOpenShiftBearerTokenCredentialFIPSTest is now using in-memo…
nevingeorgesunny 8e83a6a
This file uses 8 space indentation when the rest of the files in this…
nevingeorgesunny 87473a9
Update pom.xml
basil 18c9fbb
Update pom.xml
basil 189fef9
sort imports
basil 1330786
Format code consistently with other repositories
basil f8df999
Remove unnecessary throws and comment
basil b92127d
Improve legibility
basil cc67e8b
Make tests consistent with each other
basil 1fd801c
Code cleanup
basil 9567ac3
Merge remote-tracking branch 'origin/master' into using-in-memory-moc…
basil 70851f4
De-duplicate
basil a4ee1f1
Undo local testing changes
basil 2c1f59b
Reduce scope and size of code change
basil 939395a
No need to change test behavior
basil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
73 changes: 0 additions & 73 deletions
73
src/test/java/org/jenkinsci/plugins/kubernetes/credentials/MockHttpServlet.java
This file was deleted.
Oops, something went wrong.
107 changes: 107 additions & 0 deletions
107
...rg/jenkinsci/plugins/kubernetes/credentials/OpenShiftBearerTokenCredentialMockServer.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,107 @@ | ||
package org.jenkinsci.plugins.kubernetes.credentials; | ||
nevingeorgesunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpServer; | ||
import com.sun.net.httpserver.HttpsServer; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author Nevin Sunny | ||
*/ | ||
public class OpenShiftBearerTokenCredentialMockServer { | ||
|
||
public static void registerHttpHandlers(HttpServer server) { | ||
server.createContext( | ||
"/bad-location/oauth/authorize", | ||
OpenShiftBearerTokenCredentialMockServer::badLocationHandler); | ||
server.createContext( | ||
"/missing-location/oauth/authorize", | ||
OpenShiftBearerTokenCredentialMockServer::missingLocationHandler); | ||
server.createContext( | ||
"/bad-response/oauth/authorize", | ||
OpenShiftBearerTokenCredentialMockServer::badResponseHandler); | ||
server.createContext( | ||
"/valid-response/oauth/authorize", | ||
OpenShiftBearerTokenCredentialMockServer::validResponseHandler1); | ||
server.createContext( | ||
"/valid-response2/oauth/authorize", | ||
OpenShiftBearerTokenCredentialMockServer::validResponseHandler2); | ||
server.createContext("/", OpenShiftBearerTokenCredentialMockServer::defaultHandler); | ||
} | ||
|
||
private static void badLocationHandler(HttpExchange he) throws IOException { | ||
he.getResponseHeaders().set("Location", "bad"); | ||
he.sendResponseHeaders(302, -1); | ||
} | ||
|
||
private static void missingLocationHandler(HttpExchange he) throws IOException { | ||
he.sendResponseHeaders(302, -1); // No Location header | ||
} | ||
|
||
private static void badResponseHandler(HttpExchange he) throws IOException { | ||
he.sendResponseHeaders(400, -1); | ||
} | ||
|
||
private static void validResponseHandler1(HttpExchange he) throws IOException { | ||
he.getResponseHeaders().set("Location", "http://my-service/#access_token=1234&expires_in=86400"); | ||
he.sendResponseHeaders(302, -1); | ||
} | ||
|
||
private static void validResponseHandler2(HttpExchange he) throws IOException { | ||
he.getResponseHeaders().set("Location", "http://my-service/#access_token=1235&expires_in=86400"); | ||
he.sendResponseHeaders(302, -1); | ||
} | ||
|
||
private static void defaultHandler(HttpExchange he) throws IOException { | ||
String path = he.getRequestURI().getPath(); | ||
Pattern pattern = Pattern.compile("(.*)/.well-known/oauth-authorization-server"); | ||
Matcher matcher = pattern.matcher(path); | ||
|
||
String scheme = he.getHttpContext().getServer() instanceof HttpsServer ? "https" : "http"; | ||
String rootURL = scheme + "://localhost:" + he.getLocalAddress().getPort() + "/"; | ||
|
||
if (matcher.find()) { | ||
String responseToClient = "{\n" + | ||
" \"issuer\": \"" + rootURL + "\",\n" + | ||
" \"authorization_endpoint\": \"" + rootURL + "/" + matcher.group(1) + "/oauth/authorize\",\n" + | ||
" \"token_endpoint\": \"" + rootURL + "/oauth/token\",\n" + | ||
" \"scopes_supported\": [\n" + | ||
" \"user:check-access\",\n" + | ||
" \"user:full\",\n" + | ||
" \"user:info\",\n" + | ||
" \"user:list-projects\",\n" + | ||
" \"user:list-scoped-projects\"\n" + | ||
" ],\n" + | ||
" \"response_types_supported\": [\n" + | ||
" \"code\",\n" + | ||
" \"token\"\n" + | ||
" ],\n" + | ||
" \"grant_types_supported\": [\n" + | ||
" \"authorization_code\",\n" + | ||
" \"implicit\"\n" + | ||
" ],\n" + | ||
" \"code_challenge_methods_supported\": [\n" + | ||
" \"plain\",\n" + | ||
" \"S256\"\n" + | ||
" ]\n" + | ||
"}"; | ||
byte[] responseBytes = responseToClient.getBytes(StandardCharsets.UTF_8); | ||
he.sendResponseHeaders(200, responseBytes.length); | ||
try (OutputStream os = he.getResponseBody()) { | ||
os.write(responseBytes); | ||
} | ||
} else { | ||
String responseToClient = "Bad test: unknown path " + path; | ||
byte[] responseBytes = responseToClient.getBytes(StandardCharsets.UTF_8); | ||
he.sendResponseHeaders(500, responseBytes.length); | ||
try (OutputStream os = he.getResponseBody()) { | ||
os.write(responseBytes); | ||
} | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the original behaviour in some cases, see https://android.googlesource.com/platform/external/jetty/+/refs/heads/marshmallow-dev/src/java/org/eclipse/jetty/util/ssl/SslContextFactory.java#95 in some fips environments that property may be used.
And this can be applied to other parts, for example you are forcing a "tls" for
SslContext
but the original code may not be doing that or allow the algorithm to be overriden by a system property... This kind of things are important as we may be removing the ability for a fips env to override things.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a relevant comment I think. The purpose of the test is to ensure that TLS is in use and that TLS verification is not disabled. There are no changes to production code, so the only question is whether the effectiveness of the test is reduced. The test previously didn't require a FIPS compliant machine but verified FIPS compliance as described above, and the same conditions are true after this refactoring, so there is no issue here.