This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
/
loader.go
113 lines (97 loc) · 3.45 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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 [email protected] [:alm]
package mig /* import "github.com/mozilla/mig" */
import (
"errors"
"fmt"
"regexp"
"time"
)
// LoaderEntry describes a loader entry stored in the database
type LoaderEntry struct {
ID float64 `json:"id"` // Loader ID
Name string `json:"name"` // Loader name
Prefix string `json:"prefix"` // Loader key prefix
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
ExpectEnv string `json:"expectenv"` // Expected environment
}
// Validate validates a loader entry
func (le *LoaderEntry) Validate() (err error) {
if le.Key != "" {
err = ValidateLoaderPrefixAndKey(le.Prefix + le.Key)
}
return nil
}
// LoaderAuthDetails is a 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
}
// Validate validates a LoaderAuthDetails type
func (lad *LoaderAuthDetails) Validate() error {
if len(lad.Hash) != LoaderHashedKeyLength ||
len(lad.Salt) != LoaderSaltLength {
return fmt.Errorf("contents of LoaderAuthDetails are invalid")
}
return nil
}
// GenerateLoaderPrefix will generate a new loader prefix value
func GenerateLoaderPrefix() string {
return RandAPIKeyString(LoaderPrefixLength)
}
// GenerateLoaderKey will generate a new loader key value
func GenerateLoaderKey() string {
return RandAPIKeyString(LoaderKeyLength)
}
// LoaderPrefixAndKeyLength is the key length for a loader key including the prefix
const LoaderPrefixAndKeyLength = 40
// LoaderPrefixLength is the length of the loader prefix
const LoaderPrefixLength = 8
// LoaderKeyLength is the length of the loader key
const LoaderKeyLength = 32
// LoaderHashedKeyLength is the length of the hashed loader key in the database
const LoaderHashedKeyLength = 32
// LoaderSaltLength is the length of the salt applied to loader keys
const LoaderSaltLength = 16
// ValidateLoaderKey validates a loader key, returns nil if it is valid
func ValidateLoaderKey(key string) error {
repstr := fmt.Sprintf("^[A-Za-z0-9]{%v}$", LoaderKeyLength)
ok, err := regexp.MatchString(repstr, key)
if err != nil || !ok {
return errors.New("loader key format is invalid")
}
return nil
}
// ValidateLoaderPrefix validates a loader prefix value, returns nil if it is valid
func ValidateLoaderPrefix(prefix string) error {
repstr := fmt.Sprintf("^[A-Za-z0-9]{%v}$", LoaderPrefixLength)
ok, err := regexp.MatchString(repstr, prefix)
if err != nil || !ok {
return errors.New("loader prefix format is invalid")
}
return nil
}
// ValidateLoaderPrefixAndKey validates a loader key that includes the prefix
func ValidateLoaderPrefixAndKey(pk string) error {
if len(pk) != LoaderPrefixAndKeyLength {
return fmt.Errorf("loader key is incorrect length")
}
err := ValidateLoaderPrefix(pk[:LoaderPrefixLength])
if err != nil {
return err
}
err = ValidateLoaderKey(pk[LoaderPrefixLength:])
if err != nil {
return err
}
return nil
}