2015-07-31 00:36:02 +03:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
// Contributor: Aaron Meihm ameihm@mozilla.com [:alm]
|
|
|
|
|
|
|
|
package mig /* import "mig.ninja/mig" */
|
|
|
|
|
2016-04-14 19:44:32 +03:00
|
|
|
import (
|
2016-04-26 00:44:17 +03:00
|
|
|
"errors"
|
2016-07-07 23:13:32 +03:00
|
|
|
"fmt"
|
|
|
|
mrand "math/rand"
|
2016-04-26 00:44:17 +03:00
|
|
|
"regexp"
|
2016-04-14 19:44:32 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-02-11 23:59:42 +03:00
|
|
|
// Describes a loader entry stored in the database
|
2015-07-31 00:36:02 +03:00
|
|
|
type LoaderEntry struct {
|
2016-05-26 18:46:26 +03:00
|
|
|
ID float64 `json:"id"` // Loader ID
|
|
|
|
Name string `json:"name"` // Loader name
|
2016-07-07 23:13:32 +03:00
|
|
|
Prefix string `json:"prefix"` // Loader key prefix
|
2016-05-26 18:46:26 +03:00
|
|
|
Key string `json:"key"` // Loader key (only populated during creation)
|
|
|
|
AgentName string `json:"agentname"` // Loader environment, agent name
|
|
|
|
LastSeen time.Time `json:"lastseen"` // Last time loader was used
|
|
|
|
Enabled bool `json:"enabled"` // Loader entry is active
|
2016-08-11 19:39:23 +03:00
|
|
|
ExpectEnv string `json:"expectenv"` // Expected environment
|
2016-04-15 18:54:37 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 23:13:32 +03:00
|
|
|
func (le *LoaderEntry) Validate() (err error) {
|
|
|
|
if le.Key != "" {
|
|
|
|
err = ValidateLoaderPrefixAndKey(le.Prefix + le.Key)
|
|
|
|
}
|
2016-04-15 18:54:37 +03:00
|
|
|
return nil
|
2015-07-31 00:36:02 +03:00
|
|
|
}
|
2016-04-26 00:44:17 +03:00
|
|
|
|
2016-07-12 17:01:41 +03:00
|
|
|
// Small helper type used primarily during the loader authentication
|
|
|
|
// process between the API and database code, temporarily stores
|
|
|
|
// authentication information
|
|
|
|
type LoaderAuthDetails struct {
|
|
|
|
ID float64
|
|
|
|
Hash []byte
|
|
|
|
Salt []byte
|
|
|
|
}
|
|
|
|
|
2016-07-12 17:10:58 +03:00
|
|
|
func (lad *LoaderAuthDetails) Validate() error {
|
|
|
|
if len(lad.Hash) != LoaderHashedKeyLength ||
|
|
|
|
len(lad.Salt) != LoaderSaltLength {
|
|
|
|
return fmt.Errorf("contents of LoaderAuthDetails are invalid")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-07 23:13:32 +03:00
|
|
|
// Generate a new loader prefix value
|
|
|
|
func GenerateLoaderPrefix() string {
|
2016-07-12 15:38:40 +03:00
|
|
|
return RandLoaderKeyString(LoaderPrefixLength)
|
2016-07-07 23:13:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a new loader key value
|
|
|
|
func GenerateLoaderKey() string {
|
2016-07-12 15:38:40 +03:00
|
|
|
return RandLoaderKeyString(LoaderKeyLength)
|
2016-07-07 23:13:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RandLoaderKeyString is used for prefix and key generation, and just
|
|
|
|
// returns a random string consisting of alphanumeric characters of
|
|
|
|
// length characters long
|
|
|
|
func RandLoaderKeyString(length int) string {
|
|
|
|
ret := make([]byte, length)
|
|
|
|
lset := []byte("abcdefghijklmnopqrstuvwxyzABCDEFCHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
|
|
r := mrand.New(mrand.NewSource(time.Now().UnixNano()))
|
|
|
|
for i := 0; i < len(ret); i++ {
|
|
|
|
ret[i] = lset[r.Int()%len(lset)]
|
|
|
|
}
|
|
|
|
return string(ret[:len(ret)])
|
|
|
|
}
|
|
|
|
|
2016-07-12 15:38:40 +03:00
|
|
|
// Various constants related to properties of the loader keys
|
|
|
|
const LoaderPrefixAndKeyLength = 40 // Key length including prefix
|
|
|
|
const LoaderPrefixLength = 8 // Prefix length
|
|
|
|
const LoaderKeyLength = 32 // Length excluding prefix
|
2016-07-12 17:10:58 +03:00
|
|
|
const LoaderHashedKeyLength = 32 // Length of hashed key in the database
|
|
|
|
const LoaderSaltLength = 16 // Length of salt
|
2016-07-07 23:13:32 +03:00
|
|
|
|
|
|
|
// Validate a loader key, returns nil if it is valid
|
2016-04-26 00:44:17 +03:00
|
|
|
func ValidateLoaderKey(key string) error {
|
2016-07-12 15:38:40 +03:00
|
|
|
repstr := fmt.Sprintf("^[A-Za-z0-9]{%v}$", LoaderKeyLength)
|
|
|
|
ok, err := regexp.MatchString(repstr, key)
|
2016-04-26 00:44:17 +03:00
|
|
|
if err != nil || !ok {
|
|
|
|
return errors.New("loader key format is invalid")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-07 23:13:32 +03:00
|
|
|
|
|
|
|
// Validate a loader prefix value, returns nil if it is valid
|
|
|
|
func ValidateLoaderPrefix(prefix string) error {
|
2016-07-12 15:38:40 +03:00
|
|
|
repstr := fmt.Sprintf("^[A-Za-z0-9]{%v}$", LoaderPrefixLength)
|
|
|
|
ok, err := regexp.MatchString(repstr, prefix)
|
2016-07-07 23:13:32 +03:00
|
|
|
if err != nil || !ok {
|
|
|
|
return errors.New("loader prefix format is invalid")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate a loader key that includes the prefix
|
|
|
|
func ValidateLoaderPrefixAndKey(pk string) error {
|
|
|
|
if len(pk) != LoaderPrefixAndKeyLength {
|
|
|
|
return fmt.Errorf("loader key is incorrect length")
|
|
|
|
}
|
2016-07-12 15:38:40 +03:00
|
|
|
err := ValidateLoaderPrefix(pk[:LoaderPrefixLength])
|
2016-07-07 23:13:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-12 15:38:40 +03:00
|
|
|
err = ValidateLoaderKey(pk[LoaderPrefixLength:])
|
2016-07-07 23:13:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|