db

package
v0.37.4 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	ID                  string                 `json:"Id"`             // AWS Account ID
	AccountStatus       AccountStatus          `json:"AccountStatus"`  // Status of the AWS Account
	LastModifiedOn      int64                  `json:"LastModifiedOn"` // Last Modified Epoch Timestamp
	CreatedOn           int64                  `json:"CreatedOn"`
	AdminRoleArn        string                 `json:"AdminRoleArn"`        // Assumed by the master account, to manage this user account
	PrincipalRoleArn    string                 `json:"PrincipalRoleArn"`    // Assumed by principal users
	PrincipalPolicyHash string                 `json:"PrincipalPolicyHash"` // The the hash of the policy version deployed
	Metadata            map[string]interface{} `json:"Metadata"`            // Any org specific metadata pertaining to the account
}

Account is a type corresponding to a Account table record

type AccountLeasedError

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

AccountLeasedError is returned when a consumer attempts to delete an account that is currently at status Leased

func (*AccountLeasedError) Error

func (e *AccountLeasedError) Error() string

type AccountNotFoundError

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

AccountNotFoundError is returned when an account is not found.

func (*AccountNotFoundError) Error

func (e *AccountNotFoundError) Error() string

type AccountStatus

type AccountStatus string

AccountStatus is an account status type

const (
	// None status
	None AccountStatus = "None"
	// Ready status
	Ready AccountStatus = "Ready"
	// NotReady status
	NotReady AccountStatus = "NotReady"
	// Leased status
	Leased AccountStatus = "Leased"
	// Orphaned status
	Orphaned AccountStatus = "Orphaned"
)

func ParseAccountStatus

func ParseAccountStatus(status string) (AccountStatus, error)

ParseAccountStatus - parses the string into an account status.

type DB

type DB struct {
	// Name of the Account table
	Client dynamodbiface.DynamoDBAPI
	// Name of the RedboxAccount table
	AccountTableName string
	// Name of the Lease table
	LeaseTableName string
	// Default expiry time, in days, of the lease
	DefaultLeaseLengthInDays int
	// Use Consistent Reads when scanning or querying when possible.
	ConsistentRead bool
}

DB contains DynamoDB client and table names

func New

func New(client *dynamodb.DynamoDB, accountTableName string, leaseTableName string, defaultLeaseLengthInDays int) *DB

New creates a new DB Service struct, with all the necessary fields configured.

This method is mostly useful for testing, as it gives you fine-grained control over how the service is configured.

Elsewhere, you should generally use `db.NewFromEnv()`

func NewFromEnv

func NewFromEnv() (*DB, error)

NewFromEnv creates a DB instance configured from environment variables. Requires env vars for:

- AWS_CURRENT_REGION - ACCOUNT_DB - LEASE_DB

func (*DB) FindAccountsByStatus

func (db *DB) FindAccountsByStatus(status AccountStatus) ([]*Account, error)

FindAccountsByStatus finds account by status

func (*DB) FindLeasesByAccount

func (db *DB) FindLeasesByAccount(accountID string) ([]*Lease, error)

FindLeasesByAccount finds lease values for a given accountID

func (*DB) FindLeasesByPrincipal

func (db *DB) FindLeasesByPrincipal(principalID string) ([]*Lease, error)

FindLeasesByPrincipal finds leased accounts for a given principalID

func (*DB) FindLeasesByPrincipalAndAccount

func (db *DB) FindLeasesByPrincipalAndAccount(principalID string, accountID string) ([]*Lease, error)

FindLeasesByPrincipalAndAccount finds leased accounts for a given principalID

func (*DB) FindLeasesByStatus

func (db *DB) FindLeasesByStatus(status LeaseStatus) ([]*Lease, error)

FindLeasesByStatus finds leases by status

func (*DB) GetAccount

func (db *DB) GetAccount(accountID string) (*Account, error)

GetAccount returns an account record corresponding to an accountID string.

func (*DB) GetLease

func (db *DB) GetLease(accountID string, principalID string) (*Lease, error)

