privilege

package
v0.23.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package privilege outlines the basic privilege system for cockroach.

Index

Constants

This section is empty.

Variables

View Source
var (
	// AllPrivileges is populated during init.
	AllPrivileges         List
	ReadData              = List{SELECT}
	ReadWriteData         = List{SELECT, INSERT, DELETE, UPDATE}
	ReadWriteSequenceData = List{SELECT, UPDATE, USAGE}
	DBPrivileges          = List{ALL, BACKUP, CONNECT, CREATE, DROP, RESTORE, ZONECONFIG}
	TablePrivileges       = List{ALL, BACKUP, CHANGEFEED, CREATE, DROP, SELECT, INSERT, DELETE, UPDATE, ZONECONFIG}
	SchemaPrivileges      = List{ALL, CREATE, USAGE}
	TypePrivileges        = List{ALL, USAGE}
	RoutinePrivileges     = List{ALL, EXECUTE}
	// SequencePrivileges is appended with TablePrivileges as well. This is because
	// before v22.2 we treated Sequences the same as Tables. This is to avoid making
	// certain privileges unavailable after upgrade migration.
	// Note that "CREATE, CHANGEFEED, INSERT, DELETE, ZONECONFIG" are no-op privileges on sequences.
	SequencePrivileges = List{ALL, USAGE, SELECT, UPDATE, CREATE, CHANGEFEED, DROP, INSERT, DELETE, ZONECONFIG}
	GlobalPrivileges   = List{
		ALL, BACKUP, RESTORE, MODIFYCLUSTERSETTING, EXTERNALCONNECTION, VIEWACTIVITY, VIEWACTIVITYREDACTED,
		VIEWCLUSTERSETTING, CANCELQUERY, NOSQLLOGIN, VIEWCLUSTERMETADATA, VIEWDEBUG, EXTERNALIOIMPLICITACCESS, VIEWJOB,
		MODIFYSQLCLUSTERSETTING, REPLICATION, MANAGEVIRTUALCLUSTER, VIEWSYSTEMTABLE, CREATEROLE, CREATELOGIN, CREATEDB, CONTROLJOB,
		REPAIRCLUSTERMETADATA,
	}
	VirtualTablePrivileges       = List{ALL, SELECT}
	ExternalConnectionPrivileges = List{ALL, USAGE, DROP}
)

Predefined sets of privileges.

View Source
var ByDisplayName map[KindDisplayName]Kind

ByDisplayName is a map of display name -> kind value. It is populated by init. All names use upper case.

Note that all internal keys are also added into this map to allow for backward-compatibility.

View Source
var ByInternalKey map[KindInternalKey]Kind

ByInternalKey is a map of internal key -> kind value. It is populated by init. All keys use upper case.

Functions

func KeyToName

func KeyToName(key string) (string, error)

KeyToName converts a privilege key to its name.

func ValidatePrivileges

func ValidatePrivileges(privileges List, objectType ObjectType) error

ValidatePrivileges returns an error if any privilege in privileges cannot be granted on the given objectType.

Types

type Kind

type Kind uint32

Kind defines a privilege. This is output by the parser, and used to generate the privilege bitfields in the PrivilegeDescriptor.

const (
	ALL    Kind = 1
	CREATE Kind = 2
	DROP   Kind = 3

	SELECT                   Kind = 5
	INSERT                   Kind = 6
	DELETE                   Kind = 7
	UPDATE                   Kind = 8
	USAGE                    Kind = 9
	ZONECONFIG               Kind = 10
	CONNECT                  Kind = 11
	RULE                     Kind = 12
	MODIFYCLUSTERSETTING     Kind = 13
	EXTERNALCONNECTION       Kind = 14
	VIEWACTIVITY             Kind = 15
	VIEWACTIVITYREDACTED     Kind = 16
	VIEWCLUSTERSETTING       Kind = 17
	CANCELQUERY              Kind = 18
	NOSQLLOGIN               Kind = 19
	EXECUTE                  Kind = 20
	VIEWCLUSTERMETADATA      Kind = 21
	VIEWDEBUG                Kind = 22
	BACKUP                   Kind = 23
	RESTORE                  Kind = 24
	EXTERNALIOIMPLICITACCESS Kind = 25
	CHANGEFEED               Kind = 26
	VIEWJOB                  Kind = 27
	MODIFYSQLCLUSTERSETTING  Kind = 28
	REPLICATION              Kind = 29
	MANAGEVIRTUALCLUSTER     Kind = 30
	VIEWSYSTEMTABLE          Kind = 31
	CREATEROLE               Kind = 32
	CREATELOGIN              Kind = 33
	CREATEDB                 Kind = 34
	CONTROLJOB               Kind = 35
	REPAIRCLUSTERMETADATA    Kind = 36
)

