Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
406 changes: 284 additions & 122 deletions adminapi/auth/permission_service.go

Large diffs are not rendered by default.

677 changes: 677 additions & 0 deletions adminapi/auth/permission_service_test.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions adminapi/canary/canary_settings_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
)

func PutCanarySettingsHandler(w http.ResponseWriter, r *http.Request) {
if !auth.HasWritePermissionForTool(r) {
xhttp.WriteAdminErrorResponse(w, http.StatusForbidden, "No write permission: tools")
if _, err := auth.CanWrite(r, auth.TOOL_ENTITY); err != nil {
xhttp.AdminError(w, err)
return
}

Expand All @@ -48,7 +48,7 @@ func PutCanarySettingsHandler(w http.ResponseWriter, r *http.Request) {
return
}

tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
respEntity := SetCanarySetting(tenantId, &canarySettings)
if respEntity.Error != nil {
xhttp.WriteAdminErrorResponse(w, respEntity.Status, respEntity.Error.Error())
Expand All @@ -58,12 +58,12 @@ func PutCanarySettingsHandler(w http.ResponseWriter, r *http.Request) {
}

func GetCanarySettingsHandler(w http.ResponseWriter, r *http.Request) {
if !auth.HasReadPermissionForTool(r) {
xhttp.WriteAdminErrorResponse(w, http.StatusUnauthorized, "")
if _, err := auth.CanRead(r, auth.TOOL_ENTITY); err != nil {
xhttp.AdminError(w, err)
return
}

tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
canarySetting, err := GetCanarySettings(tenantId)
if err != nil {
xhttp.WriteAdminErrorResponse(w, http.StatusInternalServerError, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion adminapi/canary/canary_settings_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ func TestGetCanarySettingsHandler(t *testing.T) {
common.SatOn = true
req = httptest.NewRequest(http.MethodGet, testURL, nil)
GetCanarySettingsHandler(w, req)
assert.Equal(t, http.StatusUnauthorized, w.Status())
assert.Equal(t, http.StatusForbidden, w.Status())
}
40 changes: 28 additions & 12 deletions adminapi/change/change_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func GetProfileChangesHandler(w http.ResponseWriter, r *http.Request) {

searchContext := make(map[string]string)
searchContext[xwcommon.APPLICATION_TYPE] = applicationType
changes := FindByContextForChanges(searchContext)
tenantId := xhttp.GetTenantId(r)
changes := FindByContextForChanges(tenantId, searchContext)
sort.Slice(changes, func(i, j int) bool {
return changes[j].Updated < changes[i].Updated
})
Expand Down Expand Up @@ -88,13 +89,20 @@ func ApproveChangeHandler(w http.ResponseWriter, r *http.Request) {
return
}

tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
headerMap := createHeadersMap(tenantId, applicationType)
xwhttp.WriteXconfResponseWithHeaders(w, headerMap, http.StatusOK, nil)
}

func GetApprovedHandler(w http.ResponseWriter, r *http.Request) {
approvedChange, err := GetApprovedAll(r)
applicationType, err := auth.CanRead(r, auth.CHANGE_ENTITY)
if err != nil {
xhttp.AdminError(w, err)
return
}

tenantId := xhttp.GetTenantId(r)
approvedChange, err := GetApprovedAll(tenantId, applicationType)
if err != nil {
xwhttp.WriteXconfResponse(w, http.StatusBadRequest, []byte(err.Error()))
return
Expand All @@ -113,7 +121,7 @@ func RevertChangeHandler(w http.ResponseWriter, r *http.Request) {
return
}

tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)

approveId, found := mux.Vars(r)[xcommon.APPROVE_ID]
if !found || approveId == "" {
Expand All @@ -137,7 +145,7 @@ func CancelChangeHandler(w http.ResponseWriter, r *http.Request) {
return
}

tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)

changeId, found := mux.Vars(r)[xcommon.CHANGE_ID]
if !found || changeId == "" {
Expand Down Expand Up @@ -216,8 +224,8 @@ func GetGroupedChangesHandler(w http.ResponseWriter, r *http.Request) {
xhttp.AdminError(w, err)
return
}
tenantId := xwhttp.GetTenantId(r, "")

tenantId := xhttp.GetTenantId(r)
changeList := xchange.GetChangeList(tenantId)
sort.Slice(changeList, func(i, j int) bool {
return changeList[i].Updated < changeList[j].Updated
Expand Down Expand Up @@ -264,7 +272,7 @@ func GetGroupedApprovedChangesHandler(w http.ResponseWriter, r *http.Request) {
return
}

tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
changeList := xchange.GetApprovedChangeList(tenantId)
sort.Slice(changeList, func(i, j int) bool {
return changeList[j].Updated < changeList[i].Updated
Expand Down Expand Up @@ -308,7 +316,13 @@ func ApprovedChangesGeneratePage(list []*xwchange.ApprovedChange, page int, page
}

func GetChangedEntityIdsHandler(w http.ResponseWriter, r *http.Request) {
entityIds := GetChangedEntityIds()
_, err := auth.CanRead(r, auth.CHANGE_ENTITY)
if err != nil {
xhttp.AdminError(w, err)
return
}
tenantId := xhttp.GetTenantId(r)
entityIds := GetChangedEntityIds(tenantId)
response, err := util.JSONMarshal(entityIds)
if err != nil {
log.Error(fmt.Sprintf("json.Marshal entityIds error: %v", err))
Expand All @@ -323,7 +337,7 @@ func ApproveChangesHandler(w http.ResponseWriter, r *http.Request) {
return
}

tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)

xw, ok := w.(*xwhttp.XResponseWriter)
if !ok {
Expand Down Expand Up @@ -432,7 +446,8 @@ func GetApprovedFilteredHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Error(fmt.Sprintf("json.Marshal ApprovedChangesMap error: %v", err))
}
changeList := FindByContextForChanges(searchContext)
tenantId := xhttp.GetTenantId(r)
changeList := FindByContextForChanges(tenantId, searchContext)
headerMap := createHeadersWithEntitySize(len(changeList), len(approvedChangeList))
xwhttp.WriteXconfResponseWithHeaders(w, headerMap, http.StatusOK, response)
}
Expand Down Expand Up @@ -479,7 +494,8 @@ func GetChangesFilteredHandler(w http.ResponseWriter, r *http.Request) {
}
searchContext[xwcommon.APPLICATION_TYPE] = applicationType

changeList := FindByContextForChanges(searchContext)
tenantId := xhttp.GetTenantId(r)
changeList := FindByContextForChanges(tenantId, searchContext)
sort.Slice(changeList, func(i, j int) bool {
return changeList[j].Updated < changeList[i].Updated
})
Expand Down
42 changes: 17 additions & 25 deletions adminapi/change/change_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import (
xcommon "github.com/rdkcentral/xconfadmin/common"

"github.com/rdkcentral/xconfadmin/adminapi/auth"
xhttp "github.com/rdkcentral/xconfadmin/http"
xshared "github.com/rdkcentral/xconfadmin/shared"
xchange "github.com/rdkcentral/xconfadmin/shared/change"
xutil "github.com/rdkcentral/xconfadmin/util"
xwcommon "github.com/rdkcentral/xconfwebconfig/common"
"github.com/rdkcentral/xconfwebconfig/db"
xwhttp "github.com/rdkcentral/xconfwebconfig/http"
"github.com/rdkcentral/xconfwebconfig/shared"
xwshared "github.com/rdkcentral/xconfwebconfig/shared"
xwchange "github.com/rdkcentral/xconfwebconfig/shared/change"
Expand All @@ -42,16 +41,11 @@ import (
log "github.com/sirupsen/logrus"
)

func GetApprovedAll(r *http.Request) ([]*xwchange.ApprovedChange, error) {
tenantId := xwhttp.GetTenantId(r, "")
func GetApprovedAll(tenantId string, applicationType string) ([]*xwchange.ApprovedChange, error) {
approvedChangesAll := xchange.GetApprovedChangeList(tenantId)
approvedChanges := []*xwchange.ApprovedChange{}
application, err := auth.CanRead(r, auth.CHANGE_ENTITY)
if err != nil {
return nil, err
}
for _, approvedChange := range approvedChangesAll {
if xshared.ApplicationTypeEquals(application, approvedChange.ApplicationType) || xshared.ApplicationTypeEquals(application, xwshared.ALL) {
if xshared.ApplicationTypeEquals(applicationType, approvedChange.ApplicationType) || xshared.ApplicationTypeEquals(applicationType, xwshared.ALL) {
approvedChanges = append(approvedChanges, approvedChange)
}
}
Expand Down Expand Up @@ -87,7 +81,7 @@ func beforeSavingChange(r *http.Request, change *xwchange.Change) error {
return err
}

tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
return validateAllChanges(tenantId, change)
}

Expand Down Expand Up @@ -164,7 +158,7 @@ func CreateApprovedChange(r *http.Request, change *xwchange.Change) (*xwchange.A
return nil, err
}

tenantId := db.GetDefaultTenantId()
tenantId := xhttp.GetTenantId(r)
approvedChange := xwchange.ApprovedChange(*change)
xchange.SetOneApprovedChange(tenantId, &approvedChange)
jsonBytes, _ := json.Marshal(change)
Expand All @@ -176,7 +170,7 @@ func Revert(r *http.Request, approvedId string) error {
if approvedId == "" {
return xwcommon.NewRemoteErrorAS(http.StatusBadRequest, "Id is blank")
}
tenantId := db.GetDefaultTenantId()
tenantId := xhttp.GetTenantId(r)
approvedChange := xchange.GetOneApprovedChange(tenantId, approvedId)
if approvedChange == nil {
return xwcommon.NewRemoteErrorAS(http.StatusNotFound, "ApprovedChange with "+approvedId+" id does not exist")
Expand All @@ -193,13 +187,13 @@ func Revert(r *http.Request, approvedId string) error {

func revertDelete(r *http.Request, id string, approvedChange *xwchange.ApprovedChange) *xwchange.ApprovedChange {
CreatePermanentTelemetryProfile(r, approvedChange.OldEntity)
tenantId := db.GetDefaultTenantId()
tenantId := xhttp.GetTenantId(r)
xchange.DeleteOneApprovedChange(tenantId, id)
return approvedChange
}

func revertCreateOrUpdateChange(r *http.Request, changeId string, entityId string, approvedChange *xwchange.ApprovedChange) *xwchange.ApprovedChange {
tenantId := db.GetDefaultTenantId()
tenantId := xhttp.GetTenantId(r)
entityToRevert := logupload.GetOnePermanentTelemetryProfile(tenantId, entityId)
// in Java, equalPendingEntities(PermanentTelemetryProfile oldEntity, PermanentTelemetryProfile newEntity) always returns true
//if (equalPendingEntities(approvedChange.getNewEntity(), entityToRevert)) { is being ignored
Expand All @@ -213,7 +207,7 @@ func revertCreateOrUpdateChange(r *http.Request, changeId string, entityId strin
}

func CancelChange(r *http.Request, changeId string) error {
tenantId := db.GetDefaultTenantId()
tenantId := xhttp.GetTenantId(r)
canceledChange, err := Delete(tenantId, changeId)
if err != nil {
return err
Expand Down Expand Up @@ -257,9 +251,8 @@ func groupApprovedChange(change *xwchange.ApprovedChange, groupedChanges map[str
}
}

func GetChangedEntityIds() *[]string {
func GetChangedEntityIds(tenantId string) *[]string {
ids := []string{}
tenantId := db.GetDefaultTenantId()
changeList := xchange.GetChangeList(tenantId)
for _, change := range changeList {
ids = append(ids, change.EntityID)
Expand Down Expand Up @@ -297,7 +290,7 @@ func GetChangesByEntityId(tenantId, entityId string) []*xwchange.Change {
}

func Approve(r *http.Request, id string) (*xwchange.ApprovedChange, error) {
tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
change := xchange.GetOneChange(tenantId, id)
if change == nil {
return nil, xwcommon.NewRemoteErrorAS(http.StatusNotFound, "Change with "+id+" id does not exist")
Expand Down Expand Up @@ -340,7 +333,7 @@ func getChangeIds(changes []*xwchange.Change) []string {
}

func ApproveChanges(r *http.Request, changeIds *[]string) (map[string]string, error) {
tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
changesToApprove, err := GetChangesByEntityIds(tenantId, changeIds)
if err != nil {
return nil, err
Expand Down Expand Up @@ -380,7 +373,7 @@ func ApproveChanges(r *http.Request, changeIds *[]string) (map[string]string, er
}

func SaveToApprovedAndCleanUpChange(r *http.Request, change *xwchange.Change) (*xwchange.ApprovedChange, error) {
tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
userName := auth.GetUserNameOrUnknown(r)
change.ApprovedUser = userName
approvedChange, err := CreateApprovedChange(r, change)
Expand All @@ -393,7 +386,7 @@ func SaveToApprovedAndCleanUpChange(r *http.Request, change *xwchange.Change) (*
}

func CancelApprovedChangesByEntityId(r *http.Request, entityIdsToByCancelChanges []string, changeIdsToBeExcluded []string) error {
tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
for _, entityId := range entityIdsToByCancelChanges {
changes := GetChangesByEntityId(tenantId, entityId)
for _, changeByEntityId := range changes {
Expand All @@ -417,7 +410,7 @@ func logAndCollectChangeException(change *xwchange.Change, err error, errorMessa
}

func RevertChanges(r *http.Request, changeIds *[]string) (map[string]string, error) {
tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
changesToRevert := []*xwchange.ApprovedChange{}
for _, changeId := range *changeIds {
approvedChange := xchange.GetOneApprovedChange(tenantId, changeId)
Expand All @@ -440,8 +433,7 @@ func RevertChanges(r *http.Request, changeIds *[]string) (map[string]string, err
return errorMessages, nil
}

func FindByContextForChanges(searchContext map[string]string) []*xwchange.Change {
tenantId := db.GetDefaultTenantId()
func FindByContextForChanges(tenantId string, searchContext map[string]string) []*xwchange.Change {
changes := xchange.GetChangeList(tenantId)
changesFound := []*xwchange.Change{}
for _, change := range changes {
Expand Down Expand Up @@ -479,7 +471,7 @@ func FindByContextForChanges(searchContext map[string]string) []*xwchange.Change
}

func FindByContextForApprovedChanges(r *http.Request, searchContext map[string]string) []*xwchange.ApprovedChange {
tenantId := xwhttp.GetTenantId(r, "")
tenantId := xhttp.GetTenantId(r)
approvedChanges := xchange.GetApprovedChangeList(tenantId)
changesFound := []*xwchange.ApprovedChange{}
for _, change := range approvedChanges {
Expand Down
16 changes: 7 additions & 9 deletions adminapi/change/change_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ func TestFindByContextForChanges(t *testing.T) {
t.Fatalf("setup: %v", err)
}
// filter by author substring
res := FindByContextForChanges(map[string]string{"author": "ali"})
res := FindByContextForChanges(db.GetDefaultTenantId(), map[string]string{"author": "ali"})
if len(res) != 1 || res[0].Author != "alice" {
t.Fatalf("expected filter by author matched alice only")
}
// filter by profile name substring
res = FindByContextForChanges(map[string]string{"entity": "beta"})
res = FindByContextForChanges(db.GetDefaultTenantId(), map[string]string{"entity": "beta"})
if len(res) != 1 || res[0].NewEntity.Name != "telemetry-beta" {
t.Fatalf("expected beta profile filter")
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestGetChangedEntityIds(t *testing.T) {
if err := xchange.CreateOneChange(db.GetDefaultTenantId(), c); err != nil {
t.Fatalf("setup: %v", err)
}
ids := GetChangedEntityIds()
ids := GetChangedEntityIds(db.GetDefaultTenantId())
if ids == nil || len(*ids) == 0 {
t.Fatalf("expected at least one changed entity id")
}
Expand Down Expand Up @@ -359,8 +359,7 @@ func TestGetApprovedAll_EmptyResult(t *testing.T) {
xchange.DeleteOneApprovedChange(db.GetDefaultTenantId(), ac.ID)
}

r := httptest.NewRequest(http.MethodGet, "/?applicationType=stb", nil)
result, err := GetApprovedAll(r)
result, err := GetApprovedAll(db.GetDefaultTenantId(), shared.STB)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -385,8 +384,7 @@ func TestGetApprovedAll_WithResults(t *testing.T) {
t.Fatalf("failed to create approved change: %v", err)
}

r := httptest.NewRequest(http.MethodGet, "/?applicationType=stb", nil)
result, err := GetApprovedAll(r)
result, err := GetApprovedAll(db.GetDefaultTenantId(), shared.STB)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -397,7 +395,7 @@ func TestGetApprovedAll_WithResults(t *testing.T) {

func TestFindByContextForChanges_EmptyContext(t *testing.T) {
context := make(map[string]string)
result := FindByContextForChanges(context)
result := FindByContextForChanges(db.GetDefaultTenantId(), context)
if result == nil {
t.Fatalf("expected non-nil result")
}
Expand All @@ -418,7 +416,7 @@ func TestFindByContextForChanges_WithApplicationType(t *testing.T) {
}

context := map[string]string{"applicationType": shared.STB}
result := FindByContextForChanges(context)
result := FindByContextForChanges(db.GetDefaultTenantId(), context)
if len(result) == 0 {
t.Fatalf("expected results")
}
Expand Down
Loading