-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUNDECK_BYPASS_URL env var: rewrite redirects for external url
- Loading branch information
Showing
3 changed files
with
105 additions
and
12 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
50 changes: 50 additions & 0 deletions
50
src/main/java/org/rundeck/client/util/RedirectBypassInterceptor.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,50 @@ | ||
package org.rundeck.client.util; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Change redirect behavior, if Location begins with the bypass url, replace that part with the app URL. | ||
*/ | ||
public class RedirectBypassInterceptor implements Interceptor { | ||
private String appBaseUrl; | ||
private String bypassUrl; | ||
|
||
public RedirectBypassInterceptor(final String appBaseUrl, final String bypassUrl) { | ||
this.appBaseUrl = appBaseUrl; | ||
this.bypassUrl = bypassUrl; | ||
} | ||
|
||
@Override | ||
public Response intercept(final Chain chain) throws IOException { | ||
Response originalResponse = chain.proceed(chain.request()); | ||
if (originalResponse.isRedirect()) { | ||
String originalLocation = originalResponse.header("location"); | ||
String newUrl = remapUrl(originalLocation, bypassUrl, appBaseUrl); | ||
if (null != newUrl) { | ||
return originalResponse.newBuilder() | ||
.header("Location", newUrl) | ||
.build(); | ||
} | ||
} | ||
return originalResponse; | ||
} | ||
|
||
/** | ||
* Replace the prefix of the originurl that starts with the bypassurl string with the appbaseurl string | ||
* | ||
* @param origUrl | ||
* @param bypassUrl | ||
* @param appBaseUrl | ||
* | ||
* @return | ||
*/ | ||
public static String remapUrl(final String origUrl, final String bypassUrl, final String appBaseUrl) { | ||
if (origUrl.startsWith(bypassUrl)) { | ||
return appBaseUrl + origUrl.substring(bypassUrl.length()); | ||
} | ||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/groovy/org/rundeck/client/util/RedirectBypassInterceptorSpec.groovy
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,23 @@ | ||
package org.rundeck.client.util | ||
|
||
import spock.lang.Specification | ||
|
||
/** | ||
* Created by greg on 9/9/16. | ||
*/ | ||
class RedirectBypassInterceptorSpec extends Specification { | ||
def "remap"() { | ||
when: | ||
String result = RedirectBypassInterceptor.remapUrl(orig, bypass, app) | ||
|
||
|
||
then: | ||
result == expect | ||
|
||
where: | ||
orig | bypass | app | expect | ||
"http://host1/c1/path1" | "http://host1" | "http://host2/c2" | "http://host2/c2/c1/path1" | ||
"http://host1/c1/path1" | "http://host1/c1" | "http://host2/c2" | "http://host2/c2/path1" | ||
"http://host1/c1/path1" | "http://host1/c1" | "http://host2" | "http://host2/path1" | ||
} | ||
} |