func (environment *Environment) ExpireEnroll(name string) error {
	env, err := environment.Get(name)
	if err != nil {
		return fmt.Errorf("error getting environment %v", err)
	}
	if err := environment.DB.Model(&env).Update("enroll_expire", time.Now()).Error; err != nil {
		return fmt.Errorf("Update %v", err)
	}
	return nil
}
ExpireEnroll to expire the enroll in an environment
package environments
import (
	"fmt"
	"log"
	"time"
	"github.com/jinzhu/gorm"
)
const (
	DefaultEnrollPath string = "enroll"
	DefaultLogPath string = "log"
	DefaultLogInterval int = 600
	DefaultConfigPath string = "config"
	DefaultConfigInterval int = 300
	DefaultQueryReadPath string = "read"
	DefaultQueryWritePath string = "write"
	DefaultQueryInterval int = 60
	DefaultCarverInitPath string = "init"
	DefaultCarverBlockPath string = "block"
	DefaultEnvironmentIcon string = "fas fa-wrench"
	DefaultEnvironmentType string = "osquery"
	DefaultSecretLength int = 64
	DefaultLinkExpire int = 24
)
type TLSEnvironment struct {
	gorm.Model
	UUID             string `gorm:"index"`
	Name             string
	Hostname         string
	Secret           string
	EnrollSecretPath string
	EnrollExpire     time.Time
	RemoveSecretPath string
	RemoveExpire     time.Time
	Type             string
	DebugHTTP        bool
	Icon             string
	Options          string
	Schedule         string
	Packs            string
	Decorators       string
	ATC              string
	Configuration    string
	Flags            string
	Certificate      string
	ConfigTLS        bool
	ConfigInterval   int
	LoggingTLS       bool
	LogInterval      int
	QueryTLS         bool
	QueryInterval    int
	CarvesTLS        bool
	EnrollPath       string
	LogPath          string
	ConfigPath       string
	QueryReadPath    string
	QueryWritePath   string
	CarverInitPath   string
	CarverBlockPath  string
}
type MapEnvironments map[string]TLSEnvironment
type Environment struct {
	DB *gorm.DB
}
func CreateEnvironment(backend *gorm.DB) *Environment {
	var e *Environment
	e = &Environment{DB: backend}
	if err := backend.AutoMigrate(TLSEnvironment{}).Error; err != nil {
		log.Fatalf("Failed to AutoMigrate table (tls_environments): %v", err)
	}
	return e
}
func (environment *Environment) Get(identifier string) (TLSEnvironment, error) {
	var env TLSEnvironment
	if err := environment.DB.Where("name = ? OR uuid = ?", identifier, identifier).First(&env).Error; err != nil {
		return env, err
	}
	return env, nil
}
func (environment *Environment) Empty(name, hostname string) TLSEnvironment {
	return TLSEnvironment{
		UUID:             generateUUID(),
		Name:             name,
		Hostname:         hostname,
		Secret:           generateRandomString(DefaultSecretLength),
		EnrollSecretPath: generateKSUID(),
		RemoveSecretPath: generateKSUID(),
		EnrollExpire:     time.Now(),
		RemoveExpire:     time.Now(),
		Type:             DefaultEnvironmentType,
		DebugHTTP:        false,
		Icon:             DefaultEnvironmentIcon,
		Flags:            "{}",
		Options:          "{}",
		Schedule:         "{}",
		Packs:            "{}",
		Decorators:       "{}",
		ATC:              "{}",
		Configuration:    "{}",
		Certificate:      "",
		ConfigTLS:        true,
		ConfigInterval:   DefaultConfigInterval,
		LoggingTLS:       true,
		LogInterval:      DefaultLogInterval,
		QueryTLS:         true,
		CarvesTLS:        true,
		QueryInterval:    DefaultQueryInterval,
		EnrollPath:       DefaultEnrollPath,
		LogPath:          DefaultLogPath,
		ConfigPath:       DefaultConfigPath,
		QueryReadPath:    DefaultQueryReadPath,
		QueryWritePath:   DefaultQueryWritePath,
		CarverInitPath:   DefaultCarverInitPath,
		CarverBlockPath:  DefaultCarverBlockPath,
	}
}
func (environment *Environment) Create(env TLSEnvironment) error {
	if environment.DB.NewRecord(env) {
		if err := environment.DB.Create(&env).Error; err != nil {
			return fmt.Errorf("Create TLS Environment %v", err)
		}
	} else {
		return fmt.Errorf("db.NewRecord did not return true")
	}
	return nil
}
func (environment *Environment) Exists(identifier string) bool {
	var results int
	environment.DB.Model(&TLSEnvironment{}).Where("name = ? OR uuid = ?", identifier, identifier).Count(&results)
	return (results > 0)
}
func (environment *Environment) All() ([]TLSEnvironment, error) {
	var envs []TLSEnvironment
	if err := environment.DB.Find(&envs).Error; err != nil {
		return envs, err
	}
	return envs, nil
}
func (environment *Environment) Names() ([]string, error) {
	envs, err := environment.All()
	if err != nil {
		return []string{}, err
	}
	names := []string{}
	for _, e := range envs {
		names = append(names, e.Name)
	}
	return names, err
}
func (environment *Environment) UUIDs() ([]string, error) {
	envs, err := environment.All()
	if err != nil {
		return []string{}, err
	}
	uuids := []string{}
	for _, e := range envs {
		uuids = append(uuids, e.UUID)
	}
	return uuids, err
}
func (environment *Environment) GetMap() (MapEnvironments, error) {
	all, err := environment.All()
	if err != nil {
		return nil, fmt.Errorf("error getting environments %v", err)
	}
	_map := make(MapEnvironments)
	for _, e := range all {
		_map[e.Name] = e
		_map[e.UUID] = e
	}
	return _map, nil
}
func (environment *Environment) Delete(identifier string) error {
	env, err := environment.Get(identifier)
	if err != nil {
		return fmt.Errorf("error getting environment %v", err)
	}
	if err := environment.DB.Unscoped().Delete(&env).Error; err != nil {
		return fmt.Errorf("Delete %v", err)
	}
	return nil
}
func (environment *Environment) Update(e TLSEnvironment) error {
	env, err := environment.Get(e.Name)
	if err != nil {
		return fmt.Errorf("error getting environment %v", err)
	}
	if err := environment.DB.Model(&env).Updates(e).Error; err != nil {
		return fmt.Errorf("Updates %v", err)
	}
	return nil
}
func (environment *Environment) UpdateOptions(idEnv, options string) error {
	if err := environment.DB.Model(&TLSEnvironment{}).Where("name = ? OR uuid = ?", idEnv, idEnv).Update("options", options).Error; err != nil {
		return fmt.Errorf("Update options %v", err)
	}
	return nil
}
func (environment *Environment) UpdateSchedule(idEnv, schedule string) error {
	if err := environment.DB.Model(&TLSEnvironment{}).Where("name = ? OR uuid = ?", idEnv, idEnv).Update("schedule", schedule).Error; err != nil {
		return fmt.Errorf("Update schedule %v", err)
	}
	return nil
}
func (environment *Environment) UpdatePacks(idEnv, packs string) error {
	if err := environment.DB.Model(&TLSEnvironment{}).Where("name = ? OR uuid = ?", idEnv, idEnv).Update("packs", packs).Error; err != nil {
		return fmt.Errorf("Update packs %v", err)
	}
	return nil
}
func (environment *Environment) UpdateDecorators(idEnv, decorators string) error {
	if err := environment.DB.Model(&TLSEnvironment{}).Where("name = ? OR uuid = ?", idEnv, idEnv).Update("decorators", decorators).Error; err != nil {
		return fmt.Errorf("Update decorators %v", err)
	}
	return nil
}
func (environment *Environment) UpdateATC(idEnv, atc string) error {
	if err := environment.DB.Model(&TLSEnvironment{}).Where("name = ? OR uuid = ?", idEnv, idEnv).Update("atc", atc).Error; err != nil {
		return fmt.Errorf("Update ATC %v", err)
	}
	return nil
}
func (environment *Environment) UpdateCertificate(idEnv, certificate string) error {
	if err := environment.DB.Model(&TLSEnvironment{}).Where("name = ? OR uuid = ?", idEnv, idEnv).Update("certificate", certificate).Error; err != nil {
		return fmt.Errorf("Update %v", err)
	}
	return nil
}
func (environment *Environment) UpdateFlags(idEnv, flags string) error {
	if err := environment.DB.Model(&TLSEnvironment{}).Where("name = ? OR uuid = ?", idEnv, idEnv).Update("flags", flags).Error; err != nil {
		return fmt.Errorf("Update flags %v", err)
	}
	return nil
}
func (environment *Environment) UpdateHostname(idEnv, hostname string) error {
	if err := environment.DB.Model(&TLSEnvironment{}).Where("name = ? OR uuid = ?", idEnv, idEnv).Update("hostname", hostname).Error; err != nil {
		return fmt.Errorf("Update hostname %v", err)
	}
	return nil
}
func (environment *Environment) UpdateIntervals(name string, csecs, lsecs, qsecs int) error {
	env, err := environment.Get(name)
	if err != nil {
		return fmt.Errorf("error getting environment %v", err)
	}
	updated := env
	updated.ConfigInterval = csecs
	updated.LogInterval = lsecs
	updated.QueryInterval = qsecs
	if err := environment.DB.Model(&env).Updates(updated).Error; err != nil {
		return fmt.Errorf("Updates %v", err)
	}
	return nil
}
func (environment *Environment) RotateSecrets(name string) error {
	env, err := environment.Get(name)
	if err != nil {
		return fmt.Errorf("error getting environment %v", err)
	}
	rotated := env
	rotated.Secret = generateRandomString(DefaultSecretLength)
	rotated.EnrollSecretPath = generateKSUID()
	rotated.RemoveSecretPath = generateKSUID()
	rotated.EnrollExpire = time.Now().Add(time.Duration(DefaultLinkExpire) * time.Hour)
	rotated.RemoveExpire = time.Now().Add(time.Duration(DefaultLinkExpire) * time.Hour)
	if err := environment.DB.Model(&env).Updates(rotated).Error; err != nil {
		return fmt.Errorf("Updates %v", err)
	}
	return nil
}
func (environment *Environment) RotateEnrollPath(name string) error {
	env, err := environment.Get(name)
	if err != nil {
		return fmt.Errorf("error getting environment %v", err)
	}
	rotated := env
	rotated.EnrollSecretPath = generateKSUID()
	rotated.EnrollExpire = time.Now().Add(time.Duration(DefaultLinkExpire) * time.Hour)
	if err := environment.DB.Model(&env).Updates(rotated).Error; err != nil {
		return fmt.Errorf("Updates %v", err)
	}
	return nil
}
func (environment *Environment) RotateSecret(name string) error {
	env, err := environment.Get(name)
	if err != nil {
		return fmt.Errorf("error getting environment %v", err)
	}
	rotated := env
	rotated.Secret = generateRandomString(DefaultSecretLength)
	rotated.EnrollExpire = time.Now().Add(time.Duration(DefaultLinkExpire) * time.Hour)
	if err := environment.DB.Model(&env).Updates(rotated).Error; err != nil {
		return fmt.Errorf("Updates %v", err)
	}
	return nil
}