22package main
33
44import (
5+ "bufio"
56 "crypto/md5"
67 "encoding/json"
78 "flag"
89 "fmt"
9- "io"
1010 "io/ioutil"
1111 "log"
1212 "net/http"
@@ -196,26 +196,34 @@ func main() {
196196 }
197197 defer fd .Close ()
198198
199+ scanner := bufio .NewScanner (fd )
200+
199201 var keyType string
200202 var key string
201203 var comment string
202204
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 )
204216 // todo: input validation
205217 // 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 )
207219 if err != nil {
208- if err != io .EOF {
209- //log.Fatal(err)
220+ log .Fatal (err )
210221 break
211222 }
212- }
213223 fmt .Printf ("* ACL loaded: [%s %s %s]\n " , keyType , key , comment )
214224 p := newAllow (keyType , key , comment )
215225 f .Append (p )
216- }
217-
218- fd .Close ()
226+ }
219227 fmt .Printf ("* AllowList Length: %v\n " , f .length )
220228 }
221229
@@ -228,6 +236,7 @@ func main() {
228236 router .HandleFunc ("/api/v1/start/{InstanceId}" , feeds .HandleClusterStart ).Methods ("GET" )
229237 router .HandleFunc ("/api/v1/stop/{InstanceId}" , feeds .HandleClusterStop ).Methods ("GET" )
230238 router .HandleFunc ("/api/v1/cluster" , feeds .HandleClusterCluster ).Methods ("GET" )
239+ router .HandleFunc ("/images" , HandleClusterImages ).Methods ("GET" )
231240 router .HandleFunc ("/api/v1/destroy/{InstanceId}" , feeds .HandleClusterDestroy ).Methods ("GET" )
232241 fmt .Println ("* Listen" , * listen )
233242 fmt .Println ("* Server URL" , server_url )
@@ -279,6 +288,10 @@ func isPubKeyAllowed(feeds *MyFeeds, PubKey string) bool {
279288 var p * AllowList
280289 currentAllow := feeds .f .start
281290
291+ if ! acl_enable {
292+ return true
293+ }
294+
282295 for i := 0 ; i < feeds .f .length ; i ++ {
283296 p = currentAllow
284297 currentAllow = currentAllow .next
@@ -291,7 +304,7 @@ func isPubKeyAllowed(feeds *MyFeeds, PubKey string) bool {
291304
292305 if len (PubKey ) == len (KeyInList ) {
293306 if strings .Compare (PubKey , KeyInList ) == 0 {
294- fmt .Printf ("MAAAATCHED \n " )
307+ fmt .Printf ("pubkey matched \n " )
295308 return true
296309 }
297310 }
@@ -305,12 +318,16 @@ func isCidAllowed(feeds *MyFeeds, Cid string) bool {
305318 var p * AllowList
306319 currentAllow := feeds .f .start
307320
321+ if ! acl_enable {
322+ return true
323+ }
324+
308325 for i := 0 ; i < feeds .f .length ; i ++ {
309326 p = currentAllow
310327 currentAllow = currentAllow .next
311328 CidInList := (string (p .cid ))
312329 if strings .Compare (Cid , CidInList ) == 0 {
313- fmt .Printf ("MAAAATCHED \n " )
330+ fmt .Printf ("Cid ACL matched: %s \n " , Cid )
314331 return true
315332 }
316333 }
@@ -336,7 +353,8 @@ func (feeds *MyFeeds) HandleClusterStatus(w http.ResponseWriter, r *http.Request
336353 }
337354
338355 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 )
340358 return
341359 }
342360
@@ -388,7 +406,8 @@ func (feeds *MyFeeds) HandleClusterCluster(w http.ResponseWriter, r *http.Reques
388406 }
389407
390408 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 )
392411 return
393412 }
394413
@@ -419,6 +438,28 @@ func (feeds *MyFeeds) HandleClusterCluster(w http.ResponseWriter, r *http.Reques
419438 }
420439}
421440
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+
422463func realInstanceCreate (body string ) {
423464
424465 a := & body
@@ -566,7 +607,8 @@ func (feeds *MyFeeds) HandleClusterCreate(w http.ResponseWriter, r *http.Request
566607 cid := md5 .Sum (uid )
567608
568609 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 )
570612 return
571613 }
572614
@@ -823,7 +865,8 @@ func (feeds *MyFeeds) HandleClusterDestroy(w http.ResponseWriter, r *http.Reques
823865 }
824866
825867 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 )
827870 return
828871 }
829872
@@ -942,7 +985,8 @@ func (feeds *MyFeeds) HandleClusterStop(w http.ResponseWriter, r *http.Request)
942985 }
943986
944987 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 )
946990 return
947991 }
948992
@@ -1041,7 +1085,8 @@ func (feeds *MyFeeds) HandleClusterStart(w http.ResponseWriter, r *http.Request)
10411085 }
10421086
10431087 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 )
10451090 return
10461091 }
10471092
0 commit comments