-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
140 lines (120 loc) · 2.92 KB
/
config.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Copyright (c) 2020 SSH Communications Security Inc.
//
// All rights reserved.
//
package gcloudwebhook
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"github.com/SSHcom/privx-sdk-go/config"
"google.golang.org/api/iterator"
)
const (
collection = "PrivX"
)
// Instance contains configuration information for a PrivX instance.
type Instance struct {
Name string
Config *config.Config
UserMappings map[string]string
}
// AddPrivXInstance adds the named PrivX instance to the PrivX
// collection in Firestore. TODO, must change to use correct types
// instead of []byte configs.
func AddPrivXInstance(name string, config, userMappings []byte) error {
configB64 := base64.RawURLEncoding.EncodeToString(config)
userMappingsB64 := base64.RawURLEncoding.EncodeToString(userMappings)
_, _, err := fs.Collection(collection).Add(ctx,
map[string]interface{}{
"name": name,
"config": configB64,
"userMappings": userMappingsB64,
})
return err
}
// GetPrivXInstances returns all known PrivX instances.
func GetPrivXInstances() ([]*Instance, error) {
iter := fs.Collection(collection).DocumentRefs(ctx)
var result []*Instance
for {
ref, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
doc, err := ref.Get(ctx)
if err != nil {
return nil, err
}
instance, err := unmarshalInstance(doc.Data())
if err != nil {
return nil, err
}
result = append(result, instance)
}
return result, nil
}
// GetPrivXInstance gets the PrivX instance by name.
func GetPrivXInstance(name string) ([]*Instance, error) {
q := fs.Collection(collection).Where("name", "==", name)
iter := q.Documents(ctx)
defer iter.Stop()
var result []*Instance
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
instance, err := unmarshalInstance(doc.Data())
if err != nil {
return nil, err
}
result = append(result, instance)
}
return result, nil
}
func unmarshalInstance(data map[string]interface{}) (*Instance, error) {
name, ok := data["name"].(string)
if !ok {
return nil, fmt.Errorf("No 'name'")
}
configB64, ok := data["config"].(string)
if !ok {
return nil, fmt.Errorf("No 'config'")
}
configData, err := base64.RawURLEncoding.DecodeString(configB64)
if err != nil {
return nil, err
}
userMappingsB64, ok := data["userMappings"].(string)
if !ok {
return nil, fmt.Errorf("No 'userMappings'")
}
userMappingsData, err := base64.RawURLEncoding.DecodeString(userMappingsB64)
if err != nil {
return nil, err
}
cfg := new(config.Config)
err = toml.Unmarshal(configData, cfg)
if err != nil {
return nil, err
}
userMappings := make(map[string]string)
err = json.Unmarshal(userMappingsData, &userMappings)
if err != nil {
return nil, err
}
return &Instance{
Name: name,
Config: cfg,
UserMappings: userMappings,
}, nil
}