| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 | package logicimport (	"errors"	"fmt"	"net/http"	"strings"	"github.com/gravitl/netmaker/logic"	"github.com/gravitl/netmaker/models"	"github.com/gravitl/netmaker/servercfg")// constants for accounts api hostsconst (	// accountsHostDevelopment is the accounts api host for development environment	accountsHostDevelopment = "https://api.dev.accounts.netmaker.io"	// accountsHostStaging is the accounts api host for staging environment	accountsHostStaging = "https://api.staging.accounts.netmaker.io"	// accountsHostProduction is the accounts api host for production environment	accountsHostProduction = "https://api.accounts.netmaker.io")// constants for accounts UI hostsconst (	// accountsUIHostDevelopment is the accounts UI host for development environment	accountsUIHostDevelopment = "https://account.dev.netmaker.io"	// accountsUIHostStaging is the accounts UI host for staging environment	accountsUIHostStaging = "https://account.staging.netmaker.io"	// accountsUIHostProduction is the accounts UI host for production environment	accountsUIHostProduction = "https://account.netmaker.io")func NetworkPermissionsCheck(username string, r *http.Request) error {	// at this point global checks should be completed	user, err := logic.GetUser(username)	if err != nil {		return err	}	userRole, err := logic.GetRole(user.PlatformRoleID)	if err != nil {		return errors.New("access denied")	}	if userRole.FullAccess {		return nil	}	if userRole.ID == models.Auditor {		if r.Method == http.MethodGet {			return nil		} else {			return errors.New("access denied")		}	}	// get info from header to determine the target rsrc	targetRsrc := r.Header.Get("TARGET_RSRC")	targetRsrcID := r.Header.Get("TARGET_RSRC_ID")	netID := r.Header.Get("NET_ID")	if targetRsrc == "" {		return errors.New("target rsrc is missing")	}	if r.Header.Get("RAC") == "true" && r.Method == http.MethodGet {		return nil	}	if netID == "" {		return errors.New("network id is missing")	}	if r.Method == "" {		r.Method = http.MethodGet	}	if targetRsrc == models.MetricRsrc.String() {		return nil	}	// check if user has scope for target resource	// TODO - differentitate between global scope and network scope apis	// check for global network role	if netRoles, ok := user.NetworkRoles[models.AllNetworks]; ok {		for netRoleID := range netRoles {			err = checkNetworkAccessPermissions(netRoleID, username, r.Method, targetRsrc, targetRsrcID, netID)			if err == nil {				return nil			}		}	}	netRoles := user.NetworkRoles[models.NetworkID(netID)]	for netRoleID := range netRoles {		err = checkNetworkAccessPermissions(netRoleID, username, r.Method, targetRsrc, targetRsrcID, netID)		if err == nil {			return nil		}	}	for groupID := range user.UserGroups {		userG, err := GetUserGroup(groupID)		if err == nil {			if netRoles, ok := userG.NetworkRoles[models.AllNetworks]; ok {				for netRoleID := range netRoles {					err = checkNetworkAccessPermissions(netRoleID, username, r.Method, targetRsrc, targetRsrcID, netID)					if err == nil {						return nil					}				}			}			netRoles := userG.NetworkRoles[models.NetworkID(netID)]			for netRoleID := range netRoles {				err = checkNetworkAccessPermissions(netRoleID, username, r.Method, targetRsrc, targetRsrcID, netID)				if err == nil {					return nil				}			}		}	}	return errors.New("access denied")}func checkNetworkAccessPermissions(netRoleID models.UserRoleID, username, reqScope, targetRsrc, targetRsrcID, netID string) error {	networkPermissionScope, err := logic.GetRole(netRoleID)	if err != nil {		return err	}	if networkPermissionScope.FullAccess {		return nil	}	rsrcPermissionScope, ok := networkPermissionScope.NetworkLevelAccess[models.RsrcType(targetRsrc)]	if !ok {		return errors.New("access denied")	}	if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[logic.GetAllRsrcIDForRsrc(models.RsrcType(targetRsrc))]; ok {		// handle extclient apis here		if models.RsrcType(targetRsrc) == models.ExtClientsRsrc && allRsrcsTypePermissionScope.SelfOnly && targetRsrcID != "" {			extclient, err := logic.GetExtClient(targetRsrcID, netID)			if err != nil {				return err			}			if !logic.IsUserAllowedAccessToExtClient(username, extclient) {				return errors.New("access denied")			}		}		err = checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, reqScope)		if err == nil {			return nil		}	}	if targetRsrcID == "" {		return errors.New("target rsrc id is empty")	}	if scope, ok := rsrcPermissionScope[models.RsrcID(targetRsrcID)]; ok {		err = checkPermissionScopeWithReqMethod(scope, reqScope)		if err == nil {			return nil		}	}	return errors.New("access denied")}func GlobalPermissionsCheck(username string, r *http.Request) error {	user, err := logic.GetUser(username)	if err != nil {		return err	}	userRole, err := logic.GetRole(user.PlatformRoleID)	if err != nil {		return errors.New("access denied")	}	if userRole.FullAccess {		return nil	}	if userRole.ID == models.Auditor {		if r.Method == http.MethodGet {			return nil		} else {			return errors.New("access denied")		}	}	targetRsrc := r.Header.Get("TARGET_RSRC")	targetRsrcID := r.Header.Get("TARGET_RSRC_ID")	if targetRsrc == "" {		return errors.New("target rsrc is missing")	}	if r.Method == "" {		r.Method = http.MethodGet	}	if targetRsrc == models.MetricRsrc.String() {		return nil	}	if (targetRsrc == models.HostRsrc.String() || targetRsrc == models.NetworkRsrc.String()) && r.Method == http.MethodGet && targetRsrcID == "" {		return nil	}	if targetRsrc == models.UserRsrc.String() && user.PlatformRoleID == models.PlatformUser && r.Method == http.MethodPut &&		strings.Contains(r.URL.Path, "/api/v1/users/add_network_user") || strings.Contains(r.URL.Path, "/api/v1/users/remove_network_user") {		return nil	}	if targetRsrc == models.UserRsrc.String() && username == targetRsrcID && (r.Method != http.MethodDelete) {		return nil	}	rsrcPermissionScope, ok := userRole.GlobalLevelAccess[models.RsrcType(targetRsrc)]	if !ok {		return fmt.Errorf("access denied to %s", targetRsrc)	}	if allRsrcsTypePermissionScope, ok := rsrcPermissionScope[models.RsrcID(fmt.Sprintf("all_%s", targetRsrc))]; ok {		return checkPermissionScopeWithReqMethod(allRsrcsTypePermissionScope, r.Method)	}	if targetRsrcID == "" {		return errors.New("target rsrc id is missing")	}	if scope, ok := rsrcPermissionScope[models.RsrcID(targetRsrcID)]; ok {		return checkPermissionScopeWithReqMethod(scope, r.Method)	}	return errors.New("access denied")}func checkPermissionScopeWithReqMethod(scope models.RsrcPermissionScope, reqmethod string) error {	if reqmethod == http.MethodGet && scope.Read {		return nil	}	if (reqmethod == http.MethodPatch || reqmethod == http.MethodPut) && scope.Update {		return nil	}	if reqmethod == http.MethodDelete && scope.Delete {		return nil	}	if reqmethod == http.MethodPost && scope.Create {		return nil	}	return errors.New("operation not permitted")}func GetAccountsHost() string {	switch servercfg.GetEnvironment() {	case "dev":		return accountsHostDevelopment	case "staging":		return accountsHostStaging	default:		return accountsHostProduction	}}func GetAccountsUIHost() string {	switch servercfg.GetEnvironment() {	case "dev":		return accountsUIHostDevelopment	case "staging":		return accountsUIHostStaging	default:		return accountsUIHostProduction	}}
 |