GetLease retrieves a Lease for the given accountID and principalID

func (*DB) GetLeaseByID

func (db *DB) GetLeaseByID(leaseID string) (*Lease, error)

GetLeaseByID gets a lease by ID

func (*DB) GetLeases

func (db *DB) GetLeases(input GetLeasesInput) (GetLeasesOutput, error)

GetLeases takes a set of filtering criteria and scans the Leases table for the matching records.

func (*DB) GetReadyAccount

func (db *DB) GetReadyAccount() (*Account, error)

GetReadyAccount returns an available account record with a corresponding status of 'Ready'

func (*DB) OrphanAccount

func (db *DB) OrphanAccount(accountID string) (*Account, error)

OrphanAccount puts account in Oprhaned status and inactivates any active leases

func (*DB) PutAccount

func (db *DB) PutAccount(account Account) error

PutAccount stores an account in DynamoDB

func (*DB) PutLease

func (db *DB) PutLease(lease Lease) (
	*Lease, error)

PutLease writes an Lease to DynamoDB Returns the previous AccountsLease if there is one - does not return the lease that was added

func (*DB) TransitionAccountStatus

func (db *DB) TransitionAccountStatus(accountID string, prevStatus AccountStatus, nextStatus AccountStatus) (*Account, error)

TransitionAccountStatus updates account status for a given accountID and returns the updated record on success

func (*DB) TransitionLeaseStatus

func (db *DB) TransitionLeaseStatus(accountID string, principalID string, prevStatus LeaseStatus, nextStatus LeaseStatus, leaseStatusReason LeaseStatusReason) (*Lease, error)

TransitionLeaseStatus updates a lease's status from prevStatus to nextStatus. Will fail if the Lease was not previously set to `prevStatus`

For example, to set a ResetLock on an account, you could call:

db.TransitionLeaseStatus(accountId, principalID, Active, ResetLock)

And to unlock the account:

db.TransitionLeaseStatus(accountId, principalID, ResetLock, Active)

func (*DB) UpdateAccountPrincipalPolicyHash

func (db *DB) UpdateAccountPrincipalPolicyHash(accountID string, prevHash string, nextHash string) (*Account, error)

UpdateAccountPrincipalPolicyHash updates hash representing the current version of the Principal IAM Policy applied to the account

func (*DB) UpsertLease

func (db *DB) UpsertLease(lease Lease) (*Lease, error)

UpsertLease creates or updates the lease records in DynDB

type DBer

type DBer interface {
	GetAccount(accountID string) (*Account, error)
	GetReadyAccount() (*Account, error)
	GetLease(accountID string, principalID string) (*Lease, error)
	GetLeases(input GetLeasesInput) (GetLeasesOutput, error)
	GetLeaseByID(leaseID string) (*Lease, error)
	FindAccountsByStatus(status AccountStatus) ([]*Account, error)
	PutAccount(account Account) error
	PutLease(lease Lease) (*Lease, error)
	UpsertLease(lease Lease) (*Lease, error)
	TransitionAccountStatus(accountID string, prevStatus AccountStatus, nextStatus AccountStatus) (*Account, error)
	TransitionLeaseStatus(accountID string, principalID string, prevStatus LeaseStatus, nextStatus LeaseStatus, leaseStatusReason LeaseStatusReason) (*Lease, error)
	FindLeasesByAccount(accountID string) ([]*Lease, error)
	FindLeasesByPrincipal(principalID string) ([]*Lease, error)
	FindLeasesByStatus(status LeaseStatus) ([]*Lease, error)
	UpdateAccountPrincipalPolicyHash(accountID string, prevHash string, nextHash string) (*Account, error)
	OrphanAccount(accountID string) (*Account, error)
}

The DBer interface includes all methods used by the DB struct to interact with DynamoDB. This is useful if we want to mock the DB service.

type GetLeasesInput

type GetLeasesInput struct {
	StartKeys   map[string]string
	PrincipalID string
	AccountID   string
	Status      LeaseStatus
	Limit       int64
}

