mssqldb

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxPageSize = 100
	MinPageSize = 10
)
View Source
const (
	UserType         = "user"
	DatabaseUserType = "database-user"
)
View Source
const DatabaseRoleType = "database-role"
View Source
const DatabaseType = "database"
View Source
const GroupType = "group"
View Source
const ServerRoleType = "server-role"
View Source
const ServerType = "server"

Variables

View Source
var DatabasePermissions = map[string]string{
	"AADS": "Alter Any Database Event Session",
	"AAMK": "Alter Any Mask",
	"AEDS": "Alter Any External Data Source",
	"AEFF": "Alter Any External File Format",
	"AL":   "Alter",
	"ALAK": "Alter Any Asymmetric Key",
	"ALAR": "Alter Any Application Role",
	"ALAS": "Alter Any Assembly",
	"ALCF": "Alter Any Certificate",
	"ALDS": "Alter Any Dataspace",
	"ALED": "Alter Any Database Event Notification",
	"ALFT": "Alter Any Fulltext Catalog",
	"ALMT": "Alter Any Message Type",
	"ALRL": "Alter Any Role",
	"ALRT": "Alter Any Route",
	"ALSB": "Alter Any Remote Service Binding",
	"ALSC": "Alter Any Contract",
	"ALSK": "Alter Any Symmetric Key",
	"ALSM": "Alter Any Schema",
	"ALSV": "Alter Any Service",
	"ALTG": "Alter Any Database DDL Trigger",
	"ALUS": "Alter Any User",
	"AUTH": "Authenticate",
	"BADB": "Backup Database",
	"BALO": "Backup Log",
	"CL":   "Control",
	"CO":   "Connect",
	"CORP": "Connect Replication",
	"CP":   "Checkpoint",
	"CRAG": "Create Aggregate",
	"CRAK": "Create Asymmetric Key",
	"CRAS": "Create Certificate",
	"CRDB": "Create Fatabase",
	"CRDF": "Create Default",
	"CRED": "Create Database DDL Event Notification",
	"CRFN": "Create Function",
	"CRFT": "Create Fulltext Catalog",
	"CRMT": "Create Message Type",
	"CRPR": "Create Procedure",
	"CRQU": "Create Queue",
	"CRRL": "Create Role",
	"CRRT": "Create Route",
	"CRRU": "Create Rule",
	"CRSB": "Create Remote Service Binding",
	"CRSC": "Create contract",
	"CRSK": "Create symmetric key",
	"CRSM": "Create Schema",
	"CRSN": "Create Synonym",
	"CRSO": "Create Sequence",
	"CRSV": "Create Service",
	"CRTB": "Create Table",
	"CRTY": "Create Type",
	"CRVW": "Create View",
	"CRXS": "Create XML Schema Collection",
	"DL":   "Delete",
	"DABO": "Administer Database Bulk Operations",
	"EAES": "Execute Any External Script",
	"EX":   "Execute",
	"IN":   "Insert",
	"RC":   "Receive Object",
	"RF":   "References",
	"SL":   "Select",
	"SPLN": "Showplan",
	"SUQN": "Subscribe Query Notifications",
	"TO":   "Take Ownership",
	"UP":   "Update",
	"VW":   "View Definition",
	"VWCK": "View Any Column Encryption Key Definition",
	"VWCM": "View Any Column Master Key Definition",
	"VWCT": "View Change Tracking",
	"VWDS": "View Database State Database",
}
View Source
var ErrNoServerPrincipal = errors.New("no server principal found")

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, dsn string, skipUnavailableDatabases bool) (*Client, error)

func (*Client) AddUserToDatabaseRole added in v0.0.9

func (c *Client) AddUserToDatabaseRole(ctx context.Context, role string, db string, user string) error

func (*Client) AddUserToServerRole added in v0.0.9

func (c *Client) AddUserToServerRole(ctx context.Context, role string, userID string) error

func (*Client) CreateDatabaseUserForPrincipal added in v0.0.9

func (c *Client) CreateDatabaseUserForPrincipal(ctx context.Context, db, principal string) error

func (*Client) CreateLogin added in v0.0.9

func (c *Client) CreateLogin(ctx context.Context, loginType LoginType, username, password string) error

CreateLogin creates a SQL Server login with the specified authentication type. For Windows authentication (loginType=WINDOWS):

  • If domain is provided, it will create the login in the format [DOMAIN\Username]
  • otherwise it will use just [Username]

For SQL authentication (loginType=SQL):

  • It requires a password
  • Domain is ignored

For Azure AD authentication (loginType=AZURE_AD):

  • It creates from EXTERNAL PROVIDER
  • Username should be the full Azure AD username/email

For Entra ID authentication (loginType=ENTRA_ID):

  • It creates from EXTERNAL PROVIDER
  • Username should be the full Entra ID username/email

func (*Client) DeleteUserFromServer added in v0.0.12

func (c *Client) DeleteUserFromServer(ctx context.Context, userName string) error

func (*Client) GetDatabase

func (c *Client) GetDatabase(ctx context.Context, id int64) (*DbModel, error)

func (*Client) GetDatabaseRole added in v0.0.9

func (c *Client) GetDatabaseRole(ctx context.Context, dbName string, id string) (*RoleModel, error)

func (*Client) GetServer

func (c *Client) GetServer(ctx context.Context) (*ServerModel, error)

func (*Client) GetServerPrincipalForDatabasePrincipal

func (c *Client) GetServerPrincipalForDatabasePrincipal(ctx context.Context, dbName string, principalID int64) (*UserModel, error)

GetServerPrincipalForDatabasePrincipal returns the server principal for a given database user. Returns ErrNoServerPrincipal if no server principal is found.