List of privileges. ALL is specifically encoded so that it will automatically pick up new privileges. Do not change values of privileges. These correspond to the position of the privilege in a bit field and are expected to stay constant.

func (Kind) DisplayName

func (k Kind) DisplayName() KindDisplayName

DisplayName reports the display name for a privilege.

func (Kind) InternalKey

func (k Kind) InternalKey() KindInternalKey

func (Kind) IsSetIn

func (k Kind) IsSetIn(bits uint64) bool

IsSetIn returns true if this privilege kind is set in the supplied bitfield.

func (Kind) Mask

func (k Kind) Mask() uint64

Mask returns the bitmask for a given privilege.

func (Kind) SafeFormat

func (k Kind) SafeFormat(p redact.SafePrinter, _ rune)

SafeFormat implements the redact.SafeFormatter interface.

type KindDisplayName

type KindDisplayName string

KindDisplayName is the string representation of privileges displayed to end users and recognized by the parser. The name can be different from the key (e.g. when we choose a better name).

func (KindDisplayName) SafeValue

func (KindDisplayName) SafeValue()

SafeValue implements the redact.SafeValuer interface.

type KindInternalKey

type KindInternalKey string

KindInternalKey is the value stored in system tables, etc, that represent the privilege internally. It is not visible to end-users.

func (KindInternalKey) SafeValue

func (KindInternalKey) SafeValue()

SafeValue implements the redact.SafeValuer interface.

type List

type List []Kind

List is a list of privileges.

func GetValidPrivilegesForObject

func GetValidPrivilegesForObject(objectType ObjectType) (List, error)

GetValidPrivilegesForObject returns the list of valid privileges for the specified object type.

func ListFromBitField

func ListFromBitField(m uint64, objectType ObjectType) (List, error)

ListFromBitField takes a bitfield of privileges and a ObjectType returns a List. It is ordered in increasing value of privilege.Kind.

func ListFromStrings

func ListFromStrings(strs []string, origin Origin) (List, error)

ListFromStrings takes a list of internal storage keys and attempts to build a list of Kind. Each string is converted to uppercase and is searched for either in ByInternalKey or in ByDisplayName maps, depending on the origin. If an entry is not found, it is either ignored or an error is raised (also depending on the origin).

func (List) Contains

func (pl List) Contains(k Kind) bool

Contains returns true iff the list contains the given privilege kind.

func (List) FormatNames

func (pl List) FormatNames(buf *bytes.Buffer)

FormatNames prints out the list of display names in a buffer. This keeps the existing order and uses ", " as separator.

func (List) Len

func (pl List) Len() int

Len, Swap, and Less implement the Sort interface.

func (List) Less

func (pl List) Less(i, j int) bool

func (List) ListToACL

func (pl List) ListToACL(grantOptions List, objectType ObjectType) (string, error)

ListToACL converts a list of privileges to a list of Postgres ACL items. See: https://www.postgresql.org/docs/13/ddl-priv.html#PRIVILEGE-ABBREVS-TABLE

for privileges and their ACL abbreviations.

func (List) SafeFormat

func (pl List) SafeFormat(s interfaces.SafePrinter, _ rune)

SafeFormat implements the redact.SafeFormatter interface.

func (List) SortedDisplayNames

func (pl List) SortedDisplayNames() []string

SortedDisplayNames returns a list of privilege display names in sorted order.

func (List) SortedKeys

func (pl List) SortedKeys() []string

SortedKeys returns a list of privilege internal keys in sorted order.

func (List) Swap

func (pl List) Swap(i, j int)

func (List) ToBitField

func (pl List) ToBitField() uint64

ToBitField returns the bitfield representation of a list of privileges.

type Object

