-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
176 lines (155 loc) · 4.23 KB
/
configuration.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"crypto/ecdsa"
"github.com/mitchellh/go-homedir"
"github.com/zetabase/zetabase-client"
"github.com/zetabase/zetabase-client/zbprotocol"
"github.com/spf13/viper"
"log"
"runtime"
"strings"
)
const (
ConfigKeyUserEmail = "user.email"
ConfigKeyUserMobile = "user.mobile"
ConfigKeyUserName = "user.name"
ConfigKeyPubKeyFile = "user.pubkey-file"
ConfigKeyPrivKeyFile = "user.privkey-file"
ConfigKeyAdminTask = "admin.task"
ConfigKeyAdminPassword = "admin.pass"
ConfigKeySubIdSignupCode = "admin.signupcode"
ConfigKeyGroupId = "admin.groupid"
AdminTaskNewUser = "newuser"
AdminTaskNewSubUser = "newsubuser"
AdminTaskRandomBytes = "randombytes"
AdminTaskTestingGround = "test"
AdminTaskListSubUsers = "listusers"
AdminTaskTestClient = "client"
AdminTaskGetWebhook = "webhook"
ConfigKeyIdentityFile = "identity"
ConfigKeyTableId = "table"
ConfigKeyTableOwnerId = "owner"
ConfigKeyTableKey = "key"
ConfigKeyValue = "value"
ConfigKeyOutputDataType = "dtype"
ConfigKeyParentUid = "parent"
ConfigKeyKeyPattern = "pattern"
ConfigKeyZbHostPort = "host"
ConfigKeyVerbose = "verbose"
ConfigKeyConnectInsecure = "insecure"
ConfigKeyConnectNoCertVerify = "nocertverify"
ConfigKeyCreatePermissions = "permissions"
ConfigKeyCreateAllowTokens = "allowjwt"
ConfigKeyPutOverwrite = "overwrite"
ConfigKeyPutBinInFile = "inputfile"
ConfigKeyIdPassword = "password"
ConfigKeyLoginId = "loginid"
ConfigKeyLoginParentId = "loginparentid"
ConfigKeyExportDataMode = "mode.export"
)
var (
zbHostPort = ""
cfgFile = ""
identityFile = ""
idPassword = ""
loginId = ""
loginParentId = ""
subIdSignupCode = ""
subIdGroupId = ""
binInFile = ""
keyPattern = ""
tableId = ""
tableOwnerId = ""
tableKey = ""
outputDataType = ""
tableValue = ""
userEmail = ""
userMobile = ""
userName = ""
userPubKeyFile = ""
userPrivKeyFile = ""
adminTask = ""
adminPass = ""
createPermissions = ""
allowJwt = false
connectInsecure = false
connectNoCertVerify = false
verbose = false
parentUid = ""
putOverwrite = false
exportDataMode = ""
)
type IdentityDefinition struct {
Id string `json:"id"`
Handle string `json:"handle"`
ParentId string `json:"parent_id,omitempty"`
PubKeyEnc string `json:"pub_key"`
PrivKeyEnc string `json:"priv_key"`
}
func (d *IdentityDefinition) ToUserIdentity() (*UserIdentity, error) {
pub, err := zetabase.DecodeEcdsaPublicKey(d.PubKeyEnc)
if err != nil {
return nil, err
}
priv, err := zetabase.DecodeEcdsaPrivateKey(d.PrivKeyEnc)
if err != nil {
return nil, err
}
var parentPtr *string
if len(d.ParentId) > 0 {
parentPtr = &d.ParentId
}
return &UserIdentity{
Id: d.Id,
ParentId: parentPtr,
PubKey: pub,
PrivKey: priv,
}, nil
}
type UserIdentity struct {
Id string
ParentId *string
PubKey *ecdsa.PublicKey
PrivKey *ecdsa.PrivateKey
}
func (u *UserIdentity) MakeSignature(nonce int64, relBytes []byte) *zbprotocol.EcdsaSignature {
r, s := zetabase.MakeZetabaseSignature(u.Id, nonce, relBytes, u.PrivKey)
return &zbprotocol.EcdsaSignature{
R: r,
S: s,
}
}
func GetVersionString() string {
return zetabase.ClientVersion
}
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
return //er(err)
}
// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName("config")
viper.AddConfigPath("$HOME/.zetabase")
viper.AddConfigPath("/etc/zetabase")
viper.AddConfigPath(".")
//viper.SetConfigName(".cobra")
}
viper.SetEnvPrefix("zb")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
log.Println("Using config file:", viper.ConfigFileUsed())
}
}
func isWindows() bool {
s := runtime.GOOS
if strings.Contains(strings.ToLower(s), "windows") {
return true
}
return false
}