config

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2016 License: MIT Imports: 14 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// CurrentBucketName is the name of the bucket that holds the current
	// verison of the config
	CurrentBucketName = "current"

	// CurrentVersionHash is the "hash" name of the current config
	CurrentVersionHash = "current"
)
View Source
const (
	READ = iota
	WRITE
	ADMIN // bless users to higher permissions levels
)

Variables

View Source
var (
	AboutBucket = []byte("about")
	Schemas     = []Schema{
		{
			Version: Version{
				0, 10, 4,
			},
			Buckets: []string{
				"app",
				"user",
			},

			Upgrader: func(old *bolt.DB) error {
				return nil
			},
		},
		{
			Version: Version{
				0, 12, 0,
			},
			Buckets: []string{
				"app",
				"user",
			},
			Upgrader: func(old *bolt.DB) error {
				convertToKV := func(i interface{}) *event.TagSet {
					if i == nil {
						return event.NewTagset(0)
					}
					m := i.(map[string]interface{})
					kv := event.NewTagset(len(m))
					for k, v := range m {
						kv.Set(k, v.(string))
					}

					return kv
				}
				upgradeRawSnapshot := func(buff []byte) ([]byte, error) {

					oldSnap := make(map[string]interface{})
					err := json.Unmarshal(buff, &oldSnap)
					if err != nil {
						return nil, err
					}

					app, ok := oldSnap["app"].(map[string]interface{})
					if !ok {
						return nil, fmt.Errorf("Unable to upgrade snapshot. No 'app' section found.")
					}

					raw_pols, ok := app["policies"]
					if ok {
						pols, ok := raw_pols.(map[string]interface{})
						if ok {
							for k, v := range pols {
								x := v.(map[string]interface{})

								logrus.Debugf("Removing old match/not_match from %s", k)
								x["match"] = convertToKV(x["match"])
								x["not_match"] = convertToKV(x["not_match"])
								x["group_by"] = convertToKV(x["group_by"])
							}
						} else {
							log.Println(reflect.TypeOf(raw_pols).String())
						}

					}

					app["global_policy"] = map[string]interface{}{}

					oldSnap["app"] = app

					return json.Marshal(&oldSnap)

				}

				err := old.Update(func(tx *bolt.Tx) error {

					b := tx.Bucket([]byte("app"))
					if b == nil {
						logrus.Warning("No config snapshots found")
						return nil
					}

					newSnapshots := make([]struct {
						key []byte
						val []byte
					}, 0, b.Stats().KeyN)

					b.ForEach(func(k, v []byte) error {
						logrus.Debugf("Starting snapshot upgrade on: %s", string(k))
						upgraded, err := upgradeRawSnapshot(v)
						if err != nil {
							logrus.Errorf("Unable to upgrade snapshot: %s %s", string(k), err.Error())
						}

						newSnapshots = append(newSnapshots, struct {
							key []byte
							val []byte
						}{
							key: k,
							val: upgraded,
						})

						return nil
					})

					for _, snap := range newSnapshots {
						err := b.Put(snap.key, snap.val)
						if err != nil {
							logrus.Errorf("Unable to write upgraded snapshot to database %s %s", string(snap.key), err.Error())
						}

						logrus.Debugf("Finished snapshot upgrade on: %s", string(snap.key))
					}

					return nil

				})

				return err

			},
		},
		{
			Version: Version{
				0, 13, 0,
			},
			Buckets: []string{
				"app",
				"user",
			},
			Upgrader: func(old *bolt.DB) error {
				upgradeRawSnapshot := func(buff []byte) ([]byte, error) {
					oldSnap := make(map[string]interface{})
					err := json.Unmarshal(buff, &oldSnap)
					if err != nil {
						return nil, err
					}

					app, ok := oldSnap["app"].(map[string]interface{})
					if !ok {
						return nil, fmt.Errorf("Unable to upgrade snapshot. No 'app' section found.")
					}

					app["escalations"] = map[string]interface{}{}

					oldSnap["app"] = app

					return json.Marshal(&oldSnap)
				}

				err := old.Update(func(tx *bolt.Tx) error {

					b := tx.Bucket([]byte("app"))
					if b == nil {
						logrus.Warning("No config snapshots found")
						return nil
					}

					newSnapshots := make([]struct {
						key []byte
						val []byte
					}, 0, b.Stats().KeyN)

					b.ForEach(func(k, v []byte) error {
						logrus.Debugf("Starting snapshot upgrade on: %s", string(k))
						upgraded, err := upgradeRawSnapshot(v)
						if err != nil {
							logrus.Errorf("Unable to upgrade snapshot: %s %s", string(k), err.Error())
						}

						newSnapshots = append(newSnapshots, struct {
							key []byte
							val []byte
						}{
							key: k,
							val: upgraded,
						})

						return nil
					})

					for _, snap := range newSnapshots {
						err := b.Put(snap.key, snap.val)
						if err != nil {
							logrus.Errorf("Unable to write upgraded snapshot to database %s %s", string(snap.key), err.Error())
						}

						logrus.Debugf("Finished snapshot upgrade on: %s", string(snap.key))
					}

					return nil

				})

				return err

			},
		},
	}
)
View Source
var (
	Current = Version{
		Major: 0,
		Minor: 15,
		Patch: 1,
	}

	First = Version{
		0, 10, 4,
	}
)
View Source
var (
	// BucketNames holds the names of the buckets used to store versioned configs
	BucketNames = []string{
		"app",
		"user",
	}
)

