Skip to content

Commit 2efc206

Browse files
kristinapathakkcajmagic
authored andcommitted
Added comments (#82)
* Added comments * Update cipher/viper.go - fixed spelling
1 parent fac867d commit 2efc206

File tree

15 files changed

+226
-17
lines changed

15 files changed

+226
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
- Added comments for godocs
89

910
## [v0.7.0]
1011
- Added `record_id` to index

blacklist/blacklist.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,38 @@ const (
1313
defaultUpdateInterval = time.Minute
1414
)
1515

16+
// BlackListedItem is the regex that expresses the devices that are blacklisted
17+
// and the reason why.
1618
type BlackListedItem struct {
1719
ID string
1820
Reason string
1921
}
2022

23+
// TableName sets BlackListedItem's table name to be "blacklist"; for the GORM driver.
2124
func (BlackListedItem) TableName() string {
2225
return "blacklist"
2326
}
2427

28+
// List is for checking if a device id is in the blacklist.
2529
type List interface {
2630
InList(ID string) (reason string, ok bool)
2731
}
2832

33+
// SyncList is an implemention of the List interface that works synchronously.
2934
type SyncList struct {
3035
rules map[string]string
3136
dataLock sync.RWMutex
3237
}
3338

39+
// NewEmptySyncList creates a new SyncList that holds no information.
3440
func NewEmptySyncList() SyncList {
3541
return SyncList{
3642
rules: make(map[string]string),
3743
}
3844
}
3945

46+
// InList returns whether or not a device is on the blacklist and why, if it's
47+
// on the list.
4048
func (m *SyncList) InList(ID string) (string, bool) {
4149
m.dataLock.RLock()
4250
defer m.dataLock.RUnlock()
@@ -56,6 +64,8 @@ func (m *SyncList) InList(ID string) (string, bool) {
5664
return "", false
5765
}
5866

67+
// UpdateList takes the data given and overwrites the blacklist with the new
68+
// information.
5969
func (m *SyncList) UpdateList(data []BlackListedItem) {
6070

6171
newData := make(map[string]string)
@@ -68,6 +78,7 @@ func (m *SyncList) UpdateList(data []BlackListedItem) {
6878
m.dataLock.Unlock()
6979
}
7080

81+
// Updater is for getting the blacklist.
7182
type Updater interface {
7283
GetBlacklist() ([]BlackListedItem, error)
7384
}
@@ -79,6 +90,7 @@ type listRefresher struct {
7990
cache SyncList
8091
}
8192

93+
// InList checks if a specified device id is on the blacklist.
8294
func (d *listRefresher) InList(ID string) (string, bool) {
8395
return d.cache.InList(ID)
8496
}
@@ -91,11 +103,14 @@ func (d *listRefresher) updateList() {
91103
}
92104
}
93105

106+
// RefresherConfig is the configuration specifying how often to update the list
107+
// and what logger to use when logging.
94108
type RefresherConfig struct {
95109
UpdateInterval time.Duration
96110
Logger log.Logger
97111
}
98112

113+
// NewListRefresher takes the given values and uses them to create a new listRefresher
99114
func NewListRefresher(config RefresherConfig, updater Updater, stop chan struct{}) List {
100115
if config.Logger == nil {
101116
config.Logger = logging.DefaultLogger()

cipher/algo_types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package cipher
1919

20+
// AlgorithmType is an enum used to specify which algorithm is being used.
2021
type AlgorithmType string
2122

2223
const (
@@ -26,7 +27,9 @@ const (
2627
RSAAsymmetric AlgorithmType = "rsa-asy"
2728
)
2829

29-
func ParseAlogrithmType(algo string) AlgorithmType {
30+
// ParseAlgorithmType takes a string and returns an enum if one matches,
31+
// otherwise returns the None AlgorithmType enum.
32+
func ParseAlgorithmType(algo string) AlgorithmType {
3033
if algo == string(Box) {
3134
return Box
3235
} else if algo == string(RSASymmetric) {

cipher/boxLoader.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
)
2424

25+
// BoxLoader loads the box encryption/decryption.
2526
type BoxLoader struct {
2627
KID string
2728
PrivateKey KeyLoader
@@ -56,6 +57,7 @@ func (boxLoader *BoxLoader) getBoxPublicKey() ([32]byte, error) {
5657
return publicKey, nil
5758
}
5859

60+
// LoadEncrypt loads an encrypter for the box algorithm.
5961
func (boxLoader *BoxLoader) LoadEncrypt() (Encrypt, error) {
6062
publicKey, err := boxLoader.getBoxPublicKey()
6163
if err != nil {
@@ -69,6 +71,7 @@ func (boxLoader *BoxLoader) LoadEncrypt() (Encrypt, error) {
6971
return NewBoxEncrypter(privateKey, publicKey, boxLoader.KID), nil
7072
}
7173

74+
// LoadDecrypt loads a decrypter for the box algorithm.
7275
func (boxLoader *BoxLoader) LoadDecrypt() (Decrypt, error) {
7376
publicKey, err := boxLoader.getBoxPublicKey()
7477
if err != nil {

cipher/cipher.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ import (
2323
"crypto/rand"
2424
"crypto/rsa"
2525
"fmt"
26+
"hash"
27+
"io"
28+
"os"
29+
2630
"github.com/goph/emperror"
2731
"github.com/pkg/errors"
2832
"golang.org/x/crypto/blake2b"
2933
"golang.org/x/crypto/nacl/box"
30-
"hash"
31-
"io"
32-
"os"
3334
)
3435

3536
func init() {
@@ -84,40 +85,48 @@ func GeneratePrivateKey(size int) *rsa.PrivateKey {
8485
return privateKey
8586
}
8687

88+
// DefaultCipherEncrypter returns a NOOP encrypter.
8789
func DefaultCipherEncrypter() Encrypt {
8890
return &NOOP{}
8991
}
9092

93+
// DEfaultCipherDecrypter returns a NOOP decrypter.
9194
func DefaultCipherDecrypter() Decrypt {
9295
return &NOOP{}
9396
}
9497

9598
// NOOP will just return the message
9699
type NOOP struct{}
97100

101+
// GetAlgorithm returns None.
98102
func (*NOOP) GetAlgorithm() AlgorithmType {
99103
return None
100104
}
101105

106+
// GetKID returns none.
102107
func (*NOOP) GetKID() string {
103108
return "none"
104109
}
105110

111+
//EncryptMessage simply returns the message given.
106112
func (*NOOP) EncryptMessage(message []byte) (crypt []byte, nonce []byte, err error) {
107113
return message, []byte{}, nil
108114
}
109115

116+
// DecryptMessage simply returns the message given.
110117
func (*NOOP) DecryptMessage(cipher []byte, nonce []byte) (message []byte, err error) {
111118
return cipher, nil
112119
}
113120

121+
// GetAlgorithm returns the algorithm type.
114122
func (c *rsaEncrypterDecrypter) GetAlgorithm() AlgorithmType {
115123
if c.recipientPublicKey == nil || c.senderPublicKey == nil {
116124
return RSASymmetric
117125
}
118126
return RSAAsymmetric
119127
}
120128

129+
// GetKID returns the KID.
121130
func (c *rsaEncrypterDecrypter) GetKID() string {
122131
return c.kid
123132
}
@@ -132,6 +141,7 @@ type rsaEncrypterDecrypter struct {
132141
label []byte
133142
}
134143

144+
// NewRSAEncrypter returns an RSA encrypter.
135145
func NewRSAEncrypter(hash crypto.Hash, senderPrivateKey *rsa.PrivateKey, recipientPublicKey *rsa.PublicKey, kid string) Encrypt {
136146
return &rsaEncrypterDecrypter{
137147
kid: kid,
@@ -142,6 +152,7 @@ func NewRSAEncrypter(hash crypto.Hash, senderPrivateKey *rsa.PrivateKey, recipie
142152
}
143153
}
144154

155+
// NewRSADecrypter returns an RSA decrypter.
145156
func NewRSADecrypter(hash crypto.Hash, recipientPrivateKey *rsa.PrivateKey, senderPublicKey *rsa.PublicKey, kid string) Decrypt {
146157
return &rsaEncrypterDecrypter{
147158
kid: kid,
@@ -152,6 +163,7 @@ func NewRSADecrypter(hash crypto.Hash, recipientPrivateKey *rsa.PrivateKey, send
152163
}
153164
}
154165

166+
// EncryptMessage encrypts the message using RSA.
155167
func (c *rsaEncrypterDecrypter) EncryptMessage(message []byte) ([]byte, []byte, error) {
156168
cipherdata, err := rsa.EncryptOAEP(
157169
c.hasher.New(),
@@ -183,6 +195,7 @@ func (c *rsaEncrypterDecrypter) EncryptMessage(message []byte) ([]byte, []byte,
183195
return cipherdata, signature, nil
184196
}
185197

198+
// DecryptMessage decrypts the message using RSA.
186199
func (c *rsaEncrypterDecrypter) DecryptMessage(cipher []byte, nonce []byte) ([]byte, error) {
187200
decrypted, err := rsa.DecryptOAEP(
188201
c.hasher.New(),
@@ -219,14 +232,17 @@ type encryptBox struct {
219232
sharedEncryptKey *[32]byte
220233
}
221234

235+
// GetAlgorithm returns the algorithm type.
222236
func (enBox *encryptBox) GetAlgorithm() AlgorithmType {
223237
return Box
224238
}
225239

240+
// GetKID returns the KID.
226241
func (enBox *encryptBox) GetKID() string {
227242
return enBox.kid
228243
}
229244

245+
// NewBoxEncrypter returns a new box encrypter.
230246
func NewBoxEncrypter(senderPrivateKey [32]byte, recipientPublicKey [32]byte, kid string) Encrypt {
231247

232248
encrypter := encryptBox{
@@ -241,6 +257,7 @@ func NewBoxEncrypter(senderPrivateKey [32]byte, recipientPublicKey [32]byte, kid
241257
return &encrypter
242258
}
243259

260+
// Encrypt message encrypts the message using the box algorithm.
244261
func (enBox *encryptBox) EncryptMessage(message []byte) ([]byte, []byte, error) {
245262
var nonce [24]byte
246263
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
@@ -259,14 +276,17 @@ type decryptBox struct {
259276
sharedDecryptKey *[32]byte
260277
}
261278

279+
// GetAlgorithm returns the algorithm type.
262280
func (deBox *decryptBox) GetAlgorithm() AlgorithmType {
263281
return Box
264282
}
265283

284+
// GetKID returns the KID.
266285
func (deBox *decryptBox) GetKID() string {
267286
return deBox.kid
268287
}
269288

289+
// NewBoxDecrypter returns a new box decrypter.
270290
func NewBoxDecrypter(recipientPrivateKey [32]byte, senderPublicKey [32]byte, kid string) Decrypt {
271291

272292
decrypter := decryptBox{
@@ -281,6 +301,7 @@ func NewBoxDecrypter(recipientPrivateKey [32]byte, senderPublicKey [32]byte, kid
281301
return &decrypter
282302
}
283303

304+
// DecryptMessage decrypts the message using the box algorithm.
284305
func (deBox *decryptBox) DecryptMessage(cipher []byte, nonce []byte) ([]byte, error) {
285306
var decryptNonce [24]byte
286307
copy(decryptNonce[:], nonce[:24])

cipher/key_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package cipher
1919

20+
// KeyType is an enum for how the key can be used.
2021
type KeyType string
2122

2223
const (

cipher/loader.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import (
2222
"crypto/rsa"
2323
"crypto/x509"
2424
"encoding/pem"
25+
"io/ioutil"
26+
2527
"github.com/Comcast/webpa-common/logging"
2628
"github.com/go-kit/kit/log"
2729
"github.com/goph/emperror"
2830
"github.com/pkg/errors"
29-
"io/ioutil"
3031
)
3132

3233
var (
@@ -62,20 +63,27 @@ type Config struct {
6263
Keys map[KeyType]string `json:"keys,omitempty"`
6364
}
6465

66+
// KeyLoader gets the bytes for a key.
6567
type KeyLoader interface {
6668
GetBytes() ([]byte, error)
6769
}
70+
71+
// EncryptLoader loads an encrypter.
6872
type EncryptLoader interface {
6973
LoadEncrypt() (Encrypt, error)
7074
}
75+
76+
//DecryptLoader loads a decrypter.
7177
type DecryptLoader interface {
7278
LoadDecrypt() (Decrypt, error)
7379
}
7480

81+
// FileLoader loads a key from a file.
7582
type FileLoader struct {
7683
Path string
7784
}
7885

86+
// GetBytes returns the bytes found at the filepath.
7987
func (f *FileLoader) GetBytes() ([]byte, error) {
8088
return ioutil.ReadFile(f.Path)
8189
}
@@ -86,14 +94,17 @@ func CreateFileLoader(keys map[KeyType]string, keyType KeyType) KeyLoader {
8694
}
8795
}
8896

97+
// BytesLoader implements the KeyLoader.
8998
type BytesLoader struct {
9099
Data []byte
91100
}
92101

102+
// GetBytes returns the bytes stored by the BytesLoader
93103
func (b *BytesLoader) GetBytes() ([]byte, error) {
94104
return b.Data, nil
95105
}
96106

107+
// GetPrivateKey uses a keyloader to load a private key.
97108
func GetPrivateKey(loader KeyLoader) (*rsa.PrivateKey, error) {
98109
if loader == nil {
99110
return nil, errors.New("no loader")
@@ -120,6 +131,7 @@ func GetPrivateKey(loader KeyLoader) (*rsa.PrivateKey, error) {
120131
}
121132
}
122133

134+
// GetPublicKey uses a keyloader to load a public key.
123135
func GetPublicKey(loader KeyLoader) (*rsa.PublicKey, error) {
124136
if loader == nil {
125137
return nil, errors.New("no loader")
@@ -146,6 +158,7 @@ func GetPublicKey(loader KeyLoader) (*rsa.PublicKey, error) {
146158
}
147159
}
148160

161+
// LoadEncrypt uses the config to load an encrypter.
149162
func (config *Config) LoadEncrypt() (Encrypt, error) {
150163
var err error
151164
if config.Logger == nil {
@@ -197,6 +210,7 @@ func (config *Config) LoadEncrypt() (Encrypt, error) {
197210
return DefaultCipherEncrypter(), emperror.Wrap(err, "failed to load custom algorithm")
198211
}
199212

213+
// LoadDecrypt uses the config to load a decrypter.
200214
func (config *Config) LoadDecrypt() (Decrypt, error) {
201215
var err error
202216
if config.Logger == nil {

0 commit comments

Comments
 (0)