Skip to content

Commit

Permalink
fix regression bug in admin check
Browse files Browse the repository at this point in the history
  • Loading branch information
artpar committed Apr 24, 2024
1 parent c35cb7a commit 676b28b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 77 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,21 @@ jobs:


- uses: actions/upload-artifact@v1
name: Upload windows 64 artifact
name: Upload windows amd64 artifact
with:
name: daptin-windows-amd64.exe
path: build/github.com/daptin/daptin-windows-amd64.exe
if: github.head_ref == '' && github.repository == 'daptin/daptin'


- uses: actions/upload-artifact@v1
name: Upload windows arm64 artifact
with:
name: daptin-windows-amd64.exe
path: build/github.com/daptin/daptin-windows-arm64.exe
if: github.head_ref == '' && github.repository == 'daptin/daptin'


- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
Expand Down
2 changes: 1 addition & 1 deletion server/config_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func CreateConfigHandler(initConfig *resource.CmsConfig, cruds map[string]*resou

defer transaction.Commit()

if !userAccountTableCrud.IsAdmin(sessionUser.UserReferenceId, transaction) {
if !resource.IsAdminWithTransaction(sessionUser.UserReferenceId, transaction) {
c.AbortWithError(403, fmt.Errorf("unauthorized"))
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/resource/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (e ActionRow) MarshalBinary() (data []byte, err error) {
}

// UnmarshalBinary decodes the data into the struct using manual binary decoding
func (e ActionRow) UnmarshalBinary(data []byte) error {
func (e *ActionRow) UnmarshalBinary(data []byte) error {
buffer := bytes.NewBuffer(data)

// Decode Name
Expand Down
2 changes: 1 addition & 1 deletion server/resource/certificate_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (cm *CertificateManager) GetTLSConfig(hostname string, createIfNotFound boo
ClientAuth: tls.NoClientCert,
}

adminList := cm.cruds["certificate"].GetAdminReferenceId(transaction)
adminList := GetAdminReferenceIdWithTransaction(transaction)

var adminUserReferenceId daptinid.DaptinReferenceId
adminId := int64(1)
Expand Down
15 changes: 8 additions & 7 deletions server/resource/dbmethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ func (dbResource *DbResource) GetActionByName(typeName string, actionName string
if err == nil && value != nil {

var cachedActionRow ActionRow
value.Scan(&cachedActionRow)
err = value.Scan(&cachedActionRow)

err = json.Unmarshal([]byte(cachedActionRow.ActionSchema), &action)
CheckErr(err, "failed to unmarshal infields")
CheckErr(err, "failed to unmarshal ActionSchema 76")

if err == nil {
action.Name = cachedActionRow.Name
Expand All @@ -90,6 +89,7 @@ func (dbResource *DbResource) GetActionByName(typeName string, actionName string
goqu.I("w.table_name").As("ontype"),
goqu.I("a.label").As("label"),
goqu.I("action_schema").As("action_schema"),
goqu.I("a.instance_optional").As("instance_optional"),
goqu.I("a.reference_id").As("referenceid"),
).Prepared(true).From(goqu.T("action").As("a")).
Join(
Expand Down Expand Up @@ -124,17 +124,18 @@ func (dbResource *DbResource) GetActionByName(typeName string, actionName string
}

err = json.Unmarshal([]byte(actionRow.ActionSchema), &action)
CheckErr(err, "failed to unmarshal infields")
CheckErr(err, "failed to unmarshal ActionSchema 127")

action.Name = actionRow.Name
action.Label = actionRow.Name
action.ReferenceId = actionRow.ReferenceId
action.OnType = actionRow.OnType
action.InstanceOptional = actionRow.InstanceOptional

if OlricCache != nil {

err = OlricCache.Put(context.Background(), cacheKey, actionRow, olric.EX(1*time.Minute), olric.NX())
CheckErr(err, "Failed to set action in olric cache")
//CheckErr(err, "Failed to set action in olric cache")
}

return action, nil
Expand Down Expand Up @@ -196,7 +197,7 @@ func (dbResource *DbResource) GetActionsByType(typeName string, transaction *sql
continue
}
err = json.Unmarshal([]byte(a.ActionSchema), &act)
CheckErr(err, "failed to unmarshal infields")
CheckErr(err, "failed to unmarshal ActionSchema")

act.Name = a.Name
act.Label = a.Label
Expand Down Expand Up @@ -995,7 +996,7 @@ func GetObjectGroupsByObjectIdWithTransaction(objectType string, objectId int64,
// No one can become admin once we have an adminstrator
func (dbResource *DbResource) CanBecomeAdmin(transaction *sqlx.Tx) bool {

adminRefId := dbResource.GetAdminReferenceId(transaction)
adminRefId := GetAdminReferenceIdWithTransaction(transaction)
if adminRefId == nil || len(adminRefId) == 0 {
return true
}
Expand Down
66 changes: 1 addition & 65 deletions server/resource/dbresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,32 +344,6 @@ func (a AdminMapType) UnmarshalBinary(data []byte) error {
return nil
}

func (dbResource *DbResource) GetAdminReferenceId(transaction *sqlx.Tx) AdminMapType {
var err error
adminMap := make(AdminMapType)
if OlricCache != nil {
cacheValue, err := OlricCache.Get(context.Background(), "administrator_reference_id")
if err == nil && cacheValue != nil {
var amt AdminMapType
err = cacheValue.Scan(&amt)
if err == nil {
return amt
}
}
}
userRefId := dbResource.GetUserMembersByGroupName("administrators", transaction)
for _, id := range userRefId {
userUuid, _ := uuid.FromBytes(id[:])
adminMap[userUuid] = true
}

if OlricCache != nil && userRefId != nil {
err = OlricCache.Put(context.Background(), "administrator_reference_id", adminMap, olric.EX(60*time.Minute), olric.NX())
CheckErr(err, "Failed to cache admin reference ids")
}
return adminMap
}

func GetAdminReferenceIdWithTransaction(transaction *sqlx.Tx) map[uuid.UUID]bool {
adminMap := make(AdminMapType)
if OlricCache != nil {
Expand All @@ -393,44 +367,6 @@ func GetAdminReferenceIdWithTransaction(transaction *sqlx.Tx) map[uuid.UUID]bool
return adminMap
}

func (dbResource *DbResource) IsAdmin(userReferenceId daptinid.DaptinReferenceId, transaction *sqlx.Tx) bool {
start := time.Now()
userUUid, err := uuid.FromBytes(userReferenceId[:])
key := "admin." + string(userReferenceId[:])
if OlricCache != nil {
value, err := OlricCache.Get(context.Background(), key)
if err == nil && value != nil {
val, err := value.Bool()
if err != nil && val {
duration := time.Since(start)
log.Tracef("[TIMING]IsAdmin Cached[true]: %v", duration)
return true
} else {
duration := time.Since(start)
log.Tracef("[TIMING] IsAdmin Cached[false]: %v", duration)
return false
}
}
}
admins := dbResource.GetAdminReferenceId(transaction)
_, ok := admins[userUUid]
if ok {
if OlricCache != nil {
err := OlricCache.Put(context.Background(), key, true, olric.EX(5*time.Minute), olric.NX())
CheckErr(err, "[285] Failed to set admin id value in olric cache")
}
duration := time.Since(start)
log.Tracef("[TIMING] IsAdmin NotCached[true]: %v", duration)
return true
}
err = OlricCache.Put(context.Background(), key, false, olric.EX(5*time.Minute), olric.NX())
CheckErr(err, "[291] Failed to set admin id value in olric cache")

duration := time.Since(start)
log.Tracef("[TIMING] IsAdmin NotCached[true]: %v", duration)
return false

}
func IsAdminWithTransaction(userReferenceId daptinid.DaptinReferenceId, transaction *sqlx.Tx) bool {
userUUid, _ := uuid.FromBytes(userReferenceId[:])
key := "admin." + string(userReferenceId[:])
Expand All @@ -439,7 +375,7 @@ func IsAdminWithTransaction(userReferenceId daptinid.DaptinReferenceId, transact
//fmt.Println("IsAdminWithTransaction [" + key + "]")
value, err := OlricCache.Get(context.Background(), key)
if err == nil && value != nil {
if val, err := value.Bool(); val && err != nil {
if val, err := value.Bool(); val && err == nil {
return true
} else {
return false
Expand Down
2 changes: 1 addition & 1 deletion server/resource/middleware_eventgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (e EventMessage) MarshalBinary() (data []byte, err error) {
}

// UnmarshalBinary decodes the data into the struct using manual binary decoding
func (e EventMessage) UnmarshalBinary(data []byte) error {
func (e *EventMessage) UnmarshalBinary(data []byte) error {
buffer := bytes.NewBuffer(data)

// Decode MessageSource
Expand Down

0 comments on commit 676b28b

Please sign in to comment.