Functions

func CheckUserPassword added in v0.15.0

func CheckUserPassword(u *User, pass string) bool

CheckUserPassword compares a raw password against the the stored hash'ed password

func HashConfig added in v0.15.0

func HashConfig(c *AppConfig) []byte

func HashUserPassword added in v0.15.0

func HashUserPassword(u *User, raw string) string

return the password hash for a given user, given the raw password

func InsufficientPermissions added in v0.15.0

func InsufficientPermissions(need, have UserPermissions) error

func PermissionsToName added in v0.15.0

func PermissionsToName(p UserPermissions) string

Types

type AppConfig

type AppConfig struct {
	EscalationsDir  string                                  `json:"escalations_dir"`
	KeepAliveAge    time.Duration                           `json:"-"`
	RawKeepAliveAge string                                  `json:"keep_alive_age"`
	DbPath          string                                  `json:"db_path"`
	Escalations     map[string]*escalation.EscalationPolicy `json:"escalations"`
	Encoding        string                                  `json:"encoding"`
	Policies        map[string]*escalation.Policy           `json:"policies"`
	EventProviders  *provider.EventProviderCollection       `json:"event_providers"`
	LogLevel        string                                  `json:"log_level"`
	APIPort         int                                     `json:"API_port"`
	Hash            []byte                                  `json:"-"`
	// contains filtered or unexported fields
}

AppConfig provides configuration options for setting up the application

func NewDefaultConfig

func NewDefaultConfig() *AppConfig

NewDefaultConfig create and return a new instance of the default configuration

func (*AppConfig) FileName added in v0.15.0

func (c *AppConfig) FileName() string

FileName returns the name of the file that was used to create this AppConfig

func (*AppConfig) Provider added in v0.15.0

func (c *AppConfig) Provider() Provider

Provider returns the Provider that created this AppConfig

func (*AppConfig) SetProvider added in v0.15.0

func (a *AppConfig) SetProvider(p Provider)

SetProvider changes the AppConfigs provider to the givin one

type ConfigVersionNotFound added in v0.15.0

type ConfigVersionNotFound string

func (ConfigVersionNotFound) Error added in v0.15.0

func (c ConfigVersionNotFound) Error() string

type Configer

type Configer interface {
	ConfigStruct() interface{}
	Init(interface{}) error
}

A Configer provides an interface to dynamicaly load configuration

type DBConf added in v0.15.0

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

DBConf provides methods for reading and writing configs from a database

func (*DBConf) DeleteUser added in v0.15.0

func (d *DBConf) DeleteUser(name string) error

DeleteUser by the User.Id

func (*DBConf) GetConfig added in v0.15.0

func (d *DBConf) GetConfig(versionHash string) (*AppConfig, error)

GetConfig get the config file which has the hash of given version

func (*DBConf) GetCurrent added in v0.15.0

func (d *DBConf) GetCurrent() (*AppConfig, error)

GetCurrent loads the current version of the config

func (*DBConf) GetUser added in v0.15.0

func (d *DBConf) GetUser(name string) (*User, error)

GetUser by their User.Id

func (*DBConf) GetUserByUserName added in v0.15.0

func (d *DBConf) GetUserByUserName(name string) (*User, error)

GetUserByUserName

func (*DBConf) ListSnapshots added in v0.15.0

func (d *DBConf) ListSnapshots() []*Snapshot

func (*DBConf) ListUsers added in v0.15.0

func (d *DBConf) ListUsers() ([]*User, error)

ListUsers fetches all known users

func (*DBConf) PutConfig added in v0.15.0

func (d *DBConf) PutConfig(a *AppConfig, u *User) (string, error)

PutConfig writes the given config to the database and returns the new hash and an error

func (*DBConf) PutUser added in v0.15.0

