Commit d735e643 authored by Ajit Jagtap's avatar Ajit Jagtap
Browse files

Merge branch 'vi_ipaddressFixes' into 'devbranch'

Ip parse and port remove logic added

See merge request !208
parents f096efe2 b858e9d3
2 merge requests!210Staging mepdeployment05072020,!208Ip parse and port remove logic added
Showing with 34 additions and 21 deletions
...@@ -4,6 +4,7 @@ package routebuildermdl ...@@ -4,6 +4,7 @@ package routebuildermdl
import ( import (
"context" "context"
"net"
"strings" "strings"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/authmdl/jwtmdl" "corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/authmdl/jwtmdl"
...@@ -203,8 +204,7 @@ func commonHandler(c *routing.Context, isRestricted, isRoleBased, heavyDataActiv ...@@ -203,8 +204,7 @@ func commonHandler(c *routing.Context, isRestricted, isRoleBased, heavyDataActiv
func OpenHandler(c *routing.Context) error { func OpenHandler(c *routing.Context) error {
c.Response.Header.Set("content-type", "application/json") c.Response.Header.Set("content-type", "application/json")
principal := servicebuildermdl.Principal{} principal := servicebuildermdl.Principal{}
principal.ClientIP = getClientIP(c)
principal.ClientIP = c.RemoteIP().String()
commonHandler(c, false, false, false, principal) commonHandler(c, false, false, false, principal)
return nil return nil
} }
...@@ -219,7 +219,6 @@ func RestrictedHandler(c *routing.Context) error { ...@@ -219,7 +219,6 @@ func RestrictedHandler(c *routing.Context) error {
c.SetStatusCode(412) c.SetStatusCode(412)
return err return err
} }
// pricipalObj.ClientIP = c.RemoteIP().String()
commonHandler(c, true, false, false, pricipalObj) commonHandler(c, true, false, false, pricipalObj)
return nil return nil
} }
...@@ -234,7 +233,6 @@ func RoleBasedHandler(c *routing.Context) error { ...@@ -234,7 +233,6 @@ func RoleBasedHandler(c *routing.Context) error {
c.SetStatusCode(412) c.SetStatusCode(412)
return err return err
} }
// pricipalObj.ClientIP = c.RemoteIP().String()
commonHandler(c, true, true, false, pricipalObj) commonHandler(c, true, true, false, pricipalObj)
return nil return nil
} }
...@@ -243,8 +241,7 @@ func RoleBasedHandler(c *routing.Context) error { ...@@ -243,8 +241,7 @@ func RoleBasedHandler(c *routing.Context) error {
func HeavyOpenHandler(c *routing.Context) error { func HeavyOpenHandler(c *routing.Context) error {
c.Response.Header.Set("content-type", "application/json") c.Response.Header.Set("content-type", "application/json")
principal := servicebuildermdl.Principal{} principal := servicebuildermdl.Principal{}
principal.ClientIP = getClientIP(c)
principal.ClientIP = c.RemoteIP().String()
commonHandler(c, false, false, true, principal) commonHandler(c, false, false, true, principal)
return nil return nil
} }
...@@ -259,7 +256,6 @@ func HeavyRestrictedHandler(c *routing.Context) error { ...@@ -259,7 +256,6 @@ func HeavyRestrictedHandler(c *routing.Context) error {
c.SetStatusCode(412) c.SetStatusCode(412)
return err return err
} }
// pricipalObj.ClientIP = c.RemoteIP().String()
commonHandler(c, true, false, true, pricipalObj) commonHandler(c, true, false, true, pricipalObj)
return nil return nil
} }
...@@ -274,7 +270,6 @@ func HeavyRoleBasedHandler(c *routing.Context) error { ...@@ -274,7 +270,6 @@ func HeavyRoleBasedHandler(c *routing.Context) error {
c.SetStatusCode(412) c.SetStatusCode(412)
return err return err
} }
// pricipalObj.ClientIP = c.RemoteIP().String()
commonHandler(c, true, true, true, pricipalObj) commonHandler(c, true, true, true, pricipalObj)
return nil return nil
} }
...@@ -352,7 +347,19 @@ func getClientIP(c *routing.Context) string { ...@@ -352,7 +347,19 @@ func getClientIP(c *routing.Context) string {
clientIP = string(c.Request.Header.Peek("X-Forwarded-For")) clientIP = string(c.Request.Header.Peek("X-Forwarded-For"))
} }
if clientIP == "" { if clientIP == "" {
clientIP = c.RemoteIP().String() clientIP, _, splitHostPortError := net.SplitHostPort(c.RemoteIP().String())
if splitHostPortError == nil && isCorrectIP(clientIP) {
return clientIP
}
return ""
}
if isCorrectIP(clientIP) {
return clientIP
} }
return clientIP return ""
}
// isCorrectIP - return true if ip string is valid textual representation of an IP address, else returns false
func isCorrectIP(ip string) bool {
return net.ParseIP(ip) != nil
} }
...@@ -5,6 +5,7 @@ package routebuildermdl ...@@ -5,6 +5,7 @@ package routebuildermdl
import ( import (
"context" "context"
"io/ioutil" "io/ioutil"
"net"
"net/http" "net/http"
"strings" "strings"
...@@ -192,8 +193,7 @@ func commonHandler(c *gin.Context, isRestricted, isRoleBased, heavyDataActivity ...@@ -192,8 +193,7 @@ func commonHandler(c *gin.Context, isRestricted, isRoleBased, heavyDataActivity
// OpenHandler for /o // OpenHandler for /o
func OpenHandler(c *gin.Context) { func OpenHandler(c *gin.Context) {
principal := servicebuildermdl.Principal{} principal := servicebuildermdl.Principal{}
principal.ClientIP = getClientIP(c)
// principal.ClientIP = c.Request.RemoteAddr
commonHandler(c, false, false, false, principal) commonHandler(c, false, false, false, principal)
} }
...@@ -205,7 +205,6 @@ func RestrictedHandler(c *gin.Context) { ...@@ -205,7 +205,6 @@ func RestrictedHandler(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, extractError.Error()) c.JSON(http.StatusExpectationFailed, extractError.Error())
return return
} }
// pricipalObj.ClientIP = c.Request.RemoteAddr
commonHandler(c, true, false, false, pricipalObj) commonHandler(c, true, false, false, pricipalObj)
} }
...@@ -217,15 +216,13 @@ func RoleBasedHandler(c *gin.Context) { ...@@ -217,15 +216,13 @@ func RoleBasedHandler(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, extractError.Error()) c.JSON(http.StatusExpectationFailed, extractError.Error())
return return
} }
// pricipalObj.ClientIP = c.Request.RemoteAddr
commonHandler(c, true, true, false, pricipalObj) commonHandler(c, true, true, false, pricipalObj)
} }
// HeavyOpenHandler for /o // HeavyOpenHandler for /o
func HeavyOpenHandler(c *gin.Context) { func HeavyOpenHandler(c *gin.Context) {
principal := servicebuildermdl.Principal{} principal := servicebuildermdl.Principal{}
principal.ClientIP = getClientIP(c)
// principal.ClientIP = c.Request.RemoteAddr
commonHandler(c, false, false, true, principal) commonHandler(c, false, false, true, principal)
} }
...@@ -237,7 +234,6 @@ func HeavyRestrictedHandler(c *gin.Context) { ...@@ -237,7 +234,6 @@ func HeavyRestrictedHandler(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, extractError.Error()) c.JSON(http.StatusExpectationFailed, extractError.Error())
return return
} }
// pricipalObj.ClientIP = c.Request.RemoteAddr
commonHandler(c, true, false, true, pricipalObj) commonHandler(c, true, false, true, pricipalObj)
} }
...@@ -249,7 +245,6 @@ func HeavyRoleBasedHandler(c *gin.Context) { ...@@ -249,7 +245,6 @@ func HeavyRoleBasedHandler(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, extractError.Error()) c.JSON(http.StatusExpectationFailed, extractError.Error())
return return
} }
// pricipalObj.ClientIP = c.Request.RemoteAddr
commonHandler(c, true, true, true, pricipalObj) commonHandler(c, true, true, true, pricipalObj)
} }
...@@ -286,7 +281,6 @@ func extractPricipalObject(c *gin.Context) (servicebuildermdl.Principal, error) ...@@ -286,7 +281,6 @@ func extractPricipalObject(c *gin.Context) (servicebuildermdl.Principal, error)
} }
claim, decodeError := jwtmdl.DecodeToken(c.Request) claim, decodeError := jwtmdl.DecodeToken(c.Request)
if errormdl.CheckErr(decodeError) != nil { if errormdl.CheckErr(decodeError) != nil {
// loggermdl.LogError(decodeError)
return principal, errormdl.CheckErr(decodeError) return principal, errormdl.CheckErr(decodeError)
} }
...@@ -324,7 +318,19 @@ func getClientIP(c *gin.Context) string { ...@@ -324,7 +318,19 @@ func getClientIP(c *gin.Context) string {
clientIP = c.Request.Header.Get("X-Forwarded-For") clientIP = c.Request.Header.Get("X-Forwarded-For")
} }
if clientIP == "" { if clientIP == "" {
clientIP = c.Request.RemoteAddr clientIP, _, splitHostPortError := net.SplitHostPort(c.Request.RemoteAddr)
if splitHostPortError == nil && isCorrectIP(clientIP) {
return clientIP
}
return ""
}
if isCorrectIP(clientIP) {
return clientIP
} }
return clientIP return ""
}
// isCorrectIP - return true if ip string is valid textual representation of an IP address, else returns false
func isCorrectIP(ip string) bool {
return net.ParseIP(ip) != nil
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment