Commit 2938c4dc authored by Vikram Ingawale's avatar Vikram Ingawale
Browse files

CoreSLS : Get client and server ip address

parent 01695c31
Branches
Tags
2 merge requests!210Staging mepdeployment05072020,!207CoreSLS : Get client and server ip address
Showing with 87 additions and 19 deletions
...@@ -219,7 +219,7 @@ func RestrictedHandler(c *routing.Context) error { ...@@ -219,7 +219,7 @@ func RestrictedHandler(c *routing.Context) error {
c.SetStatusCode(412) c.SetStatusCode(412)
return err return err
} }
pricipalObj.ClientIP = c.RemoteIP().String() // pricipalObj.ClientIP = c.RemoteIP().String()
commonHandler(c, true, false, false, pricipalObj) commonHandler(c, true, false, false, pricipalObj)
return nil return nil
} }
...@@ -234,7 +234,7 @@ func RoleBasedHandler(c *routing.Context) error { ...@@ -234,7 +234,7 @@ func RoleBasedHandler(c *routing.Context) error {
c.SetStatusCode(412) c.SetStatusCode(412)
return err return err
} }
pricipalObj.ClientIP = c.RemoteIP().String() // pricipalObj.ClientIP = c.RemoteIP().String()
commonHandler(c, true, true, false, pricipalObj) commonHandler(c, true, true, false, pricipalObj)
return nil return nil
} }
...@@ -259,7 +259,7 @@ func HeavyRestrictedHandler(c *routing.Context) error { ...@@ -259,7 +259,7 @@ func HeavyRestrictedHandler(c *routing.Context) error {
c.SetStatusCode(412) c.SetStatusCode(412)
return err return err
} }
pricipalObj.ClientIP = c.RemoteIP().String() // pricipalObj.ClientIP = c.RemoteIP().String()
commonHandler(c, true, false, true, pricipalObj) commonHandler(c, true, false, true, pricipalObj)
return nil return nil
} }
...@@ -274,7 +274,7 @@ func HeavyRoleBasedHandler(c *routing.Context) error { ...@@ -274,7 +274,7 @@ func HeavyRoleBasedHandler(c *routing.Context) error {
c.SetStatusCode(412) c.SetStatusCode(412)
return err return err
} }
pricipalObj.ClientIP = c.RemoteIP().String() // pricipalObj.ClientIP = c.RemoteIP().String()
commonHandler(c, true, true, true, pricipalObj) commonHandler(c, true, true, true, pricipalObj)
return nil return nil
} }
...@@ -323,11 +323,6 @@ func extractPricipalObject(c *routing.Context) (servicebuildermdl.Principal, err ...@@ -323,11 +323,6 @@ func extractPricipalObject(c *routing.Context) (servicebuildermdl.Principal, err
return principal, errormdl.CheckErr(grperr) return principal, errormdl.CheckErr(grperr)
} }
userID, _ := claim["userId"].(string) userID, _ := claim["userId"].(string)
// if !ok {
// loggermdl.LogError("Unable to parse UserID from JWT Token")
// return principal, errormdl.Wrap("Unable to parse UserID from JWT Token")
// }
if len(userID) < 2 { if len(userID) < 2 {
loggermdl.LogError("UserID length is less than 2") loggermdl.LogError("UserID length is less than 2")
return principal, errormdl.Wrap("UserID length is less than 2") return principal, errormdl.Wrap("UserID length is less than 2")
...@@ -345,5 +340,19 @@ func extractPricipalObject(c *routing.Context) (servicebuildermdl.Principal, err ...@@ -345,5 +340,19 @@ func extractPricipalObject(c *routing.Context) (servicebuildermdl.Principal, err
principal.Groups = groups principal.Groups = groups
principal.UserID = userID principal.UserID = userID
principal.Token = string(c.Request.Header.Peek("Authorization")) principal.Token = string(c.Request.Header.Peek("Authorization"))
// set client ip
principal.ClientIP = getClientIP(c)
return principal, nil return principal, nil
} }
// getClientIP - returns respected header value from request header
func getClientIP(c *routing.Context) string {
clientIP := string(c.Request.Header.Peek("X-Real-Ip"))
if clientIP == "" {
clientIP = string(c.Request.Header.Peek("X-Forwarded-For"))
}
if clientIP == "" {
clientIP = c.RemoteIP().String()
}
return clientIP
}
...@@ -205,7 +205,7 @@ func RestrictedHandler(c *gin.Context) { ...@@ -205,7 +205,7 @@ func RestrictedHandler(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, extractError.Error()) c.JSON(http.StatusExpectationFailed, extractError.Error())
return return
} }
pricipalObj.ClientIP = c.Request.RemoteAddr // pricipalObj.ClientIP = c.Request.RemoteAddr
commonHandler(c, true, false, false, pricipalObj) commonHandler(c, true, false, false, pricipalObj)
} }
...@@ -217,7 +217,7 @@ func RoleBasedHandler(c *gin.Context) { ...@@ -217,7 +217,7 @@ func RoleBasedHandler(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, extractError.Error()) c.JSON(http.StatusExpectationFailed, extractError.Error())
return return
} }
pricipalObj.ClientIP = c.Request.RemoteAddr // pricipalObj.ClientIP = c.Request.RemoteAddr
commonHandler(c, true, true, false, pricipalObj) commonHandler(c, true, true, false, pricipalObj)
} }
...@@ -237,7 +237,7 @@ func HeavyRestrictedHandler(c *gin.Context) { ...@@ -237,7 +237,7 @@ func HeavyRestrictedHandler(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, extractError.Error()) c.JSON(http.StatusExpectationFailed, extractError.Error())
return return
} }
pricipalObj.ClientIP = c.Request.RemoteAddr // pricipalObj.ClientIP = c.Request.RemoteAddr
commonHandler(c, true, false, true, pricipalObj) commonHandler(c, true, false, true, pricipalObj)
} }
...@@ -249,7 +249,7 @@ func HeavyRoleBasedHandler(c *gin.Context) { ...@@ -249,7 +249,7 @@ func HeavyRoleBasedHandler(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, extractError.Error()) c.JSON(http.StatusExpectationFailed, extractError.Error())
return return
} }
pricipalObj.ClientIP = c.Request.RemoteAddr // pricipalObj.ClientIP = c.Request.RemoteAddr
commonHandler(c, true, true, true, pricipalObj) commonHandler(c, true, true, true, pricipalObj)
} }
...@@ -296,11 +296,6 @@ func extractPricipalObject(c *gin.Context) (servicebuildermdl.Principal, error) ...@@ -296,11 +296,6 @@ func extractPricipalObject(c *gin.Context) (servicebuildermdl.Principal, error)
return principal, errormdl.CheckErr(grperr) return principal, errormdl.CheckErr(grperr)
} }
userID, _ := claim["userId"].(string) userID, _ := claim["userId"].(string)
// if !ok {
// loggermdl.LogError("Unable to parse UserID from JWT Token")
// return principal, errormdl.Wrap("Unable to parse UserID from JWT Token")
// }
if len(userID) < 2 { if len(userID) < 2 {
loggermdl.LogError("UserID length is less than 2") loggermdl.LogError("UserID length is less than 2")
return principal, errormdl.Wrap("UserID length is less than 2") return principal, errormdl.Wrap("UserID length is less than 2")
...@@ -318,5 +313,18 @@ func extractPricipalObject(c *gin.Context) (servicebuildermdl.Principal, error) ...@@ -318,5 +313,18 @@ func extractPricipalObject(c *gin.Context) (servicebuildermdl.Principal, error)
principal.Groups = groups principal.Groups = groups
principal.UserID = userID principal.UserID = userID
principal.Token = c.Request.Header.Get("Authorization") principal.Token = c.Request.Header.Get("Authorization")
principal.ClientIP = getClientIP(c)
return principal, nil return principal, nil
} }
// getClientIP - returns respected header value from request header
func getClientIP(c *gin.Context) string {
clientIP := c.Request.Header.Get("X-Real-Ip")
if clientIP == "" {
clientIP = c.Request.Header.Get("X-Forwarded-For")
}
if clientIP == "" {
clientIP = c.Request.RemoteAddr
}
return clientIP
}
...@@ -6,6 +6,7 @@ package servicebuildermdl ...@@ -6,6 +6,7 @@ package servicebuildermdl
import ( import (
"database/sql" "database/sql"
"net"
"strings" "strings"
"sync" "sync"
"time" "time"
...@@ -43,6 +44,45 @@ var once sync.Once ...@@ -43,6 +44,45 @@ var once sync.Once
var ruleCache map[string]conditions.Expr var ruleCache map[string]conditions.Expr
var mutex = &sync.Mutex{} var mutex = &sync.Mutex{}
// get server ip address
var (
serverIP = func() string {
ifaces, err := net.Interfaces()
if err != nil {
return ""
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return ""
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
if ip = ip.To4(); ip == nil {
continue // not an ipv4 address
}
return ip.String()
}
}
return ""
}()
)
func init() { func init() {
ruleCache = make(map[string]conditions.Expr) ruleCache = make(map[string]conditions.Expr)
globalConfig = make(map[string]GlobalConfigModel) globalConfig = make(map[string]GlobalConfigModel)
...@@ -144,6 +184,7 @@ func (ab *AbstractBusinessLogicHolder) GetDataResultset(key string) (*gjson.Resu ...@@ -144,6 +184,7 @@ func (ab *AbstractBusinessLogicHolder) GetDataResultset(key string) (*gjson.Resu
return value, true return value, true
} }
// GetMQLRequestData - returns MQLRequestData
func (ab *AbstractBusinessLogicHolder) GetMQLRequestData() (*gjson.Result, bool) { func (ab *AbstractBusinessLogicHolder) GetMQLRequestData() (*gjson.Result, bool) {
//check in map //check in map
temp, found := ab.localServiceData[constantmdl.MQLRequestData] temp, found := ab.localServiceData[constantmdl.MQLRequestData]
...@@ -254,7 +295,7 @@ func (ab *AbstractBusinessLogicHolder) SetCustomData(key string, data interface{ ...@@ -254,7 +295,7 @@ func (ab *AbstractBusinessLogicHolder) SetCustomData(key string, data interface{
ab.localServiceData[key] = data ab.localServiceData[key] = data
} }
// SetMQLToken will set token in header // SetErrorData will set error
func (ab *AbstractBusinessLogicHolder) SetErrorData(data interface{}) { func (ab *AbstractBusinessLogicHolder) SetErrorData(data interface{}) {
ab.ServiceError = data ab.ServiceError = data
} }
...@@ -270,6 +311,16 @@ func (ab *AbstractBusinessLogicHolder) SetFinalData(data interface{}) { ...@@ -270,6 +311,16 @@ func (ab *AbstractBusinessLogicHolder) SetFinalData(data interface{}) {
ab.localServiceData["finaldata"] = data ab.localServiceData["finaldata"] = data
} }
// GetClientIP will returns client ip address
func (ab *AbstractBusinessLogicHolder) GetClientIP() string {
return ab.pricipalObject.ClientIP
}
// GetServerIP will returns server ip address
func (ab *AbstractBusinessLogicHolder) GetServerIP() string {
return serverIP
}
// SetErrorCode - SetErrorCode in service context // SetErrorCode - SetErrorCode in service context
func (ab *AbstractBusinessLogicHolder) SetErrorCode(code int) { func (ab *AbstractBusinessLogicHolder) SetErrorCode(code int) {
ab.GlobalErrorCode = code ab.GlobalErrorCode = code
......
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