forked from birdayz/kaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confluent_cloud.go
75 lines (58 loc) · 1.65 KB
/
confluent_cloud.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
package kaf
import (
"errors"
"path/filepath"
"strings"
"os"
"github.com/magiconair/properties"
homedir "github.com/mitchellh/go-homedir"
)
// Default confluent cloud config file path
var defaultCcloudSubpath = filepath.Join(".ccloud", "config")
func TryFindCcloudConfigFile() (string, error) {
homedir, err := homedir.Dir()
if err != nil {
return "", err
}
absoluteDefaultPath := filepath.Join(homedir, defaultCcloudSubpath)
_, err = os.Stat(absoluteDefaultPath)
if err == nil {
return absoluteDefaultPath, nil
}
return "", os.ErrNotExist
}
func extractValue(key, input string) (unquoted string, ok bool) {
if strings.HasPrefix(input, key+"=") {
return strings.TrimRight(strings.Replace(strings.TrimPrefix(input, key+"="), "\"", "", -1), ";"), true
}
return
}
// TODO return []string for brokers
func ParseConfluentCloudConfig(path string) (username, password, broker string, err error) {
p := properties.MustLoadFile(path, properties.UTF8).Map()
if _, ok := p["sasl.jaas.config"]; !ok {
err = errors.New("invalid or unsupported confluent cloud config")
return
}
if _, ok := p["bootstrap.servers"]; !ok {
err = errors.New("invalid or unsupported confluent cloud config")
return
}
words := strings.Split(p["sasl.jaas.config"], " ")
jaasOk := true
for _, word := range words {
if result, ok := extractValue("username", word); ok {
username = result
jaasOk = jaasOk && ok
}
if result, ok := extractValue("password", word); ok {
password = result
jaasOk = jaasOk && ok
}
}
if !jaasOk {
return "", "", "", errors.New("Could not parse sasl.jaas.config from ccloud")
}
broker = p["bootstrap.servers"]
return
}