Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Don't update identity if it hasn't changed. Fix change attribution fo…
Browse files Browse the repository at this point in the history
…r edge client updates. Fixes #1610. Fixes #1611
  • Loading branch information
plorenz committed Sep 13, 2023
1 parent 02b39f5 commit 74006ee
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
37 changes: 24 additions & 13 deletions controller/handler_edge_ctrl/common_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,25 +341,36 @@ func (self *baseTunnelRequestContext) getCreateSessionResponse() *edge_ctrl_pb.C

func (self *baseTunnelRequestContext) updateIdentityInfo(envInfo *edge_ctrl_pb.EnvInfo, sdkInfo *edge_ctrl_pb.SdkInfo) {
if self.err == nil {
updateIdentity := false
if envInfo != nil {
self.identity.EnvInfo = &model.EnvInfo{}
self.identity.EnvInfo.Arch = envInfo.Arch
self.identity.EnvInfo.Os = envInfo.Os
self.identity.EnvInfo.OsRelease = envInfo.OsRelease
self.identity.EnvInfo.OsVersion = envInfo.OsVersion
newEnvInfo := &model.EnvInfo{
Arch: envInfo.Arch,
Os: envInfo.Os,
OsRelease: envInfo.OsRelease,
OsVersion: envInfo.OsVersion,
}
if !self.identity.EnvInfo.Equals(newEnvInfo) {
self.identity.EnvInfo = newEnvInfo
updateIdentity = true
}
}

if sdkInfo != nil {
self.identity.SdkInfo = &model.SdkInfo{}
self.identity.SdkInfo.AppId = sdkInfo.AppId
self.identity.SdkInfo.AppVersion = sdkInfo.AppVersion
self.identity.SdkInfo.Branch = sdkInfo.Branch
self.identity.SdkInfo.Revision = sdkInfo.Revision
self.identity.SdkInfo.Type = sdkInfo.Type
self.identity.SdkInfo.Version = sdkInfo.Version
newSdkInfo := &model.SdkInfo{
AppId: sdkInfo.AppId,
AppVersion: sdkInfo.AppVersion,
Branch: sdkInfo.Branch,
Revision: sdkInfo.Revision,
Type: sdkInfo.Type,
Version: sdkInfo.Version,
}
if !self.identity.SdkInfo.Equals(newSdkInfo) {
self.identity.SdkInfo = newSdkInfo
updateIdentity = true
}
}

if envInfo != nil || sdkInfo != nil {
if updateIdentity {
self.err = internalError(self.handler.getAppEnv().GetManagers().Identity.PatchInfo(self.identity, self.newTunnelChangeContext()))
}
}
Expand Down
25 changes: 15 additions & 10 deletions controller/internal/routes/authenticate_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
clientApiAuthentication "github.com/openziti/edge-api/rest_client_api_server/operations/authentication"
managementApiAuthentication "github.com/openziti/edge-api/rest_management_api_server/operations/authentication"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/fabric/controller/apierror"
"github.com/openziti/edge/controller/env"
"github.com/openziti/edge/controller/internal/permissions"
"github.com/openziti/edge/controller/model"
"github.com/openziti/edge/controller/response"
"github.com/openziti/fabric/controller/apierror"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/metrics"
"net"
Expand Down Expand Up @@ -79,8 +79,6 @@ func (ro *AuthRouter) authHandler(ae *env.AppEnv, rc *response.RequestContext, h

authResult, err := ae.Managers.Authenticator.Authorize(authContext)

changeCtx := rc.NewChangeContext()

if err != nil {
rc.RespondWithError(err)
return
Expand All @@ -94,6 +92,8 @@ func (ro *AuthRouter) authHandler(ae *env.AppEnv, rc *response.RequestContext, h
rc.AuthPolicy = authResult.AuthPolicy()

identity := authResult.Identity()
rc.Identity = identity

if identity.EnvInfo == nil {
identity.EnvInfo = &model.EnvInfo{}
}
Expand All @@ -102,25 +102,30 @@ func (ro *AuthRouter) authHandler(ae *env.AppEnv, rc *response.RequestContext, h
identity.SdkInfo = &model.SdkInfo{}
}

changeCtx := rc.NewChangeContext()

if dataMap := authContext.GetData(); dataMap != nil {
shouldUpdate := false

if envInfoInterface := dataMap["envInfo"]; envInfoInterface != nil {
if envInfo := envInfoInterface.(map[string]interface{}); envInfo != nil {
if err := mapstructure.Decode(envInfo, &identity.EnvInfo); err != nil {
if envInfoMap := envInfoInterface.(map[string]interface{}); envInfoMap != nil {
var envInfo *model.EnvInfo
if err := mapstructure.Decode(envInfoMap, &envInfo); err != nil {
logger.WithError(err).Error("error processing env info")
} else {
} else if !identity.EnvInfo.Equals(envInfo) {
identity.EnvInfo = envInfo
shouldUpdate = true
}

}
}

if sdkInfoInterface := dataMap["sdkInfo"]; sdkInfoInterface != nil {
if sdkInfo := sdkInfoInterface.(map[string]interface{}); sdkInfo != nil {
if err := mapstructure.Decode(sdkInfo, &identity.SdkInfo); err != nil {
if sdkInfoMap := sdkInfoInterface.(map[string]interface{}); sdkInfoMap != nil {
var sdkInfo *model.SdkInfo
if err := mapstructure.Decode(sdkInfoMap, &sdkInfo); err != nil {
logger.WithError(err).Error("error processing sdk info")
} else {
} else if !identity.SdkInfo.Equals(sdkInfo) {
identity.SdkInfo = sdkInfo
shouldUpdate = true
}
}
Expand Down
28 changes: 28 additions & 0 deletions controller/model/identity_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ type EnvInfo struct {
OsVersion string
}

func (self *EnvInfo) Equals(other *EnvInfo) bool {
if self == nil {
return other == nil
}
if other == nil {
return false
}
return self.Arch == other.Arch &&
self.Os == other.Os &&
self.OsRelease == other.OsRelease &&
self.OsVersion == other.OsVersion
}

type SdkInfo struct {
AppId string
AppVersion string
Expand All @@ -42,6 +55,21 @@ type SdkInfo struct {
Version string
}

func (self *SdkInfo) Equals(other *SdkInfo) bool {
if self == nil {
return other == nil
}
if other == nil {
return false
}
return self.AppId == other.AppId &&
self.AppVersion == other.AppVersion &&
self.Branch == other.Branch &&
self.Revision == other.Revision &&
self.Type == other.Type &&
self.Version == other.Version
}

type Identity struct {
models.BaseEntity
Name string
Expand Down

0 comments on commit 74006ee

Please sign in to comment.