@@ -16,6 +16,7 @@ import (
16
16
"strconv"
17
17
"strings"
18
18
"sync"
19
+ "io"
19
20
20
21
"github.com/gorilla/mux"
21
22
"golang.org/x/crypto/ssh"
61
62
stopScript = flag .String ("stop_script" , "control-api" , "CBSD target run script" )
62
63
serverUrl = flag .String ("server_url" , "http://127.0.0.1:65532" , "Server URL for external requests" )
63
64
dbDir = flag .String ("dbdir" , "/var/db/cbsd-api" , "db root dir" )
65
+ allowListFile = flag .String ("allowlist" , "/usr/local/etc/cbsd-mq-api.allow" , "Path to PubKey whitelist" )
64
66
)
65
67
68
+ type AllowList struct {
69
+ keyType string
70
+ key string
71
+ comment string
72
+ cid string
73
+ next * AllowList // link to the next records
74
+ }
75
+
76
+ // linked struct
77
+ type Feed struct {
78
+ length int
79
+ start * AllowList
80
+ }
81
+
82
+ type MyFeeds struct {
83
+ f * Feed
84
+ }
85
+
86
+ func (f * Feed ) Append (newAllow * AllowList ) {
87
+ if f .length == 0 {
88
+ f .start = newAllow
89
+ } else {
90
+ currentPost := f .start
91
+ for currentPost .next != nil {
92
+ currentPost = currentPost .next
93
+ }
94
+ currentPost .next = newAllow
95
+ }
96
+ f .length ++
97
+ }
98
+
99
+ func newAllow (keyType string , key string , comment string ) * AllowList {
100
+ np := AllowList {keyType : keyType , key : key , comment : comment }
101
+ // np.Response = ""
102
+ // np.Time = 0
103
+ return & np
104
+ }
105
+
106
+
66
107
// we need overwrite Content-Type here
67
108
// https://stackoverflow.com/questions/59763852/can-you-return-json-in-golang-http-error
68
109
func JSONError (w http.ResponseWriter , message string , code int ) {
@@ -131,8 +172,68 @@ func main() {
131
172
os .MkdirAll (* dbDir , 0770 )
132
173
}
133
174
175
+
176
+ // WhiteList
177
+ if ! fileExists (* allowListFile ) {
178
+ fmt .Printf ("no such allowList file, please check config/path: %s\n " , allowListFile )
179
+ os .Exit (1 )
180
+ }
181
+ f := & Feed {}
182
+ // var p *AllowList
183
+ // loadconfig
184
+ fd , err := os .Open (* allowListFile )
185
+ if err != nil {
186
+ panic (err )
187
+ }
188
+ defer fd .Close ()
189
+
190
+ var keyType string
191
+ var key string
192
+ var comment string
193
+
194
+
195
+
196
+ for {
197
+ _ , err := fmt .Fscanf (fd ,"%s %s %s" ,& keyType ,& key ,& comment )
198
+ if err != nil {
199
+ if err != io .EOF {
200
+ //log.Fatal(err)
201
+ break
202
+ }
203
+ }
204
+ fmt .Printf ("loaded: [%s %s %s]\n " , keyType , key , comment )
205
+ p := newAllow (keyType ,key ,comment )
206
+ f .Append (p )
207
+ }
208
+
209
+ fd .Close ()
210
+
211
+ fmt .Printf ("AllowList Length: %v\n " , f .length )
212
+ // currentAllow := f.start
213
+
214
+ var p * AllowList
215
+ for i := 0 ; i < f .length ; i ++ {
216
+ currentAllow := f .start
217
+ p = currentAllow
218
+ currentAllow = currentAllow .next
219
+ ResultKeyType := (string (p .keyType ))
220
+ fmt .Println ("ResultType: " , ResultKeyType )
221
+ // if len(ResultAlias) < 1 {
222
+ // ResultNameserver := (string(p.NameServer))
223
+ // ResultNameserver = strings.Replace(ResultNameserver, ".", "_", -1)
224
+ // ResultAlias = strings.Replace(ResultNameserver, ":", "_", -1)
225
+ // }
226
+ //
227
+ // Result := fmt.Sprintf("check_dns_%s_%s: %d",ResultHost,ResultAlias,p.Time)
228
+ // fmt.Println(Result)
229
+ }
230
+
231
+ // setup: we need to pass Feed into handler function
232
+ feeds := & MyFeeds { f : f }
233
+
134
234
router := mux .NewRouter ()
135
- router .HandleFunc ("/api/v1/create/{InstanceId}" , HandleClusterCreate ).Methods ("POST" )
235
+ // router.HandleFunc("/api/v1/create/{InstanceId}", HandleClusterCreate).Methods("POST")
236
+ router .HandleFunc ("/api/v1/create/{InstanceId}" , feeds .HandleClusterCreate ).Methods ("POST" )
136
237
router .HandleFunc ("/api/v1/status/{InstanceId}" , HandleClusterStatus ).Methods ("GET" )
137
238
router .HandleFunc ("/api/v1/start/{InstanceId}" , HandleClusterStart ).Methods ("GET" )
138
239
router .HandleFunc ("/api/v1/stop/{InstanceId}" , HandleClusterStop ).Methods ("GET" )
@@ -335,7 +436,10 @@ func getJname() string {
335
436
return result
336
437
}
337
438
338
- func HandleClusterCreate (w http.ResponseWriter , r * http.Request ) {
439
+ //func (feeds *MyFeeds) HandleClusterCluster(w http.ResponseWriter, r *http.Request) {
440
+ //func HandleClusterCreate(w http.ResponseWriter, r *http.Request) {
441
+ func (feeds * MyFeeds ) HandleClusterCreate (w http.ResponseWriter , r * http.Request ) {
442
+
339
443
var InstanceId string
340
444
params := mux .Vars (r )
341
445
@@ -415,6 +519,22 @@ func HandleClusterCreate(w http.ResponseWriter, r *http.Request) {
415
519
//existance?
416
520
// check for existance
417
521
cid := md5 .Sum (uid )
522
+
523
+
524
+ //ALLOWED?
525
+ var p * AllowList
526
+
527
+ currentAllow := feeds .f .start
528
+
529
+ for i := 0 ; i < feeds .f .length ; i ++ {
530
+ p = currentAllow
531
+ currentAllow = currentAllow .next
532
+ ResultKeyType := (string (p .keyType ))
533
+ fmt .Println ("ResultType: " , ResultKeyType )
534
+ }
535
+
536
+ return
537
+
418
538
VmPathDir := fmt .Sprintf ("%s/%x" , * dbDir , cid )
419
539
420
540
if ! fileExists (VmPathDir ) {
0 commit comments