GetLeasesInput contains the filtering criteria for the GetLeases scan.

type GetLeasesOutput

type GetLeasesOutput struct {
	Results  []*Lease
	NextKeys map[string]string
}

GetLeasesOutput contains the scan results as well as the keys for retrieve the next page of the result set.

type Lease

type Lease struct {
	AccountID                string                 `json:"AccountId"`                // AWS Account ID
	PrincipalID              string                 `json:"PrincipalId"`              // Azure User Principal ID
	ID                       string                 `json:"Id"`                       // Lease ID
	LeaseStatus              LeaseStatus            `json:"LeaseStatus"`              // Status of the Lease
	LeaseStatusReason        LeaseStatusReason      `json:"LeaseStatusReason"`        // Reason for the status of the lease
	CreatedOn                int64                  `json:"CreatedOn"`                // Created Epoch Timestamp
	LastModifiedOn           int64                  `json:"LastModifiedOn"`           // Last Modified Epoch Timestamp
	BudgetAmount             float64                `json:"BudgetAmount"`             // Budget Amount allocated for this lease
	BudgetCurrency           string                 `json:"BudgetCurrency"`           // Budget currency
	BudgetNotificationEmails []string               `json:"BudgetNotificationEmails"` // Budget notification emails
	LeaseStatusModifiedOn    int64                  `json:"LeaseStatusModifiedOn"`    // Last Modified Epoch Timestamp
	ExpiresOn                int64                  `json:"ExpiresOn"`                // Lease expiration time as Epoch
	Metadata                 map[string]interface{} `json:"Metadata"`                 // Arbitrary key-value metadata to store with lease object
}

Lease is a type corresponding to a Lease table record

type LeaseStatus

type LeaseStatus string

LeaseStatus is a account lease status type

const (
	// EmptyLeaseStatus status
	EmptyLeaseStatus LeaseStatus = ""
	// Active status
	Active LeaseStatus = "Active"
	// Inactive status
	Inactive LeaseStatus = "Inactive"
)

func ParseLeaseStatus

func ParseLeaseStatus(status string) (LeaseStatus, error)

ParseLeaseStatus - parses the string into an account status.

type LeaseStatusReason

type LeaseStatusReason string

LeaseStatusReason provides consistent verbiage for lease status change reasons.

const (
	// LeaseExpired means the lease has past its expiresOn date and therefore expired.
	LeaseExpired LeaseStatusReason = "Expired"
	// LeaseOverBudget means the lease is over its budgeted amount and is therefore reset/reclaimed.
	LeaseOverBudget LeaseStatusReason = "OverBudget"
	// LeaseOverPrincipalBudget means the lease is over its principal budgeted amount and is therefore reset/reclaimed.
	LeaseOverPrincipalBudget LeaseStatusReason = "OverPrincipalBudget"
	// LeaseDestroyed means the lease has been deleted via an API call or other user action.
	LeaseDestroyed LeaseStatusReason = "Destroyed"
	// LeaseActive means the lease is still active.
	LeaseActive LeaseStatusReason = "Active"
	// LeaseRolledBack means something happened in the system that caused the lease to be inactive
	// based on an error happening and rollback occuring
	LeaseRolledBack LeaseStatusReason = "Rollback"
	// AccountOrphaned means that the health of the account was compromised.  The account has been orphaned
	// which means the leases are also made Inactive
	AccountOrphaned LeaseStatusReason = "AccountOrphaned"
)

type NotFoundError

type NotFoundError struct {
	Err string
}

NotFoundError is returned when a resource is not found.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

type StatusTransitionError

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

StatusTransitionError means that we failed to transition an Account or Lease from one status to another, likely because the prevStatus condition was not met

func (*StatusTransitionError) Error

func (e *StatusTransitionError) Error() string

type Timestamp

type Timestamp int64

Timestamp is a timestamp type for epoch format

type Timestamped

type Timestamped struct {
	CreatedOn      Timestamp
	LastModifiedOn Timestamp
}

Timestamped contains timestamp types

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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