This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding logging for X-Forwarded-For client IP address (#61)
- Loading branch information
Showing
17 changed files
with
173 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,6 @@ | |
# limitations under the License. | ||
# | ||
|
||
version=0.27.0 | ||
version=0.28.0-RC1 | ||
groupId=com.nike.cerberus | ||
artifactId=cms |
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 |
---|---|---|
@@ -1,7 +1,43 @@ | ||
package com.nike.cerberus; | ||
|
||
import com.nike.riposte.server.http.RequestInfo; | ||
import io.netty.handler.codec.http.HttpHeaders; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public final class CerberusHttpHeaders { | ||
|
||
public static final String HEADER_X_CERBERUS_CLIENT = "X-Cerberus-Client"; | ||
public static final String HEADER_X_REFRESH_TOKEN = "X-Refresh-Token"; | ||
public static final String HEADER_X_FORWARDED_FOR = "X-Forwarded-For"; | ||
private static final String UNKNOWN = "Unknown"; | ||
|
||
/** | ||
* Get the value of the X-Cerberus-Client header or "Unknown" if not found. | ||
*/ | ||
public static String getClientVersion(RequestInfo request) { | ||
final HttpHeaders headers = request.getHeaders(); | ||
if (headers != null) { | ||
String value = headers.get(HEADER_X_CERBERUS_CLIENT); | ||
if (value != null) { | ||
return value; | ||
} | ||
} | ||
return UNKNOWN; | ||
} | ||
|
||
/** | ||
* Get the first IP address from the Http header "X-Forwarded-For" | ||
* | ||
* E.g. "X-Forwarded-For: ip1, ip2, ip3" would return "ip1" | ||
*/ | ||
public static String getXForwardedClientIp(RequestInfo request) { | ||
final HttpHeaders headers = request.getHeaders(); | ||
if (headers != null) { | ||
String value = headers.get(HEADER_X_FORWARDED_FOR); | ||
if (value != null) { | ||
return StringUtils.substringBefore(value, ",").trim(); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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
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
Oops, something went wrong.