func (*Client) GetServerRole added in v0.0.9

func (c *Client) GetServerRole(ctx context.Context, id string) (*RoleModel, error)

func (*Client) GetUserFromDb added in v0.0.9

func (c *Client) GetUserFromDb(ctx context.Context, db, principalId string) (*UserDBModel, error)

GetUserFromDb find db user from Server principal.

func (*Client) GetUserPrincipal added in v0.0.9

func (c *Client) GetUserPrincipal(ctx context.Context, userId string) (*UserModel, error)

func (*Client) GetUserPrincipalByName added in v0.0.9

func (c *Client) GetUserPrincipalByName(ctx context.Context, name string) (*UserModel, error)

func (*Client) GrantPermissionOnDatabase added in v0.0.9

func (c *Client) GrantPermissionOnDatabase(ctx context.Context, permission, db, user string) error

func (*Client) ListDatabasePermissions

func (c *Client) ListDatabasePermissions(ctx context.Context, dbName string, pager *Pager) ([]*PermissionModel, string, error)

func (*Client) ListDatabaseRolePrincipals

func (c *Client) ListDatabaseRolePrincipals(ctx context.Context, dbName string, databaseRoleID string, pager *Pager) ([]*RolePrincipalModel, string, error)

func (*Client) ListDatabaseRoles

func (c *Client) ListDatabaseRoles(ctx context.Context, dbName string, pager *Pager) ([]*RoleModel, string, error)

func (*Client) ListDatabaseUserPrincipals

func (c *Client) ListDatabaseUserPrincipals(ctx context.Context, dbName string, pager *Pager) ([]*UserModel, string, error)

func (*Client) ListDatabases

func (c *Client) ListDatabases(ctx context.Context, pager *Pager) ([]*DbModel, string, error)

func (*Client) ListGroupPrincipals

func (c *Client) ListGroupPrincipals(ctx context.Context, pager *Pager) ([]*GroupModel, string, error)

func (*Client) ListServerPermissions

func (c *Client) ListServerPermissions(ctx context.Context, pager *Pager) ([]*PermissionModel, string, error)

func (*Client) ListServerRolePrincipals

func (c *Client) ListServerRolePrincipals(ctx context.Context, serverRoleID string, pager *Pager) ([]*RolePrincipalModel, string, error)

func (*Client) ListServerRoles

func (c *Client) ListServerRoles(ctx context.Context, pager *Pager) ([]*RoleModel, string, error)

func (*Client) ListServerUserPrincipals

func (c *Client) ListServerUserPrincipals(ctx context.Context, pager *Pager) ([]*UserModel, string, error)

func (*Client) RevokePermissionOnDatabase added in v0.0.9

func (c *Client) RevokePermissionOnDatabase(ctx context.Context, permission, db, user string) error

func (*Client) RevokeUserToDatabaseRole added in v0.0.9

func (c *Client) RevokeUserToDatabaseRole(ctx context.Context, role string, db string, user string) error

func (*Client) RevokeUserToServerRole added in v0.0.9

func (c *Client) RevokeUserToServerRole(ctx context.Context, role string, user string) error

type DbModel

type DbModel struct {
	ID        int64  `db:"database_id"`
	Name      string `db:"name"`
	StateDesc string `db:"state_desc"`
}

type GroupModel

type GroupModel struct {
	ID         string `db:"principal_id"`
	SecurityID string `db:"sid"`
	Name       string `db:"name"`
	Type       string `db:"type_desc"`
}

type LoginType added in v0.0.9

type LoginType string

LoginType represents the SQL Server login type.

const (
	// LoginTypeWindows represents Windows authentication.
	LoginTypeWindows LoginType = "WINDOWS"
	// LoginTypeSQL represents SQL Server authentication.
	LoginTypeSQL LoginType = "SQL"
	// LoginTypeAzureAD represents Azure AD authentication.
	LoginTypeAzureAD LoginType = "AZURE_AD"
	// LoginTypeEntraID represents Azure Entra ID authentication.
	LoginTypeEntraID LoginType = "ENTRA_ID"
)

type Pager

type Pager struct {
	Token string
	Size  int
}

func (*Pager) Parse

func (p *Pager) Parse() (int, int, error)

Parse returns the offset and page size.

type PermissionModel

type PermissionModel struct {
	PrincipalName string `db:"principal_name"`
	PrincipalID   int64  `db:"principal_id"`
	PrincipalType string `db:"principal_type"`
	State         string `db:"state"`
	Permissions   string `db:"perms"`
}

type RoleModel

type RoleModel struct {
	ID         int64  `db:"principal_id"`
	SecurityID string `db:"sid"`
	Name       string `db:"name"`
	Type       string `db:"type_desc"`
}

type RolePrincipalModel

type RolePrincipalModel struct {
	ID   int64  `db:"principal_id"`
	Name string `db:"name"`
	Type string `db:"type"`
}

type ServerModel

type ServerModel struct {
	Name string `db:"ServerName"`
}

type UserDBModel added in v0.0.9

type UserDBModel struct {
	ID                  string `db:"principal_id"`
	DatabasePrincipalId string `db:"database_principal_id"`
	Sid                 string `db:"sid"`
	Name                string `db:"name"`
	Type                string `db:"type_desc"`
	CreateDate          string `db:"create_date"`
	ModifyDate          string `db:"modify_date"`
	OwningPrincipalId   string `db:"owning_principal_id"`
}

type UserModel

type UserModel struct {
	ID         string `db:"principal_id"`
	SecurityID string `db:"sid"`
	Name       string `db:"name"`
	Type       string `db:"type_desc"`
	IsDisabled bool   `db:"is_disabled"`
}

Jump to

Keyboard shortcuts

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