func (d *DBConf) PutUser(u *User) error

PutUser inserts the user into the db

type MockProvider added in v0.15.0

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

func (*MockProvider) DeleteUser added in v0.15.0

func (m *MockProvider) DeleteUser(name string) error

func (*MockProvider) GetConfig added in v0.15.0

func (m *MockProvider) GetConfig(version string) (*AppConfig, error)

func (*MockProvider) GetCurrent added in v0.15.0

func (m *MockProvider) GetCurrent() (*AppConfig, error)

func (*MockProvider) GetUser added in v0.15.0

func (m *MockProvider) GetUser(name string) (*User, error)

func (*MockProvider) GetUserByUserName added in v0.15.0

func (m *MockProvider) GetUserByUserName(name string) (*User, error)

func (*MockProvider) ListSnapshots added in v0.15.0

func (m *MockProvider) ListSnapshots() []*Snapshot

func (*MockProvider) ListUsers added in v0.15.0

func (m *MockProvider) ListUsers() ([]*User, error)

func (*MockProvider) PutConfig added in v0.15.0

func (m *MockProvider) PutConfig(c *AppConfig, u *User) (string, error)

func (*MockProvider) PutUser added in v0.15.0

func (m *MockProvider) PutUser(u *User) error

type Provider added in v0.15.0

type Provider interface {
	GetConfig(version string) (*AppConfig, error)
	GetCurrent() (*AppConfig, error)
	PutConfig(*AppConfig, *User) (string, error)
	ListSnapshots() []*Snapshot
	GetUser(userName string) (*User, error)
	GetUserByUserName(string) (*User, error)
	DeleteUser(userName string) error
	PutUser(u *User) error
	ListUsers() ([]*User, error)
}

A Provider can read and write configs, along with querying for versions

func GetProvider added in v0.15.0

func GetProvider(kind string, path string) Provider

GetProvider returns a config provider at that given path. If nothing exists at that path, one will be created. If a bad kind id provided, a nil value will be returned

func NewMockProvider added in v0.15.0

func NewMockProvider() Provider

type Schema added in v0.15.0

type Schema struct {
	Version  Version
	Buckets  []string
	Upgrader Upgrader
}

Schema represents a bucket schema for a bolt.Db

func GetSchemaFromDb added in v0.15.0

func GetSchemaFromDb(b *bolt.DB) Schema

func LatestSchema added in v0.15.0

func LatestSchema() Schema

LatestSchema returns the newest schema

func (Schema) Apply added in v0.15.0

func (s Schema) Apply(b *bolt.DB) error

Apply creates all needed buckets and sets the version of the db

func (Schema) Greater added in v0.15.0

func (s Schema) Greater(x Schema) bool

Greater

type Snapshot added in v0.15.0

type Snapshot struct {
	Hash            string     `json:"hash"`
	Timestamp       time.Time  `json:"time_stamp"`
	App             *AppConfig `json:"app"`
	CreatorId       uint16     `json:"creator_id"` // the User.Id of who created this snapshot
	CreatorName     string     `json:"creator_name"`
	CreatorUserName string     `json:"creator_user_name"`
}

Snapshot represents a bangarang config at a given point in time

type Upgrader added in v0.15.0

type Upgrader func(old *bolt.DB) error

givena n old and new schema update the current db

type User added in v0.15.0

type User struct {
	Name         string          `json:"name"`
	UserName     string          `json:"user_name"`
	PasswordHash string          `json:"password_hash"`
	Permissions  UserPermissions `json:"permissions"`
	// contains filtered or unexported fields
}

User holds information about who a user is, and what they are allowed to do

func NewUser added in v0.15.0

func NewUser(name, userName, rawPassword string, permissions UserPermissions) *User

type UserNotFound added in v0.15.0

type UserNotFound string

func (UserNotFound) Error added in v0.15.0

func (u UserNotFound) Error() string

type UserPermissions added in v0.15.0

type UserPermissions int

UserPermissions describes what a user is allowed to do

func NameToPermissions added in v0.15.0

func NameToPermissions(name string) UserPermissions

type Version added in v0.15.0

type Version struct {
	Major int `json:"major"`
	Minor int `json:"minor"`
	Patch int `json:"patch"`
}

Version prepresents the semantic version of something

func VersionFromString added in v0.15.0

func VersionFromString(s string) Version

VersionFromString parse a string and return the corosponding version

func (Version) Greater added in v0.15.0

func (v Version) Greater(x Version) bool

Greater compares two versions and returns if v > x

func (Version) String added in v0.15.0

func (v Version) String() string

String returns the string representation of a version

Jump to

Keyboard shortcuts

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