2
2
package main
3
3
4
4
import (
5
+ "bufio"
5
6
"crypto/md5"
6
7
"encoding/json"
7
8
"flag"
8
9
"fmt"
9
- "io"
10
10
"io/ioutil"
11
11
"log"
12
12
"net/http"
@@ -196,26 +196,34 @@ func main() {
196
196
}
197
197
defer fd .Close ()
198
198
199
+ scanner := bufio .NewScanner (fd )
200
+
199
201
var keyType string
200
202
var key string
201
203
var comment string
202
204
203
- for {
205
+ scanner .Split (bufio .ScanLines )
206
+ var txtlines []string
207
+
208
+ for scanner .Scan () {
209
+ txtlines = append (txtlines , scanner .Text ())
210
+ }
211
+
212
+ fd .Close ()
213
+
214
+ for _ , eachline := range txtlines {
215
+ fmt .Println (eachline )
204
216
// todo: input validation
205
217
// todo: auto-reload, signal
206
- _ , err := fmt .Fscanf ( fd , "%s %s %s" , & keyType , & key , & comment )
218
+ _ , err := fmt .Sscanf ( eachline , "%s %s %s" , & keyType , & key , & comment )
207
219
if err != nil {
208
- if err != io .EOF {
209
- //log.Fatal(err)
220
+ log .Fatal (err )
210
221
break
211
222
}
212
- }
213
223
fmt .Printf ("* ACL loaded: [%s %s %s]\n " , keyType , key , comment )
214
224
p := newAllow (keyType , key , comment )
215
225
f .Append (p )
216
- }
217
-
218
- fd .Close ()
226
+ }
219
227
fmt .Printf ("* AllowList Length: %v\n " , f .length )
220
228
}
221
229
@@ -228,6 +236,7 @@ func main() {
228
236
router .HandleFunc ("/api/v1/start/{InstanceId}" , feeds .HandleClusterStart ).Methods ("GET" )
229
237
router .HandleFunc ("/api/v1/stop/{InstanceId}" , feeds .HandleClusterStop ).Methods ("GET" )
230
238
router .HandleFunc ("/api/v1/cluster" , feeds .HandleClusterCluster ).Methods ("GET" )
239
+ router .HandleFunc ("/images" , HandleClusterImages ).Methods ("GET" )
231
240
router .HandleFunc ("/api/v1/destroy/{InstanceId}" , feeds .HandleClusterDestroy ).Methods ("GET" )
232
241
fmt .Println ("* Listen" , * listen )
233
242
fmt .Println ("* Server URL" , server_url )
@@ -279,6 +288,10 @@ func isPubKeyAllowed(feeds *MyFeeds, PubKey string) bool {
279
288
var p * AllowList
280
289
currentAllow := feeds .f .start
281
290
291
+ if ! acl_enable {
292
+ return true
293
+ }
294
+
282
295
for i := 0 ; i < feeds .f .length ; i ++ {
283
296
p = currentAllow
284
297
currentAllow = currentAllow .next
@@ -291,7 +304,7 @@ func isPubKeyAllowed(feeds *MyFeeds, PubKey string) bool {
291
304
292
305
if len (PubKey ) == len (KeyInList ) {
293
306
if strings .Compare (PubKey , KeyInList ) == 0 {
294
- fmt .Printf ("MAAAATCHED \n " )
307
+ fmt .Printf ("pubkey matched \n " )
295
308
return true
296
309
}
297
310
}
@@ -305,12 +318,16 @@ func isCidAllowed(feeds *MyFeeds, Cid string) bool {
305
318
var p * AllowList
306
319
currentAllow := feeds .f .start
307
320
321
+ if ! acl_enable {
322
+ return true
323
+ }
324
+
308
325
for i := 0 ; i < feeds .f .length ; i ++ {
309
326
p = currentAllow
310
327
currentAllow = currentAllow .next
311
328
CidInList := (string (p .cid ))
312
329
if strings .Compare (Cid , CidInList ) == 0 {
313
- fmt .Printf ("MAAAATCHED \n " )
330
+ fmt .Printf ("Cid ACL matched: %s \n " , Cid )
314
331
return true
315
332
}
316
333
}
@@ -336,7 +353,8 @@ func (feeds *MyFeeds) HandleClusterStatus(w http.ResponseWriter, r *http.Request
336
353
}
337
354
338
355
if ! isCidAllowed (feeds , Cid ) {
339
- JSONError (w , "Not allowed" , http .StatusInternalServerError )
356
+ fmt .Printf ("CID not in ACL: %s\n " , Cid )
357
+ JSONError (w , "not allowed" , http .StatusInternalServerError )
340
358
return
341
359
}
342
360
@@ -388,7 +406,8 @@ func (feeds *MyFeeds) HandleClusterCluster(w http.ResponseWriter, r *http.Reques
388
406
}
389
407
390
408
if ! isCidAllowed (feeds , Cid ) {
391
- JSONError (w , "Not allowed" , http .StatusInternalServerError )
409
+ fmt .Printf ("CID not in ACL: %s\n " , Cid )
410
+ JSONError (w , "not allowed" , http .StatusInternalServerError )
392
411
return
393
412
}
394
413
@@ -419,6 +438,28 @@ func (feeds *MyFeeds) HandleClusterCluster(w http.ResponseWriter, r *http.Reques
419
438
}
420
439
}
421
440
441
+ func HandleClusterImages (w http.ResponseWriter , r * http.Request ) {
442
+
443
+ if fileExists (config .Cloud_images_list ) {
444
+ b , err := ioutil .ReadFile (config .Cloud_images_list ) // just pass the file name
445
+ if err != nil {
446
+ JSONError (w , "" , http .StatusNotFound )
447
+ return
448
+ } else {
449
+ // already in json - send as-is
450
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
451
+ w .Header ().Set ("X-Content-Type-Options" , "nosniff" )
452
+ w .WriteHeader (200 )
453
+ http .Error (w , string (b ), 200 )
454
+ return
455
+ }
456
+ } else {
457
+ JSONError (w , "" , http .StatusNotFound )
458
+ return
459
+ }
460
+ }
461
+
462
+
422
463
func realInstanceCreate (body string ) {
423
464
424
465
a := & body
@@ -566,7 +607,8 @@ func (feeds *MyFeeds) HandleClusterCreate(w http.ResponseWriter, r *http.Request
566
607
cid := md5 .Sum (uid )
567
608
568
609
if ! isPubKeyAllowed (feeds , vm .Pubkey ) {
569
- JSONError (w , "Not allowed" , http .StatusInternalServerError )
610
+ fmt .Printf ("Pubkey not in ACL: %s\n " , vm .Pubkey )
611
+ JSONError (w , "not allowed" , http .StatusInternalServerError )
570
612
return
571
613
}
572
614
@@ -823,7 +865,8 @@ func (feeds *MyFeeds) HandleClusterDestroy(w http.ResponseWriter, r *http.Reques
823
865
}
824
866
825
867
if ! isCidAllowed (feeds , Cid ) {
826
- JSONError (w , "Not allowed" , http .StatusInternalServerError )
868
+ fmt .Printf ("CID not in ACL: %s\n " , Cid )
869
+ JSONError (w , "not allowed" , http .StatusInternalServerError )
827
870
return
828
871
}
829
872
@@ -942,7 +985,8 @@ func (feeds *MyFeeds) HandleClusterStop(w http.ResponseWriter, r *http.Request)
942
985
}
943
986
944
987
if ! isCidAllowed (feeds , Cid ) {
945
- JSONError (w , "Not allowed" , http .StatusInternalServerError )
988
+ fmt .Printf ("CID not in ACL: %s\n " , Cid )
989
+ JSONError (w , "not allowed" , http .StatusInternalServerError )
946
990
return
947
991
}
948
992
@@ -1041,7 +1085,8 @@ func (feeds *MyFeeds) HandleClusterStart(w http.ResponseWriter, r *http.Request)
1041
1085
}
1042
1086
1043
1087
if ! isCidAllowed (feeds , Cid ) {
1044
- JSONError (w , "Not allowed" , http .StatusInternalServerError )
1088
+ fmt .Printf ("CID not in ACL: %s\n " , Cid )
1089
+ JSONError (w , "not allowed" , http .StatusInternalServerError )
1045
1090
return
1046
1091
}
1047
1092
0 commit comments