type Object interface {
	// GetObjectType returns the privilege.ObjectType of the Object.
	GetObjectType() ObjectType
	// GetObjectTypeString returns a human-readable representation of the
	// privilege.ObjectType.
	// NOTE: It may not match the privilege.ObjectType directly because it may
	// be more specific for some object types. For example, for functions and
	// procedures it will return "function" and "procedure", respectively,
	// instead of the more generic term "routine".
	GetObjectTypeString() string
	// GetName returns the name of the object. For example, the name of a
	// table, schema or database.
	GetName() string
}

Object represents an object that can have privileges. The privileges can either live on the descriptor or in the system.privileges table.

type ObjectType

type ObjectType string

ObjectType represents objects that can have privileges.

const (
	// Any represents any object type.
	Any ObjectType = "any"
	// Database represents a database object.
	Database ObjectType = "database"
	// Schema represents a schema object.
	Schema ObjectType = "schema"
	// Table represents a table object.
	Table ObjectType = "table"
	// Type represents a type object.
	Type ObjectType = "type"
	// Sequence represents a sequence object.
	Sequence ObjectType = "sequence"
	// Routine represents a function or procedure object.
	Routine ObjectType = "routine"
	// Global represents global privileges.
	Global ObjectType = "global"
	// VirtualTable represents a virtual table object.
	VirtualTable ObjectType = "virtual_table"
	// ExternalConnection represents an external connection object.
	ExternalConnection ObjectType = "external_connection"
)

func (ObjectType) IsDescriptorBacked

func (o ObjectType) IsDescriptorBacked() bool

IsDescriptorBacked returns whether o is a descriptor backed object. If o is not a descriptor backed object, then privileges are stored to system.privileges.

func (ObjectType) SafeValue

func (k ObjectType) SafeValue()

SafeValue makes ObjectType a redact.SafeValue.

type Origin

type Origin bool

Origin indicates the origin of the privileges being parsed in ListFromStrings.

const (
	// OriginFromUserInput indicates that the privilege name came from user
	// input and should be validated to make sure it refers to a real privilege.
	OriginFromUserInput Origin = false

	// OriginFromSystemTable indicates that the privilege name came from a
	// system table and should be ignored if it does not refer to a real
	// privilege.
	OriginFromSystemTable Origin = true
)

type Privilege

type Privilege struct {
	Kind Kind
	// Each privilege Kind has an optional "grant option" flag associated with
	// it. A role can only grant a privilege on an object to others if it is the
	// owner of the object or if it itself holds that privilege WITH GRANT OPTION
	// on the object. This replaced the CockroachDB-specific GRANT privilege.
	GrantOption bool
}

Privilege represents a privilege parsed from an Access Privilege Inquiry Function's privilege string argument.

func PrivilegesFromBitFields

func PrivilegesFromBitFields(
	kindBits, grantOptionBits uint64, objectType ObjectType,
) ([]Privilege, error)

PrivilegesFromBitFields takes a bitfield of privilege kinds, a bitfield of grant options, and an ObjectType returns a List. It is ordered in increasing value of privilege.Kind.

func (Privilege) SafeFormat

func (k Privilege) SafeFormat(s redact.SafePrinter, _ rune)

SafeFormat implements the redact.SafeFormatter interface.

type TargetObjectType

type TargetObjectType uint32

TargetObjectType represents the type of object that is having its default privileges altered.

const (
	Tables    TargetObjectType = 1
	Sequences TargetObjectType = 2
	Types     TargetObjectType = 3
	Schemas   TargetObjectType = 4
	Routines  TargetObjectType = 5
)

The numbers are explicitly assigned since the DefaultPrivilegesPerObject map defined in the DefaultPrivilegesPerRole proto requires the key value for the object type to remain unchanged.

func GetTargetObjectTypes

func GetTargetObjectTypes() []TargetObjectType

GetTargetObjectTypes returns a slice of all the AlterDefaultPrivilegesTargetObjects.

func (TargetObjectType) String

func (t TargetObjectType) String() string

String makes TargetObjectType a fmt.Stringer.

func (TargetObjectType) ToObjectType

func (t TargetObjectType) ToObjectType() ObjectType

ToObjectType returns the privilege.ObjectType corresponding to the TargetObjectType.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL