forked from kpn/mq-hammer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.go
70 lines (61 loc) · 1.76 KB
/
credentials.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
// Copyright 2019 Koninklijke KPN N.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/csv"
"errors"
"os"
uuid "github.com/satori/go.uuid"
)
// MqttCredentials provides mqtt credentials
type MqttCredentials interface {
Get() (username string, password string, clientID string, err error) // a zero value for clientID means: generate one
}
type fixedCreds struct {
username string
password string
clientID string
}
func (f *fixedCreds) Get() (string, string, string, error) {
return f.username, f.password, f.clientID + uuid.NewV4().String(), nil
}
type fileCreds struct {
creds [][]string
}
// csv: username,password,clientid
func newMqttCredentialsFromFile(filename string) (*fileCreds, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
vs, err := csv.NewReader(f).ReadAll()
if err != nil {
return nil, err
}
return &fileCreds{vs}, nil
}
func (f *fileCreds) Get() (string, string, string, error) {
if f.Size() == 0 {
return "", "", "", errors.New("fileCreds ran out of credentials")
}
h := f.creds[0]
if len(h) != 3 {
return "", "", "", errors.New("expect 3 fields in credentials file")
}
f.creds = f.creds[1:]
return h[0], h[1], h[2], nil
}
func (f *fileCreds) Size() int {
return len(f.creds)
}