2019-01-08 00:37:16 +00:00
|
|
|
package influxdb
|
2018-07-30 21:46:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-04 17:41:24 +00:00
|
|
|
"encoding/json"
|
2018-07-30 21:46:30 +00:00
|
|
|
"errors"
|
2021-03-30 18:10:02 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform"
|
2018-07-30 21:46:30 +00:00
|
|
|
)
|
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
var (
|
|
|
|
// ErrInvalidUserType notes that the provided UserType is invalid
|
|
|
|
ErrInvalidUserType = errors.New("unknown user type")
|
2019-03-04 17:41:24 +00:00
|
|
|
// ErrInvalidMappingType notes that the provided MappingType is invalid
|
|
|
|
ErrInvalidMappingType = errors.New("unknown mapping type")
|
2018-12-28 23:02:19 +00:00
|
|
|
// ErrUserIDRequired notes that the ID was not provided
|
|
|
|
ErrUserIDRequired = errors.New("user id is required")
|
|
|
|
// ErrResourceIDRequired notes that the provided ID was not provided
|
|
|
|
ErrResourceIDRequired = errors.New("resource id is required")
|
|
|
|
)
|
|
|
|
|
|
|
|
// UserType can either be owner or member.
|
2018-07-30 21:46:30 +00:00
|
|
|
type UserType string
|
|
|
|
|
|
|
|
const (
|
2018-12-28 23:02:19 +00:00
|
|
|
// Owner can read and write to a resource
|
|
|
|
Owner UserType = "owner" // 1
|
|
|
|
// Member can read from a resource.
|
|
|
|
Member UserType = "member" // 2
|
2018-07-30 21:46:30 +00:00
|
|
|
)
|
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
// Valid checks if the UserType is a member of the UserType enum
|
|
|
|
func (ut UserType) Valid() (err error) {
|
|
|
|
switch ut {
|
|
|
|
case Owner: // 1
|
|
|
|
case Member: // 2
|
|
|
|
default:
|
|
|
|
err = ErrInvalidUserType
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:41:24 +00:00
|
|
|
type MappingType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
UserMappingType = 0
|
|
|
|
OrgMappingType = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func (mt MappingType) Valid() error {
|
|
|
|
switch mt {
|
|
|
|
case UserMappingType, OrgMappingType:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrInvalidMappingType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt MappingType) String() string {
|
|
|
|
switch mt {
|
|
|
|
case UserMappingType:
|
|
|
|
return "user"
|
|
|
|
case OrgMappingType:
|
|
|
|
return "org"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt MappingType) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(mt.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *MappingType) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch s {
|
|
|
|
case "user":
|
|
|
|
*mt = UserMappingType
|
|
|
|
return nil
|
|
|
|
case "org":
|
|
|
|
*mt = OrgMappingType
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrInvalidMappingType
|
|
|
|
}
|
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
// UserResourceMappingService maps the relationships between users and resources.
|
2018-07-30 21:46:30 +00:00
|
|
|
type UserResourceMappingService interface {
|
2018-09-24 17:17:54 +00:00
|
|
|
// FindUserResourceMappings returns a list of UserResourceMappings that match filter and the total count of matching mappings.
|
|
|
|
FindUserResourceMappings(ctx context.Context, filter UserResourceMappingFilter, opt ...FindOptions) ([]*UserResourceMapping, int, error)
|
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
// CreateUserResourceMapping creates a user resource mapping.
|
2018-07-30 21:46:30 +00:00
|
|
|
CreateUserResourceMapping(ctx context.Context, m *UserResourceMapping) error
|
2018-09-24 17:17:54 +00:00
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
// DeleteUserResourceMapping deletes a user resource mapping.
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteUserResourceMapping(ctx context.Context, resourceID, userID platform.ID) error
|
2018-07-30 21:46:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
// UserResourceMapping represents a mapping of a resource to its user.
|
2018-07-30 21:46:30 +00:00
|
|
|
type UserResourceMapping struct {
|
2021-03-30 18:10:02 +00:00
|
|
|
UserID platform.ID `json:"userID"`
|
2019-01-15 16:09:58 +00:00
|
|
|
UserType UserType `json:"userType"`
|
2019-03-04 17:41:24 +00:00
|
|
|
MappingType MappingType `json:"mappingType"`
|
2019-01-15 16:09:58 +00:00
|
|
|
ResourceType ResourceType `json:"resourceType"`
|
2021-03-30 18:10:02 +00:00
|
|
|
ResourceID platform.ID `json:"resourceID"`
|
2018-07-30 21:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate reports any validation errors for the mapping.
|
|
|
|
func (m UserResourceMapping) Validate() error {
|
2018-09-12 11:25:17 +00:00
|
|
|
if !m.ResourceID.Valid() {
|
2018-12-28 23:02:19 +00:00
|
|
|
return ErrResourceIDRequired
|
2018-07-30 21:46:30 +00:00
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
2018-09-12 11:25:17 +00:00
|
|
|
if !m.UserID.Valid() {
|
2018-12-28 23:02:19 +00:00
|
|
|
return ErrUserIDRequired
|
2018-07-30 21:46:30 +00:00
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
|
|
|
if err := m.UserType.Valid(); err != nil {
|
|
|
|
return err
|
2018-07-30 21:46:30 +00:00
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
2019-03-04 17:41:24 +00:00
|
|
|
if err := m.MappingType.Valid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:09:58 +00:00
|
|
|
if err := m.ResourceType.Valid(); err != nil {
|
2018-12-28 23:02:19 +00:00
|
|
|
return err
|
2018-10-04 21:37:37 +00:00
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
2018-07-30 21:46:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
// UserResourceMappingFilter represents a set of filters that restrict the returned results.
|
2018-07-30 21:46:30 +00:00
|
|
|
type UserResourceMappingFilter struct {
|
2021-03-30 18:10:02 +00:00
|
|
|
ResourceID platform.ID
|
2019-01-15 16:09:58 +00:00
|
|
|
ResourceType ResourceType
|
2021-03-30 18:10:02 +00:00
|
|
|
UserID platform.ID
|
2019-01-15 16:09:58 +00:00
|
|
|
UserType UserType
|
2018-07-30 21:46:30 +00:00
|
|
|
}
|
2018-11-20 18:56:58 +00:00
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
func (m *UserResourceMapping) ownerPerms() ([]Permission, error) {
|
2019-01-15 16:09:58 +00:00
|
|
|
if m.ResourceType == OrgsResourceType {
|
2020-07-13 20:47:58 +00:00
|
|
|
return OwnerPermissions(m.ResourceID), nil
|
2018-11-20 18:56:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:42:06 +00:00
|
|
|
if m.ResourceType == InstanceResourceType {
|
|
|
|
return []Permission{
|
|
|
|
{Action: ReadAction, Resource: Resource{Type: InstanceResourceType}},
|
|
|
|
{Action: WriteAction, Resource: Resource{Type: InstanceResourceType}},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-13 20:47:58 +00:00
|
|
|
ps := []Permission{
|
|
|
|
// TODO: Uncomment these once the URM system is no longer being used for find lookups for:
|
|
|
|
// Telegraf
|
|
|
|
// DashBoard
|
|
|
|
// notification rule
|
|
|
|
// notification endpoint
|
|
|
|
// Permission{
|
|
|
|
// Action: ReadAction,
|
|
|
|
// Resource: Resource{
|
|
|
|
// Type: m.ResourceType,
|
|
|
|
// ID: &m.ResourceID,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Permission{
|
|
|
|
// Action: WriteAction,
|
|
|
|
// Resource: Resource{
|
|
|
|
// Type: m.ResourceType,
|
|
|
|
// ID: &m.ResourceID,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
return ps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UserResourceMapping) memberPerms() ([]Permission, error) {
|
2019-01-15 16:09:58 +00:00
|
|
|
if m.ResourceType == OrgsResourceType {
|
2020-07-13 20:47:58 +00:00
|
|
|
return MemberPermissions(m.ResourceID), nil
|
2018-11-20 18:56:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 01:50:37 +00:00
|
|
|
if m.ResourceType == BucketsResourceType {
|
2020-07-13 20:47:58 +00:00
|
|
|
return []Permission{MemberBucketPermission(m.ResourceID)}, nil
|
2020-03-26 01:50:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 20:47:58 +00:00
|
|
|
ps := []Permission{
|
|
|
|
// TODO: Uncomment these once the URM system is no longer being used for find lookups for:
|
|
|
|
// Telegraf
|
|
|
|
// DashBoard
|
|
|
|
// notification rule
|
|
|
|
// notification endpoint
|
|
|
|
// Permission{
|
|
|
|
// Action: ReadAction,
|
|
|
|
// Resource: Resource{
|
|
|
|
// Type: m.ResourceType,
|
|
|
|
// ID: &m.ResourceID,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
return ps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToPermissions converts a user resource mapping into a set of permissions.
|
|
|
|
func (m *UserResourceMapping) ToPermissions() ([]Permission, error) {
|
|
|
|
switch m.UserType {
|
|
|
|
case Owner:
|
|
|
|
return m.ownerPerms()
|
|
|
|
case Member:
|
|
|
|
return m.memberPerms()
|
|
|
|
default:
|
|
|
|
return nil, ErrInvalidUserType
|
|
|
|
}
|
2018-11-20 18:56:58 +00:00
|
|
|
}
|